From e3209bd4df1f609fb2d730e52d3eea0633dbd786 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 16 Aug 2024 12:42:25 +0200 Subject: 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 Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- t/t3903-stash.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 't') diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 376cc8f4ab..38906785e2 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1384,6 +1384,21 @@ test_expect_success 'stash --keep-index with file deleted in index does not resu test_path_is_missing to-remove ' +test_expect_success 'stash --keep-index --include-untracked with empty tree' ' + test_when_finished "rm -rf empty" && + git init empty && + ( + cd empty && + git commit --allow-empty --message "empty" && + echo content >file && + git stash push --keep-index --include-untracked && + test_path_is_missing file && + git stash pop && + echo content >expect && + test_cmp expect file + ) +' + test_expect_success 'stash apply should succeed with unmodified file' ' echo base >file && git add file && -- cgit v1.2.3