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