Skip to content

Commit f9eb8e0

Browse files
committed
sparse-checkout: make 'clean' clear more files
The 'git sparse-checkout clean' command is designed to be a one-command way to get the worktree in a state such that a sparse index would operate efficiently. The previous change demonstrated that files outside the sparse-checkout that were committed due to a merge conflict would persist despite attempts to run 'git sparse-checkout clean' and instead a 'git sparse-checkout reapply' would be required. Instead of requiring users to run both commands, update 'clean' to be more ruthless about tracked sparse directories. The key here is to make sure that the SKIP_WORKTREE bit is removed from more paths in the index using update_sparsity() before compressing the index to a sparse one in-memory. The tricky part here is that update_sparsity() was previously assuming that it would be in 'update' mode and would change the worktree as it made changes. However, we do not want to make these worktree changes at this point, instead relying on our later logic (that integrates with --dry-run and --verbose options) to perform those steps. One side-effect here is that we also clear out staged files that exist in the worktree, but they would also appear in the verbose output as part of the dry run. The final test in t1091 demonstrates that we no longer need the 'reapply' subcommand for merge resolutions. It also fixes an earlier case where 'git add --sparse' clears the SKIP_WORKTREE bit and avoids a directory deletion. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 7f6f62b commit f9eb8e0

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

builtin/sparse-checkout.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
962962
size_t worktree_len;
963963
int force = 0, dry_run = 0, verbose = 0;
964964
int require_force = 1;
965+
struct unpack_trees_options o = { 0 };
965966

966967
struct option builtin_sparse_checkout_clean_options[] = {
967968
OPT__DRY_RUN(&dry_run, N_("dry run")),
@@ -990,6 +991,13 @@ static int sparse_checkout_clean(int argc, const char **argv,
990991
if (repo_read_index(repo) < 0)
991992
die(_("failed to read index"));
992993

994+
o.verbose_update = verbose;
995+
o.update = 0; /* skip modifying the worktree here. */
996+
o.head_idx = -1;
997+
o.src_index = o.dst_index = repo->index;
998+
if (update_sparsity(&o, NULL))
999+
warning(_("failed to reapply sparse-checkout patterns"));
1000+
9931001
if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) ||
9941002
repo->index->sparse_index == INDEX_EXPANDED)
9951003
die(_("failed to convert index to a sparse index; resolve merge conflicts and try again"));

t/t1091-sparse-checkout-builtin.sh

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,35 +1123,25 @@ test_expect_success 'clean with sparse file states' '
11231123
# Now, stage the change to the tracked file.
11241124
git -C repo add --sparse folder2/a &&
11251125
1126-
# Clean will continue not doing anything.
1127-
git -C repo sparse-checkout clean >out &&
1128-
test_line_count = 0 out &&
1129-
test_path_exists repo/folder2/a &&
1130-
test_path_exists repo/folder2/file &&
1131-
1132-
# But we can reapply to remove the staged change.
1133-
git -C repo sparse-checkout reapply 2>err &&
1134-
test_grep folder2 err &&
1135-
test_path_is_missing repo/folder2/a &&
1136-
test_path_exists repo/folder2/file &&
1137-
1138-
# We can clean now.
1126+
# Clean will update the skip-worktree bit and remove this file.
11391127
cat >expect <<-\EOF &&
11401128
Removing folder2/
11411129
EOF
11421130
git -C repo sparse-checkout clean >out &&
11431131
test_cmp expect out &&
11441132
test_path_is_missing repo/folder2 &&
11451133
1146-
# At the moment, the file is staged.
1134+
# We are now in a strange state: the change is staged but it
1135+
# is also removed from the worktree.
11471136
cat >expect <<-\EOF &&
1148-
M folder2/a
1137+
MD folder2/a
11491138
EOF
11501139
11511140
git -C repo status -s >out &&
11521141
test_cmp expect out &&
11531142
1154-
# Reapply persists the modified state.
1143+
# Reapply updates the skip-worktree bit to remove this
1144+
# "deleted" state.
11551145
git -C repo sparse-checkout reapply &&
11561146
cat >expect <<-\EOF &&
11571147
M folder2/a
@@ -1168,15 +1158,21 @@ test_expect_success 'clean with sparse file states' '
11681158
mkdir repo/folder2/ &&
11691159
echo dirtier >repo/folder2/a &&
11701160
git -C repo add --sparse folder2/a &&
1161+
cat >expect <<-\EOF &&
1162+
Removing folder2/
1163+
EOF
11711164
git -C repo sparse-checkout clean >out &&
1172-
test_must_be_empty out &&
1173-
test_path_exists repo/folder2/a &&
1165+
test_cmp expect out &&
1166+
test_path_is_missing repo/folder2 &&
11741167
11751168
# Committing without reapplying makes it look like a deletion
11761169
# due to no skip-worktree bit.
11771170
git -C repo commit -m "dirtier" &&
1171+
cat >expect <<-\EOF &&
1172+
D folder2/a
1173+
EOF
11781174
git -C repo status -s >out &&
1179-
test_must_be_empty out &&
1175+
test_cmp expect out &&
11801176
11811177
git -C repo sparse-checkout reapply &&
11821178
git -C repo status -s >out &&
@@ -1202,7 +1198,11 @@ test_expect_success 'sparse-checkout operations with merge conflicts' '
12021198
git commit -a -m "left" &&
12031199
12041200
git checkout -b merge &&
1205-
git sparse-checkout set deep/deeper1 &&
1201+
1202+
touch deep/deeper2/extra &&
1203+
git sparse-checkout set deep/deeper1 2>err &&
1204+
grep "contains untracked files" err &&
1205+
test_path_exists deep/deeper2/extra &&
12061206
12071207
test_must_fail git merge -m "will-conflict" right &&
12081208
@@ -1214,15 +1214,26 @@ test_expect_success 'sparse-checkout operations with merge conflicts' '
12141214
git merge --continue &&
12151215
12161216
test_path_exists folder1/even/more/dirs/file &&
1217+
test_path_exists deep/deeper2/extra &&
12171218
1218-
# clean does not remove the file, because the
1219-
# SKIP_WORKTREE bit was not cleared by the merge command.
1219+
cat >expect <<-\EOF &&
1220+
Removing deep/deeper2/
1221+
Removing folder1/
1222+
EOF
12201223
git sparse-checkout clean -f >out &&
1221-
test_line_count = 0 out &&
1222-
test_path_exists folder1/even/more/dirs/file &&
1224+
test_cmp expect out &&
1225+
test_path_is_missing folder1 &&
1226+
test_path_is_missing deep/deeper2 &&
1227+
1228+
cat >expect <<-\EOF &&
1229+
D folder1/even/more/dirs/file
1230+
EOF
1231+
git status -s -uno >out &&
1232+
test_cmp expect out &&
12231233
12241234
git sparse-checkout reapply &&
1225-
test_path_is_missing folder1
1235+
git status -s -uno >out &&
1236+
test_must_be_empty out
12261237
)
12271238
'
12281239

unpack-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o,
21382138
index_state_init(&o->internal.result, o->src_index->repo);
21392139

21402140
/* Sanity checks */
2141-
if (!o->update || o->index_only || o->skip_sparse_checkout)
2141+
if (o->index_only || o->skip_sparse_checkout)
21422142
BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
21432143
if (o->src_index != o->dst_index || o->fn)
21442144
BUG("update_sparsity() called wrong");

0 commit comments

Comments
 (0)