mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-19 23:52:05 +03:00
feat: Implement Hunk::hash() method and replace diff_hash()
This commit is contained in:
parent
36c555773c
commit
027684febb
@ -156,6 +156,16 @@ impl Hunk {
|
|||||||
pub fn shallow_eq(&self, other: &diff::GitHunk) -> bool {
|
pub fn shallow_eq(&self, other: &diff::GitHunk) -> bool {
|
||||||
self.start == other.new_start && self.end == other.new_start + other.new_lines
|
self.start == other.new_start && self.end == other.new_start + other.new_lines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn hash(diff: &str) -> String {
|
||||||
|
let addition = diff
|
||||||
|
.lines()
|
||||||
|
.skip(1) // skip the first line which is the diff header
|
||||||
|
.filter(|line| line.starts_with('+') || line.starts_with('-')) // exclude context lines
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n");
|
||||||
|
format!("{:x}", md5::compute(addition))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -1838,16 +1838,6 @@ fn get_mtime(cache: &mut HashMap<PathBuf, u128>, file_path: &PathBuf) -> u128 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_hash(diff: &str) -> String {
|
|
||||||
let addition = diff
|
|
||||||
.lines()
|
|
||||||
.skip(1) // skip the first line which is the diff header
|
|
||||||
.filter(|line| line.starts_with('+') || line.starts_with('-')) // exclude context lines
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n");
|
|
||||||
format!("{:x}", md5::compute(addition))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn virtual_hunks_by_filepath(
|
pub fn virtual_hunks_by_filepath(
|
||||||
project_path: &Path,
|
project_path: &Path,
|
||||||
diff: &HashMap<PathBuf, Vec<diff::GitHunk>>,
|
diff: &HashMap<PathBuf, Vec<diff::GitHunk>>,
|
||||||
@ -1866,7 +1856,7 @@ pub fn virtual_hunks_by_filepath(
|
|||||||
start: hunk.new_start,
|
start: hunk.new_start,
|
||||||
end: hunk.new_start + hunk.new_lines,
|
end: hunk.new_start + hunk.new_lines,
|
||||||
binary: hunk.binary,
|
binary: hunk.binary,
|
||||||
hash: diff_hash(&hunk.diff),
|
hash: Hunk::hash(&hunk.diff),
|
||||||
locked: false,
|
locked: false,
|
||||||
locked_to: None,
|
locked_to: None,
|
||||||
change_type: hunk.change_type,
|
change_type: hunk.change_type,
|
||||||
@ -2052,7 +2042,7 @@ fn get_applied_status(
|
|||||||
.filter_map(|claimed_hunk| {
|
.filter_map(|claimed_hunk| {
|
||||||
// if any of the current hunks intersects with the owned hunk, we want to keep it
|
// if any of the current hunks intersects with the owned hunk, we want to keep it
|
||||||
for (i, git_diff_hunk) in git_diff_hunks.iter().enumerate() {
|
for (i, git_diff_hunk) in git_diff_hunks.iter().enumerate() {
|
||||||
let hash = diff_hash(&git_diff_hunk.diff);
|
let hash = Hunk::hash(&git_diff_hunk.diff);
|
||||||
// Eq compares hashes first, and if one of the hunks lacks a hash, it compares line numbers
|
// Eq compares hashes first, and if one of the hunks lacks a hash, it compares line numbers
|
||||||
if claimed_hunk.eq(&Hunk::from(git_diff_hunk)) {
|
if claimed_hunk.eq(&Hunk::from(git_diff_hunk)) {
|
||||||
// try to re-use old timestamp
|
// try to re-use old timestamp
|
||||||
@ -2132,7 +2122,7 @@ fn get_applied_status(
|
|||||||
file_path: filepath.clone(),
|
file_path: filepath.clone(),
|
||||||
hunks: vec![Hunk::from(&hunk)
|
hunks: vec![Hunk::from(&hunk)
|
||||||
.with_timestamp(get_mtime(&mut mtimes, &filepath))
|
.with_timestamp(get_mtime(&mut mtimes, &filepath))
|
||||||
.with_hash(diff_hash(hunk.diff.as_str()).as_str())],
|
.with_hash(Hunk::hash(hunk.diff.as_str()).as_str())],
|
||||||
});
|
});
|
||||||
diffs_by_branch
|
diffs_by_branch
|
||||||
.entry(virtual_branches[default_vbranch_pos].id)
|
.entry(virtual_branches[default_vbranch_pos].id)
|
||||||
|
Loading…
Reference in New Issue
Block a user