diff-merges: introduce log.diffMerges config variable
[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 void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
26 {
27         int i;
28
29         for (i = 0; i < pl->nr; i++) {
30                 struct path_pattern *p = pl->patterns[i];
31
32                 if (p->flags & PATTERN_FLAG_NEGATIVE)
33                         fprintf(fp, "!");
34
35                 fprintf(fp, "%s", p->pattern);
36
37                 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
38                         fprintf(fp, "/");
39
40                 fprintf(fp, "\n");
41         }
42 }
43
44 static char const * const builtin_sparse_checkout_list_usage[] = {
45         N_("git sparse-checkout list"),
46         NULL
47 };
48
49 static int sparse_checkout_list(int argc, const char **argv)
50 {
51         static struct option builtin_sparse_checkout_list_options[] = {
52                 OPT_END(),
53         };
54         struct pattern_list pl;
55         char *sparse_filename;
56         int res;
57
58         argc = parse_options(argc, argv, NULL,
59                              builtin_sparse_checkout_list_options,
60                              builtin_sparse_checkout_list_usage, 0);
61
62         memset(&pl, 0, sizeof(pl));
63
64         pl.use_cone_patterns = core_sparse_checkout_cone;
65
66         sparse_filename = get_sparse_checkout_filename();
67         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
68         free(sparse_filename);
69
70         if (res < 0) {
71                 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
72                 return 0;
73         }
74
75         if (pl.use_cone_patterns) {
76                 int i;
77                 struct pattern_entry *pe;
78                 struct hashmap_iter iter;
79                 struct string_list sl = STRING_LIST_INIT_DUP;
80
81                 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
82                         /* pe->pattern starts with "/", skip it */
83                         string_list_insert(&sl, pe->pattern + 1);
84                 }
85
86                 string_list_sort(&sl);
87
88                 for (i = 0; i < sl.nr; i++) {
89                         quote_c_style(sl.items[i].string, NULL, stdout, 0);
90                         printf("\n");
91                 }
92
93                 return 0;
94         }
95
96         write_patterns_to_file(stdout, &pl);
97         clear_pattern_list(&pl);
98
99         return 0;
100 }
101
102 static int update_working_directory(struct pattern_list *pl)
103 {
104         enum update_sparsity_result result;
105         struct unpack_trees_options o;
106         struct lock_file lock_file = LOCK_INIT;
107         struct repository *r = the_repository;
108
109         /* If no branch has been checked out, there are no updates to make. */
110         if (is_index_unborn(r->index))
111                 return UPDATE_SPARSITY_SUCCESS;
112
113         memset(&o, 0, sizeof(o));
114         o.verbose_update = isatty(2);
115         o.update = 1;
116         o.head_idx = -1;
117         o.src_index = r->index;
118         o.dst_index = r->index;
119         o.skip_sparse_checkout = 0;
120         o.pl = pl;
121
122         setup_work_tree();
123
124         repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
125
126         setup_unpack_trees_porcelain(&o, "sparse-checkout");
127         result = update_sparsity(&o);
128         clear_unpack_trees_porcelain(&o);
129
130         if (result == UPDATE_SPARSITY_WARNINGS)
131                 /*
132                  * We don't do any special handling of warnings from untracked
133                  * files in the way or dirty entries that can't be removed.
134                  */
135                 result = UPDATE_SPARSITY_SUCCESS;
136         if (result == UPDATE_SPARSITY_SUCCESS)
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 (is_glob_special(*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 (upgrade_repository_format(1) < 0)
264                 die(_("unable to upgrade repository format to enable worktreeConfig"));
265         if (git_config_set_gently("extensions.worktreeConfig", "true")) {
266                 error(_("failed to set extensions.worktreeConfig setting"));
267                 return 1;
268         }
269
270         config_path = git_path("config.worktree");
271         git_config_set_in_file_gently(config_path,
272                                       "core.sparseCheckout",
273                                       mode ? "true" : NULL);
274
275         git_config_set_in_file_gently(config_path,
276                                       "core.sparseCheckoutCone",
277                                       mode == MODE_CONE_PATTERNS ? "true" : NULL);
278
279         return 0;
280 }
281
282 static char const * const builtin_sparse_checkout_init_usage[] = {
283         N_("git sparse-checkout init [--cone]"),
284         NULL
285 };
286
287 static struct sparse_checkout_init_opts {
288         int cone_mode;
289 } init_opts;
290
291 static int sparse_checkout_init(int argc, const char **argv)
292 {
293         struct pattern_list pl;
294         char *sparse_filename;
295         int res;
296         struct object_id oid;
297         int mode;
298         struct strbuf pattern = STRBUF_INIT;
299
300         static struct option builtin_sparse_checkout_init_options[] = {
301                 OPT_BOOL(0, "cone", &init_opts.cone_mode,
302                          N_("initialize the sparse-checkout in cone mode")),
303                 OPT_END(),
304         };
305
306         repo_read_index(the_repository);
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 (strbuf_normalize_path(line))
396                 die(_("could not normalize path %s"), line->buf);
397
398         if (!line->len)
399                 return;
400
401         if (line->buf[0] != '/')
402                 strbuf_insertstr(line, 0, "/");
403
404         insert_recursive_pattern(pl, line);
405 }
406
407 static char const * const builtin_sparse_checkout_set_usage[] = {
408         N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
409         NULL
410 };
411
412 static struct sparse_checkout_set_opts {
413         int use_stdin;
414 } set_opts;
415
416 static void add_patterns_from_input(struct pattern_list *pl,
417                                     int argc, const char **argv)
418 {
419         int i;
420         if (core_sparse_checkout_cone) {
421                 struct strbuf line = STRBUF_INIT;
422
423                 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
424                 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
425                 pl->use_cone_patterns = 1;
426
427                 if (set_opts.use_stdin) {
428                         struct strbuf unquoted = STRBUF_INIT;
429                         while (!strbuf_getline(&line, stdin)) {
430                                 if (line.buf[0] == '"') {
431                                         strbuf_reset(&unquoted);
432                                         if (unquote_c_style(&unquoted, line.buf, NULL))
433                                                 die(_("unable to unquote C-style string '%s'"),
434                                                 line.buf);
435
436                                         strbuf_swap(&unquoted, &line);
437                                 }
438
439                                 strbuf_to_cone_pattern(&line, pl);
440                         }
441
442                         strbuf_release(&unquoted);
443                 } else {
444                         for (i = 0; i < argc; i++) {
445                                 strbuf_setlen(&line, 0);
446                                 strbuf_addstr(&line, argv[i]);
447                                 strbuf_to_cone_pattern(&line, pl);
448                         }
449                 }
450         } else {
451                 if (set_opts.use_stdin) {
452                         struct strbuf line = STRBUF_INIT;
453
454                         while (!strbuf_getline(&line, stdin)) {
455                                 size_t len;
456                                 char *buf = strbuf_detach(&line, &len);
457                                 add_pattern(buf, empty_base, 0, pl, 0);
458                         }
459                 } else {
460                         for (i = 0; i < argc; i++)
461                                 add_pattern(argv[i], empty_base, 0, pl, 0);
462                 }
463         }
464 }
465
466 enum modify_type {
467         REPLACE,
468         ADD,
469 };
470
471 static void add_patterns_cone_mode(int argc, const char **argv,
472                                    struct pattern_list *pl)
473 {
474         struct strbuf buffer = STRBUF_INIT;
475         struct pattern_entry *pe;
476         struct hashmap_iter iter;
477         struct pattern_list existing;
478         char *sparse_filename = get_sparse_checkout_filename();
479
480         add_patterns_from_input(pl, argc, argv);
481
482         memset(&existing, 0, sizeof(existing));
483         existing.use_cone_patterns = core_sparse_checkout_cone;
484
485         if (add_patterns_from_file_to_list(sparse_filename, "", 0,
486                                            &existing, NULL))
487                 die(_("unable to load existing sparse-checkout patterns"));
488         free(sparse_filename);
489
490         hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
491                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
492                                         pe->pattern, &buffer) ||
493                     !hashmap_contains_parent(&pl->parent_hashmap,
494                                         pe->pattern, &buffer)) {
495                         strbuf_reset(&buffer);
496                         strbuf_addstr(&buffer, pe->pattern);
497                         insert_recursive_pattern(pl, &buffer);
498                 }
499         }
500
501         clear_pattern_list(&existing);
502         strbuf_release(&buffer);
503 }
504
505 static void add_patterns_literal(int argc, const char **argv,
506                                  struct pattern_list *pl)
507 {
508         char *sparse_filename = get_sparse_checkout_filename();
509         if (add_patterns_from_file_to_list(sparse_filename, "", 0,
510                                            pl, NULL))
511                 die(_("unable to load existing sparse-checkout patterns"));
512         free(sparse_filename);
513         add_patterns_from_input(pl, argc, argv);
514 }
515
516 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
517 {
518         int result;
519         int changed_config = 0;
520         struct pattern_list pl;
521         memset(&pl, 0, sizeof(pl));
522
523         switch (m) {
524         case ADD:
525                 if (core_sparse_checkout_cone)
526                         add_patterns_cone_mode(argc, argv, &pl);
527                 else
528                         add_patterns_literal(argc, argv, &pl);
529                 break;
530
531         case REPLACE:
532                 add_patterns_from_input(&pl, argc, argv);
533                 break;
534         }
535
536         if (!core_apply_sparse_checkout) {
537                 set_config(MODE_ALL_PATTERNS);
538                 core_apply_sparse_checkout = 1;
539                 changed_config = 1;
540         }
541
542         result = write_patterns_and_update(&pl);
543
544         if (result && changed_config)
545                 set_config(MODE_NO_PATTERNS);
546
547         clear_pattern_list(&pl);
548         return result;
549 }
550
551 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
552                                enum modify_type m)
553 {
554         static struct option builtin_sparse_checkout_set_options[] = {
555                 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
556                          N_("read patterns from standard in")),
557                 OPT_END(),
558         };
559
560         repo_read_index(the_repository);
561
562         argc = parse_options(argc, argv, prefix,
563                              builtin_sparse_checkout_set_options,
564                              builtin_sparse_checkout_set_usage,
565                              PARSE_OPT_KEEP_UNKNOWN);
566
567         return modify_pattern_list(argc, argv, m);
568 }
569
570 static char const * const builtin_sparse_checkout_reapply_usage[] = {
571         N_("git sparse-checkout reapply"),
572         NULL
573 };
574
575 static int sparse_checkout_reapply(int argc, const char **argv)
576 {
577         static struct option builtin_sparse_checkout_reapply_options[] = {
578                 OPT_END(),
579         };
580
581         argc = parse_options(argc, argv, NULL,
582                              builtin_sparse_checkout_reapply_options,
583                              builtin_sparse_checkout_reapply_usage, 0);
584
585         repo_read_index(the_repository);
586         return update_working_directory(NULL);
587 }
588
589 static char const * const builtin_sparse_checkout_disable_usage[] = {
590         N_("git sparse-checkout disable"),
591         NULL
592 };
593
594 static int sparse_checkout_disable(int argc, const char **argv)
595 {
596         static struct option builtin_sparse_checkout_disable_options[] = {
597                 OPT_END(),
598         };
599         struct pattern_list pl;
600         struct strbuf match_all = STRBUF_INIT;
601
602         argc = parse_options(argc, argv, NULL,
603                              builtin_sparse_checkout_disable_options,
604                              builtin_sparse_checkout_disable_usage, 0);
605
606         repo_read_index(the_repository);
607
608         memset(&pl, 0, sizeof(pl));
609         hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
610         hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
611         pl.use_cone_patterns = 0;
612         core_apply_sparse_checkout = 1;
613
614         strbuf_addstr(&match_all, "/*");
615         add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
616
617         if (update_working_directory(&pl))
618                 die(_("error while refreshing working directory"));
619
620         clear_pattern_list(&pl);
621         return set_config(MODE_NO_PATTERNS);
622 }
623
624 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
625 {
626         static struct option builtin_sparse_checkout_options[] = {
627                 OPT_END(),
628         };
629
630         if (argc == 2 && !strcmp(argv[1], "-h"))
631                 usage_with_options(builtin_sparse_checkout_usage,
632                                    builtin_sparse_checkout_options);
633
634         argc = parse_options(argc, argv, prefix,
635                              builtin_sparse_checkout_options,
636                              builtin_sparse_checkout_usage,
637                              PARSE_OPT_STOP_AT_NON_OPTION);
638
639         git_config(git_default_config, NULL);
640
641         if (argc > 0) {
642                 if (!strcmp(argv[0], "list"))
643                         return sparse_checkout_list(argc, argv);
644                 if (!strcmp(argv[0], "init"))
645                         return sparse_checkout_init(argc, argv);
646                 if (!strcmp(argv[0], "set"))
647                         return sparse_checkout_set(argc, argv, prefix, REPLACE);
648                 if (!strcmp(argv[0], "add"))
649                         return sparse_checkout_set(argc, argv, prefix, ADD);
650                 if (!strcmp(argv[0], "reapply"))
651                         return sparse_checkout_reapply(argc, argv);
652                 if (!strcmp(argv[0], "disable"))
653                         return sparse_checkout_disable(argc, argv);
654         }
655
656         usage_with_options(builtin_sparse_checkout_usage,
657                            builtin_sparse_checkout_options);
658 }