stash: apply stash using 'merge_ort_nonrecursive()'

Update 'stash' to use 'merge_ort_nonrecursive()' to apply a stash to the current working tree. When 'git stash apply' was converted from its shell script implementation to a builtin in 8a0fc8d19d (stash: convert apply to builtin, 2019-02-25), 'merge_recursive_generic()' was used to merge a stash into the working tree as part of 'git stash (apply|pop)'. However, with the single merge base used in 'do_apply_stash()', the commit wrapping done by 'merge_recursive_generic()' is not only unnecessary, but misleading (the *real* merge base is labeled "constructed merge base"). Therefore, a non-recursive merge of the working tree, stashed tree, and stash base tree is more appropriate. There are two options for a non-recursive merge-then-update-worktree function: 'merge_trees()' and 'merge_ort_nonrecursive()'. Use 'merge_ort_nonrecursive()' to align with the default merge strategy used by 'git merge' (6a5fb96672 (Change default merge backend from recursive to ort, 2021-08-04)) and, because merge-ort does not operate in-place on the index, avoid unnecessary index expansion. Update tests in 't1092' verifying index expansion for 'git stash' accordingly. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed May 10, 2022 at 23:32 UTC 874cf2a6044462ddba7162730557354a107c3a6d
2 files changed +26 -8
builtin/stash.c
+24 -6
@@ -7,6 +7,7 @@
7 #include "cache-tree.h"
8 #include "unpack-trees.h"
9 #include "merge-recursive.h"
10 +#include "merge-ort-wrappers.h"
11 #include "strvec.h"
12 #include "run-command.h"
13 #include "dir.h"
@@ -492,13 +493,13 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
493 static int do_apply_stash(const char *prefix, struct stash_info *info,
494 int index, int quiet)
495 {
495 - int ret;
496 + int clean, ret;
497 int has_index = index;
498 struct merge_options o;
499 struct object_id c_tree;
500 struct object_id index_tree;
500 - struct commit *result;
501 - const struct object_id *bases[1];
501 + struct tree *head, *merge, *merge_base;
502 + struct lock_file lock = LOCK_INIT;
503
504 read_cache_preload(NULL);
505 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
@@ -541,6 +542,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
542
543 o.branch1 = "Updated upstream";
544 o.branch2 = "Stashed changes";
545 + o.ancestor = "Stash base";
546
547 if (oideq(&info->b_tree, &c_tree))
548 o.branch1 = "Version stash was based on";
@@ -551,10 +553,26 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
553 if (o.verbosity >= 3)
554 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
555
554 - bases[0] = &info->b_tree;
556 + head = lookup_tree(o.repo, &c_tree);
557 + merge = lookup_tree(o.repo, &info->w_tree);
558 + merge_base = lookup_tree(o.repo, &info->b_tree);
559 +
560 + repo_hold_locked_index(o.repo, &lock, LOCK_DIE_ON_ERROR);
561 + clean = merge_ort_nonrecursive(&o, head, merge, merge_base);
562 +
563 + /*
564 + * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the
565 + * merge was clean, and nonzero if the merge was unclean or encountered
566 + * an error.
567 + */
568 + ret = clean >= 0 ? !clean : clean;
569 +
570 + if (ret < 0)
571 + rollback_lock_file(&lock);
572 + else if (write_locked_index(o.repo->index, &lock,
573 + COMMIT_LOCK | SKIP_IF_UNCHANGED))
574 + ret = error(_("could not write index"));
575
556 - ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
557 - &result);
576 if (ret) {
577 rerere(0);
578
t/t1092-sparse-checkout-compatibility.sh
+2 -2
@@ -1385,7 +1385,7 @@ test_expect_success 'sparse-index is not expanded: stash' '
1385 ensure_not_expanded stash &&
1386 ensure_not_expanded stash list &&
1387 ensure_not_expanded stash show stash@{0} &&
1388 - ! ensure_not_expanded stash apply stash@{0} &&
1388 + ensure_not_expanded stash apply stash@{0} &&
1389 ensure_not_expanded stash drop stash@{0} &&
1390
1391 echo >>sparse-index/deep/new &&
@@ -1399,7 +1399,7 @@ test_expect_success 'sparse-index is not expanded: stash' '
1399 oid=$(git -C sparse-index stash create) &&
1400 ensure_not_expanded stash store -m "test" $oid &&
1401 ensure_not_expanded reset --hard &&
1402 - ! ensure_not_expanded stash pop
1402 + ensure_not_expanded stash pop
1403 '
1404
1405 test_expect_success 'sparse index is not expanded: diff' '