Skip to content

Commit

Permalink
feat: forward line-diffing capabilities curtesy of the similar crat…
Browse files Browse the repository at this point in the history
…e. (#470)

This is a first and maybe the last step towards providing diffing
functionality, and it seems like the right choice to keep this in
similar and contribute there as needed. All algorithms are well
described and thus shouldn't be git-specific per-se, and `similar`
is the best the community has to offer.
  • Loading branch information
Byron committed Sep 19, 2022
1 parent 963055b commit e164856
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions git-diff/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ doctest = false
git-hash = { version = "^0.9.9", path = "../git-hash" }
git-object = { version = "^0.20.3", path = "../git-object" }
thiserror = "1.0.32"
similar = { version = "2.2.0", features = ["bytes"] }

[dev-dependencies]
git-odb = { path = "../git-odb" }
Expand Down
3 changes: 3 additions & 0 deletions git-diff/src/lib.rs
Expand Up @@ -4,3 +4,6 @@

///
pub mod tree;

///
pub mod lines;
26 changes: 26 additions & 0 deletions git-diff/src/lines.rs
@@ -0,0 +1,26 @@
use git_object::bstr::BStr;
use similar::TextDiff;

/// The crate powering file diffs.
pub use similar;
pub use similar::Algorithm;

/// Provide an iterator over the changes needed to turn `old` into `new` with `algorithm`.
///
/// See [the `similar` crate documentation][similar::TextDiffConfig::diff_lines()] for information on how to use the iterator.
pub fn with<'old, 'new, 'bufs>(
old: &'old BStr,
new: &'new BStr,
algorithm: Algorithm,
) -> TextDiff<'old, 'new, 'bufs, [u8]> {
similar::TextDiffConfig::default()
.algorithm(algorithm)
.diff_lines(old.as_ref(), new.as_ref())
}

/// Provide an iterator over the changes needed to turn `old` into `new` with Myers algorithm, the default for `git`.
///
/// See [the `similar` crate documentation][similar::TextDiffConfig::diff_lines()] for information on how to use the iterator.
pub fn myers<'old, 'new, 'bufs>(old: &'old BStr, new: &'new BStr) -> TextDiff<'old, 'new, 'bufs, [u8]> {
with(old, new, Algorithm::Myers)
}

0 comments on commit e164856

Please sign in to comment.