5 #include "environment.h"
7 #include "parse-options.h"
9 #include "repository.h"
10 #include "run-command.h"
12 #include "string-list.h"
13 #include "cache-tree.h"
15 #include "resolve-undo.h"
16 #include "unpack-trees.h"
17 #include "wt-status.h"
20 #include "sparse-index.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>]"),
30 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
34 for (i
= 0; i
< pl
->nr
; i
++) {
35 struct path_pattern
*p
= pl
->patterns
[i
];
37 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
40 fprintf(fp
, "%s", p
->pattern
);
42 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
49 static char const * const builtin_sparse_checkout_list_usage
[] = {
50 "git sparse-checkout list",
54 static int sparse_checkout_list(int argc
, const char **argv
, const char *prefix
)
56 static struct option builtin_sparse_checkout_list_options
[] = {
59 struct pattern_list pl
;
60 char *sparse_filename
;
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
);
79 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
83 if (pl
.use_cone_patterns
) {
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);
104 write_patterns_to_file(stdout
, &pl
);
105 clear_pattern_list(&pl
);
110 static void clean_tracked_sparse_directories(struct repository
*r
)
113 struct strbuf path
= STRBUF_INIT
;
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
)
124 if (init_sparse_checkout_patterns(r
->index
) ||
125 !r
->index
->sparse_checkout_patterns
->use_cone_patterns
)
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
))
145 strbuf_addstr(&path
, r
->worktree
);
146 strbuf_complete(&path
, '/');
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
);
180 warning(_("directory '%s' contains untracked files,"
181 " but is not in the sparse-checkout cone"),
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'"),
197 string_list_clear(&sparse_dirs
, 0);
198 strbuf_release(&path
);
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);
221 o
.src_index
= r
->index
;
222 o
.dst_index
= r
->index
;
223 o
.skip_sparse_checkout
= 0;
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
);
242 rollback_lock_file(&lock_file
);
244 clean_tracked_sparse_directories(r
);
246 r
->index
->sparse_checkout_patterns
= NULL
;
250 static char *escaped_pattern(char *pattern
)
253 struct strbuf final
= STRBUF_INIT
;
256 if (is_glob_special(*p
))
257 strbuf_addch(&final
, '\\');
259 strbuf_addch(&final
, *p
);
263 return strbuf_detach(&final
, NULL
);
266 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
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
))
278 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
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
);
293 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, 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
,
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
);
318 static int write_patterns_and_update(struct pattern_list
*pl
)
320 char *sparse_filename
;
323 struct lock_file lk
= LOCK_INIT
;
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
,
333 free(sparse_filename
);
335 result
= update_working_directory(pl
);
337 rollback_lock_file(&lk
);
338 clear_pattern_list(pl
);
339 update_working_directory(NULL
);
343 fp
= xfdopen(fd
, "w");
345 if (core_sparse_checkout_cone
)
346 write_cone_to_file(fp
, pl
);
348 write_patterns_to_file(fp
, pl
);
351 commit_lock_file(&lk
);
353 clear_pattern_list(pl
);
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"));
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
?
381 if (mode
== MODE_NO_PATTERNS
)
382 return set_sparse_index_config(the_repository
, 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;
404 mode
= MODE_ALL_PATTERNS
;
405 core_sparse_checkout_cone
= 0;
407 if (record_mode
&& set_config(mode
))
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;
420 ensure_full_index(the_repository
->index
);
426 static char const * const builtin_sparse_checkout_init_usage
[] = {
427 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
431 static struct sparse_checkout_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
;
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")),
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
))
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. */
471 free(sparse_filename
);
472 return update_working_directory(NULL
);
475 if (get_oid("HEAD", &oid
)) {
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"),
482 fp
= xfopen(sparse_filename
, "w");
484 die(_("failed to open '%s'"), sparse_filename
);
486 free(sparse_filename
);
487 fprintf(fp
, "/*\n!/*/\n");
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
;
515 if (!slash
|| slash
== e
->pattern
)
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
)
533 strbuf_trim_trailing_dir_sep(line
);
535 if (strbuf_normalize_path(line
))
536 die(_("could not normalize path %s"), line
->buf
);
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
,
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;
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'"),
568 strbuf_swap(&unquoted
, &line
);
571 strbuf_to_cone_pattern(&line
, pl
);
574 strbuf_release(&unquoted
);
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
);
584 struct strbuf line
= STRBUF_INIT
;
586 while (!strbuf_getline(&line
, stdin
)) {
588 char *buf
= strbuf_detach(&line
, &len
);
589 add_pattern(buf
, empty_base
, 0, pl
, 0);
592 for (i
= 0; i
< argc
; i
++)
593 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
603 static void add_patterns_cone_mode(int argc
, const char **argv
,
604 struct pattern_list
*pl
,
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,
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
,
645 char *sparse_filename
= get_sparse_checkout_filename();
646 if (add_patterns_from_file_to_list(sparse_filename
, "", 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
,
657 int changed_config
= 0;
658 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
662 if (core_sparse_checkout_cone
)
663 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
665 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
669 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
673 if (!core_apply_sparse_checkout
) {
674 set_config(MODE_ALL_PATTERNS
);
675 core_apply_sparse_checkout
= 1;
679 result
= write_patterns_and_update(pl
);
681 if (result
&& changed_config
)
682 set_config(MODE_NO_PATTERNS
);
684 clear_pattern_list(pl
);
689 static void sanitize_paths(int argc
, const char **argv
,
690 const char *prefix
, int skip_checks
)
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
]);
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
]));
732 ce
= index
->cache
[pos
];
733 if (S_ISSPARSEDIR(ce
->ce_mode
))
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
]);
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>)"),
748 static struct sparse_checkout_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"),
759 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
760 N_("read patterns from standard in")),
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>)"),
784 static struct sparse_checkout_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"),
804 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
805 N_("read patterns from standard in"),
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
))
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
;
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]",
843 static struct sparse_checkout_reapply_opts
{
848 static int sparse_checkout_reapply(int argc
, const char **argv
,
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")),
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
))
874 return update_working_directory(NULL
);
877 static char const * const builtin_sparse_checkout_disable_usage
[] = {
878 "git sparse-checkout disable",
882 static int sparse_checkout_disable(int argc
, const char **argv
,
885 static struct option builtin_sparse_checkout_disable_options
[] = {
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
),
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
);