]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: add '--stdin' option to set subcommand
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 21 Nov 2019 22:04:37 +0000 (22:04 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 22 Nov 2019 07:11:44 +0000 (16:11 +0900)
The 'git sparse-checkout set' subcommand takes a list of patterns
and places them in the sparse-checkout file. Then, it updates the
working directory to match those patterns. For a large list of
patterns, the command-line call can get very cumbersome.

Add a '--stdin' option to instead read patterns over standard in.

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 ca62669b8cd3381876c720f8fb57f1ae41341814..a724eae09c15563016dbe4e33266114719279969 100644 (file)
@@ -47,6 +47,9 @@ To avoid interfering with other worktrees, it first enables the
        a list of arguments following the 'set' subcommand. Update the
        working directory to match the new patterns. Enable the
        core.sparseCheckout config setting if it is not already enabled.
++
+When the `--stdin` option is provided, the patterns are read from
+standard in as a newline-delimited list instead of from the arguments.
 
 SPARSE CHECKOUT
 ---------------
index 95cbd0a42c71849eb64930abf8e0aa148ac098c5..82bff0020ded96ca1ce3db8609fffebb08d94455 100644 (file)
@@ -150,6 +150,15 @@ static int write_patterns_and_update(struct pattern_list *pl)
        return update_working_directory();
 }
 
+static char const * const builtin_sparse_checkout_set_usage[] = {
+       N_("git sparse-checkout set (--stdin | <patterns>)"),
+       NULL
+};
+
+static struct sparse_checkout_set_opts {
+       int use_stdin;
+} set_opts;
+
 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
 {
        static const char *empty_base = "";
@@ -157,10 +166,31 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
        struct pattern_list pl;
        int result;
        int changed_config = 0;
+
+       static struct option builtin_sparse_checkout_set_options[] = {
+               OPT_BOOL(0, "stdin", &set_opts.use_stdin,
+                        N_("read patterns from standard in")),
+               OPT_END(),
+       };
+
        memset(&pl, 0, sizeof(pl));
 
-       for (i = 1; i < argc; i++)
-               add_pattern(argv[i], empty_base, 0, &pl, 0);
+       argc = parse_options(argc, argv, prefix,
+                            builtin_sparse_checkout_set_options,
+                            builtin_sparse_checkout_set_usage,
+                            PARSE_OPT_KEEP_UNKNOWN);
+
+       if (set_opts.use_stdin) {
+               struct strbuf line = STRBUF_INIT;
+
+               while (!strbuf_getline(&line, stdin)) {
+                       char *buf = strbuf_detach(&line, NULL);
+                       add_pattern(buf, empty_base, 0, &pl, 0);
+               }
+       } else {
+               for (i = 0; i < argc; i++)
+                       add_pattern(argv[i], empty_base, 0, &pl, 0);
+       }
 
        if (!core_apply_sparse_checkout) {
                set_config(MODE_ALL_PATTERNS);
index 72d8bc5c2517cdf3f7b415dbb72e3a3bd4bd53ac..07e73b46746bbb54da24d3cfdc073027c7a71568 100755 (executable)
@@ -128,4 +128,24 @@ test_expect_success 'set sparse-checkout using builtin' '
        test_cmp expect dir
 '
 
+test_expect_success 'set sparse-checkout using --stdin' '
+       cat >expect <<-EOF &&
+               /*
+               !/*/
+               /folder1/
+               /folder2/
+       EOF
+       git -C repo sparse-checkout set --stdin <expect &&
+       git -C repo sparse-checkout list >actual &&
+       test_cmp expect actual &&
+       test_cmp expect repo/.git/info/sparse-checkout &&
+       ls repo >dir  &&
+       cat >expect <<-EOF &&
+               a
+               folder1
+               folder2
+       EOF
+       test_cmp expect dir
+'
+
 test_done