Skip to content

fix(staging): preserve no-trailing-newline when staging partial hunks#2979

Closed
Noethix55555 wants to merge 1 commit into
gitui-org:masterfrom
Noethix55555:fix/staging-eof-newline
Closed

fix(staging): preserve no-trailing-newline when staging partial hunks#2979
Noethix55555 wants to merge 1 commit into
gitui-org:masterfrom
Noethix55555:fix/staging-eof-newline

Conversation

@Noethix55555

Copy link
Copy Markdown

Summary

finish() in NewFromOldContent unconditionally appended \n to the reconstructed file content, even when the original file had no trailing newline. The DeleteEOFNL / AddEOFNL diff markers were silently discarded with a bare break, so staging or discarding a single line in a no-newline file would silently corrupt it by adding a newline.

  • Add trailing_newline: bool field to NewFromOldContent (default true).
  • In apply_selection, when DeleteEOFNL or AddEOFNL is encountered, set trailing_newline based on whether the change is being applied or reverted, then break.
  • In finish(), only append \n when self.trailing_newline is true.
  • Add test_stage_preserves_no_trailing_newline in stage_tracked.rs that stages a single changed line in a two-line file with no trailing newline and asserts the staged blob content is also newline-free.

Track whether the output file should end with a newline via a
trailing_newline field on NewFromOldContent (default true). When
DeleteEOFNL/AddEOFNL markers are encountered in apply_selection, set
the flag according to whether the removal or addition is being applied,
instead of unconditionally breaking and letting finish() always
append \n. Add a regression test that stages a single changed line in
a file with no trailing newline and asserts the staged blob is also
newline-free.
@Noethix55555

Copy link
Copy Markdown
Author

Subsumed by #2977, which already includes this fix.

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.

1 participant