Skip to content

Commit

Permalink
Consume addition when before hunk
Browse files Browse the repository at this point in the history
  • Loading branch information
cruessler committed Sep 17, 2024
1 parent 68e5f17 commit 0b7cd03
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion gix-blame/tests/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@ fn process_change(
//
// <--> (hunk)
// <----> (added)

if added.end <= hunk.range_in_destination.start {
*offset_in_destination += added.end - added.start;
*offset_in_destination -= number_of_lines_deleted;
}

let line_range_in_next_destination =
LineRange::with_offset(hunk.range_in_destination.clone(), *offset_in_destination);

Expand All @@ -499,7 +505,13 @@ fn process_change(
hunk.offset() + *offset_in_destination,
));

(None, Some(Change::Added(added, number_of_lines_deleted)))
let new_change = if added.end <= hunk.range_in_destination.start {
None
} else {
Some(Change::Added(added.clone(), number_of_lines_deleted))
};

(None, new_change)
}
}
}
Expand Down Expand Up @@ -1209,6 +1221,36 @@ fn process_change_works_no_overlap() {
assert_eq!(offset_in_destination, Offset::Deleted(3));
}

#[test]
fn process_change_works_no_overlap_2() {
let mut lines_blamed: Vec<BlameEntry> = vec![];
let mut new_hunks_to_blame: Vec<UnblamedHunk> = vec![];
let mut offset_in_destination: Offset = Offset::Added(0);
let suspect = ObjectId::null(gix_hash::Kind::Sha1);

let (hunk, change) = process_change(
&mut lines_blamed,
&mut new_hunks_to_blame,
&mut offset_in_destination,
suspect,
// range_in_destination: 6..8
Some(UnblamedHunk::new(9..11, Offset::Added(3))),
Some(Change::Added(2..5, 0)),
);

assert_eq!(hunk, None);
assert_eq!(change, None);
assert_eq!(lines_blamed, vec![]);
assert_eq!(
new_hunks_to_blame,
vec![UnblamedHunk {
range_in_blamed_file: 9..11,
range_in_destination: 3..5
}]
);
assert_eq!(offset_in_destination, Offset::Added(3));
}

#[test]
fn process_change_works_unchanged_hunk() {
let mut lines_blamed: Vec<BlameEntry> = vec![];
Expand Down

0 comments on commit 0b7cd03

Please sign in to comment.