[PATCH v2 4/4] builtin/stash: merge index in-core
- From
D. Ben Knoble <ben.knoble@gmail.com>
- Date
- Sep 23, 2026, 12:58 UTC
- Message-ID
- <e49936ee12aaf5d82a98dddcc618cee01ac3c681.1790168285.git.ben.knoble@gmail.com>
- In-Reply-To
- <cover.1790168285.git.ben.knoble@gmail.com>
"git stash apply --index" does a 2-step dance to report index conflicts before carrying out the main unstash: first, attempt to merge the index (and remember the name of the resulting tree). If that succeeds, reset the index and carry on unstashing the working tree, then use the remembered index tree to unstash the index.
The "merge the index" step is performed on the actual index by a combination of git-diff-tree(1) and git-apply(1), which incurs an extra cost to git-reset(1) to cleanup. This also introduces an autostash bug when stash.index is true: "git reset" eventually wants to remove_merge_branch_state(), which calls save_autostash() due to a03b55530a (merge: teach --autostash option, 2020-04-07). This can happen from a "git merge --autostash", which itself calls save_autostash(). Operating on the file-system in this way is not re-entrant, so we end up trying to lock a now-deleted MERGE_AUTOSTASH ref [1]. This bug has lurked for a while, but it would have been impossible to trigger without the availability of stash.index to force the autostash apply into index mode.
[1]: https://lore.kernel.org/git/CALO-guvbk2TcrVwzdNQ3yRpzHr0HHZ3h1wite0Xp0sUyAT4otA@mail.gmail.com/
Fortunately, we can achieve 2 goals at once: avoid round-tripping to the file-system (and invoking expensive subprocesses) by performing the merge in-core. If there are conflicts, we discard the resulting tree, so we don't see the usual branch and ancestor labels, but the merge subroutines insist on their presence, so use something simple.
We *could* swap just the git-reset(1) subprocess with our internal reset_tree() and refresh_index(), which would fix the bug. We'd much prefer to clean up these vestiges of the shell-based git-stash, though.
Reported-by: Eli Barzilay <eli@barzilay.org> Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: D. Ben Knoble <ben.knoble@gmail.com> --- builtin/stash.c | 78 ++++++++++-------------------------------------- t/t7600-merge.sh | 9 ++++++ 2 files changed, 25 insertions(+), 62 deletions(-)
diff --git a/builtin/stash.c b/builtin/stash.c index 043a38cc6d..219ca457be 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -422,50 +422,6 @@ static int create_index_from_tree(const struct object_id *tree_id, return ret; } -static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) -{ - struct child_process cp = CHILD_PROCESS_INIT; - const char *w_commit_hex = oid_to_hex(w_commit); - - /* - * Diff-tree would not be very hard to replace with a native function, - * however it should be done together with apply_cached. - */ - cp.git_cmd = 1; - strvec_pushl(&cp.args, "diff-tree", "--binary", "--no-color", NULL); - strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex); - - return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); -} - -static int apply_cached(struct strbuf *out) -{ - struct child_process cp = CHILD_PROCESS_INIT; - - /* - * Apply currently only reads either from stdin or a file, thus - * apply_all_patches would have to be updated to optionally take a - * buffer. - */ - cp.git_cmd = 1; - strvec_pushl(&cp.args, "apply", "--cached", NULL); - return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); -} - -static int reset_head(void) -{ - struct child_process cp = CHILD_PROCESS_INIT; - - /* - * Reset is overall quite simple, however there is no current public - * API for resetting. - */ - cp.git_cmd = 1; - strvec_pushl(&cp.args, "reset", "--quiet", "--refresh", NULL); - - return run_command(&cp); -} - static int is_path_a_directory(const char *path) { /* @@ -671,29 +627,27 @@ 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)); - } + o.branch1 = "Upstream index"; + o.branch2 = "Stashed index changes"; + o.ancestor = "Stash base"; - ret = apply_cached(&out); - strbuf_release(&out); - if (ret) + o.verbosity = 0; + + 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_head(); - discard_index(the_repository->index); - repo_read_index(the_repository); + oidcpy(&index_tree, &result.tree->object.oid); + merge_finalize(&o, &result); } } diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh index 64fe21717d..8f6109fb91 100755 --- a/t/t7600-merge.sh +++ b/t/t7600-merge.sh @@ -801,6 +801,15 @@ verify_no_mergehead () { test_cmp result.1-5 file ' +test_expect_success 'fast-forward merge with --autostash, stash.index' ' + git reset --hard c0 && + git stash clear && + echo staged >>z && git add z && + git -c stash.index=true merge --autostash c1 2>err && + test_grep "Applied autostash." err && + test_stdout_line_count = 0 git stash list +' + test_expect_success 'failed fast-forward merge with --autostash' ' git reset --hard c0 && git merge-file file file.orig file.5 &&
-- 2.56.0.rc1.315.gc6ed9934b7.dirty