@matheusbsbs / matheus-bagliano / commits / 72918c1ad9
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 21, 2019 at 22:04 UTC 72918c1ad91504f56c395cc91c5072651125662a
3 files changed +52 -16
Documentation/git-sparse-checkout.txt
+12 -15
@@ -51,6 +51,10 @@ To avoid interfering with other worktrees, it first enables the
51 When the `--stdin` option is provided, the patterns are read from
52 standard in as a newline-delimited list instead of from the arguments.
53
54 +'disable'::
55 + Remove the sparse-checkout file, set `core.sparseCheckout` to
56 + `false`, and restore the working directory to include all files.
57 +
58 SPARSE CHECKOUT
59 ---------------
60
@@ -68,6 +72,14 @@ directory, it updates the skip-worktree bits in the index based
72 on this file. The files matching the patterns in the file will
73 appear in the working directory, and the rest will not.
74
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 +
83 ## FULL PATTERN SET
84
85 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`:
94 !unwanted
95 ----------------
96
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.
97
98 SEE ALSO
99 --------
builtin/sparse-checkout.c
+25 -1
@@ -8,7 +8,7 @@
8 #include "strbuf.h"
9
10 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>"),
12 NULL
13 };
14
@@ -207,6 +207,28 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
207 return result;
208 }
209
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 +
232 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
233 {
234 static struct option builtin_sparse_checkout_options[] = {
@@ -231,6 +253,8 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
253 return sparse_checkout_init(argc, argv);
254 if (!strcmp(argv[0], "set"))
255 return sparse_checkout_set(argc, argv, prefix);
256 + if (!strcmp(argv[0], "disable"))
257 + return sparse_checkout_disable(argc, argv);
258 }
259
260 usage_with_options(builtin_sparse_checkout_usage,
t/t1091-sparse-checkout-builtin.sh
+15
@@ -148,4 +148,19 @@ test_expect_success 'set sparse-checkout using --stdin' '
148 test_cmp expect dir
149 '
150
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 +
166 test_done