5 #include "parse-options.h"
7 #include "repository.h"
8 #include "run-command.h"
10 #include "string-list.h"
11 #include "cache-tree.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 #include "wt-status.h"
17 #include "sparse-index.h"
20 static const char *empty_base
= "";
22 static char const * const builtin_sparse_checkout_usage
[] = {
23 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
27 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
31 for (i
= 0; i
< pl
->nr
; i
++) {
32 struct path_pattern
*p
= pl
->patterns
[i
];
34 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
37 fprintf(fp
, "%s", p
->pattern
);
39 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
46 static char const * const builtin_sparse_checkout_list_usage
[] = {
47 "git sparse-checkout list",
51 static int sparse_checkout_list(int argc
, const char **argv
)
53 static struct option builtin_sparse_checkout_list_options
[] = {
56 struct pattern_list pl
;
57 char *sparse_filename
;
60 if (!core_apply_sparse_checkout
)
61 die(_("this worktree is not sparse"));
63 argc
= parse_options(argc
, argv
, NULL
,
64 builtin_sparse_checkout_list_options
,
65 builtin_sparse_checkout_list_usage
, 0);
67 memset(&pl
, 0, sizeof(pl
));
69 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
71 sparse_filename
= get_sparse_checkout_filename();
72 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
73 free(sparse_filename
);
76 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
80 if (pl
.use_cone_patterns
) {
82 struct pattern_entry
*pe
;
83 struct hashmap_iter iter
;
84 struct string_list sl
= STRING_LIST_INIT_DUP
;
86 hashmap_for_each_entry(&pl
.recursive_hashmap
, &iter
, pe
, ent
) {
87 /* pe->pattern starts with "/", skip it */
88 string_list_insert(&sl
, pe
->pattern
+ 1);
91 string_list_sort(&sl
);
93 for (i
= 0; i
< sl
.nr
; i
++) {
94 quote_c_style(sl
.items
[i
].string
, NULL
, stdout
, 0);
101 write_patterns_to_file(stdout
, &pl
);
102 clear_pattern_list(&pl
);
107 static void clean_tracked_sparse_directories(struct repository
*r
)
110 struct strbuf path
= STRBUF_INIT
;
112 struct string_list_item
*item
;
113 struct string_list sparse_dirs
= STRING_LIST_INIT_DUP
;
116 * If we are not using cone mode patterns, then we cannot
117 * delete directories outside of the sparse cone.
119 if (!r
|| !r
->index
|| !r
->worktree
)
121 if (init_sparse_checkout_patterns(r
->index
) ||
122 !r
->index
->sparse_checkout_patterns
->use_cone_patterns
)
126 * Use the sparse index as a data structure to assist finding
127 * directories that are safe to delete. This conversion to a
128 * sparse index will not delete directories that contain
129 * conflicted entries or submodules.
131 if (!r
->index
->sparse_index
) {
133 * If something, such as a merge conflict or other concern,
134 * prevents us from converting to a sparse index, then do
135 * not try deleting files.
137 if (convert_to_sparse(r
->index
, SPARSE_INDEX_MEMORY_ONLY
))
142 strbuf_addstr(&path
, r
->worktree
);
143 strbuf_complete(&path
, '/');
147 * Collect directories that have gone out of scope but also
148 * exist on disk, so there is some work to be done. We need to
149 * store the entries in a list before exploring, since that might
150 * expand the sparse-index again.
152 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
153 struct cache_entry
*ce
= r
->index
->cache
[i
];
155 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
156 repo_file_exists(r
, ce
->name
))
157 string_list_append(&sparse_dirs
, ce
->name
);
160 for_each_string_list_item(item
, &sparse_dirs
) {
161 struct dir_struct dir
= DIR_INIT
;
162 struct pathspec p
= { 0 };
163 struct strvec s
= STRVEC_INIT
;
165 strbuf_setlen(&path
, pathlen
);
166 strbuf_addstr(&path
, item
->string
);
168 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
170 setup_standard_excludes(&dir
);
171 strvec_push(&s
, path
.buf
);
173 parse_pathspec(&p
, PATHSPEC_GLOB
, 0, NULL
, s
.v
);
174 fill_directory(&dir
, r
->index
, &p
);
177 warning(_("directory '%s' contains untracked files,"
178 " but is not in the sparse-checkout cone"),
180 } else if (remove_dir_recursively(&path
, 0)) {
182 * Removal is "best effort". If something blocks
183 * the deletion, then continue with a warning.
185 warning(_("failed to remove directory '%s'"),
194 string_list_clear(&sparse_dirs
, 0);
195 strbuf_release(&path
);
198 ensure_full_index(r
->index
);
201 static int update_working_directory(struct pattern_list
*pl
)
203 enum update_sparsity_result result
;
204 struct unpack_trees_options o
;
205 struct lock_file lock_file
= LOCK_INIT
;
206 struct repository
*r
= the_repository
;
208 /* If no branch has been checked out, there are no updates to make. */
209 if (is_index_unborn(r
->index
))
210 return UPDATE_SPARSITY_SUCCESS
;
212 r
->index
->sparse_checkout_patterns
= pl
;
214 memset(&o
, 0, sizeof(o
));
215 o
.verbose_update
= isatty(2);
218 o
.src_index
= r
->index
;
219 o
.dst_index
= r
->index
;
220 o
.skip_sparse_checkout
= 0;
225 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
227 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
228 result
= update_sparsity(&o
);
229 clear_unpack_trees_porcelain(&o
);
231 if (result
== UPDATE_SPARSITY_WARNINGS
)
233 * We don't do any special handling of warnings from untracked
234 * files in the way or dirty entries that can't be removed.
236 result
= UPDATE_SPARSITY_SUCCESS
;
237 if (result
== UPDATE_SPARSITY_SUCCESS
)
238 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
240 rollback_lock_file(&lock_file
);
242 clean_tracked_sparse_directories(r
);
244 r
->index
->sparse_checkout_patterns
= NULL
;
248 static char *escaped_pattern(char *pattern
)
251 struct strbuf final
= STRBUF_INIT
;
254 if (is_glob_special(*p
))
255 strbuf_addch(&final
, '\\');
257 strbuf_addch(&final
, *p
);
261 return strbuf_detach(&final
, NULL
);
264 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
267 struct pattern_entry
*pe
;
268 struct hashmap_iter iter
;
269 struct string_list sl
= STRING_LIST_INIT_DUP
;
270 struct strbuf parent_pattern
= STRBUF_INIT
;
272 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
273 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
276 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
279 string_list_insert(&sl
, pe
->pattern
);
282 string_list_sort(&sl
);
283 string_list_remove_duplicates(&sl
, 0);
285 fprintf(fp
, "/*\n!/*/\n");
287 for (i
= 0; i
< sl
.nr
; i
++) {
288 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
291 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
295 string_list_clear(&sl
, 0);
297 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
298 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
301 string_list_insert(&sl
, pe
->pattern
);
304 strbuf_release(&parent_pattern
);
306 string_list_sort(&sl
);
307 string_list_remove_duplicates(&sl
, 0);
309 for (i
= 0; i
< sl
.nr
; i
++) {
310 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
311 fprintf(fp
, "%s/\n", pattern
);
316 static int write_patterns_and_update(struct pattern_list
*pl
)
318 char *sparse_filename
;
321 struct lock_file lk
= LOCK_INIT
;
324 sparse_filename
= get_sparse_checkout_filename();
326 if (safe_create_leading_directories(sparse_filename
))
327 die(_("failed to create directory for sparse-checkout file"));
329 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
331 free(sparse_filename
);
333 result
= update_working_directory(pl
);
335 rollback_lock_file(&lk
);
336 clear_pattern_list(pl
);
337 update_working_directory(NULL
);
341 fp
= xfdopen(fd
, "w");
343 if (core_sparse_checkout_cone
)
344 write_cone_to_file(fp
, pl
);
346 write_patterns_to_file(fp
, pl
);
349 commit_lock_file(&lk
);
351 clear_pattern_list(pl
);
356 enum sparse_checkout_mode
{
357 MODE_NO_PATTERNS
= 0,
358 MODE_ALL_PATTERNS
= 1,
359 MODE_CONE_PATTERNS
= 2,
362 static int set_config(enum sparse_checkout_mode mode
)
364 /* Update to use worktree config, if not already. */
365 if (init_worktree_config(the_repository
)) {
366 error(_("failed to initialize worktree config"));
370 if (repo_config_set_worktree_gently(the_repository
,
371 "core.sparseCheckout",
372 mode
? "true" : "false") ||
373 repo_config_set_worktree_gently(the_repository
,
374 "core.sparseCheckoutCone",
375 mode
== MODE_CONE_PATTERNS
?
379 if (mode
== MODE_NO_PATTERNS
)
380 return set_sparse_index_config(the_repository
, 0);
385 static int update_modes(int *cone_mode
, int *sparse_index
)
387 int mode
, record_mode
;
389 /* Determine if we need to record the mode; ensure sparse checkout on */
390 record_mode
= (*cone_mode
!= -1) || !core_apply_sparse_checkout
;
392 /* If not specified, use previous definition of cone mode */
393 if (*cone_mode
== -1 && core_apply_sparse_checkout
)
394 *cone_mode
= core_sparse_checkout_cone
;
396 /* Set cone/non-cone mode appropriately */
397 core_apply_sparse_checkout
= 1;
398 if (*cone_mode
== 1) {
399 mode
= MODE_CONE_PATTERNS
;
400 core_sparse_checkout_cone
= 1;
402 mode
= MODE_ALL_PATTERNS
;
403 core_sparse_checkout_cone
= 0;
405 if (record_mode
&& set_config(mode
))
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;
421 static char const * const builtin_sparse_checkout_init_usage
[] = {
422 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
426 static struct sparse_checkout_init_opts
{
431 static int sparse_checkout_init(int argc
, const char **argv
)
433 struct pattern_list pl
;
434 char *sparse_filename
;
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")),
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
))
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. */
466 free(sparse_filename
);
467 return update_working_directory(NULL
);
470 if (get_oid("HEAD", &oid
)) {
473 /* assume we are in a fresh repo, but update the sparse-checkout file */
474 if (safe_create_leading_directories(sparse_filename
))
475 die(_("unable to create leading directories of %s"),
477 fp
= xfopen(sparse_filename
, "w");
479 die(_("failed to open '%s'"), sparse_filename
);
481 free(sparse_filename
);
482 fprintf(fp
, "/*\n!/*/\n");
487 strbuf_addstr(&pattern
, "/*");
488 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
489 strbuf_addstr(&pattern
, "!/*/");
490 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
491 pl
.use_cone_patterns
= init_opts
.cone_mode
;
493 return write_patterns_and_update(&pl
);
496 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
498 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
499 e
->patternlen
= path
->len
;
500 e
->pattern
= strbuf_detach(path
, NULL
);
501 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
503 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
505 while (e
->patternlen
) {
506 char *slash
= strrchr(e
->pattern
, '/');
507 char *oldpattern
= e
->pattern
;
510 if (!slash
|| slash
== e
->pattern
)
513 newlen
= slash
- e
->pattern
;
514 e
= xmalloc(sizeof(struct pattern_entry
));
515 e
->patternlen
= newlen
;
516 e
->pattern
= xstrndup(oldpattern
, newlen
);
517 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
519 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
520 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
524 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
528 strbuf_trim_trailing_dir_sep(line
);
530 if (strbuf_normalize_path(line
))
531 die(_("could not normalize path %s"), line
->buf
);
536 if (line
->buf
[0] != '/')
537 strbuf_insertstr(line
, 0, "/");
539 insert_recursive_pattern(pl
, line
);
542 static void add_patterns_from_input(struct pattern_list
*pl
,
543 int argc
, const char **argv
,
547 if (core_sparse_checkout_cone
) {
548 struct strbuf line
= STRBUF_INIT
;
550 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
551 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
552 pl
->use_cone_patterns
= 1;
555 struct strbuf unquoted
= STRBUF_INIT
;
556 while (!strbuf_getline(&line
, stdin
)) {
557 if (line
.buf
[0] == '"') {
558 strbuf_reset(&unquoted
);
559 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
560 die(_("unable to unquote C-style string '%s'"),
563 strbuf_swap(&unquoted
, &line
);
566 strbuf_to_cone_pattern(&line
, pl
);
569 strbuf_release(&unquoted
);
571 for (i
= 0; i
< argc
; i
++) {
572 strbuf_setlen(&line
, 0);
573 strbuf_addstr(&line
, argv
[i
]);
574 strbuf_to_cone_pattern(&line
, pl
);
579 struct strbuf line
= STRBUF_INIT
;
581 while (!strbuf_getline(&line
, stdin
)) {
583 char *buf
= strbuf_detach(&line
, &len
);
584 add_pattern(buf
, empty_base
, 0, pl
, 0);
587 for (i
= 0; i
< argc
; i
++)
588 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
598 static void add_patterns_cone_mode(int argc
, const char **argv
,
599 struct pattern_list
*pl
,
602 struct strbuf buffer
= STRBUF_INIT
;
603 struct pattern_entry
*pe
;
604 struct hashmap_iter iter
;
605 struct pattern_list existing
;
606 char *sparse_filename
= get_sparse_checkout_filename();
608 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
610 memset(&existing
, 0, sizeof(existing
));
611 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
613 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
615 die(_("unable to load existing sparse-checkout patterns"));
616 free(sparse_filename
);
618 if (!existing
.use_cone_patterns
)
619 die(_("existing sparse-checkout patterns do not use cone mode"));
621 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
622 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
623 pe
->pattern
, &buffer
) ||
624 !hashmap_contains_parent(&pl
->parent_hashmap
,
625 pe
->pattern
, &buffer
)) {
626 strbuf_reset(&buffer
);
627 strbuf_addstr(&buffer
, pe
->pattern
);
628 insert_recursive_pattern(pl
, &buffer
);
632 clear_pattern_list(&existing
);
633 strbuf_release(&buffer
);
636 static void add_patterns_literal(int argc
, const char **argv
,
637 struct pattern_list
*pl
,
640 char *sparse_filename
= get_sparse_checkout_filename();
641 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
643 die(_("unable to load existing sparse-checkout patterns"));
644 free(sparse_filename
);
645 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
648 static int modify_pattern_list(int argc
, const char **argv
, int use_stdin
,
652 int changed_config
= 0;
653 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
657 if (core_sparse_checkout_cone
)
658 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
660 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
664 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
668 if (!core_apply_sparse_checkout
) {
669 set_config(MODE_ALL_PATTERNS
);
670 core_apply_sparse_checkout
= 1;
674 result
= write_patterns_and_update(pl
);
676 if (result
&& changed_config
)
677 set_config(MODE_NO_PATTERNS
);
679 clear_pattern_list(pl
);
684 static void sanitize_paths(int argc
, const char **argv
,
685 const char *prefix
, int skip_checks
)
692 if (prefix
&& *prefix
&& core_sparse_checkout_cone
) {
694 * The args are not pathspecs, so unfortunately we
695 * cannot imitate how cmd_add() uses parse_pathspec().
697 int prefix_len
= strlen(prefix
);
699 for (i
= 0; i
< argc
; i
++)
700 argv
[i
] = prefix_path(prefix
, prefix_len
, argv
[i
]);
706 if (prefix
&& *prefix
&& !core_sparse_checkout_cone
)
707 die(_("please run from the toplevel directory in non-cone mode"));
709 if (core_sparse_checkout_cone
) {
710 for (i
= 0; i
< argc
; i
++) {
711 if (argv
[i
][0] == '/')
712 die(_("specify directories rather than patterns (no leading slash)"));
713 if (argv
[i
][0] == '!')
714 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
715 if (strpbrk(argv
[i
], "*?[]"))
716 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
720 for (i
= 0; i
< argc
; i
++) {
721 struct cache_entry
*ce
;
722 struct index_state
*index
= the_repository
->index
;
723 int pos
= index_name_pos(index
, argv
[i
], strlen(argv
[i
]));
727 ce
= index
->cache
[pos
];
728 if (S_ISSPARSEDIR(ce
->ce_mode
))
731 if (core_sparse_checkout_cone
)
732 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv
[i
]);
734 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
]);
738 static char const * const builtin_sparse_checkout_add_usage
[] = {
739 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
743 static struct sparse_checkout_add_opts
{
748 static int sparse_checkout_add(int argc
, const char **argv
, const char *prefix
)
750 static struct option builtin_sparse_checkout_add_options
[] = {
751 OPT_BOOL_F(0, "skip-checks", &add_opts
.skip_checks
,
752 N_("skip some sanity checks on the given paths that might give false positives"),
754 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
755 N_("read patterns from standard in")),
759 if (!core_apply_sparse_checkout
)
760 die(_("no sparse-checkout to add to"));
762 repo_read_index(the_repository
);
764 argc
= parse_options(argc
, argv
, prefix
,
765 builtin_sparse_checkout_add_options
,
766 builtin_sparse_checkout_add_usage
,
767 PARSE_OPT_KEEP_UNKNOWN
);
769 sanitize_paths(argc
, argv
, prefix
, add_opts
.skip_checks
);
771 return modify_pattern_list(argc
, argv
, add_opts
.use_stdin
, ADD
);
774 static char const * const builtin_sparse_checkout_set_usage
[] = {
775 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
779 static struct sparse_checkout_set_opts
{
786 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
788 int default_patterns_nr
= 2;
789 const char *default_patterns
[] = {"/*", "!/*/", NULL
};
791 static struct option builtin_sparse_checkout_set_options
[] = {
792 OPT_BOOL(0, "cone", &set_opts
.cone_mode
,
793 N_("initialize the sparse-checkout in cone mode")),
794 OPT_BOOL(0, "sparse-index", &set_opts
.sparse_index
,
795 N_("toggle the use of a sparse index")),
796 OPT_BOOL_F(0, "skip-checks", &set_opts
.skip_checks
,
797 N_("skip some sanity checks on the given paths that might give false positives"),
799 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
800 N_("read patterns from standard in"),
805 repo_read_index(the_repository
);
807 set_opts
.cone_mode
= -1;
808 set_opts
.sparse_index
= -1;
810 argc
= parse_options(argc
, argv
, prefix
,
811 builtin_sparse_checkout_set_options
,
812 builtin_sparse_checkout_set_usage
,
813 PARSE_OPT_KEEP_UNKNOWN
);
815 if (update_modes(&set_opts
.cone_mode
, &set_opts
.sparse_index
))
819 * Cone mode automatically specifies the toplevel directory. For
820 * non-cone mode, if nothing is specified, manually select just the
821 * top-level directory (much as 'init' would do).
823 if (!core_sparse_checkout_cone
&& argc
== 0) {
824 argv
= default_patterns
;
825 argc
= default_patterns_nr
;
827 sanitize_paths(argc
, argv
, prefix
, set_opts
.skip_checks
);
830 return modify_pattern_list(argc
, argv
, set_opts
.use_stdin
, REPLACE
);
833 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
834 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
838 static struct sparse_checkout_reapply_opts
{
843 static int sparse_checkout_reapply(int argc
, const char **argv
)
845 static struct option builtin_sparse_checkout_reapply_options
[] = {
846 OPT_BOOL(0, "cone", &reapply_opts
.cone_mode
,
847 N_("initialize the sparse-checkout in cone mode")),
848 OPT_BOOL(0, "sparse-index", &reapply_opts
.sparse_index
,
849 N_("toggle the use of a sparse index")),
853 if (!core_apply_sparse_checkout
)
854 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
856 reapply_opts
.cone_mode
= -1;
857 reapply_opts
.sparse_index
= -1;
859 argc
= parse_options(argc
, argv
, NULL
,
860 builtin_sparse_checkout_reapply_options
,
861 builtin_sparse_checkout_reapply_usage
, 0);
863 repo_read_index(the_repository
);
865 if (update_modes(&reapply_opts
.cone_mode
, &reapply_opts
.sparse_index
))
868 return update_working_directory(NULL
);
871 static char const * const builtin_sparse_checkout_disable_usage
[] = {
872 "git sparse-checkout disable",
876 static int sparse_checkout_disable(int argc
, const char **argv
)
878 static struct option builtin_sparse_checkout_disable_options
[] = {
881 struct pattern_list pl
;
882 struct strbuf match_all
= STRBUF_INIT
;
885 * We do not exit early if !core_apply_sparse_checkout; due to the
886 * ability for users to manually muck things up between
887 * direct editing of .git/info/sparse-checkout
888 * running read-tree -m u HEAD or update-index --skip-worktree
889 * direct toggling of config options
890 * users might end up with an index with SKIP_WORKTREE bit set on
891 * some files and not know how to undo it. So, here we just
892 * forcibly return to a dense checkout regardless of initial state.
895 argc
= parse_options(argc
, argv
, NULL
,
896 builtin_sparse_checkout_disable_options
,
897 builtin_sparse_checkout_disable_usage
, 0);
899 repo_read_index(the_repository
);
901 memset(&pl
, 0, sizeof(pl
));
902 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
903 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
904 pl
.use_cone_patterns
= 0;
905 core_apply_sparse_checkout
= 1;
907 strbuf_addstr(&match_all
, "/*");
908 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
910 prepare_repo_settings(the_repository
);
911 the_repository
->settings
.sparse_index
= 0;
913 if (update_working_directory(&pl
))
914 die(_("error while refreshing working directory"));
916 clear_pattern_list(&pl
);
917 return set_config(MODE_NO_PATTERNS
);
920 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
922 static struct option builtin_sparse_checkout_options
[] = {
926 if (argc
== 2 && !strcmp(argv
[1], "-h"))
927 usage_with_options(builtin_sparse_checkout_usage
,
928 builtin_sparse_checkout_options
);
930 argc
= parse_options(argc
, argv
, prefix
,
931 builtin_sparse_checkout_options
,
932 builtin_sparse_checkout_usage
,
933 PARSE_OPT_STOP_AT_NON_OPTION
);
935 git_config(git_default_config
, NULL
);
938 if (!strcmp(argv
[0], "list"))
939 return sparse_checkout_list(argc
, argv
);
940 if (!strcmp(argv
[0], "init"))
941 return sparse_checkout_init(argc
, argv
);
942 if (!strcmp(argv
[0], "set"))
943 return sparse_checkout_set(argc
, argv
, prefix
);
944 if (!strcmp(argv
[0], "add"))
945 return sparse_checkout_add(argc
, argv
, prefix
);
946 if (!strcmp(argv
[0], "reapply"))
947 return sparse_checkout_reapply(argc
, argv
);
948 if (!strcmp(argv
[0], "disable"))
949 return sparse_checkout_disable(argc
, argv
);
952 usage_with_options(builtin_sparse_checkout_usage
,
953 builtin_sparse_checkout_options
);