Skip to content

diff: stop the trailing context at -U/-C lines when both files lack a final newline - #370

Merged
sylvestre merged 1 commit into
uutils:mainfrom
emirmrkaljevic:trailing-context-no-final-newline
Nov 10, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
emirmrkaljevic:trailing-context-no-final-newline

Conversation

@emirmrkaljevic

Copy link
Copy Markdown
Contributor

When both files end with the same unterminated line, the branch for that line flushed the context queue into the last hunk. Those queued lines are already past the trailing context, and since it's the last line there's no next hunk for them to lead into, so they should just be dropped. Now only the last line itself is added, and only when it's still within the trailing context. Same fix in unified_diff.rs and context_diff.rs.

Added a test for -u and -c from the issue, plus -U5/-U6 to check the last line still shows (with "\ No newline at end of file") when it's within the context. Output now matches GNU diff for these cases.

Fixes #364

… final newline

When the last lines of both files are equal and unterminated, the lines
waiting in the context queue were added to the hunk before them, so it got
more trailing context than asked for. Only add the last line when it is
still within the trailing context, in both unified and context output.

Fixes uutils#364
// the last line is still shown when it is within the trailing context
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.arg("diff")
.arg("-U5")

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 -C5 / -C6? only the unified side is tested at the boundary
and it could be a loop like the one above, no?

@sylvestre
sylvestre merged commit bb44ff5 into uutils:main Nov 10, 2026
31 checks passed
@sylvestre

Copy link
Copy Markdown
Collaborator

Thanks for your PR

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 add extra trailing context when both files lack a final newline

2 participants