Skip to content

diff: print the lines of identical files with -D - #444

Open
Harp404 wants to merge 1 commit into
uutils:mainfrom
Harp404:fix-ifdef-identical-files
Open

Harp404 wants to merge 1 commit into
uutils:mainfrom
Harp404:fix-ifdef-identical-files

Conversation

@Harp404

@Harp404 Harp404 commented Nov 30, 2026

Copy link
Copy Markdown

diff -D NAME prints nothing for two identical files. GNU diff prints the file itself, since the merged output of two identical files is just their common lines:

$ printf 'foo\nbar\n' > a; cp a b
$ diff -D X a b
foo
bar
$ diffutils diff -D X a b
$

The same happens when one file is passed twice (diff -D X a a), and also for -y in that case. GNU prints the lines for both, while we returned early.

compare_files has two shortcuts that return before any output: one for the same file (or - -), and one for identical contents. The identical-contents one already made an exception for -y (and -e, for its missing-newline warning). This adds -D to it, and limits the same-file shortcut to formats that don't print the lines of identical files. - - keeps the shortcut, since stdin can only be read once.

I checked these against GNU diff 3.12 and the output and exit status match byte for byte:

  • -D X with identical files, the same file twice, files without a trailing newline, empty files, and identical binary files (with and without -a)
  • -D X with --brief and with -s, and differing files
  • -y with the same file twice, and -e, -u, -s, -q with the same file twice

The new tests no_differences_ifdef and same_file_prints_lines fail on main and pass with this change. cargo test passes, and cargo fmt --check and cargo clippy -- -D warnings are clean.

I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.

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.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 18:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Harp404

Harp404 commented Nov 30, 2026

Copy link
Copy Markdown
Author

Note: main currently fails three of its own tests, ifdef_diff_identical, ifdef_diff_identical_report and ifdef_diff_missing_newline (added with -D in #422, after the identical-contents shortcut from 915505b). They expect the lines of identical files to be printed, which is what this PR restores; with it cargo test passes in full (1074 passed, 0 failed), while on main those three fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants