Skip to content

Commit 7bffca9

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f6039a9 commit 7bffca9

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

Documentation/git-sparse-checkout.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ To avoid interfering with other worktrees, it first enables the
4747
a list of arguments following the 'set' subcommand. Update the
4848
working directory to match the new patterns. Enable the
4949
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.
5053

5154
SPARSE CHECKOUT
5255
---------------

builtin/sparse-checkout.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,47 @@ static int write_patterns_and_update(struct pattern_list *pl)
150150
return update_working_directory();
151151
}
152152

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+
153162
static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
154163
{
155164
static const char *empty_base = "";
156165
int i;
157166
struct pattern_list pl;
158167
int result;
159168
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+
160176
memset(&pl, 0, sizeof(pl));
161177

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+
}
164194

165195
if (!core_apply_sparse_checkout) {
166196
set_config(MODE_ALL_PATTERNS);

t/t1091-sparse-checkout-builtin.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,24 @@ test_expect_success 'set sparse-checkout using builtin' '
128128
test_cmp expect dir
129129
'
130130

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+
131151
test_done

0 commit comments

Comments
 (0)