Skip to content

Commit 40f9045

Browse files
Byroncruessler
authored andcommitted
refactor
* make test baseline more idiomatic * make some bits of the blame implementation more idiomatic
1 parent 0580840 commit 40f9045

File tree

2 files changed

+219
-242
lines changed

2 files changed

+219
-242
lines changed

gix-blame/src/lib.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl Sink for ChangeRecorder {
286286
}
287287

288288
pub fn process_change(
289-
lines_blamed: &mut Vec<BlameEntry>,
289+
out: &mut Vec<BlameEntry>,
290290
new_hunks_to_blame: &mut Vec<UnblamedHunk>,
291291
offset_in_destination: &mut Offset,
292292
suspect: ObjectId,
@@ -297,7 +297,7 @@ pub fn process_change(
297297
(Some(hunk), Some(Change::Unchanged(unchanged))) => {
298298
match (
299299
// Since `unchanged` is a range that is not inclusive at the end,
300-
// `unchanged.end` is not part of `unchanged`. The first line that is is
300+
// `unchanged.end` is not part of `unchanged`. The first line that is
301301
// `unchanged.end - 1`.
302302
hunk.range_in_destination.contains(&unchanged.start),
303303
(unchanged.end - 1) >= hunk.range_in_destination.start
@@ -370,7 +370,7 @@ pub fn process_change(
370370
*offset_in_destination += added.end - added.start;
371371
*offset_in_destination -= number_of_lines_deleted;
372372

373-
lines_blamed.push(BlameEntry::with_offset(added.clone(), suspect, hunk.offset()));
373+
out.push(BlameEntry::with_offset(added.clone(), suspect, hunk.offset()));
374374

375375
let new_hunk = if hunk.range_in_destination.end > added.end {
376376
Some(UnblamedHunk::from_destination(
@@ -401,7 +401,7 @@ pub fn process_change(
401401
));
402402
}
403403

404-
lines_blamed.push(BlameEntry::with_offset(
404+
out.push(BlameEntry::with_offset(
405405
added.start..hunk.range_in_destination.end,
406406
suspect,
407407
hunk.offset(),
@@ -419,7 +419,7 @@ pub fn process_change(
419419
// <---> (blamed)
420420
// <--> (new hunk)
421421

422-
lines_blamed.push(BlameEntry::with_offset(
422+
out.push(BlameEntry::with_offset(
423423
hunk.range_in_destination.start..added.end,
424424
suspect,
425425
hunk.offset(),
@@ -476,7 +476,7 @@ pub fn process_change(
476476
// <----------> (added)
477477
// <---> (blamed)
478478

479-
lines_blamed.push(BlameEntry::with_offset(
479+
out.push(BlameEntry::with_offset(
480480
hunk.range_in_destination.clone(),
481481
suspect,
482482
hunk.offset(),
@@ -562,37 +562,32 @@ pub fn process_change(
562562
}
563563

564564
pub fn process_changes(
565-
lines_blamed: &mut Vec<BlameEntry>,
566-
hunks_to_blame: Vec<UnblamedHunk>,
567-
changes: Vec<Change>,
565+
out: &mut Vec<BlameEntry>,
566+
hunks_to_blame: &[UnblamedHunk],
567+
changes: &[Change],
568568
suspect: ObjectId,
569569
) -> Vec<UnblamedHunk> {
570-
let mut hunks_iter = hunks_to_blame.iter();
571-
let mut changes_iter = changes.iter();
570+
let mut hunks_iter = hunks_to_blame.iter().cloned();
571+
let mut changes_iter = changes.iter().cloned();
572572

573-
let mut hunk: Option<UnblamedHunk> = hunks_iter.next().cloned();
574-
let mut change: Option<Change> = changes_iter.next().cloned();
573+
let mut hunk: Option<UnblamedHunk> = hunks_iter.next();
574+
let mut change: Option<Change> = changes_iter.next();
575575

576576
let mut new_hunks_to_blame: Vec<UnblamedHunk> = vec![];
577577
let mut offset_in_destination: Offset = Offset::Added(0);
578578

579579
loop {
580580
(hunk, change) = process_change(
581-
lines_blamed,
581+
out,
582582
&mut new_hunks_to_blame,
583583
&mut offset_in_destination,
584584
suspect,
585585
hunk,
586586
change,
587587
);
588588

589-
if hunk.is_none() {
590-
hunk = hunks_iter.next().cloned();
591-
}
592-
593-
if change.is_none() {
594-
change = changes_iter.next().cloned();
595-
}
589+
hunk = hunk.or_else(|| hunks_iter.next());
590+
change = change.or_else(|| changes_iter.next());
596591

597592
if hunk.is_none() && change.is_none() {
598593
break;
@@ -603,7 +598,7 @@ pub fn process_changes(
603598
}
604599

605600
/// This function merges adjacent blame entries. It merges entries that are adjacent both in the
606-
/// blamed file as well as in the original file that introduced them. This follows `git`’s
601+
/// blamed file and in the original file that introduced them. This follows `git`’s
607602
/// behaviour. `libgit2`, as of 2024-09-19, only checks whether two entries are adjacent in the
608603
/// blamed file which can result in different blames in certain edge cases. See [the commit][1]
609604
/// that introduced the extra check into `git` for context. See [this commit][2] for a way to test
@@ -723,7 +718,7 @@ pub fn blame_file(worktree_path: PathBuf, file_path: &BStr) -> Vec<BlameEntry> {
723718
0..number_of_lines.try_into().unwrap(),
724719
Offset::Added(0),
725720
)];
726-
let mut lines_blamed: Vec<BlameEntry> = vec![];
721+
let mut out: Vec<BlameEntry> = vec![];
727722

728723
loop {
729724
let item = traverse.next().unwrap().unwrap();
@@ -738,7 +733,7 @@ pub fn blame_file(worktree_path: PathBuf, file_path: &BStr) -> Vec<BlameEntry> {
738733
// remaining lines to it, even though we don’t explicitly check whether that is true
739734
// here. We could perhaps use `needed_to_obtain` to compare `suspect` against an empty
740735
// tree to validate this assumption.
741-
lines_blamed.extend(lines_to_blame.iter().map(|hunk| {
736+
out.extend(lines_to_blame.iter().map(|hunk| {
742737
BlameEntry::new(
743738
hunk.range_in_blamed_file.clone(),
744739
// TODO
@@ -800,7 +795,7 @@ pub fn blame_file(worktree_path: PathBuf, file_path: &BStr) -> Vec<BlameEntry> {
800795
if matches!(modification, gix_diff::tree::recorder::Change::Addition { .. }) {
801796
// Every line that has not been blamed yet on a commit, is expected to have been
802797
// added when the file was added to the repository.
803-
lines_blamed.extend(lines_to_blame.iter().map(|hunk| {
798+
out.extend(lines_to_blame.iter().map(|hunk| {
804799
BlameEntry::new(
805800
hunk.range_in_blamed_file.clone(),
806801
// TODO
@@ -840,21 +835,17 @@ pub fn blame_file(worktree_path: PathBuf, file_path: &BStr) -> Vec<BlameEntry> {
840835

841836
let outcome = resource_cache.prepare_diff().unwrap();
842837
let input = outcome.interned_input();
843-
844838
let number_of_lines_in_destination = input.after.len();
845-
846839
let change_recorder = ChangeRecorder::new(number_of_lines_in_destination.try_into().unwrap());
847-
848840
let changes = gix_diff::blob::diff(gix_diff::blob::Algorithm::Histogram, &input, change_recorder);
849-
850-
lines_to_blame = process_changes(&mut lines_blamed, lines_to_blame, changes, suspect);
841+
lines_to_blame = process_changes(&mut out, &lines_to_blame, &changes, suspect);
851842
}
852843

853844
assert_eq!(lines_to_blame, vec![]);
854845

855846
// I don’t know yet whether it would make sense to use a data structure instead that preserves
856847
// order on insertion.
857-
lines_blamed.sort_by(|a, b| a.range_in_blamed_file.start.cmp(&b.range_in_blamed_file.start));
848+
out.sort_by(|a, b| a.range_in_blamed_file.start.cmp(&b.range_in_blamed_file.start));
858849

859-
coalesce_blame_entries(lines_blamed)
850+
coalesce_blame_entries(out)
860851
}

0 commit comments

Comments
 (0)