From df4171c361ce01cb436d0d47da8145fd7b2114a7 Mon Sep 17 00:00:00 2001 From: Harp404 <234041561+Harp404@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:38:31 +0000 Subject: [PATCH] diff: print the lines of identical files with -D Ifdef output of two identical files is the file itself, as with GNU diff. Skip the identical-contents and same-file shortcuts for -D, and skip the same-file shortcut for -y too, which also prints the lines. --- src/diff.rs | 12 ++++++++---- tests/by-util/test_diff.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index f6b353cb..8da0f7f3 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -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(¶ms.from, ¶ms.to).unwrap_or(false) + || !prints_identical_lines + && same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false) { maybe_report_identical_files(); return Outcome::Identical; @@ -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; } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 314a0997..432920e5 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -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!();