Skip to content

Commit eb53242

Browse files
derrickstoleegitster
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 02d2554 commit eb53242

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
@@ -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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ test_expect_success 'clean with staged sparse change' '
11041104
11051105
cat >expect <<-\EOF &&
11061106
Would remove deep/deeper2/
1107+
Would remove folder1/
11071108
EOF
11081109
11091110
git -C repo sparse-checkout clean --dry-run >out &&
@@ -1115,6 +1116,7 @@ test_expect_success 'clean with staged sparse change' '
11151116
# deletes deep/deeper2/ but leaves folder1/ and folder2/
11161117
cat >expect <<-\EOF &&
11171118
Removing deep/deeper2/
1119+
Removing folder1/
11181120
EOF
11191121
11201122
# The previous test case checked the -f option, so
@@ -1124,7 +1126,7 @@ test_expect_success 'clean with staged sparse change' '
11241126
test_cmp expect out &&
11251127
11261128
test_path_is_missing repo/deep/deeper2 &&
1127-
test_path_exists repo/folder1 &&
1129+
test_path_is_missing repo/folder1 &&
11281130
test_path_exists repo/folder2
11291131
'
11301132

@@ -1147,7 +1149,11 @@ test_expect_success 'sparse-checkout operations with merge conflicts' '
11471149
git commit -a -m "left" &&
11481150
11491151
git checkout -b merge &&
1150-
git sparse-checkout set deep/deeper1 &&
1152+
1153+
touch deep/deeper2/extra &&
1154+
git sparse-checkout set deep/deeper1 2>err &&
1155+
grep "contains untracked files" err &&
1156+
test_path_exists deep/deeper2/extra &&
11511157
11521158
test_must_fail git merge -m "will-conflict" right &&
11531159
@@ -1159,15 +1165,19 @@ test_expect_success 'sparse-checkout operations with merge conflicts' '
11591165
git merge --continue &&
11601166
11611167
test_path_exists folder1/even/more/dirs/file &&
1168+
test_path_exists deep/deeper2/extra &&
1169+
1170+
cat >expect <<-\EOF &&
1171+
Removing deep/deeper2/
1172+
Removing folder1/
1173+
EOF
11621174
11631175
# clean does not remove the file, because the
11641176
# SKIP_WORKTREE bit was not cleared by the merge command.
11651177
git sparse-checkout clean -f >out &&
1166-
test_line_count = 0 out &&
1167-
test_path_exists folder1/even/more/dirs/file &&
1168-
1169-
git sparse-checkout reapply &&
1170-
test_path_is_missing folder1
1178+
test_cmp expect out &&
1179+
test_path_is_missing folder1 &&
1180+
test_path_is_missing deep/deeper2
11711181
)
11721182
'
11731183

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)