Skip to content

Fix applying custom patches to a dirty working tree #4674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2025

Conversation

stefanhaller
Copy link
Collaborator

  • PR Description

Applying or reverting a custom patch when one of the files contained in the patch had unstaged modifications in the working tree would fail with the confusing error message "does not match index", regardless of whether those modifications conflicted with the patch or not. You would expect this to just work if there are no conflicts, or to get the usual conflict markers if there are. It was possible to work around this by manually staging those files, but few people knew about this. Fix this by staging the files (after asking for confirmation).

Also, fix a minor problem where an auto-stash for the "move patch to index" command was forgotten to be dropped.

@stefanhaller stefanhaller added the bug Something isn't working label Jul 1, 2025
Copy link

codacy-production bot commented Jul 1, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for eebc39d1 98.60%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (eebc39d) Report Missing Report Missing Report Missing
Head commit (3df894e) 56921 49505 86.97%

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#4674) 215 212 98.60%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Footnotes

  1. Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.

Curiously, the function was never called so far, but we're going to use it later
in this branch.
This is functionality that works already, we only add the test for more complete
test coverage. However, there's a detail problem, and the test demonstrates
this: we keep the stash even if there was no conflict. We'll fix this next.
The tests show that this currently fails with the confusing error message "does
not match index", regardless of whether the patch conflicts with the
modifications or not. We'll improve this in the next commit.

I don't bother adding tests for reverting a patch, as the code is basically the
same as for apply.
Unlike moving a patch to the index, applying or reverting a patch didn't
auto-stash, which means that applying a patch when there's a modified (but
unstaged) file in the working tree would error out with the message "error:
file1: does not match index", regardless of whether those modifications conflict
with the patch or not.

To fix this, we *could* add auto-stashing like we do for the "move patch to
index" command. However, in this case we rather simply stage the affected files
(after asking for confirmation). This has a few advantages:

- it only changes the staging state of those files that are contained in the
patch (whereas auto-stashing always changes all files to unstaged)
- it doesn't unnecessarily show a confirmation if none of the modified files are
affected by the patch
- if the patch conflicts with the modified files, the conflicts were "backwards"
("ours" was the patch, "theirs" the modified file); it is more logical if "ours"
is the current state of the file, and "theirs" is the patch.

It's a little unfortunate that the behavior isn't exactly the same as for "move
patch to index", but for that one we do need the auto-stash because of the
rebase that runs behind the scenes.
@stefanhaller stefanhaller force-pushed the fix-applying-patches-to-dirty-working-copy branch from 068daaa to 3df894e Compare July 2, 2025 14:19
@stefanhaller stefanhaller merged commit 91d8c25 into master Jul 2, 2025
14 checks passed
@stefanhaller stefanhaller deleted the fix-applying-patches-to-dirty-working-copy branch July 2, 2025 14:32
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.

1 participant