Skip to content

Commit cd663ba

Browse files
authored
Merge pull request #2378 from dscho/fix-stash-with-skip-worktree
Fix `git stash` with skip-worktree entries
2 parents e96e0b4 + 8f49a39 commit cd663ba

6 files changed

+42
-4
lines changed

Documentation/git-update-index.txt

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SYNOPSIS
1616
[--chmod=(+|-)x]
1717
[--[no-]assume-unchanged]
1818
[--[no-]skip-worktree]
19+
[--[no-]ignore-skip-worktree-entries]
1920
[--[no-]fsmonitor-valid]
2021
[--ignore-submodules]
2122
[--[no-]split-index]
@@ -113,6 +114,11 @@ you will need to handle the situation manually.
113114
set and unset the "skip-worktree" bit for the paths. See
114115
section "Skip-worktree bit" below for more information.
115116

117+
118+
--[no-]ignore-skip-worktree-entries::
119+
Do not remove skip-worktree (AKA "index-only") entries even when
120+
the `--remove` option was specified.
121+
116122
--[no-]fsmonitor-valid::
117123
When one of these flags is specified, the object name recorded
118124
for the paths are not updated. Instead, these options

builtin/stash.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,9 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
10911091
}
10921092

10931093
cp_upd_index.git_cmd = 1;
1094-
argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1095-
"--remove", "--stdin", NULL);
1094+
argv_array_pushl(&cp_upd_index.args, "update-index",
1095+
"--ignore-skip-worktree-entries",
1096+
"-z", "--add", "--remove", "--stdin", NULL);
10961097
argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
10971098
stash_index_path.buf);
10981099

builtin/update-index.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static int verbose;
3535
static int mark_valid_only;
3636
static int mark_skip_worktree_only;
3737
static int mark_fsmonitor_only;
38+
static int ignore_skip_worktree_entries;
3839
#define MARK_FLAG 1
3940
#define UNMARK_FLAG 2
4041
static struct strbuf mtime_dir = STRBUF_INIT;
@@ -381,7 +382,8 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
381382
* so updating it does not make sense.
382383
* On the other hand, removing it from index should work
383384
*/
384-
if (allow_remove && remove_file_from_cache(path))
385+
if (!ignore_skip_worktree_entries && allow_remove &&
386+
remove_file_from_cache(path))
385387
return error("%s: cannot remove from the index", path);
386388
return 0;
387389
}
@@ -1014,6 +1016,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10141016
{OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
10151017
N_("clear skip-worktree bit"),
10161018
PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1019+
OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
1020+
N_("do not touch index-only entries")),
10171021
OPT_SET_INT(0, "info-only", &info_only,
10181022
N_("add to index only; do not add content to object database"), 1),
10191023
OPT_SET_INT(0, "force-remove", &force_remove,

git-legacy-stash.sh

+2-1
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

+11
Original file line numberDiff line numberDiff line change
@@ -1269,4 +1269,15 @@ test_expect_success 'stash apply should succeed with unmodified file' '
12691269
git stash apply
12701270
'
12711271

1272+
test_expect_success 'stash handles skip-worktree entries nicely' '
1273+
test_commit A &&
1274+
echo changed >A.t &&
1275+
git add A.t &&
1276+
git update-index --skip-worktree A.t &&
1277+
rm A.t &&
1278+
git stash &&
1279+
1280+
git rev-parse --verify refs/stash:A.t
1281+
'
1282+
12721283
test_done

t/t7012-skip-worktree-writing.sh

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ test_expect_success 'git-clean, dirty case' '
134134
test_i18ncmp expected result
135135
'
136136

137+
test_expect_success '--ignore-skip-worktree-entries leaves worktree alone' '
138+
test_commit keep-me &&
139+
git update-index --skip-worktree keep-me.t &&
140+
rm keep-me.t &&
141+
142+
: ignoring the worktree &&
143+
git update-index --remove --ignore-skip-worktree-entries keep-me.t &&
144+
git diff-index --cached --exit-code HEAD &&
145+
146+
: not ignoring the worktree, a deletion is staged &&
147+
git update-index --remove keep-me.t &&
148+
test_must_fail git diff-index --cached --exit-code HEAD \
149+
--diff-filter=D -- keep-me.t
150+
'
151+
137152
#TODO test_expect_failure 'git-apply adds file' false
138153
#TODO test_expect_failure 'git-apply updates file' false
139154
#TODO test_expect_failure 'git-apply removes file' false

0 commit comments

Comments
 (0)