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
This commit is contained in:
Junio C Hamano 2024-09-13 15:26:50 -07:00
commit 480124470c
2 changed files with 37 additions and 1 deletions

View File

@ -1671,7 +1671,28 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
}
}
if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
/*
* When keeping staged entries, we need to reset the working
* directory to match the state of our index. This can be
* skipped when the index is the empty tree, because there is
* nothing to reset in that case:
*
* - When the index has any file, regardless of whether
* staged or not, the tree cannot be empty by definition
* and thus we enter the condition.
*
* - When the index has no files, the only thing we need to
* care about is untracked files when `--include-untracked`
* is given. But as we already execute git-clean(1) further
* up to delete such untracked files we don't have to do
* anything here, either.
*
* We thus skip calling git-checkout(1) in this case, also
* because running it on an empty tree will cause it to fail
* due to the pathspec not matching anything.
*/
if (keep_index == 1 && !is_null_oid(&info.i_tree) &&
!is_empty_tree_oid(&info.i_tree, the_repository->hash_algo)) {
struct child_process cp = CHILD_PROCESS_INIT;
cp.git_cmd = 1;

View File

@ -1397,6 +1397,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 &&