Skip to content

Conversation

@sina-hide
Copy link
Contributor

@sina-hide sina-hide commented Dec 20, 2025

There is a regression bug regarding staging, discarding, and unstaging hunks. Depending on the data, these actions may be ignored or only partially executed.

The following script generates a repository in the current directory (named after the current directory) with four examples. Save the script as create-hunk-regression.sh, make it executable with chmod +x create-hunk-regression.sh, and start it with ./create-hunk-regression.sh. (Tested on Linux, should also work on Windows.)

#!/usr/bin/env bash

set -e

dir=$(basename $(pwd))

rm -rf "$dir"
git init -b main "$dir" && cd "$dir"

echo -n '1n2n3n4n' | tr 'rn' '\r\n' > cannot-stage-hunks.txt
echo -n '2rn3rn' | tr 'rn' '\r\n' > partially-stages-hunks.txt
echo -n 'test1rnrnrnrntest3rn' | tr 'rn' '\r\n' > partially-stages-or-discards-hunk.txt

echo -n '1rn2rn3rn4rn' | tr 'rn' '\r\n' > cannot-unstage-hunks.txt

git add .
git commit -m 'initial commit'

echo -n '1rn2n3n4rn' | tr 'rn' '\r\n' > cannot-stage-hunks.txt
echo -n '1rn2rn3rn4rn' | tr 'rn' '\r\n' > partially-stages-hunks.txt
echo -n 'test1rnrntest2rnrntest3rn' | tr 'rn' '\r\n' > partially-stages-or-discards-hunk.txt

echo -n '1n2rn3rn4n' | tr 'rn' '\r\n' > cannot-unstage-hunks.txt

git add cannot-unstage-hunks.txt

For the examples to fully work you should configure Git/SourceGit the following way:

  • set core.autocrlf to false
  • do not enable --ignore-cr-at-eol in diff
  • do not ignore wwhitespace changes

This regression was introduced in commit 5c9d96, where breaking up the diff into lines was changed from explicitly doing it to using the stream function ReadLineAsync. Whereas the explicit code handled CR and LF correctly, the stream function handles CR, CR/LF and LF the same way, so that for Windows newlines (CR/LF) the CR will be lost.

The fix for this regression is going back to the explicit code and ignoring ReadLineAsync for reading diffs.

This should fix at least partially issues #1950 and #1995.

There is a regression bug regarding staging, discarding, and unstaging
hunks.  Depending on the data, these actions may be ignored or only
partially executed.

The following script generates a repository in the current directory
(named after the current directory) with four examples.  Save the script
as `create-hunk-regression.sh`, make it executable with `chmod +x
create-hunk-regression.sh`, and start it with
`./create-hunk-regression.sh`.  (Tested on Linux, should also work on
Windows.)

```bash
#!/usr/bin/env bash

set -e

dir=$(basename $(pwd))

rm -rf "$dir"
git init -b main "$dir" && cd "$dir"

echo -n '1n2n3n4n' | tr 'rn' '\r\n' > cannot-stage-hunks.txt
echo -n '2rn3rn' | tr 'rn' '\r\n' > partially-stages-hunks.txt
echo -n 'test1rnrnrnrntest3rn' | tr 'rn' '\r\n' > partially-stages-or-discards-hunk.txt

echo -n '1rn2rn3rn4rn' | tr 'rn' '\r\n' > cannot-unstage-hunks.txt

git add .
git commit -m 'initial commit'

echo -n '1rn2n3n4rn' | tr 'rn' '\r\n' > cannot-stage-hunks.txt
echo -n '1rn2rn3rn4rn' | tr 'rn' '\r\n' > partially-stages-hunks.txt
echo -n 'test1rnrntest2rnrntest3rn' | tr 'rn' '\r\n' > partially-stages-or-discards-hunk.txt

echo -n '1n2rn3rn4n' | tr 'rn' '\r\n' > cannot-unstage-hunks.txt

git add cannot-unstage-hunks.txt
```

For the examples to fully work you should configure Git/SourceGit the
following way:

  - set `core.autocrlf` to `false`
  - do not enable `--ignore-cr-at-eol` in diff
  - do not ignore wwhitespace changes

This regression was introduced in commit 5c9d96, where breaking up the
diff into lines was changed from explicitly doing it to using the stream
function `ReadLineAsync`.  Whereas the explicit code handled CR and LF
correctly, the stream function handles CR, CR/LF and LF the same way, so
that for Windows newlines (CR/LF) the CR will be lost.

The fix for this regression is going back to the explicit code and
ignoring `ReadLineAsync` for reading diffs.

This should fix at least partially issues sourcegit-scm#1950 and sourcegit-scm#1995.
@love-linger love-linger self-assigned this Dec 20, 2025
@love-linger love-linger added the bug Something isn't working label Dec 20, 2025
@love-linger love-linger merged commit b84b793 into sourcegit-scm:develop Dec 20, 2025
14 checks passed
@sina-hide sina-hide deleted the hunk-regression branch December 20, 2025 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants