Merge branch 'ab/checkout-branch-info-leakfix'
[git/debian.git] / builtin / sparse-checkout.c
blob679c10703684044aa02555227b5cdcdf92591d9b
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 if (!core_apply_sparse_checkout)
60 die(_("this worktree is not sparse"));
62 argc = parse_options(argc, argv, NULL,
63 builtin_sparse_checkout_list_options,
64 builtin_sparse_checkout_list_usage, 0);
66 memset(&pl, 0, sizeof(pl));
68 pl.use_cone_patterns = core_sparse_checkout_cone;
70 sparse_filename = get_sparse_checkout_filename();
71 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
72 free(sparse_filename);
74 if (res < 0) {
75 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
76 return 0;
79 if (pl.use_cone_patterns) {
80 int i;
81 struct pattern_entry *pe;
82 struct hashmap_iter iter;
83 struct string_list sl = STRING_LIST_INIT_DUP;
85 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
86 /* pe->pattern starts with "/", skip it */
87 string_list_insert(&sl, pe->pattern + 1);
90 string_list_sort(&sl);
92 for (i = 0; i < sl.nr; i++) {
93 quote_c_style(sl.items[i].string, NULL, stdout, 0);
94 printf("\n");
97 return 0;
100 write_patterns_to_file(stdout, &pl);
101 clear_pattern_list(&pl);
103 return 0;
106 static void clean_tracked_sparse_directories(struct repository *r)
108 int i, was_full = 0;
109 struct strbuf path = STRBUF_INIT;
110 size_t pathlen;
111 struct string_list_item *item;
112 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
115 * If we are not using cone mode patterns, then we cannot
116 * delete directories outside of the sparse cone.
118 if (!r || !r->index || !r->worktree)
119 return;
120 if (init_sparse_checkout_patterns(r->index) ||
121 !r->index->sparse_checkout_patterns->use_cone_patterns)
122 return;
125 * Use the sparse index as a data structure to assist finding
126 * directories that are safe to delete. This conversion to a
127 * sparse index will not delete directories that contain
128 * conflicted entries or submodules.
130 if (!r->index->sparse_index) {
132 * If something, such as a merge conflict or other concern,
133 * prevents us from converting to a sparse index, then do
134 * not try deleting files.
136 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
137 return;
138 was_full = 1;
141 strbuf_addstr(&path, r->worktree);
142 strbuf_complete(&path, '/');
143 pathlen = path.len;
146 * Collect directories that have gone out of scope but also
147 * exist on disk, so there is some work to be done. We need to
148 * store the entries in a list before exploring, since that might
149 * expand the sparse-index again.
151 for (i = 0; i < r->index->cache_nr; i++) {
152 struct cache_entry *ce = r->index->cache[i];
154 if (S_ISSPARSEDIR(ce->ce_mode) &&
155 repo_file_exists(r, ce->name))
156 string_list_append(&sparse_dirs, ce->name);
159 for_each_string_list_item(item, &sparse_dirs) {
160 struct dir_struct dir = DIR_INIT;
161 struct pathspec p = { 0 };
162 struct strvec s = STRVEC_INIT;
164 strbuf_setlen(&path, pathlen);
165 strbuf_addstr(&path, item->string);
167 dir.flags |= DIR_SHOW_IGNORED_TOO;
169 setup_standard_excludes(&dir);
170 strvec_push(&s, path.buf);
172 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
173 fill_directory(&dir, r->index, &p);
175 if (dir.nr) {
176 warning(_("directory '%s' contains untracked files,"
177 " but is not in the sparse-checkout cone"),
178 item->string);
179 } else if (remove_dir_recursively(&path, 0)) {
181 * Removal is "best effort". If something blocks
182 * the deletion, then continue with a warning.
184 warning(_("failed to remove directory '%s'"),
185 item->string);
188 dir_clear(&dir);
191 string_list_clear(&sparse_dirs, 0);
192 strbuf_release(&path);
194 if (was_full)
195 ensure_full_index(r->index);
198 static int update_working_directory(struct pattern_list *pl)
200 enum update_sparsity_result result;
201 struct unpack_trees_options o;
202 struct lock_file lock_file = LOCK_INIT;
203 struct repository *r = the_repository;
205 /* If no branch has been checked out, there are no updates to make. */
206 if (is_index_unborn(r->index))
207 return UPDATE_SPARSITY_SUCCESS;
209 r->index->sparse_checkout_patterns = pl;
211 memset(&o, 0, sizeof(o));
212 o.verbose_update = isatty(2);
213 o.update = 1;
214 o.head_idx = -1;
215 o.src_index = r->index;
216 o.dst_index = r->index;
217 o.skip_sparse_checkout = 0;
218 o.pl = pl;
220 setup_work_tree();
222 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
224 setup_unpack_trees_porcelain(&o, "sparse-checkout");
225 result = update_sparsity(&o);
226 clear_unpack_trees_porcelain(&o);
228 if (result == UPDATE_SPARSITY_WARNINGS)
230 * We don't do any special handling of warnings from untracked
231 * files in the way or dirty entries that can't be removed.
233 result = UPDATE_SPARSITY_SUCCESS;
234 if (result == UPDATE_SPARSITY_SUCCESS)
235 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
236 else
237 rollback_lock_file(&lock_file);
239 clean_tracked_sparse_directories(r);
241 r->index->sparse_checkout_patterns = NULL;
242 return result;
245 static char *escaped_pattern(char *pattern)
247 char *p = pattern;
248 struct strbuf final = STRBUF_INIT;
250 while (*p) {
251 if (is_glob_special(*p))
252 strbuf_addch(&final, '\\');
254 strbuf_addch(&final, *p);
255 p++;
258 return strbuf_detach(&final, NULL);
261 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
263 int i;
264 struct pattern_entry *pe;
265 struct hashmap_iter iter;
266 struct string_list sl = STRING_LIST_INIT_DUP;
267 struct strbuf parent_pattern = STRBUF_INIT;
269 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
270 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
271 continue;
273 if (!hashmap_contains_parent(&pl->recursive_hashmap,
274 pe->pattern,
275 &parent_pattern))
276 string_list_insert(&sl, pe->pattern);
279 string_list_sort(&sl);
280 string_list_remove_duplicates(&sl, 0);
282 fprintf(fp, "/*\n!/*/\n");
284 for (i = 0; i < sl.nr; i++) {
285 char *pattern = escaped_pattern(sl.items[i].string);
287 if (strlen(pattern))
288 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
289 free(pattern);
292 string_list_clear(&sl, 0);
294 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
295 if (!hashmap_contains_parent(&pl->recursive_hashmap,
296 pe->pattern,
297 &parent_pattern))
298 string_list_insert(&sl, pe->pattern);
301 strbuf_release(&parent_pattern);
303 string_list_sort(&sl);
304 string_list_remove_duplicates(&sl, 0);
306 for (i = 0; i < sl.nr; i++) {
307 char *pattern = escaped_pattern(sl.items[i].string);
308 fprintf(fp, "%s/\n", pattern);
309 free(pattern);
313 static int write_patterns_and_update(struct pattern_list *pl)
315 char *sparse_filename;
316 FILE *fp;
317 int fd;
318 struct lock_file lk = LOCK_INIT;
319 int result;
321 sparse_filename = get_sparse_checkout_filename();
323 if (safe_create_leading_directories(sparse_filename))
324 die(_("failed to create directory for sparse-checkout file"));
326 fd = hold_lock_file_for_update(&lk, sparse_filename,
327 LOCK_DIE_ON_ERROR);
329 result = update_working_directory(pl);
330 if (result) {
331 rollback_lock_file(&lk);
332 free(sparse_filename);
333 clear_pattern_list(pl);
334 update_working_directory(NULL);
335 return result;
338 fp = xfdopen(fd, "w");
340 if (core_sparse_checkout_cone)
341 write_cone_to_file(fp, pl);
342 else
343 write_patterns_to_file(fp, pl);
345 fflush(fp);
346 commit_lock_file(&lk);
348 free(sparse_filename);
349 clear_pattern_list(pl);
351 return 0;
354 enum sparse_checkout_mode {
355 MODE_NO_PATTERNS = 0,
356 MODE_ALL_PATTERNS = 1,
357 MODE_CONE_PATTERNS = 2,
360 static int set_config(enum sparse_checkout_mode mode)
362 const char *config_path;
364 if (upgrade_repository_format(1) < 0)
365 die(_("unable to upgrade repository format to enable worktreeConfig"));
366 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
367 error(_("failed to set extensions.worktreeConfig setting"));
368 return 1;
371 config_path = git_path("config.worktree");
372 git_config_set_in_file_gently(config_path,
373 "core.sparseCheckout",
374 mode ? "true" : NULL);
376 git_config_set_in_file_gently(config_path,
377 "core.sparseCheckoutCone",
378 mode == MODE_CONE_PATTERNS ? "true" : NULL);
380 if (mode == MODE_NO_PATTERNS)
381 set_sparse_index_config(the_repository, 0);
383 return 0;
386 static int update_modes(int *cone_mode, int *sparse_index)
388 int mode, record_mode;
390 /* Determine if we need to record the mode; ensure sparse checkout on */
391 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
393 /* If not specified, use previous definition of cone mode */
394 if (*cone_mode == -1 && core_apply_sparse_checkout)
395 *cone_mode = core_sparse_checkout_cone;
397 /* Set cone/non-cone mode appropriately */
398 core_apply_sparse_checkout = 1;
399 if (*cone_mode == 1) {
400 mode = MODE_CONE_PATTERNS;
401 core_sparse_checkout_cone = 1;
402 } else {
403 mode = MODE_ALL_PATTERNS;
405 if (record_mode && set_config(mode))
406 return 1;
408 /* Set sparse-index/non-sparse-index mode if specified */
409 if (*sparse_index >= 0) {
410 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
411 die(_("failed to modify sparse-index config"));
413 /* force an index rewrite */
414 repo_read_index(the_repository);
415 the_repository->index->updated_workdir = 1;
418 return 0;
421 static char const * const builtin_sparse_checkout_init_usage[] = {
422 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
423 NULL
426 static struct sparse_checkout_init_opts {
427 int cone_mode;
428 int sparse_index;
429 } init_opts;
431 static int sparse_checkout_init(int argc, const char **argv)
433 struct pattern_list pl;
434 char *sparse_filename;
435 int res;
436 struct object_id oid;
437 struct strbuf pattern = STRBUF_INIT;
439 static struct option builtin_sparse_checkout_init_options[] = {
440 OPT_BOOL(0, "cone", &init_opts.cone_mode,
441 N_("initialize the sparse-checkout in cone mode")),
442 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
443 N_("toggle the use of a sparse index")),
444 OPT_END(),
447 repo_read_index(the_repository);
449 init_opts.cone_mode = -1;
450 init_opts.sparse_index = -1;
452 argc = parse_options(argc, argv, NULL,
453 builtin_sparse_checkout_init_options,
454 builtin_sparse_checkout_init_usage, 0);
456 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
457 return 1;
459 memset(&pl, 0, sizeof(pl));
461 sparse_filename = get_sparse_checkout_filename();
462 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
464 /* If we already have a sparse-checkout file, use it. */
465 if (res >= 0) {
466 free(sparse_filename);
467 return update_working_directory(NULL);
470 if (get_oid("HEAD", &oid)) {
471 FILE *fp;
473 /* assume we are in a fresh repo, but update the sparse-checkout file */
474 fp = xfopen(sparse_filename, "w");
475 if (!fp)
476 die(_("failed to open '%s'"), sparse_filename);
478 free(sparse_filename);
479 fprintf(fp, "/*\n!/*/\n");
480 fclose(fp);
481 return 0;
484 strbuf_addstr(&pattern, "/*");
485 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
486 strbuf_addstr(&pattern, "!/*/");
487 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
488 pl.use_cone_patterns = init_opts.cone_mode;
490 return write_patterns_and_update(&pl);
493 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
495 struct pattern_entry *e = xmalloc(sizeof(*e));
496 e->patternlen = path->len;
497 e->pattern = strbuf_detach(path, NULL);
498 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
500 hashmap_add(&pl->recursive_hashmap, &e->ent);
502 while (e->patternlen) {
503 char *slash = strrchr(e->pattern, '/');
504 char *oldpattern = e->pattern;
505 size_t newlen;
507 if (!slash || slash == e->pattern)
508 break;
510 newlen = slash - e->pattern;
511 e = xmalloc(sizeof(struct pattern_entry));
512 e->patternlen = newlen;
513 e->pattern = xstrndup(oldpattern, newlen);
514 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
516 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
517 hashmap_add(&pl->parent_hashmap, &e->ent);
521 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
523 strbuf_trim(line);
525 strbuf_trim_trailing_dir_sep(line);
527 if (strbuf_normalize_path(line))
528 die(_("could not normalize path %s"), line->buf);
530 if (!line->len)
531 return;
533 if (line->buf[0] != '/')
534 strbuf_insertstr(line, 0, "/");
536 insert_recursive_pattern(pl, line);
539 static void add_patterns_from_input(struct pattern_list *pl,
540 int argc, const char **argv,
541 int use_stdin)
543 int i;
544 if (core_sparse_checkout_cone) {
545 struct strbuf line = STRBUF_INIT;
547 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
548 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
549 pl->use_cone_patterns = 1;
551 if (use_stdin) {
552 struct strbuf unquoted = STRBUF_INIT;
553 while (!strbuf_getline(&line, stdin)) {
554 if (line.buf[0] == '"') {
555 strbuf_reset(&unquoted);
556 if (unquote_c_style(&unquoted, line.buf, NULL))
557 die(_("unable to unquote C-style string '%s'"),
558 line.buf);
560 strbuf_swap(&unquoted, &line);
563 strbuf_to_cone_pattern(&line, pl);
566 strbuf_release(&unquoted);
567 } else {
568 for (i = 0; i < argc; i++) {
569 strbuf_setlen(&line, 0);
570 strbuf_addstr(&line, argv[i]);
571 strbuf_to_cone_pattern(&line, pl);
574 } else {
575 if (use_stdin) {
576 struct strbuf line = STRBUF_INIT;
578 while (!strbuf_getline(&line, stdin)) {
579 size_t len;
580 char *buf = strbuf_detach(&line, &len);
581 add_pattern(buf, empty_base, 0, pl, 0);
583 } else {
584 for (i = 0; i < argc; i++)
585 add_pattern(argv[i], empty_base, 0, pl, 0);
590 enum modify_type {
591 REPLACE,
592 ADD,
595 static void add_patterns_cone_mode(int argc, const char **argv,
596 struct pattern_list *pl,
597 int use_stdin)
599 struct strbuf buffer = STRBUF_INIT;
600 struct pattern_entry *pe;
601 struct hashmap_iter iter;
602 struct pattern_list existing;
603 char *sparse_filename = get_sparse_checkout_filename();
605 add_patterns_from_input(pl, argc, argv, use_stdin);
607 memset(&existing, 0, sizeof(existing));
608 existing.use_cone_patterns = core_sparse_checkout_cone;
610 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
611 &existing, NULL, 0))
612 die(_("unable to load existing sparse-checkout patterns"));
613 free(sparse_filename);
615 if (!existing.use_cone_patterns)
616 die(_("existing sparse-checkout patterns do not use cone mode"));
618 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
619 if (!hashmap_contains_parent(&pl->recursive_hashmap,
620 pe->pattern, &buffer) ||
621 !hashmap_contains_parent(&pl->parent_hashmap,
622 pe->pattern, &buffer)) {
623 strbuf_reset(&buffer);
624 strbuf_addstr(&buffer, pe->pattern);
625 insert_recursive_pattern(pl, &buffer);
629 clear_pattern_list(&existing);
630 strbuf_release(&buffer);
633 static void add_patterns_literal(int argc, const char **argv,
634 struct pattern_list *pl,
635 int use_stdin)
637 char *sparse_filename = get_sparse_checkout_filename();
638 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
639 pl, NULL, 0))
640 die(_("unable to load existing sparse-checkout patterns"));
641 free(sparse_filename);
642 add_patterns_from_input(pl, argc, argv, use_stdin);
645 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
646 enum modify_type m)
648 int result;
649 int changed_config = 0;
650 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
652 switch (m) {
653 case ADD:
654 if (core_sparse_checkout_cone)
655 add_patterns_cone_mode(argc, argv, pl, use_stdin);
656 else
657 add_patterns_literal(argc, argv, pl, use_stdin);
658 break;
660 case REPLACE:
661 add_patterns_from_input(pl, argc, argv, use_stdin);
662 break;
665 if (!core_apply_sparse_checkout) {
666 set_config(MODE_ALL_PATTERNS);
667 core_apply_sparse_checkout = 1;
668 changed_config = 1;
671 result = write_patterns_and_update(pl);
673 if (result && changed_config)
674 set_config(MODE_NO_PATTERNS);
676 clear_pattern_list(pl);
677 free(pl);
678 return result;
681 static char const * const builtin_sparse_checkout_add_usage[] = {
682 N_("git sparse-checkout add (--stdin | <patterns>)"),
683 NULL
686 static struct sparse_checkout_add_opts {
687 int use_stdin;
688 } add_opts;
690 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
692 static struct option builtin_sparse_checkout_add_options[] = {
693 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
694 N_("read patterns from standard in")),
695 OPT_END(),
698 if (!core_apply_sparse_checkout)
699 die(_("no sparse-checkout to add to"));
701 repo_read_index(the_repository);
703 argc = parse_options(argc, argv, prefix,
704 builtin_sparse_checkout_add_options,
705 builtin_sparse_checkout_add_usage,
706 PARSE_OPT_KEEP_UNKNOWN);
708 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
711 static char const * const builtin_sparse_checkout_set_usage[] = {
712 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | <patterns>)"),
713 NULL
716 static struct sparse_checkout_set_opts {
717 int cone_mode;
718 int sparse_index;
719 int use_stdin;
720 } set_opts;
722 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
724 int default_patterns_nr = 2;
725 const char *default_patterns[] = {"/*", "!/*/", NULL};
727 static struct option builtin_sparse_checkout_set_options[] = {
728 OPT_BOOL(0, "cone", &set_opts.cone_mode,
729 N_("initialize the sparse-checkout in cone mode")),
730 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
731 N_("toggle the use of a sparse index")),
732 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
733 N_("read patterns from standard in"),
734 PARSE_OPT_NONEG),
735 OPT_END(),
738 repo_read_index(the_repository);
740 set_opts.cone_mode = -1;
741 set_opts.sparse_index = -1;
743 argc = parse_options(argc, argv, prefix,
744 builtin_sparse_checkout_set_options,
745 builtin_sparse_checkout_set_usage,
746 PARSE_OPT_KEEP_UNKNOWN);
748 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
749 return 1;
752 * Cone mode automatically specifies the toplevel directory. For
753 * non-cone mode, if nothing is specified, manually select just the
754 * top-level directory (much as 'init' would do).
756 if (!core_sparse_checkout_cone && argc == 0) {
757 argv = default_patterns;
758 argc = default_patterns_nr;
761 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
764 static char const * const builtin_sparse_checkout_reapply_usage[] = {
765 N_("git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]"),
766 NULL
769 static struct sparse_checkout_reapply_opts {
770 int cone_mode;
771 int sparse_index;
772 } reapply_opts;
774 static int sparse_checkout_reapply(int argc, const char **argv)
776 static struct option builtin_sparse_checkout_reapply_options[] = {
777 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
778 N_("initialize the sparse-checkout in cone mode")),
779 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
780 N_("toggle the use of a sparse index")),
781 OPT_END(),
784 if (!core_apply_sparse_checkout)
785 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
787 argc = parse_options(argc, argv, NULL,
788 builtin_sparse_checkout_reapply_options,
789 builtin_sparse_checkout_reapply_usage, 0);
791 repo_read_index(the_repository);
793 reapply_opts.cone_mode = -1;
794 reapply_opts.sparse_index = -1;
796 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
797 return 1;
799 return update_working_directory(NULL);
802 static char const * const builtin_sparse_checkout_disable_usage[] = {
803 N_("git sparse-checkout disable"),
804 NULL
807 static int sparse_checkout_disable(int argc, const char **argv)
809 static struct option builtin_sparse_checkout_disable_options[] = {
810 OPT_END(),
812 struct pattern_list pl;
813 struct strbuf match_all = STRBUF_INIT;
816 * We do not exit early if !core_apply_sparse_checkout; due to the
817 * ability for users to manually muck things up between
818 * direct editing of .git/info/sparse-checkout
819 * running read-tree -m u HEAD or update-index --skip-worktree
820 * direct toggling of config options
821 * users might end up with an index with SKIP_WORKTREE bit set on
822 * some files and not know how to undo it. So, here we just
823 * forcibly return to a dense checkout regardless of initial state.
826 argc = parse_options(argc, argv, NULL,
827 builtin_sparse_checkout_disable_options,
828 builtin_sparse_checkout_disable_usage, 0);
830 repo_read_index(the_repository);
832 memset(&pl, 0, sizeof(pl));
833 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
834 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
835 pl.use_cone_patterns = 0;
836 core_apply_sparse_checkout = 1;
838 strbuf_addstr(&match_all, "/*");
839 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
841 prepare_repo_settings(the_repository);
842 the_repository->settings.sparse_index = 0;
844 if (update_working_directory(&pl))
845 die(_("error while refreshing working directory"));
847 clear_pattern_list(&pl);
848 return set_config(MODE_NO_PATTERNS);
851 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
853 static struct option builtin_sparse_checkout_options[] = {
854 OPT_END(),
857 if (argc == 2 && !strcmp(argv[1], "-h"))
858 usage_with_options(builtin_sparse_checkout_usage,
859 builtin_sparse_checkout_options);
861 argc = parse_options(argc, argv, prefix,
862 builtin_sparse_checkout_options,
863 builtin_sparse_checkout_usage,
864 PARSE_OPT_STOP_AT_NON_OPTION);
866 git_config(git_default_config, NULL);
868 if (argc > 0) {
869 if (!strcmp(argv[0], "list"))
870 return sparse_checkout_list(argc, argv);
871 if (!strcmp(argv[0], "init"))
872 return sparse_checkout_init(argc, argv);
873 if (!strcmp(argv[0], "set"))
874 return sparse_checkout_set(argc, argv, prefix);
875 if (!strcmp(argv[0], "add"))
876 return sparse_checkout_add(argc, argv, prefix);
877 if (!strcmp(argv[0], "reapply"))
878 return sparse_checkout_reapply(argc, argv);
879 if (!strcmp(argv[0], "disable"))
880 return sparse_checkout_disable(argc, argv);
883 usage_with_options(builtin_sparse_checkout_usage,
884 builtin_sparse_checkout_options);