]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: do not return before restoring untracked files
authorElijah Newren <newren@gmail.com>
Tue, 4 Jan 2022 23:04:58 +0000 (23:04 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 4 Jan 2022 23:37:45 +0000 (15:37 -0800)
In commit bee8691f19 ("stash: restore untracked files AFTER restoring
tracked files", 2021-09-10), we correctly identified that we should
restore changes to tracked files before attempting to restore untracked
files, and accordingly moved the code for restoring untracked files a
few lines down in do_apply_stash().  Unfortunately, the intervening
lines had some early return statements meaning that we suddenly stopped
restoring untracked files in some cases.

Even before the previous commit, there was another possible issue with
the current code -- a post-stash-apply 'git status' that was intended
to be run after restoring the stash was skipped when we hit a conflict
(or other error condition), which seems slightly inconsistent.

Fix both issues by saving the return status, and letting other
functionality run before returning.

Reported-by: AJ Henderson
Test-case-by: Randall S. Becker <randall.becker@nexbridge.ca>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index 5512f4942cd67bbe3aaecef8ccc05af19be2da7a..dd38304efc976054e81834e196c6a818339182e4 100644 (file)
@@ -559,18 +559,19 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
                if (index)
                        fprintf_ln(stderr, _("Index was not unstashed."));
 
-               return ret;
+               goto restore_untracked;
        }
 
        if (has_index) {
                if (reset_tree(&index_tree, 0, 0))
-                       return -1;
+                       ret = -1;
        } else {
                unstage_changes_unless_new(&c_tree);
        }
 
+restore_untracked:
        if (info->has_u && restore_untracked(&info->u_tree))
-               return error(_("could not restore untracked files from stash"));
+               ret = error(_("could not restore untracked files from stash"));
 
        if (!quiet) {
                struct child_process cp = CHILD_PROCESS_INIT;
@@ -590,7 +591,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
                run_command(&cp);
        }
 
-       return 0;
+       return ret;
 }
 
 static int apply_stash(int argc, const char **argv, const char *prefix)
index f0a82be9de7654a0956b1d9a4f24e161869e021e..579ee66f6d3666d60f101b09aedddd8b38c162cb 100755 (executable)
@@ -1365,4 +1365,28 @@ test_expect_success 'git stash can pop directory -> file saved changes' '
        )
 '
 
+test_expect_success 'restore untracked files even when we hit conflicts' '
+       git init restore_untracked_after_conflict &&
+       (
+               cd restore_untracked_after_conflict &&
+
+               echo hi >a &&
+               echo there >b &&
+               git add . &&
+               git commit -m first &&
+               echo hello >a &&
+               echo something >c &&
+
+               git stash push --include-untracked &&
+
+               echo conflict >a &&
+               git add a &&
+               git commit -m second &&
+
+               test_must_fail git stash pop &&
+
+               test_path_is_file c
+       )
+'
+
 test_done