Skip to content

Commit

Permalink
rename!: DiffPlatform::text() to *::lines().
Browse files Browse the repository at this point in the history
This is more specific as one could also do character changes in a single
line, and it adapts the signature to the new `imra-diff` powered
mechanism, for a 2x speed boost.
  • Loading branch information
Byron committed Oct 28, 2022
1 parent fed47d4 commit c6f92c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
27 changes: 18 additions & 9 deletions git-repository/src/object/tree/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,24 @@ pub mod change {
}

impl<'old, 'new> DiffPlatform<'old, 'new> {
/// Create a platform for performing various tasks to diff text.
///
/// It can be used to traverse [all line changes](git_diff::lines::similar::TextDiff::iter_all_changes()) for example.
// TODO: How should this integrate with configurable algorithms? Maybe users should get it themselves and pass it here?
pub fn text<'bufs>(
&self,
algorithm: git_diff::lines::Algorithm,
) -> git_diff::lines::similar::TextDiff<'_, '_, 'bufs, [u8]> {
git_diff::lines::with(self.old.data.as_bstr(), self.new.data.as_bstr(), algorithm)
/// Perform a diff on lines between the old and the new version of a blob.
/// Note that the [`Sink`][git_diff::lines::imara_diff::Sink] implementation is
/// what makes the diff usable and relies heavily on what the caller requires.
pub fn lines<FnS, S>(&self, algorithm: git_diff::text::Algorithm, make_sink: FnS) -> S::Out
where
FnS: for<'a> FnOnce(&git_diff::text::imara::intern::InternedInput<&'a [u8]>) -> S,
S: git_diff::text::imara::Sink,
{
git_diff::text::with(
self.old.data.as_bstr(),
self.new.data.as_bstr(),
algorithm,
// TODO: make use of `core.eol` and/or filters to do line-counting correctly. It's probably
// OK to just know how these objects are saved to know what constitutes a line.
|old, new| git_diff::text::imara::intern::InternedInput::new(old, new),
make_sink,
)
.1
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions git-repository/tests/object/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ mod diff {
.diff()
.expect("changed file")
.expect("objects available")
.text(git::diff::lines::Algorithm::Myers)
.iter_all_changes()
.count();
assert_eq!(count, 2);
.lines(git::diff::text::Algorithm::Myers, |_| {
git_diff::text::imara::sink::Counter::default()
});
assert_eq!(count.insertions, 1);
assert_eq!(count.removals, 0);
Ok(Default::default())
})
.unwrap();
Expand Down

0 comments on commit c6f92c1

Please sign in to comment.