Skip to content

Commit ce548ac

Browse files
authored
Merge pull request #3266 from gitbutlerapp/add-diff-hash-function
add-diff-hash-function
2 parents b9e20ae + 4ea3a98 commit ce548ac

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

gitbutler-app/src/virtual_branches/branch/hunk.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl From<&diff::GitHunk> for Hunk {
1717
Hunk {
1818
start: hunk.new_start,
1919
end: hunk.new_start + hunk.new_lines,
20-
hash: None,
20+
hash: Some(Hunk::hash(&hunk.diff)),
2121
timestamp_ms: None,
2222
}
2323
}
@@ -156,6 +156,16 @@ impl Hunk {
156156
pub fn shallow_eq(&self, other: &diff::GitHunk) -> bool {
157157
self.start == other.new_start && self.end == other.new_start + other.new_lines
158158
}
159+
160+
pub fn hash(diff: &str) -> String {
161+
let addition = diff
162+
.lines()
163+
.skip(1) // skip the first line which is the diff header
164+
.filter(|line| line.starts_with('+') || line.starts_with('-')) // exclude context lines
165+
.collect::<Vec<_>>()
166+
.join("\n");
167+
format!("{:x}", md5::compute(addition))
168+
}
159169
}
160170

161171
#[cfg(test)]

gitbutler-app/src/virtual_branches/virtual.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,16 +1838,6 @@ fn get_mtime(cache: &mut HashMap<PathBuf, u128>, file_path: &PathBuf) -> u128 {
18381838
}
18391839
}
18401840

1841-
fn diff_hash(diff: &str) -> String {
1842-
let addition = diff
1843-
.lines()
1844-
.skip(1) // skip the first line which is the diff header
1845-
.filter(|line| line.starts_with('+') || line.starts_with('-')) // exclude context lines
1846-
.collect::<Vec<_>>()
1847-
.join("\n");
1848-
format!("{:x}", md5::compute(addition))
1849-
}
1850-
18511841
pub fn virtual_hunks_by_filepath(
18521842
project_path: &Path,
18531843
diff: &HashMap<PathBuf, Vec<diff::GitHunk>>,
@@ -1866,7 +1856,7 @@ pub fn virtual_hunks_by_filepath(
18661856
start: hunk.new_start,
18671857
end: hunk.new_start + hunk.new_lines,
18681858
binary: hunk.binary,
1869-
hash: diff_hash(&hunk.diff),
1859+
hash: Hunk::hash(&hunk.diff),
18701860
locked: false,
18711861
locked_to: None,
18721862
change_type: hunk.change_type,
@@ -2052,7 +2042,7 @@ fn get_applied_status(
20522042
.filter_map(|claimed_hunk| {
20532043
// if any of the current hunks intersects with the owned hunk, we want to keep it
20542044
for (i, git_diff_hunk) in git_diff_hunks.iter().enumerate() {
2055-
let hash = diff_hash(&git_diff_hunk.diff);
2045+
let hash = Hunk::hash(&git_diff_hunk.diff);
20562046
// Eq compares hashes first, and if one of the hunks lacks a hash, it compares line numbers
20572047
if claimed_hunk.eq(&Hunk::from(git_diff_hunk)) {
20582048
// try to re-use old timestamp
@@ -2132,7 +2122,7 @@ fn get_applied_status(
21322122
file_path: filepath.clone(),
21332123
hunks: vec![Hunk::from(&hunk)
21342124
.with_timestamp(get_mtime(&mut mtimes, &filepath))
2135-
.with_hash(diff_hash(hunk.diff.as_str()).as_str())],
2125+
.with_hash(Hunk::hash(hunk.diff.as_str()).as_str())],
21362126
});
21372127
diffs_by_branch
21382128
.entry(virtual_branches[default_vbranch_pos].id)

0 commit comments

Comments
 (0)