sparse-checkout: write escaped patterns in cone mode
[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 #include "cache.h"
11 #include "cache-tree.h"
12 #include "lockfile.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 #include "wt-status.h"
16 #include "quote.h"
17
18 static const char *empty_base = "";
19
20 static char const * const builtin_sparse_checkout_usage[] = {
21         N_("git sparse-checkout (init|list|set|disable) <options>"),
22         NULL
23 };
24
25 static char *get_sparse_checkout_filename(void)
26 {
27         return git_pathdup("info/sparse-checkout");
28 }
29
30 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
31 {
32         int i;
33
34         for (i = 0; i < pl->nr; i++) {
35                 struct path_pattern *p = pl->patterns[i];
36
37                 if (p->flags & PATTERN_FLAG_NEGATIVE)
38                         fprintf(fp, "!");
39
40                 fprintf(fp, "%s", p->pattern);
41
42                 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
43                         fprintf(fp, "/");
44
45                 fprintf(fp, "\n");
46         }
47 }
48
49 static int sparse_checkout_list(int argc, const char **argv)
50 {
51         struct pattern_list pl;
52         char *sparse_filename;
53         int res;
54
55         memset(&pl, 0, sizeof(pl));
56
57         pl.use_cone_patterns = core_sparse_checkout_cone;
58
59         sparse_filename = get_sparse_checkout_filename();
60         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
61         free(sparse_filename);
62
63         if (res < 0) {
64                 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
65                 return 0;
66         }
67
68         if (pl.use_cone_patterns) {
69                 int i;
70                 struct pattern_entry *pe;
71                 struct hashmap_iter iter;
72                 struct string_list sl = STRING_LIST_INIT_DUP;
73
74                 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
75                         /* pe->pattern starts with "/", skip it */
76                         string_list_insert(&sl, pe->pattern + 1);
77                 }
78
79                 string_list_sort(&sl);
80
81                 for (i = 0; i < sl.nr; i++)
82                         printf("%s\n", sl.items[i].string);
83
84                 return 0;
85         }
86
87         write_patterns_to_file(stdout, &pl);
88         clear_pattern_list(&pl);
89
90         return 0;
91 }
92
93 static int update_working_directory(struct pattern_list *pl)
94 {
95         int result = 0;
96         struct unpack_trees_options o;
97         struct lock_file lock_file = LOCK_INIT;
98         struct object_id oid;
99         struct tree *tree;
100         struct tree_desc t;
101         struct repository *r = the_repository;
102
103         if (repo_read_index_unmerged(r))
104                 die(_("you need to resolve your current index first"));
105
106         if (get_oid("HEAD", &oid))
107                 return 0;
108
109         tree = parse_tree_indirect(&oid);
110         parse_tree(tree);
111         init_tree_desc(&t, tree->buffer, tree->size);
112
113         memset(&o, 0, sizeof(o));
114         o.verbose_update = isatty(2);
115         o.merge = 1;
116         o.update = 1;
117         o.fn = oneway_merge;
118         o.head_idx = -1;
119         o.src_index = r->index;
120         o.dst_index = r->index;
121         o.skip_sparse_checkout = 0;
122         o.pl = pl;
123         o.keep_pattern_list = !!pl;
124
125         resolve_undo_clear_index(r->index);
126         setup_work_tree();
127
128         cache_tree_free(&r->index->cache_tree);
129
130         repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
131
132         core_apply_sparse_checkout = 1;
133         result = unpack_trees(1, &t, &o);
134
135         if (!result) {
136                 prime_cache_tree(r, r->index, tree);
137                 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
138         } else
139                 rollback_lock_file(&lock_file);
140
141         return result;
142 }
143
144 static char *escaped_pattern(char *pattern)
145 {
146         char *p = pattern;
147         struct strbuf final = STRBUF_INIT;
148
149         while (*p) {
150                 if (*p == '*' || *p == '\\')
151                         strbuf_addch(&final, '\\');
152
153                 strbuf_addch(&final, *p);
154                 p++;
155         }
156
157         return strbuf_detach(&final, NULL);
158 }
159
160 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
161 {
162         int i;
163         struct pattern_entry *pe;
164         struct hashmap_iter iter;
165         struct string_list sl = STRING_LIST_INIT_DUP;
166         struct strbuf parent_pattern = STRBUF_INIT;
167
168         hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
169                 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
170                         continue;
171
172                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
173                                              pe->pattern,
174                                              &parent_pattern))
175                         string_list_insert(&sl, pe->pattern);
176         }
177
178         string_list_sort(&sl);
179         string_list_remove_duplicates(&sl, 0);
180
181         fprintf(fp, "/*\n!/*/\n");
182
183         for (i = 0; i < sl.nr; i++) {
184                 char *pattern = escaped_pattern(sl.items[i].string);
185
186                 if (strlen(pattern))
187                         fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
188                 free(pattern);
189         }
190
191         string_list_clear(&sl, 0);
192
193         hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
194                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
195                                              pe->pattern,
196                                              &parent_pattern))
197                         string_list_insert(&sl, pe->pattern);
198         }
199
200         strbuf_release(&parent_pattern);
201
202         string_list_sort(&sl);
203         string_list_remove_duplicates(&sl, 0);
204
205         for (i = 0; i < sl.nr; i++) {
206                 char *pattern = escaped_pattern(sl.items[i].string);
207                 fprintf(fp, "%s/\n", pattern);
208                 free(pattern);
209         }
210 }
211
212 static int write_patterns_and_update(struct pattern_list *pl)
213 {
214         char *sparse_filename;
215         FILE *fp;
216         int fd;
217         struct lock_file lk = LOCK_INIT;
218         int result;
219
220         sparse_filename = get_sparse_checkout_filename();
221
222         if (safe_create_leading_directories(sparse_filename))
223                 die(_("failed to create directory for sparse-checkout file"));
224
225         fd = hold_lock_file_for_update(&lk, sparse_filename,
226                                       LOCK_DIE_ON_ERROR);
227
228         result = update_working_directory(pl);
229         if (result) {
230                 rollback_lock_file(&lk);
231                 free(sparse_filename);
232                 clear_pattern_list(pl);
233                 update_working_directory(NULL);
234                 return result;
235         }
236
237         fp = xfdopen(fd, "w");
238
239         if (core_sparse_checkout_cone)
240                 write_cone_to_file(fp, pl);
241         else
242                 write_patterns_to_file(fp, pl);
243
244         fflush(fp);
245         commit_lock_file(&lk);
246
247         free(sparse_filename);
248         clear_pattern_list(pl);
249
250         return 0;
251 }
252
253 enum sparse_checkout_mode {
254         MODE_NO_PATTERNS = 0,
255         MODE_ALL_PATTERNS = 1,
256         MODE_CONE_PATTERNS = 2,
257 };
258
259 static int set_config(enum sparse_checkout_mode mode)
260 {
261         const char *config_path;
262
263         if (git_config_set_gently("extensions.worktreeConfig", "true")) {
264                 error(_("failed to set extensions.worktreeConfig setting"));
265                 return 1;
266         }
267
268         config_path = git_path("config.worktree");
269         git_config_set_in_file_gently(config_path,
270                                       "core.sparseCheckout",
271                                       mode ? "true" : NULL);
272
273         git_config_set_in_file_gently(config_path,
274                                       "core.sparseCheckoutCone",
275                                       mode == MODE_CONE_PATTERNS ? "true" : NULL);
276
277         return 0;
278 }
279
280 static char const * const builtin_sparse_checkout_init_usage[] = {
281         N_("git sparse-checkout init [--cone]"),
282         NULL
283 };
284
285 static struct sparse_checkout_init_opts {
286         int cone_mode;
287 } init_opts;
288
289 static int sparse_checkout_init(int argc, const char **argv)
290 {
291         struct pattern_list pl;
292         char *sparse_filename;
293         int res;
294         struct object_id oid;
295         int mode;
296         struct strbuf pattern = STRBUF_INIT;
297
298         static struct option builtin_sparse_checkout_init_options[] = {
299                 OPT_BOOL(0, "cone", &init_opts.cone_mode,
300                          N_("initialize the sparse-checkout in cone mode")),
301                 OPT_END(),
302         };
303
304         repo_read_index(the_repository);
305         require_clean_work_tree(the_repository,
306                                 N_("initialize sparse-checkout"), NULL, 1, 0);
307
308         argc = parse_options(argc, argv, NULL,
309                              builtin_sparse_checkout_init_options,
310                              builtin_sparse_checkout_init_usage, 0);
311
312         if (init_opts.cone_mode) {
313                 mode = MODE_CONE_PATTERNS;
314                 core_sparse_checkout_cone = 1;
315         } else
316                 mode = MODE_ALL_PATTERNS;
317
318         if (set_config(mode))
319                 return 1;
320
321         memset(&pl, 0, sizeof(pl));
322
323         sparse_filename = get_sparse_checkout_filename();
324         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
325
326         /* If we already have a sparse-checkout file, use it. */
327         if (res >= 0) {
328                 free(sparse_filename);
329                 core_apply_sparse_checkout = 1;
330                 return update_working_directory(NULL);
331         }
332
333         if (get_oid("HEAD", &oid)) {
334                 FILE *fp;
335
336                 /* assume we are in a fresh repo, but update the sparse-checkout file */
337                 fp = xfopen(sparse_filename, "w");
338                 if (!fp)
339                         die(_("failed to open '%s'"), sparse_filename);
340
341                 free(sparse_filename);
342                 fprintf(fp, "/*\n!/*/\n");
343                 fclose(fp);
344                 return 0;
345         }
346
347         strbuf_addstr(&pattern, "/*");
348         add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
349         strbuf_addstr(&pattern, "!/*/");
350         add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
351
352         return write_patterns_and_update(&pl);
353 }
354
355 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
356 {
357         struct pattern_entry *e = xmalloc(sizeof(*e));
358         e->patternlen = path->len;
359         e->pattern = strbuf_detach(path, NULL);
360         hashmap_entry_init(&e->ent,
361                            ignore_case ?
362                            strihash(e->pattern) :
363                            strhash(e->pattern));
364
365         hashmap_add(&pl->recursive_hashmap, &e->ent);
366
367         while (e->patternlen) {
368                 char *slash = strrchr(e->pattern, '/');
369                 char *oldpattern = e->pattern;
370                 size_t newlen;
371
372                 if (slash == e->pattern)
373                         break;
374
375                 newlen = slash - e->pattern;
376                 e = xmalloc(sizeof(struct pattern_entry));
377                 e->patternlen = newlen;
378                 e->pattern = xstrndup(oldpattern, newlen);
379                 hashmap_entry_init(&e->ent,
380                                    ignore_case ?
381                                    strihash(e->pattern) :
382                                    strhash(e->pattern));
383
384                 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
385                         hashmap_add(&pl->parent_hashmap, &e->ent);
386         }
387 }
388
389 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
390 {
391         strbuf_trim(line);
392
393         strbuf_trim_trailing_dir_sep(line);
394
395         if (!line->len)
396                 return;
397
398         if (line->buf[0] != '/')
399                 strbuf_insert(line, 0, "/", 1);
400
401         insert_recursive_pattern(pl, line);
402 }
403
404 static char const * const builtin_sparse_checkout_set_usage[] = {
405         N_("git sparse-checkout set (--stdin | <patterns>)"),
406         NULL
407 };
408
409 static struct sparse_checkout_set_opts {
410         int use_stdin;
411 } set_opts;
412
413 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
414 {
415         int i;
416         struct pattern_list pl;
417         int result;
418         int changed_config = 0;
419
420         static struct option builtin_sparse_checkout_set_options[] = {
421                 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
422                          N_("read patterns from standard in")),
423                 OPT_END(),
424         };
425
426         repo_read_index(the_repository);
427         require_clean_work_tree(the_repository,
428                                 N_("set sparse-checkout patterns"), NULL, 1, 0);
429
430         memset(&pl, 0, sizeof(pl));
431
432         argc = parse_options(argc, argv, prefix,
433                              builtin_sparse_checkout_set_options,
434                              builtin_sparse_checkout_set_usage,
435                              PARSE_OPT_KEEP_UNKNOWN);
436
437         if (core_sparse_checkout_cone) {
438                 struct strbuf line = STRBUF_INIT;
439
440                 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
441                 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
442                 pl.use_cone_patterns = 1;
443
444                 if (set_opts.use_stdin) {
445                         while (!strbuf_getline(&line, stdin))
446                                 strbuf_to_cone_pattern(&line, &pl);
447                 } else {
448                         for (i = 0; i < argc; i++) {
449                                 strbuf_setlen(&line, 0);
450                                 strbuf_addstr(&line, argv[i]);
451                                 strbuf_to_cone_pattern(&line, &pl);
452                         }
453                 }
454         } else {
455                 if (set_opts.use_stdin) {
456                         struct strbuf line = STRBUF_INIT;
457
458                         while (!strbuf_getline(&line, stdin)) {
459                                 size_t len;
460                                 char *buf = strbuf_detach(&line, &len);
461                                 add_pattern(buf, empty_base, 0, &pl, 0);
462                         }
463                 } else {
464                         for (i = 0; i < argc; i++)
465                                 add_pattern(argv[i], empty_base, 0, &pl, 0);
466                 }
467         }
468
469         if (!core_apply_sparse_checkout) {
470                 set_config(MODE_ALL_PATTERNS);
471                 core_apply_sparse_checkout = 1;
472                 changed_config = 1;
473         }
474
475         result = write_patterns_and_update(&pl);
476
477         if (result && changed_config)
478                 set_config(MODE_NO_PATTERNS);
479
480         clear_pattern_list(&pl);
481         return result;
482 }
483
484 static int sparse_checkout_disable(int argc, const char **argv)
485 {
486         struct pattern_list pl;
487         struct strbuf match_all = STRBUF_INIT;
488
489         repo_read_index(the_repository);
490         require_clean_work_tree(the_repository,
491                                 N_("disable sparse-checkout"), NULL, 1, 0);
492
493         memset(&pl, 0, sizeof(pl));
494         hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
495         hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
496         pl.use_cone_patterns = 0;
497         core_apply_sparse_checkout = 1;
498
499         strbuf_addstr(&match_all, "/*");
500         add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
501
502         if (update_working_directory(&pl))
503                 die(_("error while refreshing working directory"));
504
505         clear_pattern_list(&pl);
506         return set_config(MODE_NO_PATTERNS);
507 }
508
509 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
510 {
511         static struct option builtin_sparse_checkout_options[] = {
512                 OPT_END(),
513         };
514
515         if (argc == 2 && !strcmp(argv[1], "-h"))
516                 usage_with_options(builtin_sparse_checkout_usage,
517                                    builtin_sparse_checkout_options);
518
519         argc = parse_options(argc, argv, prefix,
520                              builtin_sparse_checkout_options,
521                              builtin_sparse_checkout_usage,
522                              PARSE_OPT_STOP_AT_NON_OPTION);
523
524         git_config(git_default_config, NULL);
525
526         if (argc > 0) {
527                 if (!strcmp(argv[0], "list"))
528                         return sparse_checkout_list(argc, argv);
529                 if (!strcmp(argv[0], "init"))
530                         return sparse_checkout_init(argc, argv);
531                 if (!strcmp(argv[0], "set"))
532                         return sparse_checkout_set(argc, argv, prefix);
533                 if (!strcmp(argv[0], "disable"))
534                         return sparse_checkout_disable(argc, argv);
535         }
536
537         usage_with_options(builtin_sparse_checkout_usage,
538                            builtin_sparse_checkout_options);
539 }