Repository navigation
Conversation
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.
There was a problem hiding this comment.
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.
|
Note: |
| // 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 == "-" |
There was a problem hiding this comment.
does GNU print stdin for diff -D X - - too?
if so we could read it once and compare it with itself
There was a problem hiding this comment.
Yes, GNU diff 3.12 prints it for both -D and -y:
$ printf 'a\nb\n' | diff -D X - -
a
b
$ printf 'a\nb\n' | diff -y - -
a a
b b
Done in d0075bc (plus c34f4b9 for the test). When both files are -, standard input is now read once and compared with itself: to_content is a copy of from_content. Without -D or -y the early return stays, and nothing is read. New tests: stdin_twice_ifdef_prints_lines and stdin_twice_side_by_side_prints_lines fail without the change, and stdin_twice_normal_prints_nothing checks the early return.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
diff -D NAMEprints nothing for two identical files. GNU diff prints the file itself, since the merged output of two identical files is just their common lines:The same happens when one file is passed twice (
diff -D X a a), and also for-yin that case. GNU prints the lines for both, while we returned early.compare_fileshas 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-Dto 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 Xwith identical files, the same file twice, files without a trailing newline, empty files, and identical binary files (with and without-a)-D Xwith--briefand with-s, and differing files-ywith the same file twice, and-e,-u,-s,-qwith the same file twiceThe new tests
no_differences_ifdefandsame_file_prints_linesfail on main and pass with this change.cargo testpasses, andcargo fmt --checkandcargo clippy -- -D warningsare clean.I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.