4 #include "parse-options.h"
6 #include "repository.h"
7 #include "run-command.h"
9 #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"
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>"),
26 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
30 for (i
= 0; i
< pl
->nr
; i
++) {
31 struct path_pattern
*p
= pl
->patterns
[i
];
33 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
36 fprintf(fp
, "%s", p
->pattern
);
38 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
45 static char const * const builtin_sparse_checkout_list_usage
[] = {
46 N_("git sparse-checkout list"),
50 static int sparse_checkout_list(int argc
, const char **argv
)
52 static struct option builtin_sparse_checkout_list_options
[] = {
55 struct pattern_list pl
;
56 char *sparse_filename
;
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
);
75 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
79 if (pl
.use_cone_patterns
) {
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);
100 write_patterns_to_file(stdout
, &pl
);
101 clear_pattern_list(&pl
);
106 static void clean_tracked_sparse_directories(struct repository
*r
)
109 struct strbuf path
= STRBUF_INIT
;
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
)
120 if (init_sparse_checkout_patterns(r
->index
) ||
121 !r
->index
->sparse_checkout_patterns
->use_cone_patterns
)
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
))
141 strbuf_addstr(&path
, r
->worktree
);
142 strbuf_complete(&path
, '/');
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
);
176 warning(_("directory '%s' contains untracked files,"
177 " but is not in the sparse-checkout cone"),
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'"),
191 string_list_clear(&sparse_dirs
, 0);
192 strbuf_release(&path
);
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);
215 o
.src_index
= r
->index
;
216 o
.dst_index
= r
->index
;
217 o
.skip_sparse_checkout
= 0;
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
);
237 rollback_lock_file(&lock_file
);
239 clean_tracked_sparse_directories(r
);
241 r
->index
->sparse_checkout_patterns
= NULL
;
245 static char *escaped_pattern(char *pattern
)
248 struct strbuf final
= STRBUF_INIT
;
251 if (is_glob_special(*p
))
252 strbuf_addch(&final
, '\\');
254 strbuf_addch(&final
, *p
);
258 return strbuf_detach(&final
, NULL
);
261 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
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
))
273 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
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
);
288 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, 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
,
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
);
313 static int write_patterns_and_update(struct pattern_list
*pl
)
315 char *sparse_filename
;
318 struct lock_file lk
= LOCK_INIT
;
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
,
329 result
= update_working_directory(pl
);
331 rollback_lock_file(&lk
);
332 free(sparse_filename
);
333 clear_pattern_list(pl
);
334 update_working_directory(NULL
);
338 fp
= xfdopen(fd
, "w");
340 if (core_sparse_checkout_cone
)
341 write_cone_to_file(fp
, pl
);
343 write_patterns_to_file(fp
, pl
);
346 commit_lock_file(&lk
);
348 free(sparse_filename
);
349 clear_pattern_list(pl
);
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"));
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);
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;
403 mode
= MODE_ALL_PATTERNS
;
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 N_("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 fp
= xfopen(sparse_filename
, "w");
476 die(_("failed to open '%s'"), sparse_filename
);
478 free(sparse_filename
);
479 fprintf(fp
, "/*\n!/*/\n");
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
;
507 if (!slash
|| slash
== e
->pattern
)
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
)
525 strbuf_trim_trailing_dir_sep(line
);
527 if (strbuf_normalize_path(line
))
528 die(_("could not normalize path %s"), line
->buf
);
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
,
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;
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'"),
560 strbuf_swap(&unquoted
, &line
);
563 strbuf_to_cone_pattern(&line
, pl
);
566 strbuf_release(&unquoted
);
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
);
576 struct strbuf line
= STRBUF_INIT
;
578 while (!strbuf_getline(&line
, stdin
)) {
580 char *buf
= strbuf_detach(&line
, &len
);
581 add_pattern(buf
, empty_base
, 0, pl
, 0);
584 for (i
= 0; i
< argc
; i
++)
585 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
595 static void add_patterns_cone_mode(int argc
, const char **argv
,
596 struct pattern_list
*pl
,
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,
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
,
637 char *sparse_filename
= get_sparse_checkout_filename();
638 if (add_patterns_from_file_to_list(sparse_filename
, "", 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
,
649 int changed_config
= 0;
650 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
654 if (core_sparse_checkout_cone
)
655 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
657 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
661 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
665 if (!core_apply_sparse_checkout
) {
666 set_config(MODE_ALL_PATTERNS
);
667 core_apply_sparse_checkout
= 1;
671 result
= write_patterns_and_update(pl
);
673 if (result
&& changed_config
)
674 set_config(MODE_NO_PATTERNS
);
676 clear_pattern_list(pl
);
681 static char const * const builtin_sparse_checkout_add_usage
[] = {
682 N_("git sparse-checkout add (--stdin | <patterns>)"),
686 static struct sparse_checkout_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")),
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>)"),
716 static struct sparse_checkout_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"),
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
))
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]"),
769 static struct sparse_checkout_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")),
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
))
799 return update_working_directory(NULL
);
802 static char const * const builtin_sparse_checkout_disable_usage
[] = {
803 N_("git sparse-checkout disable"),
807 static int sparse_checkout_disable(int argc
, const char **argv
)
809 static struct option builtin_sparse_checkout_disable_options
[] = {
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
[] = {
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
);
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
);