Skip to content

Commit 72918c1

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: create 'disable' subcommand
The instructions for disabling a sparse-checkout to a full working directory are complicated and non-intuitive. Add a subcommand, 'git sparse-checkout disable', to perform those steps for the user. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bffca9 commit 72918c1

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

Documentation/git-sparse-checkout.txt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ To avoid interfering with other worktrees, it first enables the
5151
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

54+
'disable'::
55+
Remove the sparse-checkout file, set `core.sparseCheckout` to
56+
`false`, and restore the working directory to include all files.
57+
5458
SPARSE CHECKOUT
5559
---------------
5660

@@ -68,6 +72,14 @@ directory, it updates the skip-worktree bits in the index based
6872
on this file. The files matching the patterns in the file will
6973
appear in the working directory, and the rest will not.
7074

75+
To enable the sparse-checkout feature, run `git sparse-checkout init` to
76+
initialize a simple sparse-checkout file and enable the `core.sparseCheckout`
77+
config setting. Then, run `git sparse-checkout set` to modify the patterns in
78+
the sparse-checkout file.
79+
80+
To repopulate the working directory with all files, use the
81+
`git sparse-checkout disable` command.
82+
7183
## FULL PATTERN SET
7284

7385
By default, the sparse-checkout file uses the same syntax as `.gitignore`
@@ -82,21 +94,6 @@ using negative patterns. For example, to remove the file `unwanted`:
8294
!unwanted
8395
----------------
8496

85-
Another tricky thing is fully repopulating the working directory when you
86-
no longer want sparse checkout. You cannot just disable "sparse
87-
checkout" because skip-worktree bits are still in the index and your working
88-
directory is still sparsely populated. You should re-populate the working
89-
directory with the `$GIT_DIR/info/sparse-checkout` file content as
90-
follows:
91-
92-
----------------
93-
/*
94-
----------------
95-
96-
Then you can disable sparse checkout. Sparse checkout support in 'git
97-
checkout' and similar commands is disabled by default. You need to
98-
set `core.sparseCheckout` to `true` in order to have sparse checkout
99-
support.
10097

10198
SEE ALSO
10299
--------

builtin/sparse-checkout.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "strbuf.h"
99

1010
static char const * const builtin_sparse_checkout_usage[] = {
11-
N_("git sparse-checkout (init|list|set) <options>"),
11+
N_("git sparse-checkout (init|list|set|disable) <options>"),
1212
NULL
1313
};
1414

@@ -207,6 +207,28 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
207207
return result;
208208
}
209209

210+
static int sparse_checkout_disable(int argc, const char **argv)
211+
{
212+
char *sparse_filename;
213+
FILE *fp;
214+
215+
if (set_config(MODE_ALL_PATTERNS))
216+
die(_("failed to change config"));
217+
218+
sparse_filename = get_sparse_checkout_filename();
219+
fp = xfopen(sparse_filename, "w");
220+
fprintf(fp, "/*\n");
221+
fclose(fp);
222+
223+
if (update_working_directory())
224+
die(_("error while refreshing working directory"));
225+
226+
unlink(sparse_filename);
227+
free(sparse_filename);
228+
229+
return set_config(MODE_NO_PATTERNS);
230+
}
231+
210232
int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
211233
{
212234
static struct option builtin_sparse_checkout_options[] = {
@@ -231,6 +253,8 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
231253
return sparse_checkout_init(argc, argv);
232254
if (!strcmp(argv[0], "set"))
233255
return sparse_checkout_set(argc, argv, prefix);
256+
if (!strcmp(argv[0], "disable"))
257+
return sparse_checkout_disable(argc, argv);
234258
}
235259

236260
usage_with_options(builtin_sparse_checkout_usage,

t/t1091-sparse-checkout-builtin.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,19 @@ test_expect_success 'set sparse-checkout using --stdin' '
148148
test_cmp expect dir
149149
'
150150

151+
test_expect_success 'sparse-checkout disable' '
152+
git -C repo sparse-checkout disable &&
153+
test_path_is_missing repo/.git/info/sparse-checkout &&
154+
git -C repo config --list >config &&
155+
test_must_fail git config core.sparseCheckout &&
156+
ls repo >dir &&
157+
cat >expect <<-EOF &&
158+
a
159+
deep
160+
folder1
161+
folder2
162+
EOF
163+
test_cmp expect dir
164+
'
165+
151166
test_done

0 commit comments

Comments
 (0)