Skip to content

Don't show a trailing blank line at the end of single-column diffs - #996

Open
StressTestor wants to merge 1 commit into
Wilfred:masterfrom
StressTestor:fix/phantom-trailing-blank-line
Open

Don't show a trailing blank line at the end of single-column diffs#996
StressTestor wants to merge 1 commit into
Wilfred:masterfrom
StressTestor:fix/phantom-trailing-blank-line

Conversation

@StressTestor

Copy link
Copy Markdown

what

diffing a file that ends in a newline against /dev/null (a file addition or removal) shows an extra blank line after the last line:

$ difft /dev/null file.txt
file.txt --- Text
1 foo
2
3 bar
4        <- phantom, the file only has 3 lines

this is #966.

why

split_on_newlines returns a trailing empty element when the source ends in a newline (documented and intentional). the two-column display already drops that trailing element, but the single-column path (used when one side is empty, e.g. a file addition or removal) rendered it as a real line.

fix

pull the existing trailing-empty check into a small has_trailing_blank_line helper and apply it in the single-column branches too. the two-column path keeps the same behaviour (it reuses its existing line vec, no extra allocation). a genuine trailing blank line, e.g. a file ending in two newlines, is preserved; only the empty element from the terminating newline is dropped.

tests

  • no_phantom_trailing_line in tests/cli.rs: diffs a 3-line file against /dev/null and asserts there is no line 4. fails before the fix, passes after.
  • a unit test for the helper.
  • regenerated the many_newlines entry in sample_files/compare.expected, which is a single-column (file addition) case whose output this corrects.

Fixes #966.

When a whole file is displayed in a single column (e.g. a file addition
or removal), a file ending in a newline rendered an extra empty line
after its last line. split_on_newlines yields a trailing empty element
for the terminating newline, and the single-column path didn't drop it,
unlike the two-column path. Extract that check into a helper and apply it
to both paths.

Also regenerates the many_newlines entry in the compare.expected
regression baseline, which is a single-column (file addition) case.

Fixes Wilfred#966.
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.

Extra blank line at the end of files

1 participant