Skip to content

Commit 0b7cd03

Browse files
committed
Consume addition when before hunk
1 parent 68e5f17 commit 0b7cd03

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

gix-blame/tests/blame.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,12 @@ fn process_change(
491491
//
492492
// <--> (hunk)
493493
// <----> (added)
494+
495+
if added.end <= hunk.range_in_destination.start {
496+
*offset_in_destination += added.end - added.start;
497+
*offset_in_destination -= number_of_lines_deleted;
498+
}
499+
494500
let line_range_in_next_destination =
495501
LineRange::with_offset(hunk.range_in_destination.clone(), *offset_in_destination);
496502

@@ -499,7 +505,13 @@ fn process_change(
499505
hunk.offset() + *offset_in_destination,
500506
));
501507

502-
(None, Some(Change::Added(added, number_of_lines_deleted)))
508+
let new_change = if added.end <= hunk.range_in_destination.start {
509+
None
510+
} else {
511+
Some(Change::Added(added.clone(), number_of_lines_deleted))
512+
};
513+
514+
(None, new_change)
503515
}
504516
}
505517
}
@@ -1209,6 +1221,36 @@ fn process_change_works_no_overlap() {
12091221
assert_eq!(offset_in_destination, Offset::Deleted(3));
12101222
}
12111223

1224+
#[test]
1225+
fn process_change_works_no_overlap_2() {
1226+
let mut lines_blamed: Vec<BlameEntry> = vec![];
1227+
let mut new_hunks_to_blame: Vec<UnblamedHunk> = vec![];
1228+
let mut offset_in_destination: Offset = Offset::Added(0);
1229+
let suspect = ObjectId::null(gix_hash::Kind::Sha1);
1230+
1231+
let (hunk, change) = process_change(
1232+
&mut lines_blamed,
1233+
&mut new_hunks_to_blame,
1234+
&mut offset_in_destination,
1235+
suspect,
1236+
// range_in_destination: 6..8
1237+
Some(UnblamedHunk::new(9..11, Offset::Added(3))),
1238+
Some(Change::Added(2..5, 0)),
1239+
);
1240+
1241+
assert_eq!(hunk, None);
1242+
assert_eq!(change, None);
1243+
assert_eq!(lines_blamed, vec![]);
1244+
assert_eq!(
1245+
new_hunks_to_blame,
1246+
vec![UnblamedHunk {
1247+
range_in_blamed_file: 9..11,
1248+
range_in_destination: 3..5
1249+
}]
1250+
);
1251+
assert_eq!(offset_in_destination, Offset::Added(3));
1252+
}
1253+
12121254
#[test]
12131255
fn process_change_works_unchanged_hunk() {
12141256
let mut lines_blamed: Vec<BlameEntry> = vec![];

0 commit comments

Comments
 (0)