Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome {
);
}
};
// Side by side and ifdef output print the lines of identical files, so
// they still need the contents.
let prints_identical_lines = matches!(params.format, Format::SideBySide | Format::Ifdef);
// if from and to are the same file, no need to perform any comparison
if params.from == "-" && params.to == "-"
|| same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
|| !prints_identical_lines
&& same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
{
maybe_report_identical_files();
return Outcome::Identical;
Expand Down Expand Up @@ -132,9 +136,9 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome {
return Outcome::Trouble;
}

// identical contents need no diff, unless side by side output shows their
// lines or an ed script warns about a missing newline
if from_content == to_content && !matches!(params.format, Format::SideBySide | Format::Ed) {
// identical contents need no diff, unless side by side or ifdef output
// shows their lines or an ed script warns about a missing newline
if from_content == to_content && !prints_identical_lines && params.format != Format::Ed {
maybe_report_identical_files();
return Outcome::Identical;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ fn no_differences_side_by_side() {
.stdout_is("foo\t\t foo\nbar\t\t bar\n");
}

// Ifdef output of two identical files is the file itself.
#[test]
fn no_differences_ifdef() {
let (at, ts) = at_and_ts!();
at.write("file1", "foo\nbar\n");
at.write("file2", "foo\nbar\n");
ts.ucmd()
.args(&["-D", "X", "file1", "file2"])
.succeeds()
.stdout_is("foo\nbar\n");
ts.ucmd()
.args(&["-D", "X", "-s", "file1", "file2"])
.succeeds()
.stdout_is("foo\nbar\nFiles file1 and file2 are identical\n");
}

// Comparing a file with itself still prints its lines in side by side and
// ifdef output.
#[test]
fn same_file_prints_lines() {
let (at, ts) = at_and_ts!();
at.write("file1", "foo\nbar\n");
ts.ucmd()
.args(&["-D", "X", "file1", "file1"])
.succeeds()
.stdout_is("foo\nbar\n");
ts.ucmd()
.args(&["-y", "--width=40", "file1", "file1"])
.succeeds()
.stdout_is("foo\t\t foo\nbar\t\t bar\n");
ts.ucmd()
.args(&["-u", "file1", "file1"])
.succeeds()
.no_stdout();
}

#[rstest]
fn differences(#[values(&[][..], &["-u"], &["-c"], &["-e"])] format: &[&str]) {
let (at, ts) = at_and_ts!();
Expand Down
Loading