Skip to content

Commit 1bff8e8

Browse files
committed
Don't consume addition preceding unblamed hunk
The addition might match subsequent unblamed hunks.
1 parent 9ceb3ef commit 1bff8e8

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

gix-blame/tests/blame.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -534,21 +534,25 @@ fn process_change(
534534
*offset_in_destination -= number_of_lines_deleted;
535535
}
536536

537-
let line_range_in_next_destination =
538-
LineRange::with_offset(hunk.range_in_destination.clone(), *offset_in_destination);
539-
540-
new_hunks_to_blame.push(UnblamedHunk::from_destination(
541-
line_range_in_next_destination.into(),
542-
hunk.offset() + *offset_in_destination,
543-
));
544-
545537
let new_change = if added.end <= hunk.range_in_destination.start {
546538
None
547539
} else {
548540
Some(Change::Added(added.clone(), number_of_lines_deleted))
549541
};
550542

551-
(None, new_change)
543+
if added.end <= hunk.range_in_destination.start {
544+
(Some(hunk.clone()), new_change)
545+
} else {
546+
let line_range_in_next_destination =
547+
LineRange::with_offset(hunk.range_in_destination.clone(), *offset_in_destination);
548+
549+
new_hunks_to_blame.push(UnblamedHunk::from_destination(
550+
line_range_in_next_destination.into(),
551+
hunk.offset() + *offset_in_destination,
552+
));
553+
554+
(None, new_change)
555+
}
552556
}
553557
}
554558
}
@@ -1439,19 +1443,49 @@ fn process_change_works_no_overlap_2() {
14391443
Some(Change::Added(2..5, 0)),
14401444
);
14411445

1442-
assert_eq!(hunk, None);
1443-
assert_eq!(change, None);
1444-
assert_eq!(lines_blamed, vec![]);
14451446
assert_eq!(
1446-
new_hunks_to_blame,
1447-
vec![UnblamedHunk {
1447+
hunk,
1448+
Some(UnblamedHunk {
14481449
range_in_blamed_file: 9..11,
1449-
range_in_destination: 3..5
1450-
}]
1450+
range_in_destination: 6..8
1451+
})
14511452
);
1453+
assert_eq!(change, None);
1454+
assert_eq!(lines_blamed, vec![]);
1455+
assert_eq!(new_hunks_to_blame, vec![]);
14521456
assert_eq!(offset_in_destination, Offset::Added(3));
14531457
}
14541458

1459+
#[test]
1460+
fn process_change_works_no_overlap_3() {
1461+
let mut lines_blamed: Vec<BlameEntry> = vec![];
1462+
let mut new_hunks_to_blame: Vec<UnblamedHunk> = vec![];
1463+
let mut offset_in_destination: Offset = Offset::Added(0);
1464+
let suspect = ObjectId::null(gix_hash::Kind::Sha1);
1465+
1466+
let (hunk, change) = process_change(
1467+
&mut lines_blamed,
1468+
&mut new_hunks_to_blame,
1469+
&mut offset_in_destination,
1470+
suspect,
1471+
// range_in_destination: 5..15
1472+
Some(UnblamedHunk::new(4..15, Offset::Deleted(1))),
1473+
Some(Change::Added(4..5, 1)),
1474+
);
1475+
1476+
assert_eq!(
1477+
hunk,
1478+
Some(UnblamedHunk {
1479+
range_in_blamed_file: 4..15,
1480+
range_in_destination: 5..16
1481+
})
1482+
);
1483+
assert_eq!(change, None);
1484+
assert_eq!(lines_blamed, vec![]);
1485+
assert_eq!(new_hunks_to_blame, vec![]);
1486+
assert_eq!(offset_in_destination, Offset::Added(0));
1487+
}
1488+
14551489
#[test]
14561490
fn process_change_works_unchanged_hunk() {
14571491
let mut lines_blamed: Vec<BlameEntry> = vec![];

0 commit comments

Comments
 (0)