Skip to content

fix: stage hunk with existing index file endings - #2012

Open
madebyly wants to merge 2 commits into
NeogitOrg:masterfrom
madebyly:fix-stage-hunk-diff
Open

madebyly wants to merge 2 commits into
NeogitOrg:masterfrom
madebyly:fix-stage-hunk-diff

Conversation

@madebyly

Copy link
Copy Markdown

(This fixes #2011)

Previously, patches would be generated for hunks the user wanted to stage with lines concatenated by either:

  • a specific platform line ending (currently only Windows) if no file exists at the path the hunk came from, or;
  • reading the file and determining the line endings of the file by checking the line ending of the first line, only.

While both of these worked for entirely new files and / or on platforms where the environment default line ending is the same as the indexed file's line endings, it would clobber the indexed file's line endings when the environment's default line endings differed from the index.

The reason why this only clobbers the indexed files line endings is because the patch is eventually applied only to the index, not the working copy.

This would result in the entire file being considered changed in the diff without additional options to filter out the noise.

This usually only affects Windows due to it using CRLF by default, and only when you work on a repository that primarily commits in LF line endings, but could've probably affected it in the reverse if it was also vice-versa, with the system being in LF while the repository was in CRLF, a setup typical for Windows-specific code repositories being contributed to cross-platform by developers on other platforms.

(NOTE: I say probably since I haven't had a chance to test that specific reverse scenario yet, I'm only working off of the core line ending mismatch problem)

This patch fixes the problem by leveraging Git to determine the existing indexed file's line endings and using those to concatenate the patch's lines with, falling back to the existing logic if the file isn't currently in the repository's index OR if the file has mixed line endings.

This keeps the line endings consistent as to what would be written to the repository index on commits.

This is one possible solution to the problem, one I've been using locally on my Windows machine to great effect.

Previously, patches would be generated for hunks the user wanted to
stage with lines concatenated by either:

- a specific platform line ending (currently only Windows) if no file
  exists at the path the hunk came from, or;
- reading the file and determining the line endings of the file by
  checking the line ending of the first line, only.

While both of these worked for entirely new files and / or on platforms
where the environment default line ending is the same as the indexed
file's line endings, it would clobber the indexed file's line endings
when the environment's default line endings differed from the index.

The reason why this only clobbers the indexed files line endings is
because the patch is eventually applied only to the index, not the
working copy.

This would result in the entire file being considered changed in the
diff without additional options to filter out the noise.

This usually only affects Windows due to it using `CRLF` by default, and
only when you work on a repository that primarily commits in `LF` line
endings, but could've probably affected it in the reverse if it was also
vice-versa, with the system being in `LF` while the repository was in
`CRLF`, a setup typical for Windows-specific code repositories being
contributed to cross-platform by developers on other platforms.

(NOTE: I say probably since I haven't had a chance to test that specific
reverse scenario yet, I'm only working off of the core line ending
mismatch problem)

This patch fixes the problem by leveraging Git to determine the existing
indexed file's line endings and using those to concatenate the patch's
lines with, falling back to the existing logic if the file isn't
currently in the repository's index OR if the file has mixed line
endings.

This keeps the line endings consistent as to what would be written to
the repository index on commits.
This was an erroneous change where the variable was used even though the
value returned from `git ls-files --eol` doesn't include the leading
slash.

Using the "constant" doesn't actually perform the match correctly.
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.

Wrong line endings applied to staged hunk when applying hunk to repository index

1 participant