@matheusbsbs / matheus-bagliano / commits / 7bffca95ea
Fork 0

sparse-checkout: add '--stdin' option to set subcommand

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>

Derrick Stolee committed Nov 21, 2019 at 22:04 UTC 7bffca95ea1ca4f55663374ea9b929b9df5be04b
3 files changed +55 -2
Documentation/git-sparse-checkout.txt
+3
@@ -47,6 +47,9 @@ To avoid interfering with other worktrees, it first enables the
47 a list of arguments following the 'set' subcommand. Update the
48 working directory to match the new patterns. Enable the
49 core.sparseCheckout config setting if it is not already enabled.
50 ++
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 SPARSE CHECKOUT
55 ---------------
builtin/sparse-checkout.c
+32 -2
@@ -150,6 +150,15 @@ static int write_patterns_and_update(struct pattern_list *pl)
150 return update_working_directory();
151 }
152
153 +static char const * const builtin_sparse_checkout_set_usage[] = {
154 + N_("git sparse-checkout set (--stdin | <patterns>)"),
155 + NULL
156 +};
157 +
158 +static struct sparse_checkout_set_opts {
159 + int use_stdin;
160 +} set_opts;
161 +
162 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
163 {
164 static const char *empty_base = "";
@@ -157,10 +166,31 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
166 struct pattern_list pl;
167 int result;
168 int changed_config = 0;
169 +
170 + static struct option builtin_sparse_checkout_set_options[] = {
171 + OPT_BOOL(0, "stdin", &set_opts.use_stdin,
172 + N_("read patterns from standard in")),
173 + OPT_END(),
174 + };
175 +
176 memset(&pl, 0, sizeof(pl));
177
162 - for (i = 1; i < argc; i++)
163 - add_pattern(argv[i], empty_base, 0, &pl, 0);
178 + argc = parse_options(argc, argv, prefix,
179 + builtin_sparse_checkout_set_options,
180 + builtin_sparse_checkout_set_usage,
181 + PARSE_OPT_KEEP_UNKNOWN);
182 +
183 + if (set_opts.use_stdin) {
184 + struct strbuf line = STRBUF_INIT;
185 +
186 + while (!strbuf_getline(&line, stdin)) {
187 + char *buf = strbuf_detach(&line, NULL);
188 + add_pattern(buf, empty_base, 0, &pl, 0);
189 + }
190 + } else {
191 + for (i = 0; i < argc; i++)
192 + add_pattern(argv[i], empty_base, 0, &pl, 0);
193 + }
194
195 if (!core_apply_sparse_checkout) {
196 set_config(MODE_ALL_PATTERNS);
t/t1091-sparse-checkout-builtin.sh
+20
@@ -128,4 +128,24 @@ test_expect_success 'set sparse-checkout using builtin' '
128 test_cmp expect dir
129 '
130
131 +test_expect_success 'set sparse-checkout using --stdin' '
132 + cat >expect <<-EOF &&
133 + /*
134 + !/*/
135 + /folder1/
136 + /folder2/
137 + EOF
138 + git -C repo sparse-checkout set --stdin <expect &&
139 + git -C repo sparse-checkout list >actual &&
140 + test_cmp expect actual &&
141 + test_cmp expect repo/.git/info/sparse-checkout &&
142 + ls repo >dir &&
143 + cat >expect <<-EOF &&
144 + a
145 + folder1
146 + folder2
147 + EOF
148 + test_cmp expect dir
149 +'
150 +
151 test_done