← Back to Pull requests

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

Merged
Harp404 merged 2 files (112 lines) on Dec 01, 2026 18:50 UTC
View on GitHub
Harp404 Harp404 commented on Nov 30, 2026 18:38 UTC
Author

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.

    Diff: 112 lines (+95 / -17) across 2 files Reviewer: sylvestre Review duration: 1451m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.33) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.33 · 112 lines diff across 2 files
    0.33
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.93 · 4 substantive comment(s)
    0.93
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    92%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.283
    Real PR Conversation Timeline
    8 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 30, 2026 18:38 UTC
    Reviewer

    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 on Nov 30, 2026 19:14 UTC
    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.

    sylvestre sylvestre commented on code on Dec 01, 2026 06:58 UTC
    Code Review
    src/diff.rs:1
    @@ -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 == "-"

    does GNU print stdin for diff -D X - - too?
    if so we could read it once and compare it with itself

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Dec 01, 2026 08:36 UTC
    Reviewer

    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 on code on Dec 01, 2026 08:36 UTC
    Code Review
    src/diff.rs:1
    @@ -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 == "-"

    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.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Dec 01, 2026 08:36 UTC
    Reviewer

    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.

    sylvestre sylvestre commented on Dec 01, 2026 18:50 UTC
    Contributor

    Thanks for your PR

    Reviewers
    Assignees
    Harp404 Harp404
    Labels
    risk: low confidence: 92% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 32.7% (LOW) · Higher = larger blast radius
    32.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 92% · Lower = rushed or shallow review
    92%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.6% · Safe to merge
    2.6%