Skip to content

Commit 7684cc1

Browse files
derrickstoleedscho
authored andcommitted
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 160fe8e commit 7684cc1

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

builtin/sparse-checkout.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
971971
size_t worktree_len;
972972
int force = 0, dry_run = 0, verbose = 0;
973973
int require_force = 1;
974+
struct unpack_trees_options o = { 0 };
974975

975976
struct option builtin_sparse_checkout_clean_options[] = {
976977
OPT__DRY_RUN(&dry_run, N_("dry run")),
@@ -999,6 +1000,13 @@ static int sparse_checkout_clean(int argc, const char **argv,
9991000
if (repo_read_index(repo) < 0)
10001001
die(_("failed to read index"));
10011002

1003+
o.verbose_update = verbose;
1004+
o.update = 0; /* skip modifying the worktree here. */
1005+
o.head_idx = -1;
1006+
o.src_index = o.dst_index = repo->index;
1007+
if (update_sparsity(&o, NULL))
1008+
warning(_("failed to reapply sparse-checkout patterns"));
1009+
10021010
if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) ||
10031011
repo->index->sparse_index == INDEX_EXPANDED)
10041012
die(_("failed to convert index to a sparse index; resolve merge conflicts and try again"));

t/t1091-sparse-checkout-builtin.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ test_expect_success 'clean with staged sparse change' '
11091109
11101110
cat >expect <<-\EOF &&
11111111
Would remove deep/deeper2/
1112+
Would remove folder1/
11121113
EOF
11131114
11141115
git -C repo sparse-checkout clean --dry-run >out &&
@@ -1120,6 +1121,7 @@ test_expect_success 'clean with staged sparse change' '
11201121
# deletes deep/deeper2/ but leaves folder1/ and folder2/
11211122
cat >expect <<-\EOF &&
11221123
Removing deep/deeper2/
1124+
Removing folder1/
11231125
EOF
11241126
11251127
# The previous test case checked the -f option, so
@@ -1129,7 +1131,7 @@ test_expect_success 'clean with staged sparse change' '
11291131
test_cmp expect out &&
11301132
11311133
test_path_is_missing repo/deep/deeper2 &&
1132-
test_path_exists repo/folder1 &&
1134+
test_path_is_missing repo/folder1 &&
11331135
test_path_exists repo/folder2
11341136
'
11351137

@@ -1152,7 +1154,11 @@ test_expect_success 'sparse-checkout operations with merge conflicts' '
11521154
git commit -a -m "left" &&
11531155
11541156
git checkout -b merge &&
1155-
git sparse-checkout set deep/deeper1 &&
1157+
1158+
touch deep/deeper2/extra &&
1159+
git sparse-checkout set deep/deeper1 2>err &&
1160+
grep "contains untracked files" err &&
1161+
test_path_exists deep/deeper2/extra &&
11561162
11571163
test_must_fail git merge -m "will-conflict" right &&
11581164
@@ -1164,15 +1170,19 @@ test_expect_success 'sparse-checkout operations with merge conflicts' '
11641170
git merge --continue &&
11651171
11661172
test_path_exists folder1/even/more/dirs/file &&
1173+
test_path_exists deep/deeper2/extra &&
1174+
1175+
cat >expect <<-\EOF &&
1176+
Removing deep/deeper2/
1177+
Removing folder1/
1178+
EOF
11671179
11681180
# clean does not remove the file, because the
11691181
# SKIP_WORKTREE bit was not cleared by the merge command.
11701182
git sparse-checkout clean -f >out &&
1171-
test_line_count = 0 out &&
1172-
test_path_exists folder1/even/more/dirs/file &&
1173-
1174-
git sparse-checkout reapply &&
1175-
test_path_is_missing folder1
1183+
test_cmp expect out &&
1184+
test_path_is_missing folder1 &&
1185+
test_path_is_missing deep/deeper2
11761186
)
11771187
'
11781188

unpack-trees.c

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

21802180
/* Sanity checks */
2181-
if (!o->update || o->index_only || o->skip_sparse_checkout)
2181+
if (o->index_only || o->skip_sparse_checkout)
21822182
BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
21832183
if (o->src_index != o->dst_index || o->fn)
21842184
BUG("update_sparsity() called wrong");

0 commit comments

Comments
 (0)