@matheusbsbs / matheus-bagliano / commits / f6039a9423
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 21, 2019 at 22:04 UTC f6039a9423d042d61fb4cfccb395bd04c4bd5322
3 files changed +84 -2
Documentation/git-sparse-checkout.txt
+6
@@ -42,6 +42,12 @@ To avoid interfering with other worktrees, it first enables the
42 `extensions.worktreeConfig` setting and makes sure to set the
43 `core.sparseCheckout` setting in the worktree-specific config file.
44
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 +
51 SPARSE CHECKOUT
52 ---------------
53
builtin/sparse-checkout.c
+45 -2
@@ -8,7 +8,7 @@
8 #include "strbuf.h"
9
10 static char const * const builtin_sparse_checkout_usage[] = {
11 - N_("git sparse-checkout (init|list)"),
11 + N_("git sparse-checkout (init|list|set) <options>"),
12 NULL
13 };
14
@@ -66,7 +66,7 @@ static int update_working_directory(void)
66 argv_array_pushl(&argv, "read-tree", "-m", "-u", "HEAD", NULL);
67
68 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"));
70 result = 1;
71 }
72
@@ -136,6 +136,47 @@ reset_dir:
136 return update_working_directory();
137 }
138
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 +
180 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
181 {
182 static struct option builtin_sparse_checkout_options[] = {
@@ -158,6 +199,8 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
199 return sparse_checkout_list(argc, argv);
200 if (!strcmp(argv[0], "init"))
201 return sparse_checkout_init(argc, argv);
202 + if (!strcmp(argv[0], "set"))
203 + return sparse_checkout_set(argc, argv, prefix);
204 }
205
206 usage_with_options(builtin_sparse_checkout_usage,
t/t1091-sparse-checkout-builtin.sh
+33
@@ -95,4 +95,37 @@ test_expect_success 'clone --sparse' '
95 test_cmp expect dir
96 '
97
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 +
131 test_done