Skip to content

Commit 8f49a39

Browse files
committed
stash: handle staged changes in skip-worktree files correctly
When calling `git stash` while changes were staged for files that are marked with the `skip-worktree` bit (e.g. files that are excluded in a sparse checkout), the files are recorded as _deleted_ instead. The reason is that `git stash` tries to construct the tree reflecting the worktree essentially by copying the index to a temporary one and then updating the files from the worktree. Crucially, it calls `git diff-index` to update also those files that are in the HEAD but have been unstaged in the index. However, when the temporary index is updated via `git update-index --add --remove`, skip-worktree entries mark the files as deleted by mistake. Let's use the newly-introduced `--ignore-skip-worktree-entries` option of `git update-index` to prevent exactly this from happening. Note that the regression test case deliberately avoids replicating the scenario described above and instead tries to recreate just the symptom. Reported by Dan Thompson. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 163b42d commit 8f49a39

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

builtin/stash.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,9 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
10821082
}
10831083

10841084
cp_upd_index.git_cmd = 1;
1085-
argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1086-
"--remove", "--stdin", NULL);
1085+
argv_array_pushl(&cp_upd_index.args, "update-index",
1086+
"--ignore-skip-worktree-entries",
1087+
"-z", "--add", "--remove", "--stdin", NULL);
10871088
argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
10881089
stash_index_path.buf);
10891090

git-legacy-stash.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ create_stash () {
193193
GIT_INDEX_FILE="$TMPindex" &&
194194
export GIT_INDEX_FILE &&
195195
git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" &&
196-
git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
196+
git update-index --ignore-skip-worktree-entries \
197+
-z --add --remove --stdin <"$TMP-stagenames" &&
197198
git write-tree &&
198199
rm -f "$TMPindex"
199200
) ) ||

t/t3903-stash.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,4 +1257,15 @@ test_expect_success 'stash apply should succeed with unmodified file' '
12571257
git stash apply
12581258
'
12591259

1260+
test_expect_success 'stash handles skip-worktree entries nicely' '
1261+
test_commit A &&
1262+
echo changed >A.t &&
1263+
git add A.t &&
1264+
git update-index --skip-worktree A.t &&
1265+
rm A.t &&
1266+
git stash &&
1267+
1268+
git rev-parse --verify refs/stash:A.t
1269+
'
1270+
12601271
test_done

0 commit comments

Comments
 (0)