Skip to content

Commit

Permalink
Code cleanup for hunk dependency crate
Browse files Browse the repository at this point in the history
- reformat comments
- fewer derives
- tighter types
  • Loading branch information
mtsgrd committed Oct 25, 2024
1 parent 7a03e89 commit 3777791
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
21 changes: 11 additions & 10 deletions crates/gitbutler-hunk-dependency/src/locks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ pub fn compute_hunk_locks(
options: HunkDependencyOptions,
) -> anyhow::Result<HashMap<HunkHash, Vec<HunkLock>>> {
let HunkDependencyOptions { workdir, stacks } = options;

// Transforms stack specific line numbers to workspace line numbers.
let ranges = WorkspaceRanges::create(stacks)?;

Ok(workdir
.iter()
.flat_map(|(path, workspace_hunks)| {
workspace_hunks.iter().filter_map(|hunk| {
let locks = ranges
ranges
.intersection(path, hunk.old_start, hunk.old_lines)
.iter()
.map(|dependency| HunkLock {
commit_id: dependency.commit_id,
branch_id: dependency.stack_id,
.map(|intersection| {
intersection
.iter()
.map(|dependency| HunkLock {
commit_id: dependency.commit_id,
branch_id: dependency.stack_id,
})
.collect_vec()
})
.collect_vec();
if locks.is_empty() {
return None;
}
Some((Hunk::hash_diff(&hunk.diff_lines), locks))
.map(|locks| (Hunk::hash_diff(&hunk.diff_lines), locks))
})
})
.collect())
Expand Down
15 changes: 9 additions & 6 deletions crates/gitbutler-hunk-dependency/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ impl WorkspaceRanges {
}

/// Finds commits that intersect with a given path and range combination.
pub fn intersection(&self, path: &Path, start: u32, lines: u32) -> Vec<&HunkRange> {
if let Some(stack_hunks) = self.paths.get(path) {
return stack_hunks
pub fn intersection(&self, path: &Path, start: u32, lines: u32) -> Option<Vec<&HunkRange>> {
if let Some(hunk_range) = self.paths.get(path) {
let intersection = hunk_range
.iter()
.filter(|hunk| hunk.intersects(start, lines))
.collect_vec();
if !intersection.is_empty() {
return Some(intersection);
}
}
vec![]
None
}
}

Expand Down Expand Up @@ -202,12 +205,12 @@ mod tests {
},
])?;

let dependencies_1 = workspace_ranges.intersection(&path, 2, 1);
let dependencies_1 = workspace_ranges.intersection(&path, 2, 1).unwrap();
assert_eq!(dependencies_1.len(), 1);
assert_eq!(dependencies_1[0].commit_id, commit1_id);
assert_eq!(dependencies_1[0].stack_id, stack1_id);

let dependencies_2 = workspace_ranges.intersection(&path, 12, 1);
let dependencies_2 = workspace_ranges.intersection(&path, 12, 1).unwrap();
assert_eq!(dependencies_2.len(), 1);
assert_eq!(dependencies_2[0].commit_id, commit2_id);
assert_eq!(dependencies_2[0].stack_id, stack2_id);
Expand Down

0 comments on commit 3777791

Please sign in to comment.