sparse-checkout: sanitize for nested folders
[git] / builtin / sparse-checkout.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 #include "pathspec.h"
6 #include "repository.h"
7 #include "run-command.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10
11 static char const * const builtin_sparse_checkout_usage[] = {
12         N_("git sparse-checkout (init|list|set|disable) <options>"),
13         NULL
14 };
15
16 static char *get_sparse_checkout_filename(void)
17 {
18         return git_pathdup("info/sparse-checkout");
19 }
20
21 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
22 {
23         int i;
24
25         for (i = 0; i < pl->nr; i++) {
26                 struct path_pattern *p = pl->patterns[i];
27
28                 if (p->flags & PATTERN_FLAG_NEGATIVE)
29                         fprintf(fp, "!");
30
31                 fprintf(fp, "%s", p->pattern);
32
33                 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
34                         fprintf(fp, "/");
35
36                 fprintf(fp, "\n");
37         }
38 }
39
40 static int sparse_checkout_list(int argc, const char **argv)
41 {
42         struct pattern_list pl;
43         char *sparse_filename;
44         int res;
45
46         memset(&pl, 0, sizeof(pl));
47
48         sparse_filename = get_sparse_checkout_filename();
49         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
50         free(sparse_filename);
51
52         if (res < 0) {
53                 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
54                 return 0;
55         }
56
57         write_patterns_to_file(stdout, &pl);
58         clear_pattern_list(&pl);
59
60         return 0;
61 }
62
63 static int update_working_directory(void)
64 {
65         struct argv_array argv = ARGV_ARRAY_INIT;
66         int result = 0;
67         argv_array_pushl(&argv, "read-tree", "-m", "-u", "HEAD", NULL);
68
69         if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
70                 error(_("failed to update index with new sparse-checkout patterns"));
71                 result = 1;
72         }
73
74         argv_array_clear(&argv);
75         return result;
76 }
77
78 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
79 {
80         int i;
81         struct pattern_entry *pe;
82         struct hashmap_iter iter;
83         struct string_list sl = STRING_LIST_INIT_DUP;
84         struct strbuf parent_pattern = STRBUF_INIT;
85
86         hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
87                 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
88                         continue;
89
90                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
91                                              pe->pattern,
92                                              &parent_pattern))
93                         string_list_insert(&sl, pe->pattern);
94         }
95
96         string_list_sort(&sl);
97         string_list_remove_duplicates(&sl, 0);
98
99         fprintf(fp, "/*\n!/*/\n");
100
101         for (i = 0; i < sl.nr; i++) {
102                 char *pattern = sl.items[i].string;
103
104                 if (strlen(pattern))
105                         fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
106         }
107
108         string_list_clear(&sl, 0);
109
110         hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
111                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
112                                              pe->pattern,
113                                              &parent_pattern))
114                         string_list_insert(&sl, pe->pattern);
115         }
116
117         strbuf_release(&parent_pattern);
118
119         string_list_sort(&sl);
120         string_list_remove_duplicates(&sl, 0);
121
122         for (i = 0; i < sl.nr; i++) {
123                 char *pattern = sl.items[i].string;
124                 fprintf(fp, "%s/\n", pattern);
125         }
126 }
127
128 static int write_patterns_and_update(struct pattern_list *pl)
129 {
130         char *sparse_filename;
131         FILE *fp;
132
133         sparse_filename = get_sparse_checkout_filename();
134         fp = fopen(sparse_filename, "w");
135
136         if (core_sparse_checkout_cone)
137                 write_cone_to_file(fp, pl);
138         else
139                 write_patterns_to_file(fp, pl);
140
141         fclose(fp);
142         free(sparse_filename);
143
144         return update_working_directory();
145 }
146
147 enum sparse_checkout_mode {
148         MODE_NO_PATTERNS = 0,
149         MODE_ALL_PATTERNS = 1,
150         MODE_CONE_PATTERNS = 2,
151 };
152
153 static int set_config(enum sparse_checkout_mode mode)
154 {
155         const char *config_path;
156
157         if (git_config_set_gently("extensions.worktreeConfig", "true")) {
158                 error(_("failed to set extensions.worktreeConfig setting"));
159                 return 1;
160         }
161
162         config_path = git_path("config.worktree");
163         git_config_set_in_file_gently(config_path,
164                                       "core.sparseCheckout",
165                                       mode ? "true" : NULL);
166
167         git_config_set_in_file_gently(config_path,
168                                       "core.sparseCheckoutCone",
169                                       mode == MODE_CONE_PATTERNS ? "true" : NULL);
170
171         return 0;
172 }
173
174 static char const * const builtin_sparse_checkout_init_usage[] = {
175         N_("git sparse-checkout init [--cone]"),
176         NULL
177 };
178
179 static struct sparse_checkout_init_opts {
180         int cone_mode;
181 } init_opts;
182
183 static int sparse_checkout_init(int argc, const char **argv)
184 {
185         struct pattern_list pl;
186         char *sparse_filename;
187         FILE *fp;
188         int res;
189         struct object_id oid;
190         int mode;
191
192         static struct option builtin_sparse_checkout_init_options[] = {
193                 OPT_BOOL(0, "cone", &init_opts.cone_mode,
194                          N_("initialize the sparse-checkout in cone mode")),
195                 OPT_END(),
196         };
197
198         argc = parse_options(argc, argv, NULL,
199                              builtin_sparse_checkout_init_options,
200                              builtin_sparse_checkout_init_usage, 0);
201
202         mode = init_opts.cone_mode ? MODE_CONE_PATTERNS : MODE_ALL_PATTERNS;
203
204         if (set_config(mode))
205                 return 1;
206
207         memset(&pl, 0, sizeof(pl));
208
209         sparse_filename = get_sparse_checkout_filename();
210         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
211
212         /* If we already have a sparse-checkout file, use it. */
213         if (res >= 0) {
214                 free(sparse_filename);
215                 goto reset_dir;
216         }
217
218         /* initial mode: all blobs at root */
219         fp = xfopen(sparse_filename, "w");
220         if (!fp)
221                 die(_("failed to open '%s'"), sparse_filename);
222
223         free(sparse_filename);
224         fprintf(fp, "/*\n!/*/\n");
225         fclose(fp);
226
227         if (get_oid("HEAD", &oid)) {
228                 /* assume we are in a fresh repo */
229                 return 0;
230         }
231
232 reset_dir:
233         return update_working_directory();
234 }
235
236 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
237 {
238         struct pattern_entry *e = xmalloc(sizeof(*e));
239         e->patternlen = path->len;
240         e->pattern = strbuf_detach(path, NULL);
241         hashmap_entry_init(&e->ent, memhash(e->pattern, e->patternlen));
242
243         hashmap_add(&pl->recursive_hashmap, &e->ent);
244
245         while (e->patternlen) {
246                 char *slash = strrchr(e->pattern, '/');
247                 char *oldpattern = e->pattern;
248                 size_t newlen;
249
250                 if (slash == e->pattern)
251                         break;
252
253                 newlen = slash - e->pattern;
254                 e = xmalloc(sizeof(struct pattern_entry));
255                 e->patternlen = newlen;
256                 e->pattern = xstrndup(oldpattern, newlen);
257                 hashmap_entry_init(&e->ent, memhash(e->pattern, e->patternlen));
258
259                 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
260                         hashmap_add(&pl->parent_hashmap, &e->ent);
261         }
262 }
263
264 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
265 {
266         strbuf_trim(line);
267
268         strbuf_trim_trailing_dir_sep(line);
269
270         if (!line->len)
271                 return;
272
273         if (line->buf[0] != '/')
274                 strbuf_insert(line, 0, "/", 1);
275
276         insert_recursive_pattern(pl, line);
277 }
278
279 static char const * const builtin_sparse_checkout_set_usage[] = {
280         N_("git sparse-checkout set (--stdin | <patterns>)"),
281         NULL
282 };
283
284 static struct sparse_checkout_set_opts {
285         int use_stdin;
286 } set_opts;
287
288 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
289 {
290         static const char *empty_base = "";
291         int i;
292         struct pattern_list pl;
293         int result;
294         int changed_config = 0;
295
296         static struct option builtin_sparse_checkout_set_options[] = {
297                 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
298                          N_("read patterns from standard in")),
299                 OPT_END(),
300         };
301
302         memset(&pl, 0, sizeof(pl));
303
304         argc = parse_options(argc, argv, prefix,
305                              builtin_sparse_checkout_set_options,
306                              builtin_sparse_checkout_set_usage,
307                              PARSE_OPT_KEEP_UNKNOWN);
308
309         if (core_sparse_checkout_cone) {
310                 struct strbuf line = STRBUF_INIT;
311
312                 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
313                 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
314
315                 if (set_opts.use_stdin) {
316                         while (!strbuf_getline(&line, stdin))
317                                 strbuf_to_cone_pattern(&line, &pl);
318                 } else {
319                         for (i = 0; i < argc; i++) {
320                                 strbuf_setlen(&line, 0);
321                                 strbuf_addstr(&line, argv[i]);
322                                 strbuf_to_cone_pattern(&line, &pl);
323                         }
324                 }
325         } else {
326                 if (set_opts.use_stdin) {
327                         struct strbuf line = STRBUF_INIT;
328
329                         while (!strbuf_getline(&line, stdin)) {
330                                 size_t len;
331                                 char *buf = strbuf_detach(&line, &len);
332                                 add_pattern(buf, empty_base, 0, &pl, 0);
333                         }
334                 } else {
335                         for (i = 0; i < argc; i++)
336                                 add_pattern(argv[i], empty_base, 0, &pl, 0);
337                 }
338         }
339
340         if (!core_apply_sparse_checkout) {
341                 set_config(MODE_ALL_PATTERNS);
342                 core_apply_sparse_checkout = 1;
343                 changed_config = 1;
344         }
345
346         result = write_patterns_and_update(&pl);
347
348         if (result && changed_config)
349                 set_config(MODE_NO_PATTERNS);
350
351         clear_pattern_list(&pl);
352         return result;
353 }
354
355 static int sparse_checkout_disable(int argc, const char **argv)
356 {
357         char *sparse_filename;
358         FILE *fp;
359
360         if (set_config(MODE_ALL_PATTERNS))
361                 die(_("failed to change config"));
362
363         sparse_filename = get_sparse_checkout_filename();
364         fp = xfopen(sparse_filename, "w");
365         fprintf(fp, "/*\n");
366         fclose(fp);
367
368         if (update_working_directory())
369                 die(_("error while refreshing working directory"));
370
371         unlink(sparse_filename);
372         free(sparse_filename);
373
374         return set_config(MODE_NO_PATTERNS);
375 }
376
377 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
378 {
379         static struct option builtin_sparse_checkout_options[] = {
380                 OPT_END(),
381         };
382
383         if (argc == 2 && !strcmp(argv[1], "-h"))
384                 usage_with_options(builtin_sparse_checkout_usage,
385                                    builtin_sparse_checkout_options);
386
387         argc = parse_options(argc, argv, prefix,
388                              builtin_sparse_checkout_options,
389                              builtin_sparse_checkout_usage,
390                              PARSE_OPT_STOP_AT_NON_OPTION);
391
392         git_config(git_default_config, NULL);
393
394         if (argc > 0) {
395                 if (!strcmp(argv[0], "list"))
396                         return sparse_checkout_list(argc, argv);
397                 if (!strcmp(argv[0], "init"))
398                         return sparse_checkout_init(argc, argv);
399                 if (!strcmp(argv[0], "set"))
400                         return sparse_checkout_set(argc, argv, prefix);
401                 if (!strcmp(argv[0], "disable"))
402                         return sparse_checkout_disable(argc, argv);
403         }
404
405         usage_with_options(builtin_sparse_checkout_usage,
406                            builtin_sparse_checkout_options);
407 }