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