Skip to content

Commit 99dfa6f

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: use in-process update for disable subcommand
The 'git sparse-checkout disable' subcommand returns a user to a full working directory. The old process for doing this required updating the sparse-checkout file with the "/*" pattern and then updating the working directory with core.sparseCheckout enabled. Finally, the sparse-checkout file could be removed and the config setting disabled. However, it is valuable to keep a user's sparse-checkout file intact so they can re-enable the sparse-checkout they previously used with 'git sparse-checkout init'. This is now possible with the in-process mechanism for updating the working directory. Reported-by: Szeder Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e091228 commit 99dfa6f

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

Documentation/git-sparse-checkout.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ When the `--stdin` option is provided, the patterns are read from
5252
standard in as a newline-delimited list instead of from the arguments.
5353

5454
'disable'::
55-
Remove the sparse-checkout file, set `core.sparseCheckout` to
56-
`false`, and restore the working directory to include all files.
55+
Disable the `core.sparseCheckout` config setting, and restore the
56+
working directory to include all files. Leaves the sparse-checkout
57+
file intact so a later 'git sparse-checkout init' command may
58+
return the working directory to the same state.
5759

5860
SPARSE CHECKOUT
5961
---------------

builtin/sparse-checkout.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,24 +412,23 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
412412

413413
static int sparse_checkout_disable(int argc, const char **argv)
414414
{
415-
char *sparse_filename;
416-
FILE *fp;
415+
static const char *empty_base = "";
416+
struct pattern_list pl;
417+
struct strbuf match_all = STRBUF_INIT;
417418

418-
if (set_config(MODE_ALL_PATTERNS))
419-
die(_("failed to change config"));
419+
memset(&pl, 0, sizeof(pl));
420+
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
421+
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
422+
pl.use_cone_patterns = 0;
423+
core_apply_sparse_checkout = 1;
420424

421-
sparse_filename = get_sparse_checkout_filename();
422-
fp = xfopen(sparse_filename, "w");
423-
fprintf(fp, "/*\n");
424-
fclose(fp);
425+
strbuf_addstr(&match_all, "/*");
426+
add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
425427

426-
core_apply_sparse_checkout = 1;
427-
if (update_working_directory(NULL))
428+
if (update_working_directory(&pl))
428429
die(_("error while refreshing working directory"));
429430

430-
unlink(sparse_filename);
431-
free(sparse_filename);
432-
431+
clear_pattern_list(&pl);
433432
return set_config(MODE_NO_PATTERNS);
434433
}
435434

t/t1091-sparse-checkout-builtin.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ test_expect_success 'cone mode: warn on bad pattern' '
172172
'
173173

174174
test_expect_success 'sparse-checkout disable' '
175+
test_when_finished rm -rf repo/.git/info/sparse-checkout &&
175176
git -C repo sparse-checkout disable &&
176-
test_path_is_missing repo/.git/info/sparse-checkout &&
177+
test_path_is_file repo/.git/info/sparse-checkout &&
177178
git -C repo config --list >config &&
178179
test_must_fail git config core.sparseCheckout &&
179180
ls repo >dir &&

0 commit comments

Comments
 (0)