setup.h: move declarations for setup.c functions from cache.h
[git.git] / builtin / sparse-checkout.c
blob512df0f8f5466f532b6f3f0dc42eac514e803171
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "parse-options.h"
8 #include "pathspec.h"
9 #include "repository.h"
10 #include "run-command.h"
11 #include "strbuf.h"
12 #include "string-list.h"
13 #include "cache-tree.h"
14 #include "lockfile.h"
15 #include "resolve-undo.h"
16 #include "unpack-trees.h"
17 #include "wt-status.h"
18 #include "quote.h"
19 #include "setup.h"
20 #include "sparse-index.h"
21 #include "worktree.h"
23 static const char *empty_base = "";
25 static char const * const builtin_sparse_checkout_usage[] = {
26 N_("git sparse-checkout (init | list | set | add | reapply | disable) [<options>]"),
27 NULL
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 char const * const builtin_sparse_checkout_list_usage[] = {
50 "git sparse-checkout list",
51 NULL
54 static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
56 static struct option builtin_sparse_checkout_list_options[] = {
57 OPT_END(),
59 struct pattern_list pl;
60 char *sparse_filename;
61 int res;
63 if (!core_apply_sparse_checkout)
64 die(_("this worktree is not sparse"));
66 argc = parse_options(argc, argv, prefix,
67 builtin_sparse_checkout_list_options,
68 builtin_sparse_checkout_list_usage, 0);
70 memset(&pl, 0, sizeof(pl));
72 pl.use_cone_patterns = core_sparse_checkout_cone;
74 sparse_filename = get_sparse_checkout_filename();
75 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
76 free(sparse_filename);
78 if (res < 0) {
79 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
80 return 0;
83 if (pl.use_cone_patterns) {
84 int i;
85 struct pattern_entry *pe;
86 struct hashmap_iter iter;
87 struct string_list sl = STRING_LIST_INIT_DUP;
89 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
90 /* pe->pattern starts with "/", skip it */
91 string_list_insert(&sl, pe->pattern + 1);
94 string_list_sort(&sl);
96 for (i = 0; i < sl.nr; i++) {
97 quote_c_style(sl.items[i].string, NULL, stdout, 0);
98 printf("\n");
101 return 0;
104 write_patterns_to_file(stdout, &pl);
105 clear_pattern_list(&pl);
107 return 0;
110 static void clean_tracked_sparse_directories(struct repository *r)
112 int i, was_full = 0;
113 struct strbuf path = STRBUF_INIT;
114 size_t pathlen;
115 struct string_list_item *item;
116 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
119 * If we are not using cone mode patterns, then we cannot
120 * delete directories outside of the sparse cone.
122 if (!r || !r->index || !r->worktree)
123 return;
124 if (init_sparse_checkout_patterns(r->index) ||
125 !r->index->sparse_checkout_patterns->use_cone_patterns)
126 return;
129 * Use the sparse index as a data structure to assist finding
130 * directories that are safe to delete. This conversion to a
131 * sparse index will not delete directories that contain
132 * conflicted entries or submodules.
134 if (r->index->sparse_index == INDEX_EXPANDED) {
136 * If something, such as a merge conflict or other concern,
137 * prevents us from converting to a sparse index, then do
138 * not try deleting files.
140 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
141 return;
142 was_full = 1;
145 strbuf_addstr(&path, r->worktree);
146 strbuf_complete(&path, '/');
147 pathlen = path.len;
150 * Collect directories that have gone out of scope but also
151 * exist on disk, so there is some work to be done. We need to
152 * store the entries in a list before exploring, since that might
153 * expand the sparse-index again.
155 for (i = 0; i < r->index->cache_nr; i++) {
156 struct cache_entry *ce = r->index->cache[i];
158 if (S_ISSPARSEDIR(ce->ce_mode) &&
159 repo_file_exists(r, ce->name))
160 string_list_append(&sparse_dirs, ce->name);
163 for_each_string_list_item(item, &sparse_dirs) {
164 struct dir_struct dir = DIR_INIT;
165 struct pathspec p = { 0 };
166 struct strvec s = STRVEC_INIT;
168 strbuf_setlen(&path, pathlen);
169 strbuf_addstr(&path, item->string);
171 dir.flags |= DIR_SHOW_IGNORED_TOO;
173 setup_standard_excludes(&dir);
174 strvec_push(&s, path.buf);
176 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
177 fill_directory(&dir, r->index, &p);
179 if (dir.nr) {
180 warning(_("directory '%s' contains untracked files,"
181 " but is not in the sparse-checkout cone"),
182 item->string);
183 } else if (remove_dir_recursively(&path, 0)) {
185 * Removal is "best effort". If something blocks
186 * the deletion, then continue with a warning.
188 warning(_("failed to remove directory '%s'"),
189 item->string);
192 strvec_clear(&s);
193 clear_pathspec(&p);
194 dir_clear(&dir);
197 string_list_clear(&sparse_dirs, 0);
198 strbuf_release(&path);
200 if (was_full)
201 ensure_full_index(r->index);
204 static int update_working_directory(struct pattern_list *pl)
206 enum update_sparsity_result result;
207 struct unpack_trees_options o;
208 struct lock_file lock_file = LOCK_INIT;
209 struct repository *r = the_repository;
211 /* If no branch has been checked out, there are no updates to make. */
212 if (is_index_unborn(r->index))
213 return UPDATE_SPARSITY_SUCCESS;
215 r->index->sparse_checkout_patterns = pl;
217 memset(&o, 0, sizeof(o));
218 o.verbose_update = isatty(2);
219 o.update = 1;
220 o.head_idx = -1;
221 o.src_index = r->index;
222 o.dst_index = r->index;
223 o.skip_sparse_checkout = 0;
225 setup_work_tree();
227 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
229 setup_unpack_trees_porcelain(&o, "sparse-checkout");
230 result = update_sparsity(&o, pl);
231 clear_unpack_trees_porcelain(&o);
233 if (result == UPDATE_SPARSITY_WARNINGS)
235 * We don't do any special handling of warnings from untracked
236 * files in the way or dirty entries that can't be removed.
238 result = UPDATE_SPARSITY_SUCCESS;
239 if (result == UPDATE_SPARSITY_SUCCESS)
240 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
241 else
242 rollback_lock_file(&lock_file);
244 clean_tracked_sparse_directories(r);
246 r->index->sparse_checkout_patterns = NULL;
247 return result;
250 static char *escaped_pattern(char *pattern)
252 char *p = pattern;
253 struct strbuf final = STRBUF_INIT;
255 while (*p) {
256 if (is_glob_special(*p))
257 strbuf_addch(&final, '\\');
259 strbuf_addch(&final, *p);
260 p++;
263 return strbuf_detach(&final, NULL);
266 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
268 int i;
269 struct pattern_entry *pe;
270 struct hashmap_iter iter;
271 struct string_list sl = STRING_LIST_INIT_DUP;
272 struct strbuf parent_pattern = STRBUF_INIT;
274 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
275 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
276 continue;
278 if (!hashmap_contains_parent(&pl->recursive_hashmap,
279 pe->pattern,
280 &parent_pattern))
281 string_list_insert(&sl, pe->pattern);
284 string_list_sort(&sl);
285 string_list_remove_duplicates(&sl, 0);
287 fprintf(fp, "/*\n!/*/\n");
289 for (i = 0; i < sl.nr; i++) {
290 char *pattern = escaped_pattern(sl.items[i].string);
292 if (strlen(pattern))
293 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
294 free(pattern);
297 string_list_clear(&sl, 0);
299 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
300 if (!hashmap_contains_parent(&pl->recursive_hashmap,
301 pe->pattern,
302 &parent_pattern))
303 string_list_insert(&sl, pe->pattern);
306 strbuf_release(&parent_pattern);
308 string_list_sort(&sl);
309 string_list_remove_duplicates(&sl, 0);
311 for (i = 0; i < sl.nr; i++) {
312 char *pattern = escaped_pattern(sl.items[i].string);
313 fprintf(fp, "%s/\n", pattern);
314 free(pattern);
318 static int write_patterns_and_update(struct pattern_list *pl)
320 char *sparse_filename;
321 FILE *fp;
322 int fd;
323 struct lock_file lk = LOCK_INIT;
324 int result;
326 sparse_filename = get_sparse_checkout_filename();
328 if (safe_create_leading_directories(sparse_filename))
329 die(_("failed to create directory for sparse-checkout file"));
331 fd = hold_lock_file_for_update(&lk, sparse_filename,
332 LOCK_DIE_ON_ERROR);
333 free(sparse_filename);
335 result = update_working_directory(pl);
336 if (result) {
337 rollback_lock_file(&lk);
338 clear_pattern_list(pl);
339 update_working_directory(NULL);
340 return result;
343 fp = xfdopen(fd, "w");
345 if (core_sparse_checkout_cone)
346 write_cone_to_file(fp, pl);
347 else
348 write_patterns_to_file(fp, pl);
350 fflush(fp);
351 commit_lock_file(&lk);
353 clear_pattern_list(pl);
355 return 0;
358 enum sparse_checkout_mode {
359 MODE_NO_PATTERNS = 0,
360 MODE_ALL_PATTERNS = 1,
361 MODE_CONE_PATTERNS = 2,
364 static int set_config(enum sparse_checkout_mode mode)
366 /* Update to use worktree config, if not already. */
367 if (init_worktree_config(the_repository)) {
368 error(_("failed to initialize worktree config"));
369 return 1;
372 if (repo_config_set_worktree_gently(the_repository,
373 "core.sparseCheckout",
374 mode ? "true" : "false") ||
375 repo_config_set_worktree_gently(the_repository,
376 "core.sparseCheckoutCone",
377 mode == MODE_CONE_PATTERNS ?
378 "true" : "false"))
379 return 1;
381 if (mode == MODE_NO_PATTERNS)
382 return set_sparse_index_config(the_repository, 0);
384 return 0;
387 static int update_modes(int *cone_mode, int *sparse_index)
389 int mode, record_mode;
391 /* Determine if we need to record the mode; ensure sparse checkout on */
392 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
394 /* If not specified, use previous definition of cone mode */
395 if (*cone_mode == -1 && core_apply_sparse_checkout)
396 *cone_mode = core_sparse_checkout_cone;
398 /* Set cone/non-cone mode appropriately */
399 core_apply_sparse_checkout = 1;
400 if (*cone_mode == 1 || *cone_mode == -1) {
401 mode = MODE_CONE_PATTERNS;
402 core_sparse_checkout_cone = 1;
403 } else {
404 mode = MODE_ALL_PATTERNS;
405 core_sparse_checkout_cone = 0;
407 if (record_mode && set_config(mode))
408 return 1;
410 /* Set sparse-index/non-sparse-index mode if specified */
411 if (*sparse_index >= 0) {
412 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
413 die(_("failed to modify sparse-index config"));
415 /* force an index rewrite */
416 repo_read_index(the_repository);
417 the_repository->index->updated_workdir = 1;
419 if (!*sparse_index)
420 ensure_full_index(the_repository->index);
423 return 0;
426 static char const * const builtin_sparse_checkout_init_usage[] = {
427 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
428 NULL
431 static struct sparse_checkout_init_opts {
432 int cone_mode;
433 int sparse_index;
434 } init_opts;
436 static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
438 struct pattern_list pl;
439 char *sparse_filename;
440 int res;
441 struct object_id oid;
442 struct strbuf pattern = STRBUF_INIT;
444 static struct option builtin_sparse_checkout_init_options[] = {
445 OPT_BOOL(0, "cone", &init_opts.cone_mode,
446 N_("initialize the sparse-checkout in cone mode")),
447 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
448 N_("toggle the use of a sparse index")),
449 OPT_END(),
452 repo_read_index(the_repository);
454 init_opts.cone_mode = -1;
455 init_opts.sparse_index = -1;
457 argc = parse_options(argc, argv, prefix,
458 builtin_sparse_checkout_init_options,
459 builtin_sparse_checkout_init_usage, 0);
461 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
462 return 1;
464 memset(&pl, 0, sizeof(pl));
466 sparse_filename = get_sparse_checkout_filename();
467 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
469 /* If we already have a sparse-checkout file, use it. */
470 if (res >= 0) {
471 free(sparse_filename);
472 return update_working_directory(NULL);
475 if (get_oid("HEAD", &oid)) {
476 FILE *fp;
478 /* assume we are in a fresh repo, but update the sparse-checkout file */
479 if (safe_create_leading_directories(sparse_filename))
480 die(_("unable to create leading directories of %s"),
481 sparse_filename);
482 fp = xfopen(sparse_filename, "w");
483 if (!fp)
484 die(_("failed to open '%s'"), sparse_filename);
486 free(sparse_filename);
487 fprintf(fp, "/*\n!/*/\n");
488 fclose(fp);
489 return 0;
492 strbuf_addstr(&pattern, "/*");
493 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
494 strbuf_addstr(&pattern, "!/*/");
495 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
496 pl.use_cone_patterns = init_opts.cone_mode;
498 return write_patterns_and_update(&pl);
501 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
503 struct pattern_entry *e = xmalloc(sizeof(*e));
504 e->patternlen = path->len;
505 e->pattern = strbuf_detach(path, NULL);
506 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
508 hashmap_add(&pl->recursive_hashmap, &e->ent);
510 while (e->patternlen) {
511 char *slash = strrchr(e->pattern, '/');
512 char *oldpattern = e->pattern;
513 size_t newlen;
515 if (!slash || slash == e->pattern)
516 break;
518 newlen = slash - e->pattern;
519 e = xmalloc(sizeof(struct pattern_entry));
520 e->patternlen = newlen;
521 e->pattern = xstrndup(oldpattern, newlen);
522 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
524 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
525 hashmap_add(&pl->parent_hashmap, &e->ent);
529 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
531 strbuf_trim(line);
533 strbuf_trim_trailing_dir_sep(line);
535 if (strbuf_normalize_path(line))
536 die(_("could not normalize path %s"), line->buf);
538 if (!line->len)
539 return;
541 if (line->buf[0] != '/')
542 strbuf_insertstr(line, 0, "/");
544 insert_recursive_pattern(pl, line);
547 static void add_patterns_from_input(struct pattern_list *pl,
548 int argc, const char **argv,
549 int use_stdin)
551 int i;
552 if (core_sparse_checkout_cone) {
553 struct strbuf line = STRBUF_INIT;
555 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
556 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
557 pl->use_cone_patterns = 1;
559 if (use_stdin) {
560 struct strbuf unquoted = STRBUF_INIT;
561 while (!strbuf_getline(&line, stdin)) {
562 if (line.buf[0] == '"') {
563 strbuf_reset(&unquoted);
564 if (unquote_c_style(&unquoted, line.buf, NULL))
565 die(_("unable to unquote C-style string '%s'"),
566 line.buf);
568 strbuf_swap(&unquoted, &line);
571 strbuf_to_cone_pattern(&line, pl);
574 strbuf_release(&unquoted);
575 } else {
576 for (i = 0; i < argc; i++) {
577 strbuf_setlen(&line, 0);
578 strbuf_addstr(&line, argv[i]);
579 strbuf_to_cone_pattern(&line, pl);
582 } else {
583 if (use_stdin) {
584 struct strbuf line = STRBUF_INIT;
586 while (!strbuf_getline(&line, stdin)) {
587 size_t len;
588 char *buf = strbuf_detach(&line, &len);
589 add_pattern(buf, empty_base, 0, pl, 0);
591 } else {
592 for (i = 0; i < argc; i++)
593 add_pattern(argv[i], empty_base, 0, pl, 0);
598 enum modify_type {
599 REPLACE,
600 ADD,
603 static void add_patterns_cone_mode(int argc, const char **argv,
604 struct pattern_list *pl,
605 int use_stdin)
607 struct strbuf buffer = STRBUF_INIT;
608 struct pattern_entry *pe;
609 struct hashmap_iter iter;
610 struct pattern_list existing;
611 char *sparse_filename = get_sparse_checkout_filename();
613 add_patterns_from_input(pl, argc, argv, use_stdin);
615 memset(&existing, 0, sizeof(existing));
616 existing.use_cone_patterns = core_sparse_checkout_cone;
618 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
619 &existing, NULL, 0))
620 die(_("unable to load existing sparse-checkout patterns"));
621 free(sparse_filename);
623 if (!existing.use_cone_patterns)
624 die(_("existing sparse-checkout patterns do not use cone mode"));
626 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
627 if (!hashmap_contains_parent(&pl->recursive_hashmap,
628 pe->pattern, &buffer) ||
629 !hashmap_contains_parent(&pl->parent_hashmap,
630 pe->pattern, &buffer)) {
631 strbuf_reset(&buffer);
632 strbuf_addstr(&buffer, pe->pattern);
633 insert_recursive_pattern(pl, &buffer);
637 clear_pattern_list(&existing);
638 strbuf_release(&buffer);
641 static void add_patterns_literal(int argc, const char **argv,
642 struct pattern_list *pl,
643 int use_stdin)
645 char *sparse_filename = get_sparse_checkout_filename();
646 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
647 pl, NULL, 0))
648 die(_("unable to load existing sparse-checkout patterns"));
649 free(sparse_filename);
650 add_patterns_from_input(pl, argc, argv, use_stdin);
653 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
654 enum modify_type m)
656 int result;
657 int changed_config = 0;
658 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
660 switch (m) {
661 case ADD:
662 if (core_sparse_checkout_cone)
663 add_patterns_cone_mode(argc, argv, pl, use_stdin);
664 else
665 add_patterns_literal(argc, argv, pl, use_stdin);
666 break;
668 case REPLACE:
669 add_patterns_from_input(pl, argc, argv, use_stdin);
670 break;
673 if (!core_apply_sparse_checkout) {
674 set_config(MODE_ALL_PATTERNS);
675 core_apply_sparse_checkout = 1;
676 changed_config = 1;
679 result = write_patterns_and_update(pl);
681 if (result && changed_config)
682 set_config(MODE_NO_PATTERNS);
684 clear_pattern_list(pl);
685 free(pl);
686 return result;
689 static void sanitize_paths(int argc, const char **argv,
690 const char *prefix, int skip_checks)
692 int i;
694 if (!argc)
695 return;
697 if (prefix && *prefix && core_sparse_checkout_cone) {
699 * The args are not pathspecs, so unfortunately we
700 * cannot imitate how cmd_add() uses parse_pathspec().
702 int prefix_len = strlen(prefix);
704 for (i = 0; i < argc; i++)
705 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
708 if (skip_checks)
709 return;
711 if (prefix && *prefix && !core_sparse_checkout_cone)
712 die(_("please run from the toplevel directory in non-cone mode"));
714 if (core_sparse_checkout_cone) {
715 for (i = 0; i < argc; i++) {
716 if (argv[i][0] == '/')
717 die(_("specify directories rather than patterns (no leading slash)"));
718 if (argv[i][0] == '!')
719 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
720 if (strpbrk(argv[i], "*?[]"))
721 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
725 for (i = 0; i < argc; i++) {
726 struct cache_entry *ce;
727 struct index_state *index = the_repository->index;
728 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
730 if (pos < 0)
731 continue;
732 ce = index->cache[pos];
733 if (S_ISSPARSEDIR(ce->ce_mode))
734 continue;
736 if (core_sparse_checkout_cone)
737 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
738 else
739 warning(_("pass a leading slash before paths such as '%s' if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)."), argv[i]);
743 static char const * const builtin_sparse_checkout_add_usage[] = {
744 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
745 NULL
748 static struct sparse_checkout_add_opts {
749 int skip_checks;
750 int use_stdin;
751 } add_opts;
753 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
755 static struct option builtin_sparse_checkout_add_options[] = {
756 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
757 N_("skip some sanity checks on the given paths that might give false positives"),
758 PARSE_OPT_NONEG),
759 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
760 N_("read patterns from standard in")),
761 OPT_END(),
764 if (!core_apply_sparse_checkout)
765 die(_("no sparse-checkout to add to"));
767 repo_read_index(the_repository);
769 argc = parse_options(argc, argv, prefix,
770 builtin_sparse_checkout_add_options,
771 builtin_sparse_checkout_add_usage,
772 PARSE_OPT_KEEP_UNKNOWN_OPT);
774 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
776 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
779 static char const * const builtin_sparse_checkout_set_usage[] = {
780 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
781 NULL
784 static struct sparse_checkout_set_opts {
785 int cone_mode;
786 int sparse_index;
787 int skip_checks;
788 int use_stdin;
789 } set_opts;
791 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
793 int default_patterns_nr = 2;
794 const char *default_patterns[] = {"/*", "!/*/", NULL};
796 static struct option builtin_sparse_checkout_set_options[] = {
797 OPT_BOOL(0, "cone", &set_opts.cone_mode,
798 N_("initialize the sparse-checkout in cone mode")),
799 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
800 N_("toggle the use of a sparse index")),
801 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
802 N_("skip some sanity checks on the given paths that might give false positives"),
803 PARSE_OPT_NONEG),
804 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
805 N_("read patterns from standard in"),
806 PARSE_OPT_NONEG),
807 OPT_END(),
810 repo_read_index(the_repository);
812 set_opts.cone_mode = -1;
813 set_opts.sparse_index = -1;
815 argc = parse_options(argc, argv, prefix,
816 builtin_sparse_checkout_set_options,
817 builtin_sparse_checkout_set_usage,
818 PARSE_OPT_KEEP_UNKNOWN_OPT);
820 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
821 return 1;
824 * Cone mode automatically specifies the toplevel directory. For
825 * non-cone mode, if nothing is specified, manually select just the
826 * top-level directory (much as 'init' would do).
828 if (!core_sparse_checkout_cone && argc == 0) {
829 argv = default_patterns;
830 argc = default_patterns_nr;
831 } else {
832 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
835 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
838 static char const * const builtin_sparse_checkout_reapply_usage[] = {
839 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
840 NULL
843 static struct sparse_checkout_reapply_opts {
844 int cone_mode;
845 int sparse_index;
846 } reapply_opts;
848 static int sparse_checkout_reapply(int argc, const char **argv,
849 const char *prefix)
851 static struct option builtin_sparse_checkout_reapply_options[] = {
852 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
853 N_("initialize the sparse-checkout in cone mode")),
854 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
855 N_("toggle the use of a sparse index")),
856 OPT_END(),
859 if (!core_apply_sparse_checkout)
860 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
862 reapply_opts.cone_mode = -1;
863 reapply_opts.sparse_index = -1;
865 argc = parse_options(argc, argv, prefix,
866 builtin_sparse_checkout_reapply_options,
867 builtin_sparse_checkout_reapply_usage, 0);
869 repo_read_index(the_repository);
871 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
872 return 1;
874 return update_working_directory(NULL);
877 static char const * const builtin_sparse_checkout_disable_usage[] = {
878 "git sparse-checkout disable",
879 NULL
882 static int sparse_checkout_disable(int argc, const char **argv,
883 const char *prefix)
885 static struct option builtin_sparse_checkout_disable_options[] = {
886 OPT_END(),
888 struct pattern_list pl;
889 struct strbuf match_all = STRBUF_INIT;
892 * We do not exit early if !core_apply_sparse_checkout; due to the
893 * ability for users to manually muck things up between
894 * direct editing of .git/info/sparse-checkout
895 * running read-tree -m u HEAD or update-index --skip-worktree
896 * direct toggling of config options
897 * users might end up with an index with SKIP_WORKTREE bit set on
898 * some files and not know how to undo it. So, here we just
899 * forcibly return to a dense checkout regardless of initial state.
902 argc = parse_options(argc, argv, prefix,
903 builtin_sparse_checkout_disable_options,
904 builtin_sparse_checkout_disable_usage, 0);
906 repo_read_index(the_repository);
908 memset(&pl, 0, sizeof(pl));
909 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
910 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
911 pl.use_cone_patterns = 0;
912 core_apply_sparse_checkout = 1;
914 strbuf_addstr(&match_all, "/*");
915 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
917 prepare_repo_settings(the_repository);
918 the_repository->settings.sparse_index = 0;
920 if (update_working_directory(&pl))
921 die(_("error while refreshing working directory"));
923 clear_pattern_list(&pl);
924 return set_config(MODE_NO_PATTERNS);
927 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
929 parse_opt_subcommand_fn *fn = NULL;
930 struct option builtin_sparse_checkout_options[] = {
931 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
932 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
933 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
934 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
935 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
936 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
937 OPT_END(),
940 argc = parse_options(argc, argv, prefix,
941 builtin_sparse_checkout_options,
942 builtin_sparse_checkout_usage, 0);
944 git_config(git_default_config, NULL);
946 prepare_repo_settings(the_repository);
947 the_repository->settings.command_requires_full_index = 0;
949 return fn(argc, argv, prefix);