Skip to content

Commit e164856

Browse files
committed
feat: forward line-diffing capabilities curtesy of the similar crate. (#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.
1 parent 963055b commit e164856

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-diff/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ doctest = false
1515
git-hash = { version = "^0.9.9", path = "../git-hash" }
1616
git-object = { version = "^0.20.3", path = "../git-object" }
1717
thiserror = "1.0.32"
18+
similar = { version = "2.2.0", features = ["bytes"] }
1819

1920
[dev-dependencies]
2021
git-odb = { path = "../git-odb" }

git-diff/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44

55
///
66
pub mod tree;
7+
8+
///
9+
pub mod lines;

git-diff/src/lines.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use git_object::bstr::BStr;
2+
use similar::TextDiff;
3+
4+
/// The crate powering file diffs.
5+
pub use similar;
6+
pub use similar::Algorithm;
7+
8+
/// Provide an iterator over the changes needed to turn `old` into `new` with `algorithm`.
9+
///
10+
/// See [the `similar` crate documentation][similar::TextDiffConfig::diff_lines()] for information on how to use the iterator.
11+
pub fn with<'old, 'new, 'bufs>(
12+
old: &'old BStr,
13+
new: &'new BStr,
14+
algorithm: Algorithm,
15+
) -> TextDiff<'old, 'new, 'bufs, [u8]> {
16+
similar::TextDiffConfig::default()
17+
.algorithm(algorithm)
18+
.diff_lines(old.as_ref(), new.as_ref())
19+
}
20+
21+
/// Provide an iterator over the changes needed to turn `old` into `new` with Myers algorithm, the default for `git`.
22+
///
23+
/// See [the `similar` crate documentation][similar::TextDiffConfig::diff_lines()] for information on how to use the iterator.
24+
pub fn myers<'old, 'new, 'bufs>(old: &'old BStr, new: &'new BStr) -> TextDiff<'old, 'new, 'bufs, [u8]> {
25+
with(old, new, Algorithm::Myers)
26+
}

0 commit comments

Comments
 (0)