git-apply: add --quiet flag
[alt-git.git] / builtin / sparse-checkout.c
blobd0f5c4702be69d0c9fade08ffbf5183c5f3dbe7e
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 #include "sparse-index.h"
19 static const char *empty_base = "";
21 static char const * const builtin_sparse_checkout_usage[] = {
22 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
23 NULL
26 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
28 int i;
30 for (i = 0; i < pl->nr; i++) {
31 struct path_pattern *p = pl->patterns[i];
33 if (p->flags & PATTERN_FLAG_NEGATIVE)
34 fprintf(fp, "!");
36 fprintf(fp, "%s", p->pattern);
38 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
39 fprintf(fp, "/");
41 fprintf(fp, "\n");
45 static char const * const builtin_sparse_checkout_list_usage[] = {
46 N_("git sparse-checkout list"),
47 NULL
50 static int sparse_checkout_list(int argc, const char **argv)
52 static struct option builtin_sparse_checkout_list_options[] = {
53 OPT_END(),
55 struct pattern_list pl;
56 char *sparse_filename;
57 int res;
59 argc = parse_options(argc, argv, NULL,
60 builtin_sparse_checkout_list_options,
61 builtin_sparse_checkout_list_usage, 0);
63 memset(&pl, 0, sizeof(pl));
65 pl.use_cone_patterns = core_sparse_checkout_cone;
67 sparse_filename = get_sparse_checkout_filename();
68 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
69 free(sparse_filename);
71 if (res < 0) {
72 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
73 return 0;
76 if (pl.use_cone_patterns) {
77 int i;
78 struct pattern_entry *pe;
79 struct hashmap_iter iter;
80 struct string_list sl = STRING_LIST_INIT_DUP;
82 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
83 /* pe->pattern starts with "/", skip it */
84 string_list_insert(&sl, pe->pattern + 1);
87 string_list_sort(&sl);
89 for (i = 0; i < sl.nr; i++) {
90 quote_c_style(sl.items[i].string, NULL, stdout, 0);
91 printf("\n");
94 return 0;
97 write_patterns_to_file(stdout, &pl);
98 clear_pattern_list(&pl);
100 return 0;
103 static void clean_tracked_sparse_directories(struct repository *r)
105 int i, was_full = 0;
106 struct strbuf path = STRBUF_INIT;
107 size_t pathlen;
108 struct string_list_item *item;
109 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
112 * If we are not using cone mode patterns, then we cannot
113 * delete directories outside of the sparse cone.
115 if (!r || !r->index || !r->worktree)
116 return;
117 if (init_sparse_checkout_patterns(r->index) ||
118 !r->index->sparse_checkout_patterns->use_cone_patterns)
119 return;
122 * Use the sparse index as a data structure to assist finding
123 * directories that are safe to delete. This conversion to a
124 * sparse index will not delete directories that contain
125 * conflicted entries or submodules.
127 if (!r->index->sparse_index) {
129 * If something, such as a merge conflict or other concern,
130 * prevents us from converting to a sparse index, then do
131 * not try deleting files.
133 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
134 return;
135 was_full = 1;
138 strbuf_addstr(&path, r->worktree);
139 strbuf_complete(&path, '/');
140 pathlen = path.len;
143 * Collect directories that have gone out of scope but also
144 * exist on disk, so there is some work to be done. We need to
145 * store the entries in a list before exploring, since that might
146 * expand the sparse-index again.
148 for (i = 0; i < r->index->cache_nr; i++) {
149 struct cache_entry *ce = r->index->cache[i];
151 if (S_ISSPARSEDIR(ce->ce_mode) &&
152 repo_file_exists(r, ce->name))
153 string_list_append(&sparse_dirs, ce->name);
156 for_each_string_list_item(item, &sparse_dirs) {
157 struct dir_struct dir = DIR_INIT;
158 struct pathspec p = { 0 };
159 struct strvec s = STRVEC_INIT;
161 strbuf_setlen(&path, pathlen);
162 strbuf_addstr(&path, item->string);
164 dir.flags |= DIR_SHOW_IGNORED_TOO;
166 setup_standard_excludes(&dir);
167 strvec_push(&s, path.buf);
169 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
170 fill_directory(&dir, r->index, &p);
172 if (dir.nr) {
173 warning(_("directory '%s' contains untracked files,"
174 " but is not in the sparse-checkout cone"),
175 item->string);
176 } else if (remove_dir_recursively(&path, 0)) {
178 * Removal is "best effort". If something blocks
179 * the deletion, then continue with a warning.
181 warning(_("failed to remove directory '%s'"),
182 item->string);
185 dir_clear(&dir);
188 string_list_clear(&sparse_dirs, 0);
189 strbuf_release(&path);
191 if (was_full)
192 ensure_full_index(r->index);
195 static int update_working_directory(struct pattern_list *pl)
197 enum update_sparsity_result result;
198 struct unpack_trees_options o;
199 struct lock_file lock_file = LOCK_INIT;
200 struct repository *r = the_repository;
202 /* If no branch has been checked out, there are no updates to make. */
203 if (is_index_unborn(r->index))
204 return UPDATE_SPARSITY_SUCCESS;
206 r->index->sparse_checkout_patterns = pl;
208 memset(&o, 0, sizeof(o));
209 o.verbose_update = isatty(2);
210 o.update = 1;
211 o.head_idx = -1;
212 o.src_index = r->index;
213 o.dst_index = r->index;
214 o.skip_sparse_checkout = 0;
215 o.pl = pl;
217 setup_work_tree();
219 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
221 setup_unpack_trees_porcelain(&o, "sparse-checkout");
222 result = update_sparsity(&o);
223 clear_unpack_trees_porcelain(&o);
225 if (result == UPDATE_SPARSITY_WARNINGS)
227 * We don't do any special handling of warnings from untracked
228 * files in the way or dirty entries that can't be removed.
230 result = UPDATE_SPARSITY_SUCCESS;
231 if (result == UPDATE_SPARSITY_SUCCESS)
232 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
233 else
234 rollback_lock_file(&lock_file);
236 clean_tracked_sparse_directories(r);
238 r->index->sparse_checkout_patterns = NULL;
239 return result;
242 static char *escaped_pattern(char *pattern)
244 char *p = pattern;
245 struct strbuf final = STRBUF_INIT;
247 while (*p) {
248 if (is_glob_special(*p))
249 strbuf_addch(&final, '\\');
251 strbuf_addch(&final, *p);
252 p++;
255 return strbuf_detach(&final, NULL);
258 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
260 int i;
261 struct pattern_entry *pe;
262 struct hashmap_iter iter;
263 struct string_list sl = STRING_LIST_INIT_DUP;
264 struct strbuf parent_pattern = STRBUF_INIT;
266 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
267 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
268 continue;
270 if (!hashmap_contains_parent(&pl->recursive_hashmap,
271 pe->pattern,
272 &parent_pattern))
273 string_list_insert(&sl, pe->pattern);
276 string_list_sort(&sl);
277 string_list_remove_duplicates(&sl, 0);
279 fprintf(fp, "/*\n!/*/\n");
281 for (i = 0; i < sl.nr; i++) {
282 char *pattern = escaped_pattern(sl.items[i].string);
284 if (strlen(pattern))
285 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
286 free(pattern);
289 string_list_clear(&sl, 0);
291 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
292 if (!hashmap_contains_parent(&pl->recursive_hashmap,
293 pe->pattern,
294 &parent_pattern))
295 string_list_insert(&sl, pe->pattern);
298 strbuf_release(&parent_pattern);
300 string_list_sort(&sl);
301 string_list_remove_duplicates(&sl, 0);
303 for (i = 0; i < sl.nr; i++) {
304 char *pattern = escaped_pattern(sl.items[i].string);
305 fprintf(fp, "%s/\n", pattern);
306 free(pattern);
310 static int write_patterns_and_update(struct pattern_list *pl)
312 char *sparse_filename;
313 FILE *fp;
314 int fd;
315 struct lock_file lk = LOCK_INIT;
316 int result;
318 sparse_filename = get_sparse_checkout_filename();
320 if (safe_create_leading_directories(sparse_filename))
321 die(_("failed to create directory for sparse-checkout file"));
323 fd = hold_lock_file_for_update(&lk, sparse_filename,
324 LOCK_DIE_ON_ERROR);
326 result = update_working_directory(pl);
327 if (result) {
328 rollback_lock_file(&lk);
329 free(sparse_filename);
330 clear_pattern_list(pl);
331 update_working_directory(NULL);
332 return result;
335 fp = xfdopen(fd, "w");
337 if (core_sparse_checkout_cone)
338 write_cone_to_file(fp, pl);
339 else
340 write_patterns_to_file(fp, pl);
342 fflush(fp);
343 commit_lock_file(&lk);
345 free(sparse_filename);
346 clear_pattern_list(pl);
348 return 0;
351 enum sparse_checkout_mode {
352 MODE_NO_PATTERNS = 0,
353 MODE_ALL_PATTERNS = 1,
354 MODE_CONE_PATTERNS = 2,
357 static int set_config(enum sparse_checkout_mode mode)
359 const char *config_path;
361 if (upgrade_repository_format(1) < 0)
362 die(_("unable to upgrade repository format to enable worktreeConfig"));
363 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
364 error(_("failed to set extensions.worktreeConfig setting"));
365 return 1;
368 config_path = git_path("config.worktree");
369 git_config_set_in_file_gently(config_path,
370 "core.sparseCheckout",
371 mode ? "true" : NULL);
373 git_config_set_in_file_gently(config_path,
374 "core.sparseCheckoutCone",
375 mode == MODE_CONE_PATTERNS ? "true" : NULL);
377 if (mode == MODE_NO_PATTERNS)
378 set_sparse_index_config(the_repository, 0);
380 return 0;
383 static char const * const builtin_sparse_checkout_init_usage[] = {
384 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
385 NULL
388 static struct sparse_checkout_init_opts {
389 int cone_mode;
390 int sparse_index;
391 } init_opts;
393 static int sparse_checkout_init(int argc, const char **argv)
395 struct pattern_list pl;
396 char *sparse_filename;
397 int res;
398 struct object_id oid;
399 int mode;
400 struct strbuf pattern = STRBUF_INIT;
402 static struct option builtin_sparse_checkout_init_options[] = {
403 OPT_BOOL(0, "cone", &init_opts.cone_mode,
404 N_("initialize the sparse-checkout in cone mode")),
405 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
406 N_("toggle the use of a sparse index")),
407 OPT_END(),
410 repo_read_index(the_repository);
412 init_opts.sparse_index = -1;
414 argc = parse_options(argc, argv, NULL,
415 builtin_sparse_checkout_init_options,
416 builtin_sparse_checkout_init_usage, 0);
418 if (init_opts.cone_mode) {
419 mode = MODE_CONE_PATTERNS;
420 core_sparse_checkout_cone = 1;
421 } else
422 mode = MODE_ALL_PATTERNS;
424 if (set_config(mode))
425 return 1;
427 memset(&pl, 0, sizeof(pl));
429 sparse_filename = get_sparse_checkout_filename();
430 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
432 if (init_opts.sparse_index >= 0) {
433 if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0)
434 die(_("failed to modify sparse-index config"));
436 /* force an index rewrite */
437 repo_read_index(the_repository);
438 the_repository->index->updated_workdir = 1;
441 core_apply_sparse_checkout = 1;
443 /* If we already have a sparse-checkout file, use it. */
444 if (res >= 0) {
445 free(sparse_filename);
446 return update_working_directory(NULL);
449 if (get_oid("HEAD", &oid)) {
450 FILE *fp;
452 /* assume we are in a fresh repo, but update the sparse-checkout file */
453 fp = xfopen(sparse_filename, "w");
454 if (!fp)
455 die(_("failed to open '%s'"), sparse_filename);
457 free(sparse_filename);
458 fprintf(fp, "/*\n!/*/\n");
459 fclose(fp);
460 return 0;
463 strbuf_addstr(&pattern, "/*");
464 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
465 strbuf_addstr(&pattern, "!/*/");
466 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
467 pl.use_cone_patterns = init_opts.cone_mode;
469 return write_patterns_and_update(&pl);
472 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
474 struct pattern_entry *e = xmalloc(sizeof(*e));
475 e->patternlen = path->len;
476 e->pattern = strbuf_detach(path, NULL);
477 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
479 hashmap_add(&pl->recursive_hashmap, &e->ent);
481 while (e->patternlen) {
482 char *slash = strrchr(e->pattern, '/');
483 char *oldpattern = e->pattern;
484 size_t newlen;
486 if (slash == e->pattern)
487 break;
489 newlen = slash - e->pattern;
490 e = xmalloc(sizeof(struct pattern_entry));
491 e->patternlen = newlen;
492 e->pattern = xstrndup(oldpattern, newlen);
493 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
495 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
496 hashmap_add(&pl->parent_hashmap, &e->ent);
500 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
502 strbuf_trim(line);
504 strbuf_trim_trailing_dir_sep(line);
506 if (strbuf_normalize_path(line))
507 die(_("could not normalize path %s"), line->buf);
509 if (!line->len)
510 return;
512 if (line->buf[0] != '/')
513 strbuf_insertstr(line, 0, "/");
515 insert_recursive_pattern(pl, line);
518 static char const * const builtin_sparse_checkout_set_usage[] = {
519 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
520 NULL
523 static struct sparse_checkout_set_opts {
524 int use_stdin;
525 } set_opts;
527 static void add_patterns_from_input(struct pattern_list *pl,
528 int argc, const char **argv)
530 int i;
531 if (core_sparse_checkout_cone) {
532 struct strbuf line = STRBUF_INIT;
534 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
535 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
536 pl->use_cone_patterns = 1;
538 if (set_opts.use_stdin) {
539 struct strbuf unquoted = STRBUF_INIT;
540 while (!strbuf_getline(&line, stdin)) {
541 if (line.buf[0] == '"') {
542 strbuf_reset(&unquoted);
543 if (unquote_c_style(&unquoted, line.buf, NULL))
544 die(_("unable to unquote C-style string '%s'"),
545 line.buf);
547 strbuf_swap(&unquoted, &line);
550 strbuf_to_cone_pattern(&line, pl);
553 strbuf_release(&unquoted);
554 } else {
555 for (i = 0; i < argc; i++) {
556 strbuf_setlen(&line, 0);
557 strbuf_addstr(&line, argv[i]);
558 strbuf_to_cone_pattern(&line, pl);
561 } else {
562 if (set_opts.use_stdin) {
563 struct strbuf line = STRBUF_INIT;
565 while (!strbuf_getline(&line, stdin)) {
566 size_t len;
567 char *buf = strbuf_detach(&line, &len);
568 add_pattern(buf, empty_base, 0, pl, 0);
570 } else {
571 for (i = 0; i < argc; i++)
572 add_pattern(argv[i], empty_base, 0, pl, 0);
577 enum modify_type {
578 REPLACE,
579 ADD,
582 static void add_patterns_cone_mode(int argc, const char **argv,
583 struct pattern_list *pl)
585 struct strbuf buffer = STRBUF_INIT;
586 struct pattern_entry *pe;
587 struct hashmap_iter iter;
588 struct pattern_list existing;
589 char *sparse_filename = get_sparse_checkout_filename();
591 add_patterns_from_input(pl, argc, argv);
593 memset(&existing, 0, sizeof(existing));
594 existing.use_cone_patterns = core_sparse_checkout_cone;
596 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
597 &existing, NULL, 0))
598 die(_("unable to load existing sparse-checkout patterns"));
599 free(sparse_filename);
601 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
602 if (!hashmap_contains_parent(&pl->recursive_hashmap,
603 pe->pattern, &buffer) ||
604 !hashmap_contains_parent(&pl->parent_hashmap,
605 pe->pattern, &buffer)) {
606 strbuf_reset(&buffer);
607 strbuf_addstr(&buffer, pe->pattern);
608 insert_recursive_pattern(pl, &buffer);
612 clear_pattern_list(&existing);
613 strbuf_release(&buffer);
616 static void add_patterns_literal(int argc, const char **argv,
617 struct pattern_list *pl)
619 char *sparse_filename = get_sparse_checkout_filename();
620 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
621 pl, NULL, 0))
622 die(_("unable to load existing sparse-checkout patterns"));
623 free(sparse_filename);
624 add_patterns_from_input(pl, argc, argv);
627 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
629 int result;
630 int changed_config = 0;
631 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
633 switch (m) {
634 case ADD:
635 if (core_sparse_checkout_cone)
636 add_patterns_cone_mode(argc, argv, pl);
637 else
638 add_patterns_literal(argc, argv, pl);
639 break;
641 case REPLACE:
642 add_patterns_from_input(pl, argc, argv);
643 break;
646 if (!core_apply_sparse_checkout) {
647 set_config(MODE_ALL_PATTERNS);
648 core_apply_sparse_checkout = 1;
649 changed_config = 1;
652 result = write_patterns_and_update(pl);
654 if (result && changed_config)
655 set_config(MODE_NO_PATTERNS);
657 clear_pattern_list(pl);
658 free(pl);
659 return result;
662 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
663 enum modify_type m)
665 static struct option builtin_sparse_checkout_set_options[] = {
666 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
667 N_("read patterns from standard in")),
668 OPT_END(),
671 repo_read_index(the_repository);
673 argc = parse_options(argc, argv, prefix,
674 builtin_sparse_checkout_set_options,
675 builtin_sparse_checkout_set_usage,
676 PARSE_OPT_KEEP_UNKNOWN);
678 return modify_pattern_list(argc, argv, m);
681 static char const * const builtin_sparse_checkout_reapply_usage[] = {
682 N_("git sparse-checkout reapply"),
683 NULL
686 static int sparse_checkout_reapply(int argc, const char **argv)
688 static struct option builtin_sparse_checkout_reapply_options[] = {
689 OPT_END(),
692 argc = parse_options(argc, argv, NULL,
693 builtin_sparse_checkout_reapply_options,
694 builtin_sparse_checkout_reapply_usage, 0);
696 repo_read_index(the_repository);
697 return update_working_directory(NULL);
700 static char const * const builtin_sparse_checkout_disable_usage[] = {
701 N_("git sparse-checkout disable"),
702 NULL
705 static int sparse_checkout_disable(int argc, const char **argv)
707 static struct option builtin_sparse_checkout_disable_options[] = {
708 OPT_END(),
710 struct pattern_list pl;
711 struct strbuf match_all = STRBUF_INIT;
713 argc = parse_options(argc, argv, NULL,
714 builtin_sparse_checkout_disable_options,
715 builtin_sparse_checkout_disable_usage, 0);
717 repo_read_index(the_repository);
719 memset(&pl, 0, sizeof(pl));
720 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
721 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
722 pl.use_cone_patterns = 0;
723 core_apply_sparse_checkout = 1;
725 strbuf_addstr(&match_all, "/*");
726 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
728 prepare_repo_settings(the_repository);
729 the_repository->settings.sparse_index = 0;
731 if (update_working_directory(&pl))
732 die(_("error while refreshing working directory"));
734 clear_pattern_list(&pl);
735 return set_config(MODE_NO_PATTERNS);
738 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
740 static struct option builtin_sparse_checkout_options[] = {
741 OPT_END(),
744 if (argc == 2 && !strcmp(argv[1], "-h"))
745 usage_with_options(builtin_sparse_checkout_usage,
746 builtin_sparse_checkout_options);
748 argc = parse_options(argc, argv, prefix,
749 builtin_sparse_checkout_options,
750 builtin_sparse_checkout_usage,
751 PARSE_OPT_STOP_AT_NON_OPTION);
753 git_config(git_default_config, NULL);
755 if (argc > 0) {
756 if (!strcmp(argv[0], "list"))
757 return sparse_checkout_list(argc, argv);
758 if (!strcmp(argv[0], "init"))
759 return sparse_checkout_init(argc, argv);
760 if (!strcmp(argv[0], "set"))
761 return sparse_checkout_set(argc, argv, prefix, REPLACE);
762 if (!strcmp(argv[0], "add"))
763 return sparse_checkout_set(argc, argv, prefix, ADD);
764 if (!strcmp(argv[0], "reapply"))
765 return sparse_checkout_reapply(argc, argv);
766 if (!strcmp(argv[0], "disable"))
767 return sparse_checkout_disable(argc, argv);
770 usage_with_options(builtin_sparse_checkout_usage,
771 builtin_sparse_checkout_options);