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 1/3] 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!(); From d0075bc3046332c828f03f70a8787485d8b7eb2d Mon Sep 17 00:00:00 2001 From: Harp404 <234041561+Harp404@users.noreply.github.com> Date: Tue, 1 Dec 2026 08:35:46 +0000 Subject: [PATCH 2/3] diff: compare standard input given twice with itself for -D and -y --- src/diff.rs | 37 ++++++++++++++++++++++--------------- tests/by-util/test_diff.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 8da0f7f3..cb845a6b 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -83,10 +83,11 @@ 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); + // standard input given twice is compared with itself + let stdin_twice = params.from == "-" && params.to == "-"; // if from and to are the same file, no need to perform any comparison - if params.from == "-" && params.to == "-" - || !prints_identical_lines - && same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false) + if !prints_identical_lines + && (stdin_twice || same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false)) { maybe_report_identical_files(); return Outcome::Identical; @@ -118,18 +119,24 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { vec![] } }; - let to_content = match read_file_contents(¶ms.to) { - Ok(to_content) => to_content, - Err(e) - if e.kind() == io::ErrorKind::NotFound && params.missing_file == MissingFile::Empty => - { - to_missing = true; - vec![] - } - Err(e) => { - report_failure_to_read_input_file(¶ms.executable, ¶ms.to, &e); - io_error = true; - vec![] + let to_content = if stdin_twice { + // standard input can only be read once + from_content.clone() + } else { + match read_file_contents(¶ms.to) { + Ok(to_content) => to_content, + Err(e) + if e.kind() == io::ErrorKind::NotFound + && params.missing_file == MissingFile::Empty => + { + to_missing = true; + vec![] + } + Err(e) => { + report_failure_to_read_input_file(¶ms.executable, ¶ms.to, &e); + io_error = true; + vec![] + } } }; if io_error { diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 432920e5..03522eea 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -84,6 +84,35 @@ fn same_file_prints_lines() { .no_stdout(); } +// Standard input given as both files is read once and compared with itself, +// so side by side and ifdef output print its lines, as GNU diff does. +#[test] +fn stdin_twice_ifdef_prints_lines() { + new_ucmd!() + .args(&["-D", "X", "-", "-"]) + .pipe_in("foo\nbar\n") + .succeeds() + .stdout_is("foo\nbar\n"); +} + +#[test] +fn stdin_twice_side_by_side_prints_lines() { + new_ucmd!() + .args(&["-y", "--width=40", "-", "-"]) + .pipe_in("foo\nbar\n") + .succeeds() + .stdout_is("foo\t\t foo\nbar\t\t bar\n"); +} + +#[test] +fn stdin_twice_normal_prints_nothing() { + new_ucmd!() + .args(&["-", "-"]) + .pipe_in("foo\nbar\n") + .succeeds() + .no_stdout(); +} + #[rstest] fn differences(#[values(&[][..], &["-u"], &["-c"], &["-e"])] format: &[&str]) { let (at, ts) = at_and_ts!(); From c34f4b9a70688e86044c48d53b542f90e1a48520 Mon Sep 17 00:00:00 2001 From: Harp404 <234041561+Harp404@users.noreply.github.com> Date: Tue, 1 Dec 2026 08:36:15 +0000 Subject: [PATCH 3/3] diff: allow the unread standard input in the stdin_twice normal test --- tests/by-util/test_diff.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 03522eea..9e945da0 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -109,6 +109,8 @@ fn stdin_twice_normal_prints_nothing() { new_ucmd!() .args(&["-", "-"]) .pipe_in("foo\nbar\n") + // without lines to print, standard input is not read + .ignore_stdin_write_error() .succeeds() .no_stdout(); }