Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count removed bytes correctly #1100

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions gix/src/object/tree/diff/tracked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,80 @@ mod diff {
type Out = usize;

fn process_change(&mut self, before: Range<u32>, _after: Range<u32>) {
self.removed_bytes = self.input.before[before.start as usize..before.end as usize]
self.removed_bytes += self.input.before[before.start as usize..before.end as usize]
.iter()
.map(|token| self.input.interner[*token].len())
.sum();
.sum::<usize>();
}

fn finish(self) -> Self::Out {
self.removed_bytes
}
}

#[cfg(test)]
mod tests {
fn removed_bytes(before: &[u8], after: &[u8]) -> usize {
let input = gix_diff::blob::intern::InternedInput::new(before, after);
gix_diff::blob::diff(
gix_diff::blob::Algorithm::Myers,
&input,
super::Statistics {
removed_bytes: 0,
input: &input,
},
)
}

#[test]
fn counts_removed_bytes_correctly() {
assert_eq!(1, removed_bytes(b"a", b""));
assert_eq!(0, removed_bytes(b"", b"a"));

// need the inputs to have more than one "hunk"
// of differences to stress the fact that
// process_change is called multiple times
let before = b"
a
a
a
b
b
b
b
a
a
a
a
a
b
b
a
a
a
a
a
";
// it's `before`, with the "b" lines removed
let after = b"
a
a
a
a
a
a
a
a
a
a
a
a
a
";

assert_eq!(6, removed_bytes(before, after));
}
}
}

mod tree_to_events {
Expand Down