Skip to content

Commit 0b7c1dd

Browse files
committed
refactor
- fix probably critical, and untested error related to the last hunk
1 parent fcbdd65 commit 0b7c1dd

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

gix-blame/src/file/function.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use std::num::NonZeroU32;
22

3-
#[cfg(feature = "blob-experimental")]
4-
use gix_diff::blob::v2::Hunk;
5-
63
use gix_diff::{blob::intern::TokenSource, tree::Visit};
74
use gix_hash::ObjectId;
85
use gix_object::{
@@ -762,6 +759,8 @@ fn blob_changes(
762759
diff_algorithm: gix_diff::blob::Algorithm,
763760
stats: &mut Statistics,
764761
) -> Result<Vec<Change>, Error> {
762+
use std::ops::Range;
763+
765764
/// Record all [`Change`]s to learn about additions, deletions and unchanged portions of a *Source File*.
766765
struct ChangeRecorder {
767766
last_seen_after_end: u32,
@@ -781,8 +780,6 @@ fn blob_changes(
781780
}
782781
}
783782

784-
use std::ops::Range;
785-
786783
impl gix_diff::blob::Sink for ChangeRecorder {
787784
type Out = Vec<Change>;
788785

@@ -857,6 +854,8 @@ fn blob_changes(
857854
diff_algorithm: gix_diff::blob::Algorithm,
858855
stats: &mut Statistics,
859856
) -> Result<Vec<Change>, Error> {
857+
use gix_diff::blob::v2::Hunk;
858+
860859
resource_cache.set_resource(
861860
previous_oid,
862861
gix_object::tree::EntryKind::Blob,
@@ -886,36 +885,40 @@ fn blob_changes(
886885
let mut diff = gix_diff::blob::v2::Diff::compute(diff_algorithm, &input);
887886
diff.postprocess_lines(&input);
888887

889-
let changes = diff
890-
.hunks()
891-
.fold((Vec::new(), 0), |(mut hunks, mut last_seen_after_end), hunk| {
892-
let Hunk { before, after } = hunk;
888+
let mut last_seen_after_end = 0;
889+
let mut changes = diff.hunks().fold(Vec::new(), |mut hunks, hunk| {
890+
let Hunk { before, after } = hunk;
893891

894-
// This checks for unchanged hunks.
895-
if after.start > last_seen_after_end {
896-
hunks.push(Change::Unchanged(last_seen_after_end..after.start));
897-
}
892+
// This checks for unchanged hunks.
893+
if after.start > last_seen_after_end {
894+
hunks.push(Change::Unchanged(last_seen_after_end..after.start));
895+
}
898896

899-
match (!before.is_empty(), !after.is_empty()) {
900-
(_, true) => {
901-
hunks.push(Change::AddedOrReplaced(
902-
after.start..after.end,
903-
before.end - before.start,
904-
));
905-
}
906-
(true, false) => {
907-
hunks.push(Change::Deleted(after.start, before.end - before.start));
908-
}
909-
(false, false) => unreachable!("BUG: imara-diff provided a non-change"),
897+
match (!before.is_empty(), !after.is_empty()) {
898+
(_, true) => {
899+
hunks.push(Change::AddedOrReplaced(
900+
after.start..after.end,
901+
before.end - before.start,
902+
));
903+
}
904+
(true, false) => {
905+
hunks.push(Change::Deleted(after.start, before.end - before.start));
910906
}
907+
(false, false) => unreachable!("BUG: imara-diff provided a non-change"),
908+
}
911909

912-
last_seen_after_end = after.end;
910+
last_seen_after_end = after.end;
913911

914-
(hunks, last_seen_after_end)
915-
});
912+
hunks
913+
});
914+
915+
let total_number_of_lines = input.after.len() as u32;
916+
if input.after.len() > last_seen_after_end as usize {
917+
changes.push(Change::Unchanged(last_seen_after_end..total_number_of_lines));
918+
}
916919

917920
stats.blobs_diffed += 1;
918-
Ok(changes.0)
921+
Ok(changes)
919922
}
920923

921924
fn find_path_entry_in_commit(

gix-blame/tests/blame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ fn diff_disparity_imara_diff_v1() {
303303
diff_disparity_base();
304304
}
305305

306-
/// As of 2025-12-07, both tests are expected to pass. They use `imara-diff` 0.2 under the hood.
306+
/// As of 2025-12-07, both algorithms are expected to pass. They use `imara-diff` 0.2 under the hood.
307307
///
308308
/// Context: https://github.com/Byron/gitoxide/pull/1453#issuecomment-2371013904
309309
#[test]

0 commit comments

Comments
 (0)