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