Merge branch 'fc/zsh-completion'
[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|add|reapply|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 char const * const builtin_sparse_checkout_list_usage[] = {
50         N_("git sparse-checkout list"),
51         NULL
52 };
53
54 static int sparse_checkout_list(int argc, const char **argv)
55 {
56         static struct option builtin_sparse_checkout_list_options[] = {
57                 OPT_END(),
58         };
59         struct pattern_list pl;
60         char *sparse_filename;
61         int res;
62
63         argc = parse_options(argc, argv, NULL,
64                              builtin_sparse_checkout_list_options,
65                              builtin_sparse_checkout_list_usage, 0);
66
67         memset(&pl, 0, sizeof(pl));
68
69         pl.use_cone_patterns = core_sparse_checkout_cone;
70
71         sparse_filename = get_sparse_checkout_filename();
72         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
73         free(sparse_filename);
74
75         if (res < 0) {
76                 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
77                 return 0;
78         }
79
80         if (pl.use_cone_patterns) {
81                 int i;
82                 struct pattern_entry *pe;
83                 struct hashmap_iter iter;
84                 struct string_list sl = STRING_LIST_INIT_DUP;
85
86                 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
87                         /* pe->pattern starts with "/", skip it */
88                         string_list_insert(&sl, pe->pattern + 1);
89                 }
90
91                 string_list_sort(&sl);
92
93                 for (i = 0; i < sl.nr; i++) {
94                         quote_c_style(sl.items[i].string, NULL, stdout, 0);
95                         printf("\n");
96                 }
97
98                 return 0;
99         }
100
101         write_patterns_to_file(stdout, &pl);
102         clear_pattern_list(&pl);
103
104         return 0;
105 }
106
107 static int update_working_directory(struct pattern_list *pl)
108 {
109         enum update_sparsity_result result;
110         struct unpack_trees_options o;
111         struct lock_file lock_file = LOCK_INIT;
112         struct repository *r = the_repository;
113
114         /* If no branch has been checked out, there are no updates to make. */
115         if (is_index_unborn(r->index))
116                 return UPDATE_SPARSITY_SUCCESS;
117
118         memset(&o, 0, sizeof(o));
119         o.verbose_update = isatty(2);
120         o.update = 1;
121         o.head_idx = -1;
122         o.src_index = r->index;
123         o.dst_index = r->index;
124         o.skip_sparse_checkout = 0;
125         o.pl = pl;
126
127         setup_work_tree();
128
129         repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
130
131         setup_unpack_trees_porcelain(&o, "sparse-checkout");
132         result = update_sparsity(&o);
133         clear_unpack_trees_porcelain(&o);
134
135         if (result == UPDATE_SPARSITY_WARNINGS)
136                 /*
137                  * We don't do any special handling of warnings from untracked
138                  * files in the way or dirty entries that can't be removed.
139                  */
140                 result = UPDATE_SPARSITY_SUCCESS;
141         if (result == UPDATE_SPARSITY_SUCCESS)
142                 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
143         else
144                 rollback_lock_file(&lock_file);
145
146         return result;
147 }
148
149 static char *escaped_pattern(char *pattern)
150 {
151         char *p = pattern;
152         struct strbuf final = STRBUF_INIT;
153
154         while (*p) {
155                 if (is_glob_special(*p))
156                         strbuf_addch(&final, '\\');
157
158                 strbuf_addch(&final, *p);
159                 p++;
160         }
161
162         return strbuf_detach(&final, NULL);
163 }
164
165 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
166 {
167         int i;
168         struct pattern_entry *pe;
169         struct hashmap_iter iter;
170         struct string_list sl = STRING_LIST_INIT_DUP;
171         struct strbuf parent_pattern = STRBUF_INIT;
172
173         hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
174                 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
175                         continue;
176
177                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
178                                              pe->pattern,
179                                              &parent_pattern))
180                         string_list_insert(&sl, pe->pattern);
181         }
182
183         string_list_sort(&sl);
184         string_list_remove_duplicates(&sl, 0);
185
186         fprintf(fp, "/*\n!/*/\n");
187
188         for (i = 0; i < sl.nr; i++) {
189                 char *pattern = escaped_pattern(sl.items[i].string);
190
191                 if (strlen(pattern))
192                         fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
193                 free(pattern);
194         }
195
196         string_list_clear(&sl, 0);
197
198         hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
199                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
200                                              pe->pattern,
201                                              &parent_pattern))
202                         string_list_insert(&sl, pe->pattern);
203         }
204
205         strbuf_release(&parent_pattern);
206
207         string_list_sort(&sl);
208         string_list_remove_duplicates(&sl, 0);
209
210         for (i = 0; i < sl.nr; i++) {
211                 char *pattern = escaped_pattern(sl.items[i].string);
212                 fprintf(fp, "%s/\n", pattern);
213                 free(pattern);
214         }
215 }
216
217 static int write_patterns_and_update(struct pattern_list *pl)
218 {
219         char *sparse_filename;
220         FILE *fp;
221         int fd;
222         struct lock_file lk = LOCK_INIT;
223         int result;
224
225         sparse_filename = get_sparse_checkout_filename();
226
227         if (safe_create_leading_directories(sparse_filename))
228                 die(_("failed to create directory for sparse-checkout file"));
229
230         fd = hold_lock_file_for_update(&lk, sparse_filename,
231                                       LOCK_DIE_ON_ERROR);
232
233         result = update_working_directory(pl);
234         if (result) {
235                 rollback_lock_file(&lk);
236                 free(sparse_filename);
237                 clear_pattern_list(pl);
238                 update_working_directory(NULL);
239                 return result;
240         }
241
242         fp = xfdopen(fd, "w");
243
244         if (core_sparse_checkout_cone)
245                 write_cone_to_file(fp, pl);
246         else
247                 write_patterns_to_file(fp, pl);
248
249         fflush(fp);
250         commit_lock_file(&lk);
251
252         free(sparse_filename);
253         clear_pattern_list(pl);
254
255         return 0;
256 }
257
258 enum sparse_checkout_mode {
259         MODE_NO_PATTERNS = 0,
260         MODE_ALL_PATTERNS = 1,
261         MODE_CONE_PATTERNS = 2,
262 };
263
264 static int set_config(enum sparse_checkout_mode mode)
265 {
266         const char *config_path;
267
268         if (upgrade_repository_format(1) < 0)
269                 die(_("unable to upgrade repository format to enable worktreeConfig"));
270         if (git_config_set_gently("extensions.worktreeConfig", "true")) {
271                 error(_("failed to set extensions.worktreeConfig setting"));
272                 return 1;
273         }
274
275         config_path = git_path("config.worktree");
276         git_config_set_in_file_gently(config_path,
277                                       "core.sparseCheckout",
278                                       mode ? "true" : NULL);
279
280         git_config_set_in_file_gently(config_path,
281                                       "core.sparseCheckoutCone",
282                                       mode == MODE_CONE_PATTERNS ? "true" : NULL);
283
284         return 0;
285 }
286
287 static char const * const builtin_sparse_checkout_init_usage[] = {
288         N_("git sparse-checkout init [--cone]"),
289         NULL
290 };
291
292 static struct sparse_checkout_init_opts {
293         int cone_mode;
294 } init_opts;
295
296 static int sparse_checkout_init(int argc, const char **argv)
297 {
298         struct pattern_list pl;
299         char *sparse_filename;
300         int res;
301         struct object_id oid;
302         int mode;
303         struct strbuf pattern = STRBUF_INIT;
304
305         static struct option builtin_sparse_checkout_init_options[] = {
306                 OPT_BOOL(0, "cone", &init_opts.cone_mode,
307                          N_("initialize the sparse-checkout in cone mode")),
308                 OPT_END(),
309         };
310
311         repo_read_index(the_repository);
312
313         argc = parse_options(argc, argv, NULL,
314                              builtin_sparse_checkout_init_options,
315                              builtin_sparse_checkout_init_usage, 0);
316
317         if (init_opts.cone_mode) {
318                 mode = MODE_CONE_PATTERNS;
319                 core_sparse_checkout_cone = 1;
320         } else
321                 mode = MODE_ALL_PATTERNS;
322
323         if (set_config(mode))
324                 return 1;
325
326         memset(&pl, 0, sizeof(pl));
327
328         sparse_filename = get_sparse_checkout_filename();
329         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
330
331         /* If we already have a sparse-checkout file, use it. */
332         if (res >= 0) {
333                 free(sparse_filename);
334                 core_apply_sparse_checkout = 1;
335                 return update_working_directory(NULL);
336         }
337
338         if (get_oid("HEAD", &oid)) {
339                 FILE *fp;
340
341                 /* assume we are in a fresh repo, but update the sparse-checkout file */
342                 fp = xfopen(sparse_filename, "w");
343                 if (!fp)
344                         die(_("failed to open '%s'"), sparse_filename);
345
346                 free(sparse_filename);
347                 fprintf(fp, "/*\n!/*/\n");
348                 fclose(fp);
349                 return 0;
350         }
351
352         strbuf_addstr(&pattern, "/*");
353         add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
354         strbuf_addstr(&pattern, "!/*/");
355         add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
356
357         return write_patterns_and_update(&pl);
358 }
359
360 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
361 {
362         struct pattern_entry *e = xmalloc(sizeof(*e));
363         e->patternlen = path->len;
364         e->pattern = strbuf_detach(path, NULL);
365         hashmap_entry_init(&e->ent,
366                            ignore_case ?
367                            strihash(e->pattern) :
368                            strhash(e->pattern));
369
370         hashmap_add(&pl->recursive_hashmap, &e->ent);
371
372         while (e->patternlen) {
373                 char *slash = strrchr(e->pattern, '/');
374                 char *oldpattern = e->pattern;
375                 size_t newlen;
376
377                 if (slash == e->pattern)
378                         break;
379
380                 newlen = slash - e->pattern;
381                 e = xmalloc(sizeof(struct pattern_entry));
382                 e->patternlen = newlen;
383                 e->pattern = xstrndup(oldpattern, newlen);
384                 hashmap_entry_init(&e->ent,
385                                    ignore_case ?
386                                    strihash(e->pattern) :
387                                    strhash(e->pattern));
388
389                 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
390                         hashmap_add(&pl->parent_hashmap, &e->ent);
391         }
392 }
393
394 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
395 {
396         strbuf_trim(line);
397
398         strbuf_trim_trailing_dir_sep(line);
399
400         if (strbuf_normalize_path(line))
401                 die(_("could not normalize path %s"), line->buf);
402
403         if (!line->len)
404                 return;
405
406         if (line->buf[0] != '/')
407                 strbuf_insertstr(line, 0, "/");
408
409         insert_recursive_pattern(pl, line);
410 }
411
412 static char const * const builtin_sparse_checkout_set_usage[] = {
413         N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
414         NULL
415 };
416
417 static struct sparse_checkout_set_opts {
418         int use_stdin;
419 } set_opts;
420
421 static void add_patterns_from_input(struct pattern_list *pl,
422                                     int argc, const char **argv)
423 {
424         int i;
425         if (core_sparse_checkout_cone) {
426                 struct strbuf line = STRBUF_INIT;
427
428                 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
429                 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
430                 pl->use_cone_patterns = 1;
431
432                 if (set_opts.use_stdin) {
433                         struct strbuf unquoted = STRBUF_INIT;
434                         while (!strbuf_getline(&line, stdin)) {
435                                 if (line.buf[0] == '"') {
436                                         strbuf_reset(&unquoted);
437                                         if (unquote_c_style(&unquoted, line.buf, NULL))
438                                                 die(_("unable to unquote C-style string '%s'"),
439                                                 line.buf);
440
441                                         strbuf_swap(&unquoted, &line);
442                                 }
443
444                                 strbuf_to_cone_pattern(&line, pl);
445                         }
446
447                         strbuf_release(&unquoted);
448                 } else {
449                         for (i = 0; i < argc; i++) {
450                                 strbuf_setlen(&line, 0);
451                                 strbuf_addstr(&line, argv[i]);
452                                 strbuf_to_cone_pattern(&line, pl);
453                         }
454                 }
455         } else {
456                 if (set_opts.use_stdin) {
457                         struct strbuf line = STRBUF_INIT;
458
459                         while (!strbuf_getline(&line, stdin)) {
460                                 size_t len;
461                                 char *buf = strbuf_detach(&line, &len);
462                                 add_pattern(buf, empty_base, 0, pl, 0);
463                         }
464                 } else {
465                         for (i = 0; i < argc; i++)
466                                 add_pattern(argv[i], empty_base, 0, pl, 0);
467                 }
468         }
469 }
470
471 enum modify_type {
472         REPLACE,
473         ADD,
474 };
475
476 static void add_patterns_cone_mode(int argc, const char **argv,
477                                    struct pattern_list *pl)
478 {
479         struct strbuf buffer = STRBUF_INIT;
480         struct pattern_entry *pe;
481         struct hashmap_iter iter;
482         struct pattern_list existing;
483         char *sparse_filename = get_sparse_checkout_filename();
484
485         add_patterns_from_input(pl, argc, argv);
486
487         memset(&existing, 0, sizeof(existing));
488         existing.use_cone_patterns = core_sparse_checkout_cone;
489
490         if (add_patterns_from_file_to_list(sparse_filename, "", 0,
491                                            &existing, NULL))
492                 die(_("unable to load existing sparse-checkout patterns"));
493         free(sparse_filename);
494
495         hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
496                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
497                                         pe->pattern, &buffer) ||
498                     !hashmap_contains_parent(&pl->parent_hashmap,
499                                         pe->pattern, &buffer)) {
500                         strbuf_reset(&buffer);
501                         strbuf_addstr(&buffer, pe->pattern);
502                         insert_recursive_pattern(pl, &buffer);
503                 }
504         }
505
506         clear_pattern_list(&existing);
507         strbuf_release(&buffer);
508 }
509
510 static void add_patterns_literal(int argc, const char **argv,
511                                  struct pattern_list *pl)
512 {
513         char *sparse_filename = get_sparse_checkout_filename();
514         if (add_patterns_from_file_to_list(sparse_filename, "", 0,
515                                            pl, NULL))
516                 die(_("unable to load existing sparse-checkout patterns"));
517         free(sparse_filename);
518         add_patterns_from_input(pl, argc, argv);
519 }
520
521 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
522 {
523         int result;
524         int changed_config = 0;
525         struct pattern_list pl;
526         memset(&pl, 0, sizeof(pl));
527
528         switch (m) {
529         case ADD:
530                 if (core_sparse_checkout_cone)
531                         add_patterns_cone_mode(argc, argv, &pl);
532                 else
533                         add_patterns_literal(argc, argv, &pl);
534                 break;
535
536         case REPLACE:
537                 add_patterns_from_input(&pl, argc, argv);
538                 break;
539         }
540
541         if (!core_apply_sparse_checkout) {
542                 set_config(MODE_ALL_PATTERNS);
543                 core_apply_sparse_checkout = 1;
544                 changed_config = 1;
545         }
546
547         result = write_patterns_and_update(&pl);
548
549         if (result && changed_config)
550                 set_config(MODE_NO_PATTERNS);
551
552         clear_pattern_list(&pl);
553         return result;
554 }
555
556 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
557                                enum modify_type m)
558 {
559         static struct option builtin_sparse_checkout_set_options[] = {
560                 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
561                          N_("read patterns from standard in")),
562                 OPT_END(),
563         };
564
565         repo_read_index(the_repository);
566
567         argc = parse_options(argc, argv, prefix,
568                              builtin_sparse_checkout_set_options,
569                              builtin_sparse_checkout_set_usage,
570                              PARSE_OPT_KEEP_UNKNOWN);
571
572         return modify_pattern_list(argc, argv, m);
573 }
574
575 static char const * const builtin_sparse_checkout_reapply_usage[] = {
576         N_("git sparse-checkout reapply"),
577         NULL
578 };
579
580 static int sparse_checkout_reapply(int argc, const char **argv)
581 {
582         static struct option builtin_sparse_checkout_reapply_options[] = {
583                 OPT_END(),
584         };
585
586         argc = parse_options(argc, argv, NULL,
587                              builtin_sparse_checkout_reapply_options,
588                              builtin_sparse_checkout_reapply_usage, 0);
589
590         repo_read_index(the_repository);
591         return update_working_directory(NULL);
592 }
593
594 static char const * const builtin_sparse_checkout_disable_usage[] = {
595         N_("git sparse-checkout disable"),
596         NULL
597 };
598
599 static int sparse_checkout_disable(int argc, const char **argv)
600 {
601         static struct option builtin_sparse_checkout_disable_options[] = {
602                 OPT_END(),
603         };
604         struct pattern_list pl;
605         struct strbuf match_all = STRBUF_INIT;
606
607         argc = parse_options(argc, argv, NULL,
608                              builtin_sparse_checkout_disable_options,
609                              builtin_sparse_checkout_disable_usage, 0);
610
611         repo_read_index(the_repository);
612
613         memset(&pl, 0, sizeof(pl));
614         hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
615         hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
616         pl.use_cone_patterns = 0;
617         core_apply_sparse_checkout = 1;
618
619         strbuf_addstr(&match_all, "/*");
620         add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
621
622         if (update_working_directory(&pl))
623                 die(_("error while refreshing working directory"));
624
625         clear_pattern_list(&pl);
626         return set_config(MODE_NO_PATTERNS);
627 }
628
629 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
630 {
631         static struct option builtin_sparse_checkout_options[] = {
632                 OPT_END(),
633         };
634
635         if (argc == 2 && !strcmp(argv[1], "-h"))
636                 usage_with_options(builtin_sparse_checkout_usage,
637                                    builtin_sparse_checkout_options);
638
639         argc = parse_options(argc, argv, prefix,
640                              builtin_sparse_checkout_options,
641                              builtin_sparse_checkout_usage,
642                              PARSE_OPT_STOP_AT_NON_OPTION);
643
644         git_config(git_default_config, NULL);
645
646         if (argc > 0) {
647                 if (!strcmp(argv[0], "list"))
648                         return sparse_checkout_list(argc, argv);
649                 if (!strcmp(argv[0], "init"))
650                         return sparse_checkout_init(argc, argv);
651                 if (!strcmp(argv[0], "set"))
652                         return sparse_checkout_set(argc, argv, prefix, REPLACE);
653                 if (!strcmp(argv[0], "add"))
654                         return sparse_checkout_set(argc, argv, prefix, ADD);
655                 if (!strcmp(argv[0], "reapply"))
656                         return sparse_checkout_reapply(argc, argv);
657                 if (!strcmp(argv[0], "disable"))
658                         return sparse_checkout_disable(argc, argv);
659         }
660
661         usage_with_options(builtin_sparse_checkout_usage,
662                            builtin_sparse_checkout_options);
663 }