]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: create 'disable' subcommand
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 21 Nov 2019 22:04:38 +0000 (22:04 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 22 Nov 2019 07:11:44 +0000 (16:11 +0900)
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>
Documentation/git-sparse-checkout.txt
builtin/sparse-checkout.c
t/t1091-sparse-checkout-builtin.sh

index a724eae09c15563016dbe4e33266114719279969..c2cb19f80d36e556e3643cd94b67fa0a52dee2e8 100644 (file)
@@ -51,6 +51,10 @@ To avoid interfering with other worktrees, it first enables the
 When the `--stdin` option is provided, the patterns are read from
 standard in as a newline-delimited list instead of from the arguments.
 
+'disable'::
+       Remove the sparse-checkout file, set `core.sparseCheckout` to
+       `false`, and restore the working directory to include all files.
+
 SPARSE CHECKOUT
 ---------------
 
@@ -68,6 +72,14 @@ directory, it updates the skip-worktree bits in the index based
 on this file. The files matching the patterns in the file will
 appear in the working directory, and the rest will not.
 
+To enable the sparse-checkout feature, run `git sparse-checkout init` to
+initialize a simple sparse-checkout file and enable the `core.sparseCheckout`
+config setting. Then, run `git sparse-checkout set` to modify the patterns in
+the sparse-checkout file.
+
+To repopulate the working directory with all files, use the
+`git sparse-checkout disable` command.
+
 ## FULL PATTERN SET
 
 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`:
 !unwanted
 ----------------
 
-Another tricky thing is fully repopulating the working directory when you
-no longer want sparse checkout. You cannot just disable "sparse
-checkout" because skip-worktree bits are still in the index and your working
-directory is still sparsely populated. You should re-populate the working
-directory with the `$GIT_DIR/info/sparse-checkout` file content as
-follows:
-
-----------------
-/*
-----------------
-
-Then you can disable sparse checkout. Sparse checkout support in 'git
-checkout' and similar commands is disabled by default. You need to
-set `core.sparseCheckout` to `true` in order to have sparse checkout
-support.
 
 SEE ALSO
 --------
index 82bff0020ded96ca1ce3db8609fffebb08d94455..e3a8d3460ad695c9b4ac2f757f792082951f1dab 100644 (file)
@@ -8,7 +8,7 @@
 #include "strbuf.h"
 
 static char const * const builtin_sparse_checkout_usage[] = {
-       N_("git sparse-checkout (init|list|set) <options>"),
+       N_("git sparse-checkout (init|list|set|disable) <options>"),
        NULL
 };
 
@@ -207,6 +207,28 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
        return result;
 }
 
+static int sparse_checkout_disable(int argc, const char **argv)
+{
+       char *sparse_filename;
+       FILE *fp;
+
+       if (set_config(MODE_ALL_PATTERNS))
+               die(_("failed to change config"));
+
+       sparse_filename = get_sparse_checkout_filename();
+       fp = xfopen(sparse_filename, "w");
+       fprintf(fp, "/*\n");
+       fclose(fp);
+
+       if (update_working_directory())
+               die(_("error while refreshing working directory"));
+
+       unlink(sparse_filename);
+       free(sparse_filename);
+
+       return set_config(MODE_NO_PATTERNS);
+}
+
 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
 {
        static struct option builtin_sparse_checkout_options[] = {
@@ -231,6 +253,8 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
                        return sparse_checkout_init(argc, argv);
                if (!strcmp(argv[0], "set"))
                        return sparse_checkout_set(argc, argv, prefix);
+               if (!strcmp(argv[0], "disable"))
+                       return sparse_checkout_disable(argc, argv);
        }
 
        usage_with_options(builtin_sparse_checkout_usage,
index 07e73b46746bbb54da24d3cfdc073027c7a71568..c385c62c92748f149d0e0aa3b32bec12471781c9 100755 (executable)
@@ -148,4 +148,19 @@ test_expect_success 'set sparse-checkout using --stdin' '
        test_cmp expect dir
 '
 
+test_expect_success 'sparse-checkout disable' '
+       git -C repo sparse-checkout disable &&
+       test_path_is_missing repo/.git/info/sparse-checkout &&
+       git -C repo config --list >config &&
+       test_must_fail git config core.sparseCheckout &&
+       ls repo >dir &&
+       cat >expect <<-EOF &&
+               a
+               deep
+               folder1
+               folder2
+       EOF
+       test_cmp expect dir
+'
+
 test_done