Merge branch 'tb/commit-graph-no-check-oids' into master
[git/raj.git] / builtin / sparse-checkout.c
blob4003f4d13a7ffeafbf9827de9eaab491c600ed96
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"
18 static const char *empty_base = "";
20 static char const * const builtin_sparse_checkout_usage[] = {
21 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
22 NULL
25 static char *get_sparse_checkout_filename(void)
27 return git_pathdup("info/sparse-checkout");
30 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
32 int i;
34 for (i = 0; i < pl->nr; i++) {
35 struct path_pattern *p = pl->patterns[i];
37 if (p->flags & PATTERN_FLAG_NEGATIVE)
38 fprintf(fp, "!");
40 fprintf(fp, "%s", p->pattern);
42 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
43 fprintf(fp, "/");
45 fprintf(fp, "\n");
49 static int sparse_checkout_list(int argc, const char **argv)
51 struct pattern_list pl;
52 char *sparse_filename;
53 int res;
55 memset(&pl, 0, sizeof(pl));
57 pl.use_cone_patterns = core_sparse_checkout_cone;
59 sparse_filename = get_sparse_checkout_filename();
60 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
61 free(sparse_filename);
63 if (res < 0) {
64 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
65 return 0;
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;
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);
79 string_list_sort(&sl);
81 for (i = 0; i < sl.nr; i++) {
82 quote_c_style(sl.items[i].string, NULL, stdout, 0);
83 printf("\n");
86 return 0;
89 write_patterns_to_file(stdout, &pl);
90 clear_pattern_list(&pl);
92 return 0;
95 static int update_working_directory(struct pattern_list *pl)
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;
102 /* If no branch has been checked out, there are no updates to make. */
103 if (is_index_unborn(r->index))
104 return UPDATE_SPARSITY_SUCCESS;
106 memset(&o, 0, sizeof(o));
107 o.verbose_update = isatty(2);
108 o.update = 1;
109 o.head_idx = -1;
110 o.src_index = r->index;
111 o.dst_index = r->index;
112 o.skip_sparse_checkout = 0;
113 o.pl = pl;
115 setup_work_tree();
117 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
119 setup_unpack_trees_porcelain(&o, "sparse-checkout");
120 result = update_sparsity(&o);
121 clear_unpack_trees_porcelain(&o);
123 if (result == UPDATE_SPARSITY_WARNINGS)
125 * We don't do any special handling of warnings from untracked
126 * files in the way or dirty entries that can't be removed.
128 result = UPDATE_SPARSITY_SUCCESS;
129 if (result == UPDATE_SPARSITY_SUCCESS)
130 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
131 else
132 rollback_lock_file(&lock_file);
134 return result;
137 static char *escaped_pattern(char *pattern)
139 char *p = pattern;
140 struct strbuf final = STRBUF_INIT;
142 while (*p) {
143 if (is_glob_special(*p))
144 strbuf_addch(&final, '\\');
146 strbuf_addch(&final, *p);
147 p++;
150 return strbuf_detach(&final, NULL);
153 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
155 int i;
156 struct pattern_entry *pe;
157 struct hashmap_iter iter;
158 struct string_list sl = STRING_LIST_INIT_DUP;
159 struct strbuf parent_pattern = STRBUF_INIT;
161 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
162 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
163 continue;
165 if (!hashmap_contains_parent(&pl->recursive_hashmap,
166 pe->pattern,
167 &parent_pattern))
168 string_list_insert(&sl, pe->pattern);
171 string_list_sort(&sl);
172 string_list_remove_duplicates(&sl, 0);
174 fprintf(fp, "/*\n!/*/\n");
176 for (i = 0; i < sl.nr; i++) {
177 char *pattern = escaped_pattern(sl.items[i].string);
179 if (strlen(pattern))
180 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
181 free(pattern);
184 string_list_clear(&sl, 0);
186 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
187 if (!hashmap_contains_parent(&pl->recursive_hashmap,
188 pe->pattern,
189 &parent_pattern))
190 string_list_insert(&sl, pe->pattern);
193 strbuf_release(&parent_pattern);
195 string_list_sort(&sl);
196 string_list_remove_duplicates(&sl, 0);
198 for (i = 0; i < sl.nr; i++) {
199 char *pattern = escaped_pattern(sl.items[i].string);
200 fprintf(fp, "%s/\n", pattern);
201 free(pattern);
205 static int write_patterns_and_update(struct pattern_list *pl)
207 char *sparse_filename;
208 FILE *fp;
209 int fd;
210 struct lock_file lk = LOCK_INIT;
211 int result;
213 sparse_filename = get_sparse_checkout_filename();
215 if (safe_create_leading_directories(sparse_filename))
216 die(_("failed to create directory for sparse-checkout file"));
218 fd = hold_lock_file_for_update(&lk, sparse_filename,
219 LOCK_DIE_ON_ERROR);
221 result = update_working_directory(pl);
222 if (result) {
223 rollback_lock_file(&lk);
224 free(sparse_filename);
225 clear_pattern_list(pl);
226 update_working_directory(NULL);
227 return result;
230 fp = xfdopen(fd, "w");
232 if (core_sparse_checkout_cone)
233 write_cone_to_file(fp, pl);
234 else
235 write_patterns_to_file(fp, pl);
237 fflush(fp);
238 commit_lock_file(&lk);
240 free(sparse_filename);
241 clear_pattern_list(pl);
243 return 0;
246 enum sparse_checkout_mode {
247 MODE_NO_PATTERNS = 0,
248 MODE_ALL_PATTERNS = 1,
249 MODE_CONE_PATTERNS = 2,
252 static int set_config(enum sparse_checkout_mode mode)
254 const char *config_path;
256 if (upgrade_repository_format(1) < 0)
257 die(_("unable to upgrade repository format to enable worktreeConfig"));
258 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
259 error(_("failed to set extensions.worktreeConfig setting"));
260 return 1;
263 config_path = git_path("config.worktree");
264 git_config_set_in_file_gently(config_path,
265 "core.sparseCheckout",
266 mode ? "true" : NULL);
268 git_config_set_in_file_gently(config_path,
269 "core.sparseCheckoutCone",
270 mode == MODE_CONE_PATTERNS ? "true" : NULL);
272 return 0;
275 static char const * const builtin_sparse_checkout_init_usage[] = {
276 N_("git sparse-checkout init [--cone]"),
277 NULL
280 static struct sparse_checkout_init_opts {
281 int cone_mode;
282 } init_opts;
284 static int sparse_checkout_init(int argc, const char **argv)
286 struct pattern_list pl;
287 char *sparse_filename;
288 int res;
289 struct object_id oid;
290 int mode;
291 struct strbuf pattern = STRBUF_INIT;
293 static struct option builtin_sparse_checkout_init_options[] = {
294 OPT_BOOL(0, "cone", &init_opts.cone_mode,
295 N_("initialize the sparse-checkout in cone mode")),
296 OPT_END(),
299 repo_read_index(the_repository);
301 argc = parse_options(argc, argv, NULL,
302 builtin_sparse_checkout_init_options,
303 builtin_sparse_checkout_init_usage, 0);
305 if (init_opts.cone_mode) {
306 mode = MODE_CONE_PATTERNS;
307 core_sparse_checkout_cone = 1;
308 } else
309 mode = MODE_ALL_PATTERNS;
311 if (set_config(mode))
312 return 1;
314 memset(&pl, 0, sizeof(pl));
316 sparse_filename = get_sparse_checkout_filename();
317 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
319 /* If we already have a sparse-checkout file, use it. */
320 if (res >= 0) {
321 free(sparse_filename);
322 core_apply_sparse_checkout = 1;
323 return update_working_directory(NULL);
326 if (get_oid("HEAD", &oid)) {
327 FILE *fp;
329 /* assume we are in a fresh repo, but update the sparse-checkout file */
330 fp = xfopen(sparse_filename, "w");
331 if (!fp)
332 die(_("failed to open '%s'"), sparse_filename);
334 free(sparse_filename);
335 fprintf(fp, "/*\n!/*/\n");
336 fclose(fp);
337 return 0;
340 strbuf_addstr(&pattern, "/*");
341 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
342 strbuf_addstr(&pattern, "!/*/");
343 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
345 return write_patterns_and_update(&pl);
348 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
350 struct pattern_entry *e = xmalloc(sizeof(*e));
351 e->patternlen = path->len;
352 e->pattern = strbuf_detach(path, NULL);
353 hashmap_entry_init(&e->ent,
354 ignore_case ?
355 strihash(e->pattern) :
356 strhash(e->pattern));
358 hashmap_add(&pl->recursive_hashmap, &e->ent);
360 while (e->patternlen) {
361 char *slash = strrchr(e->pattern, '/');
362 char *oldpattern = e->pattern;
363 size_t newlen;
365 if (slash == e->pattern)
366 break;
368 newlen = slash - e->pattern;
369 e = xmalloc(sizeof(struct pattern_entry));
370 e->patternlen = newlen;
371 e->pattern = xstrndup(oldpattern, newlen);
372 hashmap_entry_init(&e->ent,
373 ignore_case ?
374 strihash(e->pattern) :
375 strhash(e->pattern));
377 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
378 hashmap_add(&pl->parent_hashmap, &e->ent);
382 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
384 strbuf_trim(line);
386 strbuf_trim_trailing_dir_sep(line);
388 if (strbuf_normalize_path(line))
389 die(_("could not normalize path %s"), line->buf);
391 if (!line->len)
392 return;
394 if (line->buf[0] != '/')
395 strbuf_insertstr(line, 0, "/");
397 insert_recursive_pattern(pl, line);
400 static char const * const builtin_sparse_checkout_set_usage[] = {
401 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
402 NULL
405 static struct sparse_checkout_set_opts {
406 int use_stdin;
407 } set_opts;
409 static void add_patterns_from_input(struct pattern_list *pl,
410 int argc, const char **argv)
412 int i;
413 if (core_sparse_checkout_cone) {
414 struct strbuf line = STRBUF_INIT;
416 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
417 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
418 pl->use_cone_patterns = 1;
420 if (set_opts.use_stdin) {
421 struct strbuf unquoted = STRBUF_INIT;
422 while (!strbuf_getline(&line, stdin)) {
423 if (line.buf[0] == '"') {
424 strbuf_reset(&unquoted);
425 if (unquote_c_style(&unquoted, line.buf, NULL))
426 die(_("unable to unquote C-style string '%s'"),
427 line.buf);
429 strbuf_swap(&unquoted, &line);
432 strbuf_to_cone_pattern(&line, pl);
435 strbuf_release(&unquoted);
436 } else {
437 for (i = 0; i < argc; i++) {
438 strbuf_setlen(&line, 0);
439 strbuf_addstr(&line, argv[i]);
440 strbuf_to_cone_pattern(&line, pl);
443 } else {
444 if (set_opts.use_stdin) {
445 struct strbuf line = STRBUF_INIT;
447 while (!strbuf_getline(&line, stdin)) {
448 size_t len;
449 char *buf = strbuf_detach(&line, &len);
450 add_pattern(buf, empty_base, 0, pl, 0);
452 } else {
453 for (i = 0; i < argc; i++)
454 add_pattern(argv[i], empty_base, 0, pl, 0);
459 enum modify_type {
460 REPLACE,
461 ADD,
464 static void add_patterns_cone_mode(int argc, const char **argv,
465 struct pattern_list *pl)
467 struct strbuf buffer = STRBUF_INIT;
468 struct pattern_entry *pe;
469 struct hashmap_iter iter;
470 struct pattern_list existing;
471 char *sparse_filename = get_sparse_checkout_filename();
473 add_patterns_from_input(pl, argc, argv);
475 memset(&existing, 0, sizeof(existing));
476 existing.use_cone_patterns = core_sparse_checkout_cone;
478 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
479 &existing, NULL))
480 die(_("unable to load existing sparse-checkout patterns"));
481 free(sparse_filename);
483 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
484 if (!hashmap_contains_parent(&pl->recursive_hashmap,
485 pe->pattern, &buffer) ||
486 !hashmap_contains_parent(&pl->parent_hashmap,
487 pe->pattern, &buffer)) {
488 strbuf_reset(&buffer);
489 strbuf_addstr(&buffer, pe->pattern);
490 insert_recursive_pattern(pl, &buffer);
494 clear_pattern_list(&existing);
495 strbuf_release(&buffer);
498 static void add_patterns_literal(int argc, const char **argv,
499 struct pattern_list *pl)
501 char *sparse_filename = get_sparse_checkout_filename();
502 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
503 pl, NULL))
504 die(_("unable to load existing sparse-checkout patterns"));
505 free(sparse_filename);
506 add_patterns_from_input(pl, argc, argv);
509 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
511 int result;
512 int changed_config = 0;
513 struct pattern_list pl;
514 memset(&pl, 0, sizeof(pl));
516 switch (m) {
517 case ADD:
518 if (core_sparse_checkout_cone)
519 add_patterns_cone_mode(argc, argv, &pl);
520 else
521 add_patterns_literal(argc, argv, &pl);
522 break;
524 case REPLACE:
525 add_patterns_from_input(&pl, argc, argv);
526 break;
529 if (!core_apply_sparse_checkout) {
530 set_config(MODE_ALL_PATTERNS);
531 core_apply_sparse_checkout = 1;
532 changed_config = 1;
535 result = write_patterns_and_update(&pl);
537 if (result && changed_config)
538 set_config(MODE_NO_PATTERNS);
540 clear_pattern_list(&pl);
541 return result;
544 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
545 enum modify_type m)
547 static struct option builtin_sparse_checkout_set_options[] = {
548 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
549 N_("read patterns from standard in")),
550 OPT_END(),
553 repo_read_index(the_repository);
555 argc = parse_options(argc, argv, prefix,
556 builtin_sparse_checkout_set_options,
557 builtin_sparse_checkout_set_usage,
558 PARSE_OPT_KEEP_UNKNOWN);
560 return modify_pattern_list(argc, argv, m);
563 static int sparse_checkout_reapply(int argc, const char **argv)
565 repo_read_index(the_repository);
566 return update_working_directory(NULL);
569 static int sparse_checkout_disable(int argc, const char **argv)
571 struct pattern_list pl;
572 struct strbuf match_all = STRBUF_INIT;
574 repo_read_index(the_repository);
576 memset(&pl, 0, sizeof(pl));
577 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
578 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
579 pl.use_cone_patterns = 0;
580 core_apply_sparse_checkout = 1;
582 strbuf_addstr(&match_all, "/*");
583 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
585 if (update_working_directory(&pl))
586 die(_("error while refreshing working directory"));
588 clear_pattern_list(&pl);
589 return set_config(MODE_NO_PATTERNS);
592 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
594 static struct option builtin_sparse_checkout_options[] = {
595 OPT_END(),
598 if (argc == 2 && !strcmp(argv[1], "-h"))
599 usage_with_options(builtin_sparse_checkout_usage,
600 builtin_sparse_checkout_options);
602 argc = parse_options(argc, argv, prefix,
603 builtin_sparse_checkout_options,
604 builtin_sparse_checkout_usage,
605 PARSE_OPT_STOP_AT_NON_OPTION);
607 git_config(git_default_config, NULL);
609 if (argc > 0) {
610 if (!strcmp(argv[0], "list"))
611 return sparse_checkout_list(argc, argv);
612 if (!strcmp(argv[0], "init"))
613 return sparse_checkout_init(argc, argv);
614 if (!strcmp(argv[0], "set"))
615 return sparse_checkout_set(argc, argv, prefix, REPLACE);
616 if (!strcmp(argv[0], "add"))
617 return sparse_checkout_set(argc, argv, prefix, ADD);
618 if (!strcmp(argv[0], "reapply"))
619 return sparse_checkout_reapply(argc, argv);
620 if (!strcmp(argv[0], "disable"))
621 return sparse_checkout_disable(argc, argv);
624 usage_with_options(builtin_sparse_checkout_usage,
625 builtin_sparse_checkout_options);