Skip to content

Commit 71cade5

Browse files
newrengitster
authored andcommitted
stash: do not return before restoring untracked files
In commit bee8691 ("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 <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bee8691 commit 71cade5

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

builtin/stash.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,18 +559,19 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
559559
if (index)
560560
fprintf_ln(stderr, _("Index was not unstashed."));
561561

562-
return ret;
562+
goto restore_untracked;
563563
}
564564

565565
if (has_index) {
566566
if (reset_tree(&index_tree, 0, 0))
567-
return -1;
567+
ret = -1;
568568
} else {
569569
unstage_changes_unless_new(&c_tree);
570570
}
571571

572+
restore_untracked:
572573
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"));
574575

575576
if (!quiet) {
576577
struct child_process cp = CHILD_PROCESS_INIT;
@@ -590,7 +591,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
590591
run_command(&cp);
591592
}
592593

593-
return 0;
594+
return ret;
594595
}
595596

596597
static int apply_stash(int argc, const char **argv, const char *prefix)

t/t3903-stash.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,4 +1365,28 @@ test_expect_success 'git stash can pop directory -> file saved changes' '
13651365
)
13661366
'
13671367

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+
13681392
test_done

0 commit comments

Comments
 (0)