Skip to content

diff: use the largest context length given, like GNU diff - #341

Open
qodelicious wants to merge 2 commits into
uutils:mainfrom
qodelicious:diff-context-largest-count
Open

qodelicious wants to merge 2 commits into
uutils:mainfrom
qodelicious:diff-context-largest-count

Conversation

@qodelicious

Copy link
Copy Markdown
Contributor

When more than one context length is given, GNU diff uses the biggest, and a bare -u or -c counts as 3. We kept the last number given and ignored a bare -u or -c, so -U1 -u gave 1 line of context instead of 3, and -U2 -U1 gave 1 instead of 2.

Now the parser keeps the largest one. Added a unit test with a few orders of -u/-U and -c/-C, checked against GNU diff 3.10.

Fixes #340

Comment thread src/params.rs Outdated
}

#[test]
fn largest_context_count_wins() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please move them to tests/by-util/test_diff.rs , with the -U1 -u case from the issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved them to tests/by-util/test_diff.rs and added the -U1 -u case from the issue. The expected hunk headers match what GNU diff 3.10 prints for the same files.

Comment thread src/params.rs Outdated
(vec!["-C1", "-c"], 3),
(vec!["-c", "-C1"], 3),
(vec!["-C", "7", "-C2"], 7),
(vec!["--unified=1", "--unified"], 3),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could you please also cover --context=1 --context and the -5u form?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added both, plus --context --context=1 and -5c. --context=1 --context ends up with 3 lines and -5u with 5, same as GNU.

@sylvestre

Copy link
Copy Markdown
Collaborator

thanks for working on this!
pre-commit.ci is still red, could you please have a look?
please also run the upstream test suite and tell us if it changes anything:
TESTS= ./tests/run-upstream-testsuite.sh

Cover the -U1 -u case from uutils#340, --context=1 --context and the -5u form,
with the expected hunks checked against GNU diff 3.10.
@qodelicious
qodelicious force-pushed the diff-context-largest-count branch from 74e4f83 to 59741a1 Compare November 10, 2026 22:45
@qodelicious

Copy link
Copy Markdown
Contributor Author

pre-commit.ci was red because of a merge conflict with main, not a lint error. Rebased and it's green now.

Ran the whole upstream suite on main and on this branch. Both give 19 pass, 10 fail, 4 skip, and the output is identical for every test. None of the upstream tests use two context lengths though, so the new test in test_diff.rs is what covers this.

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.

diff: -U/-C given with -u/-c uses the wrong context length

2 participants