Skip to content

Commit 6fe93c2

Browse files
committed
Use correct diff algorithm when diffing text (#450)
1 parent 71c6a20 commit 6fe93c2

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

Diff for: git-repository/src/object/tree/diff/change.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,41 @@ impl<'old, 'new> Event<'old, 'new> {
5151
}
5252
}
5353

54+
///
55+
pub mod event {
56+
///
57+
pub mod diff {
58+
/// The error returned by [`Event::diff()`][super::Event::diff()].
59+
#[derive(Debug, thiserror::Error)]
60+
#[allow(missing_docs)]
61+
pub enum Error {
62+
#[error("Could not find the previous object to diff against")]
63+
FindPrevious(#[from] crate::object::find::existing::Error),
64+
#[error("Could not obtain diff algorithm from configuration")]
65+
DiffAlgorithm(#[from] crate::config::diff::algorithm::Error),
66+
}
67+
}
68+
}
69+
5470
impl<'old, 'new> Event<'old, 'new> {
5571
/// Produce a platform for performing a line-diff, or `None` if this is not a [`Modification`][Event::Modification]
5672
/// or one of the entries to compare is not a blob.
57-
pub fn diff(&self) -> Option<Result<DiffPlatform<'old, 'new>, crate::object::find::existing::Error>> {
58-
// let algo = self.repo().config.diff_algorithm()?;
73+
pub fn diff(&self) -> Option<Result<DiffPlatform<'old, 'new>, event::diff::Error>> {
5974
match self {
6075
Event::Modification {
6176
previous_entry_mode: EntryMode::BlobExecutable | EntryMode::Blob,
6277
previous_id,
6378
entry_mode: EntryMode::BlobExecutable | EntryMode::Blob,
6479
id,
6580
} => match previous_id.object().and_then(|old| id.object().map(|new| (old, new))) {
66-
Ok((old, new)) => Some(Ok(DiffPlatform {
67-
old,
68-
new,
69-
algo: git_diff::text::Algorithm::Myers,
70-
})),
71-
Err(err) => Some(Err(err)),
81+
Ok((old, new)) => {
82+
let algo = match self.repo().config.diff_algorithm() {
83+
Ok(algo) => algo,
84+
Err(err) => return Some(Err(err.into())),
85+
};
86+
Some(Ok(DiffPlatform { old, new, algo }))
87+
}
88+
Err(err) => Some(Err(err.into())),
7289
},
7390
_ => None,
7491
}
@@ -88,7 +105,7 @@ impl<'old, 'new> DiffPlatform<'old, 'new> {
88105
git_diff::text::with(
89106
self.old.data.as_bstr(),
90107
self.new.data.as_bstr(),
91-
git_diff::text::Algorithm::Myers, // TODO: use diff.algorithm
108+
self.algo,
92109
// TODO: make use of `core.eol` and/or filters to do line-counting correctly. It's probably
93110
// OK to just know how these objects are saved to know what constitutes a line.
94111
git_diff::text::imara::intern::InternedInput::new,

Diff for: git-repository/tests/repository/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod worktree;
1111
#[test]
1212
fn size_in_memory() {
1313
let actual_size = std::mem::size_of::<Repository>();
14-
let limit = 810;
14+
let limit = 850;
1515
assert!(
1616
actual_size < limit,
1717
"size of Repository shouldn't change without us noticing, it's meant to be cloned: should have been below {:?}, was {}",

0 commit comments

Comments
 (0)