Skip to content

Commit

Permalink
Normalise newlines before diffing
Browse files Browse the repository at this point in the history
This produces good, consistent results on different trailing newlines
before, consistent with the behaviour before 7edd2a8 (see #755).
  • Loading branch information
Wilfred committed Oct 21, 2024
1 parent f58c9e0 commit 8cdb59f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
10 changes: 5 additions & 5 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sample_files/ada_1.adb sample_files/ada_2.adb
eb3b0e12e239ae33789136380e81406b -

sample_files/added_line_1.txt sample_files/added_line_2.txt
f935214c79833318d39364145d36d8e2 -
8a1587e6b5fc53f2ec2ac665a5d00372 -

sample_files/align_footer_1.txt sample_files/align_footer_2.txt
d640bd2de31e56a39f0efb92aff0f379 -
Expand All @@ -23,7 +23,7 @@ sample_files/bad_combine_1.rs sample_files/bad_combine_2.rs
f5051bf7d2b8afa3a677388cbd458891 -

sample_files/big_text_hunk_1.txt sample_files/big_text_hunk_2.txt
dcc22684c2a200afdd583d621b47dfa3 -
fc26d41a5ff771670e04033b177973d2 -

sample_files/change_outer_1.el sample_files/change_outer_2.el
2b9334a4cc72da63bba28eff958f0038 -
Expand Down Expand Up @@ -74,7 +74,7 @@ sample_files/erlang_1.erl sample_files/erlang_2.erl
dccdb8f65d2f099ab1a8cb66011376a2 -

sample_files/f_sharp_1.fs sample_files/f_sharp_2.fs
1a9173c15a42c1ebb9522109df172faf -
0a6651925acadf366f213d15968ae353 -

sample_files/hack_1.php sample_files/hack_2.php
c2bb0aa7d7b07d6ced79f6a5363e878b -
Expand Down Expand Up @@ -215,7 +215,7 @@ sample_files/racket_1.rkt sample_files/racket_2.rkt
605aec93fa7b89d08e5d8ed56ad3c1be -

sample_files/repeated_line_no_eol_1.txt sample_files/repeated_line_no_eol_2.txt
ac714893a2d28dc0204855d308972ccd -
3786f55d2c9b1897e866b4602e50408d -

sample_files/ruby_1.rb sample_files/ruby_2.rb
d4d591902030355656f5c18c78f965a6 -
Expand Down Expand Up @@ -302,5 +302,5 @@ sample_files/yaml_1.yaml sample_files/yaml_2.yaml
f068239fc7bade0e6de96d81136c1ac5 -

sample_files/zig_1.zig sample_files/zig_2.zig
e36d1ea126b8b68e3344434bb63f205e -
4516796003b81f35bfa57d84bb7c0cbe -

11 changes: 9 additions & 2 deletions src/display/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,15 @@ pub(crate) fn print(
let mut prev_lhs_line_num = None;
let mut prev_rhs_line_num = None;

let lhs_lines = split_on_newlines(lhs_src).collect::<Vec<_>>();
let rhs_lines = split_on_newlines(rhs_src).collect::<Vec<_>>();
let mut lhs_lines = split_on_newlines(lhs_src).collect::<Vec<_>>();
let mut rhs_lines = split_on_newlines(rhs_src).collect::<Vec<_>>();

if lhs_lines.last() == Some(&"") && lhs_lines.len() > 1 {
lhs_lines.pop();
}
if rhs_lines.last() == Some(&"") && rhs_lines.len() > 1 {
rhs_lines.pop();
}

let matched_lines = all_matched_lines_filled(lhs_mps, rhs_mps, &lhs_lines, &rhs_lines);
let mut matched_lines_to_print = &matched_lines[..];
Expand Down
23 changes: 14 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,21 @@ fn diff_file(
rhs_src.retain(|c| c != '\r');
}

// If "foo" is one line, is "foo\n" two lines? Generally we want
// to care about newlines when deciding whether content differs.
// Ensure that lhs_src and rhs_src both have trailing
// newlines.
//
// Ending a file with a trailing newline is extremely common
// though. If both files have a trailing newline, consider "foo\n"
// to be "foo" so we don't end up displaying a blank line on both
// sides.
if lhs_src.ends_with('\n') && rhs_src.ends_with('\n') {
lhs_src.pop();
rhs_src.pop();
// This is important when textually diffing files that don't have
// a trailing newline, e.g. "foo\n\bar\n" versus "foo". We want to
// consider `foo` to be unchanged in this case.
//
// Theoretically a tree-sitter parser coud change its AST due to
// the additional trailing newline, but it seems vanishingly
// unlikely.
if !lhs_src.is_empty() && !lhs_src.ends_with('\n') {
lhs_src.push('\n');
}
if !rhs_src.is_empty() && !rhs_src.ends_with('\n') {
rhs_src.push('\n');
}

let mut extra_info = renamed;
Expand Down

0 comments on commit 8cdb59f

Please sign in to comment.