stash: do not return before restoring untracked files

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>

Elijah Newren committed Jan 4, 2022 at 23:04 UTC 71cade5a0b172ece7edf0ccb4420dd5b9a07e71a
2 files changed +29 -4
builtin/stash.c
+5 -4
@@ -559,18 +559,19 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
559 if (index)
560 fprintf_ln(stderr, _("Index was not unstashed."));
561
562 - return ret;
562 + goto restore_untracked;
563 }
564
565 if (has_index) {
566 if (reset_tree(&index_tree, 0, 0))
567 - return -1;
567 + ret = -1;
568 } else {
569 unstage_changes_unless_new(&c_tree);
570 }
571
572 +restore_untracked:
573 if (info->has_u && restore_untracked(&info->u_tree))
573 - return error(_("could not restore untracked files from stash"));
574 + ret = error(_("could not restore untracked files from stash"));
575
576 if (!quiet) {
577 struct child_process cp = CHILD_PROCESS_INIT;
@@ -590,7 +591,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
591 run_command(&cp);
592 }
593
593 - return 0;
594 + return ret;
595 }
596
597 static int apply_stash(int argc, const char **argv, const char *prefix)
t/t3903-stash.sh
+24
@@ -1365,4 +1365,28 @@ test_expect_success 'git stash can pop directory -> file saved changes' '
1365 )
1366 '
1367
1368 +test_expect_success 'restore untracked files even when we hit conflicts' '
1369 + git init restore_untracked_after_conflict &&
1370 + (
1371 + cd restore_untracked_after_conflict &&
1372 +
1373 + echo hi >a &&
1374 + echo there >b &&
1375 + git add . &&
1376 + git commit -m first &&
1377 + echo hello >a &&
1378 + echo something >c &&
1379 +
1380 + git stash push --include-untracked &&
1381 +
1382 + echo conflict >a &&
1383 + git add a &&
1384 + git commit -m second &&
1385 +
1386 + test_must_fail git stash pop &&
1387 +
1388 + test_path_is_file c
1389 + )
1390 +'
1391 +
1392 test_done