Skip to content

Commit 4801244

Browse files
committed
Merge branch 'ps/stash-keep-untrack-empty-fix' into maint-2.46
A corner case bug in "git stash" was fixed. * ps/stash-keep-untrack-empty-fix: builtin/stash: fix `--keep-index --include-untracked` with empty HEAD
2 parents be344f3 + e3209bd commit 4801244

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

builtin/stash.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,28 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
16711671
}
16721672
}
16731673

1674-
if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1674+
/*
1675+
* When keeping staged entries, we need to reset the working
1676+
* directory to match the state of our index. This can be
1677+
* skipped when the index is the empty tree, because there is
1678+
* nothing to reset in that case:
1679+
*
1680+
* - When the index has any file, regardless of whether
1681+
* staged or not, the tree cannot be empty by definition
1682+
* and thus we enter the condition.
1683+
*
1684+
* - When the index has no files, the only thing we need to
1685+
* care about is untracked files when `--include-untracked`
1686+
* is given. But as we already execute git-clean(1) further
1687+
* up to delete such untracked files we don't have to do
1688+
* anything here, either.
1689+
*
1690+
* We thus skip calling git-checkout(1) in this case, also
1691+
* because running it on an empty tree will cause it to fail
1692+
* due to the pathspec not matching anything.
1693+
*/
1694+
if (keep_index == 1 && !is_null_oid(&info.i_tree) &&
1695+
!is_empty_tree_oid(&info.i_tree, the_repository->hash_algo)) {
16751696
struct child_process cp = CHILD_PROCESS_INIT;
16761697

16771698
cp.git_cmd = 1;

t/t3903-stash.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,21 @@ test_expect_success 'stash --keep-index with file deleted in index does not resu
13971397
test_path_is_missing to-remove
13981398
'
13991399

1400+
test_expect_success 'stash --keep-index --include-untracked with empty tree' '
1401+
test_when_finished "rm -rf empty" &&
1402+
git init empty &&
1403+
(
1404+
cd empty &&
1405+
git commit --allow-empty --message "empty" &&
1406+
echo content >file &&
1407+
git stash push --keep-index --include-untracked &&
1408+
test_path_is_missing file &&
1409+
git stash pop &&
1410+
echo content >expect &&
1411+
test_cmp expect file
1412+
)
1413+
'
1414+
14001415
test_expect_success 'stash apply should succeed with unmodified file' '
14011416
echo base >file &&
14021417
git add file &&

0 commit comments

Comments
 (0)