Skip to content

Commit f6039a9

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: 'set' subcommand
The 'git sparse-checkout set' subcommand takes a list of patterns as arguments and writes them to the sparse-checkout file. Then, it updates the working directory using 'git read-tree -mu HEAD'. The 'set' subcommand will replace the entire contents of the sparse-checkout file. The write_patterns_and_update() method is extracted from cmd_sparse_checkout() to make it easier to implement 'add' and/or 'remove' subcommands in the future. If the core.sparseCheckout config setting is disabled, then enable the config setting in the worktree config. If we set the config this way and the sparse-checkout fails, then re-disable the config setting. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d89f09c commit f6039a9

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

Documentation/git-sparse-checkout.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ To avoid interfering with other worktrees, it first enables the
4242
`extensions.worktreeConfig` setting and makes sure to set the
4343
`core.sparseCheckout` setting in the worktree-specific config file.
4444

45+
'set'::
46+
Write a set of patterns to the sparse-checkout file, as given as
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+
4551
SPARSE CHECKOUT
4652
---------------
4753

builtin/sparse-checkout.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "strbuf.h"
99

1010
static char const * const builtin_sparse_checkout_usage[] = {
11-
N_("git sparse-checkout (init|list)"),
11+
N_("git sparse-checkout (init|list|set) <options>"),
1212
NULL
1313
};
1414

@@ -66,7 +66,7 @@ static int update_working_directory(void)
6666
argv_array_pushl(&argv, "read-tree", "-m", "-u", "HEAD", NULL);
6767

6868
if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
69-
error(_("failed to update index with new sparse-checkout paths"));
69+
error(_("failed to update index with new sparse-checkout patterns"));
7070
result = 1;
7171
}
7272

@@ -136,6 +136,47 @@ static int sparse_checkout_init(int argc, const char **argv)
136136
return update_working_directory();
137137
}
138138

139+
static int write_patterns_and_update(struct pattern_list *pl)
140+
{
141+
char *sparse_filename;
142+
FILE *fp;
143+
144+
sparse_filename = get_sparse_checkout_filename();
145+
fp = fopen(sparse_filename, "w");
146+
write_patterns_to_file(fp, pl);
147+
fclose(fp);
148+
free(sparse_filename);
149+
150+
return update_working_directory();
151+
}
152+
153+
static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
154+
{
155+
static const char *empty_base = "";
156+
int i;
157+
struct pattern_list pl;
158+
int result;
159+
int changed_config = 0;
160+
memset(&pl, 0, sizeof(pl));
161+
162+
for (i = 1; i < argc; i++)
163+
add_pattern(argv[i], empty_base, 0, &pl, 0);
164+
165+
if (!core_apply_sparse_checkout) {
166+
set_config(MODE_ALL_PATTERNS);
167+
core_apply_sparse_checkout = 1;
168+
changed_config = 1;
169+
}
170+
171+
result = write_patterns_and_update(&pl);
172+
173+
if (result && changed_config)
174+
set_config(MODE_NO_PATTERNS);
175+
176+
clear_pattern_list(&pl);
177+
return result;
178+
}
179+
139180
int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
140181
{
141182
static struct option builtin_sparse_checkout_options[] = {
@@ -158,6 +199,8 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
158199
return sparse_checkout_list(argc, argv);
159200
if (!strcmp(argv[0], "init"))
160201
return sparse_checkout_init(argc, argv);
202+
if (!strcmp(argv[0], "set"))
203+
return sparse_checkout_set(argc, argv, prefix);
161204
}
162205

163206
usage_with_options(builtin_sparse_checkout_usage,

t/t1091-sparse-checkout-builtin.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,37 @@ test_expect_success 'clone --sparse' '
9595
test_cmp expect dir
9696
'
9797

98+
test_expect_success 'set enables config' '
99+
git init empty-config &&
100+
(
101+
cd empty-config &&
102+
test_commit test file &&
103+
test_path_is_missing .git/config.worktree &&
104+
test_must_fail git sparse-checkout set nothing &&
105+
test_path_is_file .git/config.worktree &&
106+
test_must_fail git config core.sparseCheckout &&
107+
git sparse-checkout set "/*" &&
108+
test_cmp_config true core.sparseCheckout
109+
)
110+
'
111+
112+
test_expect_success 'set sparse-checkout using builtin' '
113+
git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
114+
cat >expect <<-EOF &&
115+
/*
116+
!/*/
117+
*folder*
118+
EOF
119+
git -C repo sparse-checkout list >actual &&
120+
test_cmp expect actual &&
121+
test_cmp expect repo/.git/info/sparse-checkout &&
122+
ls repo >dir &&
123+
cat >expect <<-EOF &&
124+
a
125+
folder1
126+
folder2
127+
EOF
128+
test_cmp expect dir
129+
'
130+
98131
test_done

0 commit comments

Comments
 (0)