Skip to content

Commit 914bf28

Browse files
authored
Merge pull request #1821 from GitoxideLabs/improvements
improvements
2 parents daa6d4a + 37582b0 commit 914bf28

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

gix-diff/src/blob/platform.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,25 @@ pub mod resource {
110110
}
111111
}
112112

113-
/// Produce an iterator over lines, separated by LF or CRLF, suitable to create tokens using
114-
/// [`imara_diff::intern::InternedInput`].
113+
/// Produce an iterator over lines, separated by LF or CRLF and thus keeping newlines.
114+
///
115+
/// Note that this will cause unusual diffs if a file didn't end in newline but lines were added
116+
/// on the other side.
117+
///
118+
/// Suitable to create tokens using [`imara_diff::intern::InternedInput`].
115119
pub fn intern_source(&self) -> imara_diff::sources::ByteLines<'a, true> {
116120
crate::blob::sources::byte_lines_with_terminator(self.data.as_slice().unwrap_or_default())
117121
}
122+
123+
/// Produce an iterator over lines, but remove LF or CRLF.
124+
///
125+
/// This produces the expected diffs when lines were added at the end of a file that didn't end
126+
/// with a newline before the change.
127+
///
128+
/// Suitable to create tokens using [`imara_diff::intern::InternedInput`].
129+
pub fn intern_source_strip_newline_separators(&self) -> imara_diff::sources::ByteLines<'a, false> {
130+
crate::blob::sources::byte_lines(self.data.as_slice().unwrap_or_default())
131+
}
118132
}
119133

120134
/// The data of a diffable resource, as it could be determined and computed previously.
@@ -228,8 +242,15 @@ pub mod prepare_diff {
228242

229243
impl<'a> Outcome<'a> {
230244
/// Produce an instance of an interner which `git` would use to perform diffs.
245+
///
246+
/// Note that newline separators will be removed to improve diff quality
247+
/// at the end of files that didn't have a newline, but had lines added
248+
/// past the end.
231249
pub fn interned_input(&self) -> imara_diff::intern::InternedInput<&'a [u8]> {
232-
crate::blob::intern::InternedInput::new(self.old.intern_source(), self.new.intern_source())
250+
crate::blob::intern::InternedInput::new(
251+
self.old.intern_source_strip_newline_separators(),
252+
self.new.intern_source_strip_newline_separators(),
253+
)
233254
}
234255
}
235256

gix/tests/gix/object/tree/diff.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,17 @@ fn changes_against_tree_modified() -> crate::Result {
5959
diff.lines(|hunk| {
6060
match hunk {
6161
lines::Change::Deletion { .. } => unreachable!("there was no deletion"),
62-
lines::Change::Addition { lines } => assert_eq!(
63-
lines,
64-
vec![expected_data[expected_previous_data.len()..].as_bytes().as_bstr()]
65-
),
62+
lines::Change::Addition { lines } => {
63+
assert_eq!(lines.len(), 1);
64+
assert_eq!(
65+
lines[0],
66+
expected_data[expected_previous_data.len()..]
67+
.as_bytes()
68+
.as_bstr()
69+
.trim(),
70+
"diffed lines don't have newlines anymore"
71+
);
72+
}
6673
lines::Change::Modification { .. } => unreachable!("there was no modification"),
6774
};
6875
Ok::<_, Infallible>(())

0 commit comments

Comments
 (0)