Skip to content

Commit e3209bd

Browse files
pks-tgitster
authored andcommitted
builtin/stash: fix --keep-index --include-untracked with empty HEAD
It was reported that creating a stash with `--keep-index --include-untracked` causes an error when HEAD points to a commit whose tree is empty: $ git stash push --keep-index --include-untracked error: pathspec ':/' did not match any file(s) known to git This error comes from `git checkout --no-overlay $i_tree -- :/`, which we execute to reset the working tree to the state in our index. As the tree generated from the index is empty in our case, ':/' does not match any files and thus causes git-checkout(1) to error out. Fix the issue by skipping the checkout when the index tree is empty. As explained in the in-code comment, this should be the correct thing to do as there is nothing that we'd have to reset in the first place. Reported-by: Piotr Siupa <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dbecc61 commit e3209bd

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
@@ -1647,7 +1647,28 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
16471647
}
16481648
}
16491649

1650-
if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1650+
/*
1651+
* When keeping staged entries, we need to reset the working
1652+
* directory to match the state of our index. This can be
1653+
* skipped when the index is the empty tree, because there is
1654+
* nothing to reset in that case:
1655+
*
1656+
* - When the index has any file, regardless of whether
1657+
* staged or not, the tree cannot be empty by definition
1658+
* and thus we enter the condition.
1659+
*
1660+
* - When the index has no files, the only thing we need to
1661+
* care about is untracked files when `--include-untracked`
1662+
* is given. But as we already execute git-clean(1) further
1663+
* up to delete such untracked files we don't have to do
1664+
* anything here, either.
1665+
*
1666+
* We thus skip calling git-checkout(1) in this case, also
1667+
* because running it on an empty tree will cause it to fail
1668+
* due to the pathspec not matching anything.
1669+
*/
1670+
if (keep_index == 1 && !is_null_oid(&info.i_tree) &&
1671+
!is_empty_tree_oid(&info.i_tree, the_repository->hash_algo)) {
16511672
struct child_process cp = CHILD_PROCESS_INIT;
16521673

16531674
cp.git_cmd = 1;

t/t3903-stash.sh

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

1387+
test_expect_success 'stash --keep-index --include-untracked with empty tree' '
1388+
test_when_finished "rm -rf empty" &&
1389+
git init empty &&
1390+
(
1391+
cd empty &&
1392+
git commit --allow-empty --message "empty" &&
1393+
echo content >file &&
1394+
git stash push --keep-index --include-untracked &&
1395+
test_path_is_missing file &&
1396+
git stash pop &&
1397+
echo content >expect &&
1398+
test_cmp expect file
1399+
)
1400+
'
1401+
13871402
test_expect_success 'stash apply should succeed with unmodified file' '
13881403
echo base >file &&
13891404
git add file &&

0 commit comments

Comments
 (0)