Eli BarzilaySep 7, 2026, 04:44 UTC on loreDisclaimer, the following is written by an agent, but the bug is a real problem that I have.
With stash.index=true, an autostash that is applied successfully is nevertheless stored as a stash entry, and deleting MERGE_AUTOSTASH fails. A staged change at the time of the merge is required to trigger it.
Reproduction (independent of the reporter's configuration):
#!/bin/sh
set -e
export GIT_AUTHOR_NAME=A GIT_AUTHOR_EMAIL=a@b \
GIT_COMMITTER_NAME=A GIT_COMMITTER_EMAIL=a@b
rm -rf /tmp/gitbug && mkdir /tmp/gitbug && cd /tmp/gitbug
git init -q -b main up
cd up && echo a >u && echo z >z && git add . &&
git commit -qm base && cd ..
git clone -q up dn
cd up && echo more >>u && git commit -qam up2 && cd ../dn
echo staged >>z && git add z # a STAGED change is required
git fetch -q origin
git -c stash.index=true merge --ff-only --autostash origin/main
echo "--- git stash list:"; git stash listActual output:
Updating 34a5e40..84ccd9d
Created autostash: 71d4617
Fast-forward
u | 1 +
1 file changed, 1 insertion(+)
Applied autostash.
error: cannot lock ref 'MERGE_AUTOSTASH': unable to resolve
reference 'MERGE_AUTOSTASH'
--- git stash list:
stash@{0}: autostashExpected: the same without the error and with an empty stash list, as
happens with stash.index=false (the only change to the script).
The autostash is applied correctly, so nothing is lost: the leftover entry duplicates what is already in the working tree and index. The command also exits 0, so the error is easy to miss, and the entries accumulate one per merge.
Reported via `git merge` above for brevity, but the common way to meet this is `git pull` with rebase.autoStash and pull.rebase set: when the pull can fast-forward, builtin/pull.c:1164 hands off to `git merge --ff-only --autostash` rather than to rebase. Every such pull with something staged leaves an entry behind.
Analysis --------
Merge keeps its autostash in the MERGE_AUTOSTASH ref (builtin/merge.c:1675) and applies it from finish() (builtin/merge.c:540). apply_save_autostash_ref() resolves the ref, applies it, and then deletes it (sequencer.c:4821-4848).
The apply is a child process, `git stash apply <oid>` (sequencer.c:4737-4751). stash.index turns that into an --index apply, which takes the index-restoring branch of do_apply_stash() and calls reset_head() (builtin/stash.c:684-691), i.e. a `git reset --quiet --refresh` child (builtin/stash.c:455-467).
That reset has no pathspec, so it calls remove_branch_state() (builtin/reset.c:543) -> remove_merge_branch_state() (branch.c:829-838), whose last statement is
save_autostash_ref(r, "MERGE_AUTOSTASH");
which stores the autostash into refs/stash and deletes the ref -- in the middle of the very apply that was about to consume it. Control returns to apply_save_autostash_ref(), the apply reports success ("Applied autostash."), and its refs_delete_ref() then fails on a ref that is already gone, producing the error line.
The child's own explanation, "Autostash exists; creating a new stash entry." (sequencer.c:4775-4779), never reaches the user because the parent mutes the apply child's output (sequencer.c:4736-4738).
A staged change is required because with a clean index the stash's base and index trees are equal, has_index is cleared (builtin/stash.c:665-668), and reset_head() is never reached.
The rebase backend is unaffected: it keeps its autostash in the file .git/rebase-merge/autostash, which remove_merge_branch_state() does not touch. Only the merge (ref-based) autostash is exposed.
Possible directions, in case they are useful: remove_merge_branch_state() is about ending a merge, and `git stash apply --index` is not ending one -- having stash's reset_head() avoid the branch-state cleanup, or teaching an in-flight autostash apply to shield MERGE_AUTOSTASH, would both close it. Making apply_save_autostash_ref() tolerate a missing ref would silence the error but leave the duplicate entry.
Versions --------
Seen with git 2.55.0 on Linux (WSL2). The code paths above are unchanged on master as of 2026-09-07.
--
((x=>x(x))(x=>x(x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
Hi Eli,
Since I added stash.index configuration, thought I'd take a look. I'm out of my depth, but let's persevere anyway!
On Mon, Sep 7, 2026 at 12:47 AM Eli Barzilay <eli@barzilay.org> wrote:
Show 42 quoted lines
>
> Disclaimer, the following is written by an agent, but the bug is a
> real problem that I have.
>
>
> With stash.index=true, an autostash that is applied successfully is
> nevertheless stored as a stash entry, and deleting MERGE_AUTOSTASH
> fails. A staged change at the time of the merge is required to
> trigger it.
>
> Reproduction (independent of the reporter's configuration):
>
> #!/bin/sh
> set -e
> export GIT_AUTHOR_NAME=A GIT_AUTHOR_EMAIL=a@b \
> GIT_COMMITTER_NAME=A GIT_COMMITTER_EMAIL=a@b
> rm -rf /tmp/gitbug && mkdir /tmp/gitbug && cd /tmp/gitbug
> git init -q -b main up
> cd up && echo a >u && echo z >z && git add . &&
> git commit -qm base && cd ..
> git clone -q up dn
> cd up && echo more >>u && git commit -qam up2 && cd ../dn
> echo staged >>z && git add z # a STAGED change is required
> git fetch -q origin
> git -c stash.index=true merge --ff-only --autostash origin/main
> echo "--- git stash list:"; git stash list
>
> Actual output:
>
> Updating 34a5e40..84ccd9d
> Created autostash: 71d4617
> Fast-forward
> u | 1 +
> 1 file changed, 1 insertion(+)
> Applied autostash.
> error: cannot lock ref 'MERGE_AUTOSTASH': unable to resolve
> reference 'MERGE_AUTOSTASH'
> --- git stash list:
> stash@{0}: autostash
>
> Expected: the same without the error and with an empty stash list, as
> happens with stash.index=false (the only change to the script).I can reproduce this locally. Thanks for the helpful script. For some extra tweaking, I've put a "PATH=…:$PATH" assignment at the top that prepends my local Git build's bin-wrappers, then put "GIT_DEBUGGER=$1 GIT_TRACE2=$2" in front of the merge command; that way I can debug a few things.
Show 25 quoted lines
> Analysis
> --------
>
> Merge keeps its autostash in the MERGE_AUTOSTASH ref
> (builtin/merge.c:1675) and applies it from finish()
> (builtin/merge.c:540). apply_save_autostash_ref() resolves the ref,
> applies it, and then deletes it (sequencer.c:4821-4848).
>
> The apply is a child process, `git stash apply <oid>`
> (sequencer.c:4737-4751). stash.index turns that into an --index
> apply, which takes the index-restoring branch of do_apply_stash() and
> calls reset_head() (builtin/stash.c:684-691), i.e. a
> `git reset --quiet --refresh` child (builtin/stash.c:455-467).
>
> That reset has no pathspec, so it calls remove_branch_state()
> (builtin/reset.c:543) -> remove_merge_branch_state()
> (branch.c:829-838), whose last statement is
>
> save_autostash_ref(r, "MERGE_AUTOSTASH");
>
> which stores the autostash into refs/stash and deletes the ref -- in
> the middle of the very apply that was about to consume it. Control
> returns to apply_save_autostash_ref(), the apply reports success
> ("Applied autostash."), and its refs_delete_ref() then fails on a ref
> that is already gone, producing the error line.And this lines up with the code, I think. I find it a bit odd that "git stash apply --index" ends up getting to a reset mode that tries to throw away a bunch of branch state!
Ideally that would be simpler, I think, but I don't see an easy way to do it with the existing "git reset" subprocess.
I'm experimenting with something that swaps that out for a call to reset_working_tree(), but I don't think I've gotten it quite right for this bug yet (let alone run other test cases that might be affected by this change).
BTW, it's really weird to me that the reset manual doesn't mention all these "extra" cleanups reset does via remove_merge_branch_state()!
Show 6 quoted lines
> Possible directions, in case they are useful: remove_merge_branch_state()
> is about ending a merge, and `git stash apply --index` is not ending
> one -- having stash's reset_head() avoid the branch-state cleanup, or
> teaching an in-flight autostash apply to shield MERGE_AUTOSTASH, would
> both close it. Making apply_save_autostash_ref() tolerate a missing
> ref would silence the error but leave the duplicate entry.
I also thought briefly about disabling stash.index for a merge autostash, but that's really papering over things, I think.
I'll keep noodling on this (hopefully tomorrow morning), but in the meantime input from others welcome :)
--
D. Ben Knoble
Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
Hi Ben
On 15/09/2026 22:16, D. Ben Knoble wrote:
Show 5 quoted lines
>
> I'm experimenting with something that swaps that out for a call to
> reset_working_tree(), but I don't think I've gotten it quite right for
> this bug yet (let alone run other test cases that might be affected by
> this change).
It looks like stash has its own unpack_trees() wrapper, so I think the simplest fix is to replace reset_head() with
reset_tree(&c_tree, 0, 1);
Taking a step back, this code applies the stashed index changes into the current index, writes the result to a tree and then resets the index to HEAD. We could avoid touching the index at all if we used merge_incore_nonrecursive() to cherry pick the index changes instead. That way we'd get a proper three-way merge and avoid spawning subprocesses for "git diff-tree", "git apply --cached", and "git reset". We're already using merge_ort_nonrecursive() to merge the working tree changes in that function so we have nearly everything we need already set up to merge the index changes as well. Essentially, when merging the index, we just need to call merge_incore_nonrecursive() instead of merge_ort_nonrecursive() and use info->i_tree instead of info->w_tree.
> BTW, it's really weird to me that the reset manual doesn't mention all
> these "extra" cleanups reset does via remove_merge_branch_state()!
Agreed, I think it comes from "git foo --abort" calling "git reset (--merge|--hard)" though that doesn't really explain why a mixed reset also removes the branch state.
Thanks
Phillip
Show 13 quoted lines
>> Possible directions, in case they are useful: remove_merge_branch_state()
>> is about ending a merge, and `git stash apply --index` is not ending
>> one -- having stash's reset_head() avoid the branch-state cleanup, or
>> teaching an in-flight autostash apply to shield MERGE_AUTOSTASH, would
>> both close it. Making apply_save_autostash_ref() tolerate a missing
>> ref would silence the error but leave the duplicate entry.
>
> I also thought briefly about disabling stash.index for a merge
> autostash, but that's really papering over things, I think.
>
> I'll keep noodling on this (hopefully tomorrow morning), but in the
> meantime input from others welcome :)
>
Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
Show 15 quoted lines
> Le 16 sept. 2026 à 09:35, Phillip Wood <phillip.wood123@gmail.com> a écrit :
>
> Hi Ben
>
>> On 15/09/2026 22:16, D. Ben Knoble wrote:
>> I'm experimenting with something that swaps that out for a call to
>> reset_working_tree(), but I don't think I've gotten it quite right for
>> this bug yet (let alone run other test cases that might be affected by
>> this change).
>
> It looks like stash has its own unpack_trees() wrapper, so I think the simplest fix is to replace reset_head() with
>
> reset_tree(&c_tree, 0, 1);
>
> Taking a step back, this code applies the stashed index changes into the current index, writes the result to a tree and then resets the index to HEAD. We could avoid touching the index at all if we used merge_incore_nonrecursive() to cherry pick the index changes instead. That way we'd get a proper three-way merge and avoid spawning subprocesses for "git diff-tree", "git apply --cached", and "git reset". We're already using merge_ort_nonrecursive() to merge the working tree changes in that function so we have nearly everything we need already set up to merge the index changes as well. Essentially, when merging the index, we just need to call merge_incore_nonrecursive() instead of merge_ort_nonrecursive() and use info->i_tree instead of info->w_tree.
Wow, I wish I’d had this info this morning! I spent a couple hours trying to understand this flow and still don’t have it in my head :) Thanks for the pointers.
With the way I batch my side project time, it’ll be tomorrow before I get to trying to make and test patches for this, but I’ excited now.
I may try to summarize my own notes (= questions about the existing code) and send those out later today, though, since I’d love to make my understanding line up with yours!
>> BTW, it's really weird to me that the reset manual doesn't mention all
>> these "extra" cleanups reset does via remove_merge_branch_state()!
>
> Agreed, I think it comes from "git foo --abort" calling "git reset (--merge|--hard)" though that doesn't really explain why a mixed reset also removes the branch state.
Yeah, that abort bit makes sense. I wonder if we should have had a better side-channel for communicating that, but I’m a bit too afraid to touch that for now ;)
> Thanks
>
> Phillip
Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
[Note from the peanut gallery since I can't spend time diving into the code -- that's result of collecting dead autostashes is the only thorn in my joy of discovering `stash.index` + `rebase.autoStash`. So while I can't spend time in the code, I'll be happy to try patches or whatever if it helps...]
On Wed, Sep 16, 2026 at 10:30 AM Ben Knoble <ben.knoble@gmail.com> wrote:
Show 37 quoted lines
>
> Hi Phillip,
>
> > Le 16 sept. 2026 à 09:35, Phillip Wood <phillip.wood123@gmail.com> a écrit :
> >
> > Hi Ben
> >
> >> On 15/09/2026 22:16, D. Ben Knoble wrote:
> >> I'm experimenting with something that swaps that out for a call to
> >> reset_working_tree(), but I don't think I've gotten it quite right for
> >> this bug yet (let alone run other test cases that might be affected by
> >> this change).
> >
> > It looks like stash has its own unpack_trees() wrapper, so I think the simplest fix is to replace reset_head() with
> >
> > reset_tree(&c_tree, 0, 1);
> >
> > Taking a step back, this code applies the stashed index changes into the current index, writes the result to a tree and then resets the index to HEAD. We could avoid touching the index at all if we used merge_incore_nonrecursive() to cherry pick the index changes instead. That way we'd get a proper three-way merge and avoid spawning subprocesses for "git diff-tree", "git apply --cached", and "git reset". We're already using merge_ort_nonrecursive() to merge the working tree changes in that function so we have nearly everything we need already set up to merge the index changes as well. Essentially, when merging the index, we just need to call merge_incore_nonrecursive() instead of merge_ort_nonrecursive() and use info->i_tree instead of info->w_tree.
>
> Wow, I wish I’d had this info this morning! I spent a couple hours trying to understand this flow and still don’t have it in my head :) Thanks for the pointers.
>
> With the way I batch my side project time, it’ll be tomorrow before I get to trying to make and test patches for this, but I’ excited now.
>
> I may try to summarize my own notes (= questions about the existing code) and send those out later today, though, since I’d love to make my understanding line up with yours!
>
> >> BTW, it's really weird to me that the reset manual doesn't mention all
> >> these "extra" cleanups reset does via remove_merge_branch_state()!
> >
> > Agreed, I think it comes from "git foo --abort" calling "git reset (--merge|--hard)" though that doesn't really explain why a mixed reset also removes the branch state.
>
> Yeah, that abort bit makes sense. I wonder if we should have had a better side-channel for communicating that, but I’m a bit too afraid to touch that for now ;)
>
> > Thanks
> >
> > Phillip
>
> Thank *you*!
--
((x=>x(x))(x=>x(x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
Hi Ben
On 16/09/2026 15:30, Ben Knoble wrote:
Show 13 quoted lines
>> Le 16 sept. 2026 à 09:35, Phillip Wood <phillip.wood123@gmail.com> a écrit :
>> Taking a step back, this code applies the stashed index changes
>> into the current index, writes the result to a tree and then resets
>> the index to HEAD. We could avoid touching the index at all if we
>> used merge_incore_nonrecursive() to cherry pick the index changes
>> instead. That way we'd get a proper three-way merge and avoid
>> spawning subprocesses for "git diff-tree", "git apply --cached",
>> and "git reset". We're already using merge_ort_nonrecursive() to
>> merge the working tree changes in that function so we have nearly
>> everything we need already set up to merge the index changes as
>> well. Essentially, when merging the index, we just need to call
>> merge_incore_nonrecursive() instead of merge_ort_nonrecursive()
>> and use info->i_tree instead of info->w_tree.
>
> Wow, I wish I’d had this info this morning! I spent a couple hours
> trying to understand this flow and still don’t have it in my head :)
> Thanks for the pointers.
It is a bit confusing the way it updates the index, then resets it only
to update it again at the end. I don't think we can avoid that though if
we want to error out when there are conflicts merging the index.
> I may try to summarize my own notes (= questions about the existing
> code) and send those out later today, though, since I’d love to make
> my understanding line up with yours!
I'm happy to try and answer any questions, on or off the list - it would
be really nice if we can merge the index changes and avoid a bunch a
subprocesses.
Thanks
Phillip
Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
Ok, here we go.
On Thu, Sep 17, 2026 at 5:24 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 8 quoted lines
>
> Hi Ben
>
> On 16/09/2026 15:30, Ben Knoble wrote:
> >> Le 16 sept. 2026 à 09:35, Phillip Wood <phillip.wood123@gmail.com> a écrit :
> >> Taking a step back, this code applies the stashed index changes
> >> into the current index, writes the result to a tree and then resets
> >> the index to HEAD.
I noted that we save the current index (?) into c_tree with write_index_as_tree(), then feed diff_tree_binary() into git apply --cache, aka applying the stashed index changes.
[from my notes]
- that is, diff H..I in the diagram from the manual: A stash entry is represented as a commit whose tree records the state
of the working directory, and its first parent is the commit at HEAD
when the entry was created. The tree of the second parent records the
state of the index when the entry is made, and it is made a child of
the HEAD commit. The ancestry graph looks like this: .----W
/ /
-----H----I where H is the HEAD commit, I is a commit that records the state of the
index, and W is a commit that records the state of the working tree.We then have a discard_index()/repo_read_index() pair, which I assume is refreshing the in-memory index? Followed by write_index_as_tree(&index_tree)… so that must be the "save the results of applying the stashed changes" part.
What I totally misunderstood was "reset the index to HEAD"---of course that's what "git reset" does! But I didn't understand why until…
> It is a bit confusing the way it updates the index, then resets it only
> to update it again at the end. I don't think we can avoid that though if
> we want to error out when there are conflicts merging the index.
…which now makes (some) sense. That also explains why my attempts to use reset_working_tree() in various forms could never work :) I had the wrong idea entirely. But it seemed in my debugging like something was touching the working tree, so I wish I had kept better notes.
Just finishing up the flow:
- we then refresh the in-memory index again (we just reset the on-disk
index to HEAD)
- we continue on with a "normal" stash apply merge of c_tree (old
index), w_tree (W), and b_tree (which I assume is H?); this applies
the stashed working tree changes on the current state?
- [skipping ahead] in the index case, we reset_tree(&index_tree, 0,
0), restoring the stashed index changes. Sans doc comments for
unpack_trees() beyond "N-way merge len trees […] resulting index […]",
I haven't puzzled out what's going on, but it _looks_ like we merge a
single tree, possibly with the original index (opts.src_index) and
write to a destination which is the repository's index.
It's extra unclear what the reset bit is applied to in
> It looks like stash has its own unpack_trees() wrapper, so I think the
> simplest fix is to replace reset_head() with
>
> reset_tree(&c_tree, 0, 1);
I'm not sure if that is a pre-merge reset, post-merge reset, or something in between. Unfortunately, a whole bunch of tests fail (6 files) with this suggestion :/
Summary of Failures:
339/1062 git:t3904-stash-patch ERROR
0.45s exit status 1
503/1062 git:t3903-stash ERROR
4.40s exit status 1
763/1062 git:t6424-merge-unrelated-index-changes ERROR
0.90s exit status 1
778/1062 git:t6402-merge-rename ERROR
2.06s exit status 1
864/1062 git:t1092-sparse-checkout-compatibility ERROR
26.19s exit status 1
868/1062 git:t7611-merge-abort ERROR
0.45s exit status 1It _also_ doesn't make the bug go away, hm.
Show 10 quoted lines
> >> We could avoid touching the index at all if we
> >> used merge_incore_nonrecursive() to cherry pick the index changes
> >> instead. That way we'd get a proper three-way merge and avoid
> >> spawning subprocesses for "git diff-tree", "git apply --cached",
> >> and "git reset". We're already using merge_ort_nonrecursive() to
> >> merge the working tree changes in that function so we have nearly
> >> everything we need already set up to merge the index changes as
> >> well. Essentially, when merging the index, we just need to call
> >> merge_incore_nonrecursive() instead of merge_ort_nonrecursive()
> >> and use info->i_tree instead of info->w_tree.
If I'm following this, the suggestion is to replace (parts of) the early "if (index)" block with a merge_incore_nonrecursive() to merge index changes, reporting conflicts as we do today, and saving the tree for later… and this would not touch the real index, so we wouldn't have to reset at all? Interesting!
The attached patch [Gmail headaches, sorry], which needs some polishing [*], passes tests and fixes the bug! Yahoo. I'll send a series later, tomorrow probably. (It won't apply directly, because it's on top of the experimental reset_tree() version, but resolving conflicts should be easy.)
[*] namely, the log message, some tiny first cleanups, and removing now-unused functions
--
D. Ben Knoble
From d53a04217dd2c8b967e71717a020cff1c8668fa1 Mon Sep 17 00:00:00 2001
Message-ID: <d53a04217dd2c8b967e71717a020cff1c8668fa1.1789650783.git.ben.knoble@gmail.com>
From: "D. Ben Knoble" <ben.knoble@gmail.com>
Date: Thu, 17 Sep 2026 09:12:50 -0400
Subject: [PATCH] wip: fix stash bug
Signed-off-by: D. Ben Knoble <ben.knoble@gmail.com>
---
builtin/stash.c | 43 +++++++++++++++++++++++++------------------
1 file changed, 25 insertions(+), 18 deletions(-)
diff --git a/builtin/stash.c b/builtin/stash.c
index b0895687d9..87d764ee32 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -655,29 +655,36 @@ static enum stash_apply_result do_apply_stash(const char *prefix,
oideq(&c_tree, &info->i_tree)) {
has_index = 0;
} else {
- struct strbuf out = STRBUF_INIT;
+ struct merge_result result = { 0 };
- if (diff_tree_binary(&out, &info->w_commit)) {
- strbuf_release(&out);
- return error(_("could not generate diff %s^!."),
- oid_to_hex(&info->w_commit));
- }
+ init_ui_merge_options(&o, the_repository);
- ret = apply_cached(&out);
- strbuf_release(&out);
- if (ret)
+ o.branch1 = label_ours ? label_ours : "Updated upstream";
+ o.branch2 = "Stashed index changes";
+ o.ancestor = label_base ? label_base : "Stash base";
+
+ if (oideq(&info->b_tree, &c_tree))
+ o.branch1 = "Version stash was based on";
+
+ if (quiet)
+ o.verbosity = 0;
+
+ if (o.verbosity >= 3)
+ printf_ln(_("Merging %s with %s"),
+ o.branch1, o.branch2);
+
+ head = lookup_tree(o.repo, &c_tree);
+ merge = lookup_tree(o.repo, &info->i_tree);
+ merge_base = lookup_tree(o.repo, &info->b_tree);
+
+ merge_incore_nonrecursive(&o, head, merge, merge_base,
+ &result);
+
+ if (!result.clean)
return error(_("conflicts in index. "
"Try without --index."));
- discard_index(the_repository->index);
- repo_read_index(the_repository);
- if (write_index_as_tree(&index_tree, the_repository->index,
- repo_get_index_file(the_repository), 0, NULL))
- return error(_("could not save index tree"));
-
- reset_tree(&c_tree, 0, 1);
- discard_index(the_repository->index);
- repo_read_index(the_repository);
+ index_tree = result.tree->object.oid;
}
}
base-commit: 339ab2a8f14c0c304ae2f28df1a859f3d2cf610c
prerequisite-patch-id: 74b6847e4daaa8814f5975ce3d077bede199504b
prerequisite-patch-id: a2465f4d108ada71b84c8a87e3506dc3e3b52683
--
2.55.0.1003.g10538fe699.dirtyRe: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
On 17/09/2026 14:15, D. Ben Knoble wrote:
> Ok, here we go.
Show 29 quoted lines
> On Thu, Sep 17, 2026 at 5:24 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>> On 16/09/2026 15:30, Ben Knoble wrote:
>>>> Le 16 sept. 2026 à 09:35, Phillip Wood <phillip.wood123@gmail.com> a écrit :
>>>> Taking a step back, this code applies the stashed index changes
>>>> into the current index, writes the result to a tree and then resets
>>>> the index to HEAD.
>
> I noted that we save the current index (?) into c_tree with
> write_index_as_tree(), then feed diff_tree_binary() into git apply
> --cache, aka applying the stashed index changes.
>
> [from my notes]
> - that is, diff H..I in the diagram from the manual:
>
> A stash entry is represented as a commit whose tree records the state
> of the working directory, and its first parent is the commit at HEAD
> when the entry was created. The tree of the second parent records the
> state of the index when the entry is made, and it is made a child of
> the HEAD commit. The ancestry graph looks like this:
>
> .----W
> / /
> -----H----I
>
> where H is the HEAD commit, I is a commit that records the state of the
> index, and W is a commit that records the state of the working tree.
>
> We then have a discard_index()/repo_read_index() pair, which I assume
> is refreshing the in-memory index?
Yep - because we're forking another git process that updates the index we need to read the index file again when that process exits.
> Followed by
> write_index_as_tree(&index_tree)… so that must be the "save the
> results of applying the stashed changes" part.
Show 19 quoted lines
> What I totally misunderstood was "reset the index to HEAD"---of course
> that's what "git reset" does! But I didn't understand why until…
>
>> It is a bit confusing the way it updates the index, then resets it only
>> to update it again at the end. I don't think we can avoid that though if
>> we want to error out when there are conflicts merging the index.
>
> …which now makes (some) sense. That also explains why my attempts to
> use reset_working_tree() in various forms could never work :) I had
> the wrong idea entirely. But it seemed in my debugging like something
> was touching the working tree, so I wish I had kept better notes.
>
> Just finishing up the flow:
>
> - we then refresh the in-memory index again (we just reset the on-disk
> index to HEAD)
> - we continue on with a "normal" stash apply merge of c_tree (old
> index), w_tree (W), and b_tree (which I assume is H?); this applies
> the stashed working tree changes on the current state?
Yes, b_tree is (H) and it cherry-picks the stashed working tree changes
on to the current worktree. c_tree is the current index at that point.
> - [skipping ahead] in the index case, we reset_tree(&index_tree, 0,
> 0), restoring the stashed index changes.
Show 5 quoted lines
> Sans doc comments for
> unpack_trees() beyond "N-way merge len trees […] resulting index […]",
> I haven't puzzled out what's going on, but it _looks_ like we merge a
> single tree, possibly with the original index (opts.src_index) and
> write to a destination which is the repository's index.
Apart from the "git read-tree" man page, the documentation for unpack_trees() is basically non-existent which is a real shame for such a fundamental function. As I understand it, the one-way merge copies across the stat data from the old index to the new index for entries that are unchanged between the two. Without the "reset" bit it also checks that there are no unmerged index entries and unstaged changes for paths that are removed by the merge.
Show 27 quoted lines
> It's extra unclear what the reset bit is applied to in
>
>> It looks like stash has its own unpack_trees() wrapper, so I think the
>> simplest fix is to replace reset_head() with
>>
>> reset_tree(&c_tree, 0, 1);
>
> I'm not sure if that is a pre-merge reset, post-merge reset, or
> something in between.
> Unfortunately, a whole bunch of tests fail (6 files) with this suggestion :/
>
> Summary of Failures:
>
> 339/1062 git:t3904-stash-patch ERROR
> 0.45s exit status 1
> 503/1062 git:t3903-stash ERROR
> 4.40s exit status 1
> 763/1062 git:t6424-merge-unrelated-index-changes ERROR
> 0.90s exit status 1
> 778/1062 git:t6402-merge-rename ERROR
> 2.06s exit status 1
> 864/1062 git:t1092-sparse-checkout-compatibility ERROR
> 26.19s exit status 1
> 868/1062 git:t7611-merge-abort ERROR
> 0.45s exit status 1
>
> It _also_ doesn't make the bug go away, hm.
Oh, I wonder what's happening there.
Show 22 quoted lines
>>>> We could avoid touching the index at all if we
>>>> used merge_incore_nonrecursive() to cherry pick the index changes
>>>> instead. That way we'd get a proper three-way merge and avoid
>>>> spawning subprocesses for "git diff-tree", "git apply --cached",
>>>> and "git reset". We're already using merge_ort_nonrecursive() to
>>>> merge the working tree changes in that function so we have nearly
>>>> everything we need already set up to merge the index changes as
>>>> well. Essentially, when merging the index, we just need to call
>>>> merge_incore_nonrecursive() instead of merge_ort_nonrecursive()
>>>> and use info->i_tree instead of info->w_tree.
>
> If I'm following this, the suggestion is to replace (parts of) the
> early "if (index)" block with a merge_incore_nonrecursive() to merge
> index changes, reporting conflicts as we do today, and saving the tree
> for later… and this would not touch the real index, so we wouldn't
> have to reset at all? Interesting!
>
> The attached patch [Gmail headaches, sorry], which needs some
> polishing [*], passes tests and fixes the bug! Yahoo. I'll send a
> series later, tomorrow probably.
> (It won't apply directly, because it's on top of the experimental
> reset_tree() version, but resolving conflicts should be easy.)
I had a quick look at the patch, it looks good, but I think we can simplify it a bit. As we abort if there are conflicts I don't think we need to spend any effort setting the conflict labels (I'm not sure if we can pass NULL, but "" would certainly suffice). Does the current code print any errors from apply when the patch does not apply? If not we should silence the merge by setting verbosity=0. Also I think we should use oidcpy to copy the merged tree (it probably does not matter in this case, but it I think it does some extra checks on the hash function which a simple assignment does not)
Thanks for working on this, it will be a nice improvement
Phillip
> [*] namely, the log message, some tiny first cleanups, and removing
> now-unused functions
>
Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
On 17/09/2026 16:55, Phillip Wood wrote:
Show 29 quoted lines
> On 17/09/2026 14:15, D. Ben Knoble wrote:
>>> It looks like stash has its own unpack_trees() wrapper, so I think the
>>> simplest fix is to replace reset_head() with
>>>
>>> reset_tree(&c_tree, 0, 1);
>>
>> I'm not sure if that is a pre-merge reset, post-merge reset, or
>> something in between.
>> Unfortunately, a whole bunch of tests fail (6 files) with this
>> suggestion :/
>>
>> Summary of Failures:
>>
>> 339/1062 git:t3904-stash-patch ERROR
>> 0.45s exit status 1
>> 503/1062 git:t3903-stash ERROR
>> 4.40s exit status 1
>> 763/1062 git:t6424-merge-unrelated-index-changes ERROR
>> 0.90s exit status 1
>> 778/1062 git:t6402-merge-rename ERROR
>> 2.06s exit status 1
>> 864/1062 git:t1092-sparse-checkout-compatibility ERROR
>> 26.19s exit status 1
>> 868/1062 git:t7611-merge-abort ERROR
>> 0.45s exit status 1
>>
>> It _also_ doesn't make the bug go away, hm.
>
> Oh, I wonder what's happening there.
So the tests fail because when git tries to merge the stashed worktree changes it thinks there are unstaged changes in the worktree. That's because when we merge the stashed index changes the stat data and CE_UPTODATE flag are cleared for paths that are updated by the merge. When we remove those merged changes from the index we need to refresh the index to restore the stat data. Adding a call to refresh_index() after reset_tree() makes the tests pass.
Anyway that's all a bit irrelevant if we're going to start using merge_incore_nonrecursive() but the test failures were bugging me so I thought I'd have a look at what was causing them.
Thanks
Phillip
Show 41 quoted lines
>>>>> We could avoid touching the index at all if we
>>>>> used merge_incore_nonrecursive() to cherry pick the index changes
>>>>> instead. That way we'd get a proper three-way merge and avoid
>>>>> spawning subprocesses for "git diff-tree", "git apply --cached",
>>>>> and "git reset". We're already using merge_ort_nonrecursive() to
>>>>> merge the working tree changes in that function so we have nearly
>>>>> everything we need already set up to merge the index changes as
>>>>> well. Essentially, when merging the index, we just need to call
>>>>> merge_incore_nonrecursive() instead of merge_ort_nonrecursive()
>>>>> and use info->i_tree instead of info->w_tree.
>>
>> If I'm following this, the suggestion is to replace (parts of) the
>> early "if (index)" block with a merge_incore_nonrecursive() to merge
>> index changes, reporting conflicts as we do today, and saving the tree
>> for later… and this would not touch the real index, so we wouldn't
>> have to reset at all? Interesting!
>>
>> The attached patch [Gmail headaches, sorry], which needs some
>> polishing [*], passes tests and fixes the bug! Yahoo. I'll send a
>> series later, tomorrow probably.
>> (It won't apply directly, because it's on top of the experimental
>> reset_tree() version, but resolving conflicts should be easy.)
>
> I had a quick look at the patch, it looks good, but I think we can
> simplify it a bit. As we abort if there are conflicts I don't think we
> need to spend any effort setting the conflict labels (I'm not sure if we
> can pass NULL, but "" would certainly suffice). Does the current code
> print any errors from apply when the patch does not apply? If not we
> should silence the merge by setting verbosity=0. Also I think we should
> use oidcpy to copy the merged tree (it probably does not matter in this
> case, but it I think it does some extra checks on the hash function
> which a simple assignment does not)
>
> Thanks for working on this, it will be a nice improvement
>
> Phillip
>
>> [*] namely, the log message, some tiny first cleanups, and removing
>> now-unused functions
>>
>
Re: [BUG] stash.index=true leaves a redundant stash entry after an autostash fast-forward
On Thu, Sep 17, 2026 at 11:55 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>
> On 17/09/2026 14:15, D. Ben Knoble wrote:
Show 11 quoted lines
> > Sans doc comments for
> > unpack_trees() beyond "N-way merge len trees […] resulting index […]",
> > I haven't puzzled out what's going on, but it _looks_ like we merge a
> > single tree, possibly with the original index (opts.src_index) and
> > write to a destination which is the repository's index.
>
> Apart from the "git read-tree" man page, the documentation for
> unpack_trees() is basically non-existent which is a real shame for such
> a fundamental function. As I understand it, the one-way merge copies
> across the stat data from the old index to the new index for entries
> that are unchanged between the two.
Up to here, I'm with you.
> Without the "reset" bit it also
> checks that there are no unmerged index entries and unstaged changes for
> paths that are removed by the merge.
That is such a precise check to be covered by only "reset", heh. I suppose the idea is that if "reset" is on, we don't need to check because we'll reset those entries/paths? More documentation from experts in this area welcome…
Show 11 quoted lines
> > It's extra unclear what the reset bit is applied to in
> >
> >> It looks like stash has its own unpack_trees() wrapper, so I think the
> >> simplest fix is to replace reset_head() with
> >>
> >> reset_tree(&c_tree, 0, 1);
> >
> > I'm not sure if that is a pre-merge reset, post-merge reset, or
> > something in between.
> > Unfortunately, a whole bunch of tests fail (6 files) with this suggestion :/
> >
> >
> > It _also_ doesn't make the bug go away, hm.
>
> Oh, I wonder what's happening there.
Looks like you figured it out :) When I was thinking about a series, I thought it might be nice to do the "trivial" fix first, then the refactor we prefer, but… I'm not too sure. Since the other version works, I might just go with it and omit the intermediate reset_tree state.
Show 6 quoted lines
> I had a quick look at the patch, it looks good, but I think we can
> simplify it a bit. As we abort if there are conflicts I don't think we
> need to spend any effort setting the conflict labels (I'm not sure if we
> can pass NULL, but "" would certainly suffice). Does the current code
> print any errors from apply when the patch does not apply? If not we
> should silence the merge by setting verbosity=0.
Actually, this was my reason to set the labels, too! I thought they might show up in any logged messages. I'll take another look at what happens here.
> Also I think we should
> use oidcpy to copy the merged tree (it probably does not matter in this
> case, but it I think it does some extra checks on the hash function
> which a simple assignment does not)
--
D. Ben Knoble