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'"),
193 string_list_clear(&sparse_dirs
, 0);
194 strbuf_release(&path
);
197 ensure_full_index(r
->index
);
200 static int update_working_directory(struct pattern_list
*pl
)
202 enum update_sparsity_result result
;
203 struct unpack_trees_options o
;
204 struct lock_file lock_file
= LOCK_INIT
;
205 struct repository
*r
= the_repository
;
207 /* If no branch has been checked out, there are no updates to make. */
208 if (is_index_unborn(r
->index
))
209 return UPDATE_SPARSITY_SUCCESS
;
211 r
->index
->sparse_checkout_patterns
= pl
;
213 memset(&o
, 0, sizeof(o
));
214 o
.verbose_update
= isatty(2);
217 o
.src_index
= r
->index
;
218 o
.dst_index
= r
->index
;
219 o
.skip_sparse_checkout
= 0;
224 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
226 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
227 result
= update_sparsity(&o
);
228 clear_unpack_trees_porcelain(&o
);
230 if (result
== UPDATE_SPARSITY_WARNINGS
)
232 * We don't do any special handling of warnings from untracked
233 * files in the way or dirty entries that can't be removed.
235 result
= UPDATE_SPARSITY_SUCCESS
;
236 if (result
== UPDATE_SPARSITY_SUCCESS
)
237 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
239 rollback_lock_file(&lock_file
);
241 clean_tracked_sparse_directories(r
);
243 r
->index
->sparse_checkout_patterns
= NULL
;
247 static char *escaped_pattern(char *pattern
)
250 struct strbuf final
= STRBUF_INIT
;
253 if (is_glob_special(*p
))
254 strbuf_addch(&final
, '\\');
256 strbuf_addch(&final
, *p
);
260 return strbuf_detach(&final
, NULL
);
263 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
266 struct pattern_entry
*pe
;
267 struct hashmap_iter iter
;
268 struct string_list sl
= STRING_LIST_INIT_DUP
;
269 struct strbuf parent_pattern
= STRBUF_INIT
;
271 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
272 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
275 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
278 string_list_insert(&sl
, pe
->pattern
);
281 string_list_sort(&sl
);
282 string_list_remove_duplicates(&sl
, 0);
284 fprintf(fp
, "/*\n!/*/\n");
286 for (i
= 0; i
< sl
.nr
; i
++) {
287 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
290 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
294 string_list_clear(&sl
, 0);
296 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
297 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
300 string_list_insert(&sl
, pe
->pattern
);
303 strbuf_release(&parent_pattern
);
305 string_list_sort(&sl
);
306 string_list_remove_duplicates(&sl
, 0);
308 for (i
= 0; i
< sl
.nr
; i
++) {
309 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
310 fprintf(fp
, "%s/\n", pattern
);
315 static int write_patterns_and_update(struct pattern_list
*pl
)
317 char *sparse_filename
;
320 struct lock_file lk
= LOCK_INIT
;
323 sparse_filename
= get_sparse_checkout_filename();
325 if (safe_create_leading_directories(sparse_filename
))
326 die(_("failed to create directory for sparse-checkout file"));
328 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
331 result
= update_working_directory(pl
);
333 rollback_lock_file(&lk
);
334 free(sparse_filename
);
335 clear_pattern_list(pl
);
336 update_working_directory(NULL
);
340 fp
= xfdopen(fd
, "w");
342 if (core_sparse_checkout_cone
)
343 write_cone_to_file(fp
, pl
);
345 write_patterns_to_file(fp
, pl
);
348 commit_lock_file(&lk
);
350 free(sparse_filename
);
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 const char *config_path
;
366 if (upgrade_repository_format(1) < 0)
367 die(_("unable to upgrade repository format to enable worktreeConfig"));
368 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
369 error(_("failed to set extensions.worktreeConfig setting"));
373 config_path
= git_path("config.worktree");
374 git_config_set_in_file_gently(config_path
,
375 "core.sparseCheckout",
376 mode
? "true" : NULL
);
378 git_config_set_in_file_gently(config_path
,
379 "core.sparseCheckoutCone",
380 mode
== MODE_CONE_PATTERNS
? "true" : NULL
);
382 if (mode
== MODE_NO_PATTERNS
)
383 set_sparse_index_config(the_repository
, 0);
388 static int update_modes(int *cone_mode
, int *sparse_index
)
390 int mode
, record_mode
;
392 /* Determine if we need to record the mode; ensure sparse checkout on */
393 record_mode
= (*cone_mode
!= -1) || !core_apply_sparse_checkout
;
395 /* If not specified, use previous definition of cone mode */
396 if (*cone_mode
== -1 && core_apply_sparse_checkout
)
397 *cone_mode
= core_sparse_checkout_cone
;
399 /* Set cone/non-cone mode appropriately */
400 core_apply_sparse_checkout
= 1;
401 if (*cone_mode
== 1) {
402 mode
= MODE_CONE_PATTERNS
;
403 core_sparse_checkout_cone
= 1;
405 mode
= MODE_ALL_PATTERNS
;
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;
423 static char const * const builtin_sparse_checkout_init_usage
[] = {
424 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
428 static struct sparse_checkout_init_opts
{
433 static int sparse_checkout_init(int argc
, const char **argv
)
435 struct pattern_list pl
;
436 char *sparse_filename
;
438 struct object_id oid
;
439 struct strbuf pattern
= STRBUF_INIT
;
441 static struct option builtin_sparse_checkout_init_options
[] = {
442 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
443 N_("initialize the sparse-checkout in cone mode")),
444 OPT_BOOL(0, "sparse-index", &init_opts
.sparse_index
,
445 N_("toggle the use of a sparse index")),
449 repo_read_index(the_repository
);
451 init_opts
.cone_mode
= -1;
452 init_opts
.sparse_index
= -1;
454 argc
= parse_options(argc
, argv
, NULL
,
455 builtin_sparse_checkout_init_options
,
456 builtin_sparse_checkout_init_usage
, 0);
458 if (update_modes(&init_opts
.cone_mode
, &init_opts
.sparse_index
))
461 memset(&pl
, 0, sizeof(pl
));
463 sparse_filename
= get_sparse_checkout_filename();
464 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
466 /* If we already have a sparse-checkout file, use it. */
468 free(sparse_filename
);
469 return update_working_directory(NULL
);
472 if (get_oid("HEAD", &oid
)) {
475 /* assume we are in a fresh repo, but update the sparse-checkout file */
476 if (safe_create_leading_directories(sparse_filename
))
477 die(_("unable to create leading directories of %s"),
479 fp
= xfopen(sparse_filename
, "w");
481 die(_("failed to open '%s'"), sparse_filename
);
483 free(sparse_filename
);
484 fprintf(fp
, "/*\n!/*/\n");
489 strbuf_addstr(&pattern
, "/*");
490 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
491 strbuf_addstr(&pattern
, "!/*/");
492 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
493 pl
.use_cone_patterns
= init_opts
.cone_mode
;
495 return write_patterns_and_update(&pl
);
498 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
500 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
501 e
->patternlen
= path
->len
;
502 e
->pattern
= strbuf_detach(path
, NULL
);
503 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
505 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
507 while (e
->patternlen
) {
508 char *slash
= strrchr(e
->pattern
, '/');
509 char *oldpattern
= e
->pattern
;
512 if (!slash
|| slash
== e
->pattern
)
515 newlen
= slash
- e
->pattern
;
516 e
= xmalloc(sizeof(struct pattern_entry
));
517 e
->patternlen
= newlen
;
518 e
->pattern
= xstrndup(oldpattern
, newlen
);
519 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
521 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
522 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
526 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
530 strbuf_trim_trailing_dir_sep(line
);
532 if (strbuf_normalize_path(line
))
533 die(_("could not normalize path %s"), line
->buf
);
538 if (line
->buf
[0] != '/')
539 strbuf_insertstr(line
, 0, "/");
541 insert_recursive_pattern(pl
, line
);
544 static void add_patterns_from_input(struct pattern_list
*pl
,
545 int argc
, const char **argv
,
549 if (core_sparse_checkout_cone
) {
550 struct strbuf line
= STRBUF_INIT
;
552 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
553 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
554 pl
->use_cone_patterns
= 1;
557 struct strbuf unquoted
= STRBUF_INIT
;
558 while (!strbuf_getline(&line
, stdin
)) {
559 if (line
.buf
[0] == '"') {
560 strbuf_reset(&unquoted
);
561 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
562 die(_("unable to unquote C-style string '%s'"),
565 strbuf_swap(&unquoted
, &line
);
568 strbuf_to_cone_pattern(&line
, pl
);
571 strbuf_release(&unquoted
);
573 for (i
= 0; i
< argc
; i
++) {
574 strbuf_setlen(&line
, 0);
575 strbuf_addstr(&line
, argv
[i
]);
576 strbuf_to_cone_pattern(&line
, pl
);
581 struct strbuf line
= STRBUF_INIT
;
583 while (!strbuf_getline(&line
, stdin
)) {
585 char *buf
= strbuf_detach(&line
, &len
);
586 add_pattern(buf
, empty_base
, 0, pl
, 0);
589 for (i
= 0; i
< argc
; i
++)
590 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
600 static void add_patterns_cone_mode(int argc
, const char **argv
,
601 struct pattern_list
*pl
,
604 struct strbuf buffer
= STRBUF_INIT
;
605 struct pattern_entry
*pe
;
606 struct hashmap_iter iter
;
607 struct pattern_list existing
;
608 char *sparse_filename
= get_sparse_checkout_filename();
610 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
612 memset(&existing
, 0, sizeof(existing
));
613 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
615 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
617 die(_("unable to load existing sparse-checkout patterns"));
618 free(sparse_filename
);
620 if (!existing
.use_cone_patterns
)
621 die(_("existing sparse-checkout patterns do not use cone mode"));
623 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
624 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
625 pe
->pattern
, &buffer
) ||
626 !hashmap_contains_parent(&pl
->parent_hashmap
,
627 pe
->pattern
, &buffer
)) {
628 strbuf_reset(&buffer
);
629 strbuf_addstr(&buffer
, pe
->pattern
);
630 insert_recursive_pattern(pl
, &buffer
);
634 clear_pattern_list(&existing
);
635 strbuf_release(&buffer
);
638 static void add_patterns_literal(int argc
, const char **argv
,
639 struct pattern_list
*pl
,
642 char *sparse_filename
= get_sparse_checkout_filename();
643 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
645 die(_("unable to load existing sparse-checkout patterns"));
646 free(sparse_filename
);
647 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
650 static int modify_pattern_list(int argc
, const char **argv
, int use_stdin
,
654 int changed_config
= 0;
655 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
659 if (core_sparse_checkout_cone
)
660 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
662 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
666 add_patterns_from_input(pl
, argc
, argv
, use_stdin
);
670 if (!core_apply_sparse_checkout
) {
671 set_config(MODE_ALL_PATTERNS
);
672 core_apply_sparse_checkout
= 1;
676 result
= write_patterns_and_update(pl
);
678 if (result
&& changed_config
)
679 set_config(MODE_NO_PATTERNS
);
681 clear_pattern_list(pl
);
686 static char const * const builtin_sparse_checkout_add_usage
[] = {
687 N_("git sparse-checkout add (--stdin | <patterns>)"),
691 static struct sparse_checkout_add_opts
{
695 static int sparse_checkout_add(int argc
, const char **argv
, const char *prefix
)
697 static struct option builtin_sparse_checkout_add_options
[] = {
698 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
699 N_("read patterns from standard in")),
703 if (!core_apply_sparse_checkout
)
704 die(_("no sparse-checkout to add to"));
706 repo_read_index(the_repository
);
708 argc
= parse_options(argc
, argv
, prefix
,
709 builtin_sparse_checkout_add_options
,
710 builtin_sparse_checkout_add_usage
,
711 PARSE_OPT_KEEP_UNKNOWN
);
713 return modify_pattern_list(argc
, argv
, add_opts
.use_stdin
, ADD
);
716 static char const * const builtin_sparse_checkout_set_usage
[] = {
717 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | <patterns>)"),
721 static struct sparse_checkout_set_opts
{
727 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
729 int default_patterns_nr
= 2;
730 const char *default_patterns
[] = {"/*", "!/*/", NULL
};
732 static struct option builtin_sparse_checkout_set_options
[] = {
733 OPT_BOOL(0, "cone", &set_opts
.cone_mode
,
734 N_("initialize the sparse-checkout in cone mode")),
735 OPT_BOOL(0, "sparse-index", &set_opts
.sparse_index
,
736 N_("toggle the use of a sparse index")),
737 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
738 N_("read patterns from standard in"),
743 repo_read_index(the_repository
);
745 set_opts
.cone_mode
= -1;
746 set_opts
.sparse_index
= -1;
748 argc
= parse_options(argc
, argv
, prefix
,
749 builtin_sparse_checkout_set_options
,
750 builtin_sparse_checkout_set_usage
,
751 PARSE_OPT_KEEP_UNKNOWN
);
753 if (update_modes(&set_opts
.cone_mode
, &set_opts
.sparse_index
))
757 * Cone mode automatically specifies the toplevel directory. For
758 * non-cone mode, if nothing is specified, manually select just the
759 * top-level directory (much as 'init' would do).
761 if (!core_sparse_checkout_cone
&& argc
== 0) {
762 argv
= default_patterns
;
763 argc
= default_patterns_nr
;
766 return modify_pattern_list(argc
, argv
, set_opts
.use_stdin
, REPLACE
);
769 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
770 N_("git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]"),
774 static struct sparse_checkout_reapply_opts
{
779 static int sparse_checkout_reapply(int argc
, const char **argv
)
781 static struct option builtin_sparse_checkout_reapply_options
[] = {
782 OPT_BOOL(0, "cone", &reapply_opts
.cone_mode
,
783 N_("initialize the sparse-checkout in cone mode")),
784 OPT_BOOL(0, "sparse-index", &reapply_opts
.sparse_index
,
785 N_("toggle the use of a sparse index")),
789 if (!core_apply_sparse_checkout
)
790 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
792 argc
= parse_options(argc
, argv
, NULL
,
793 builtin_sparse_checkout_reapply_options
,
794 builtin_sparse_checkout_reapply_usage
, 0);
796 repo_read_index(the_repository
);
798 reapply_opts
.cone_mode
= -1;
799 reapply_opts
.sparse_index
= -1;
801 if (update_modes(&reapply_opts
.cone_mode
, &reapply_opts
.sparse_index
))
804 return update_working_directory(NULL
);
807 static char const * const builtin_sparse_checkout_disable_usage
[] = {
808 N_("git sparse-checkout disable"),
812 static int sparse_checkout_disable(int argc
, const char **argv
)
814 static struct option builtin_sparse_checkout_disable_options
[] = {
817 struct pattern_list pl
;
818 struct strbuf match_all
= STRBUF_INIT
;
821 * We do not exit early if !core_apply_sparse_checkout; due to the
822 * ability for users to manually muck things up between
823 * direct editing of .git/info/sparse-checkout
824 * running read-tree -m u HEAD or update-index --skip-worktree
825 * direct toggling of config options
826 * users might end up with an index with SKIP_WORKTREE bit set on
827 * some files and not know how to undo it. So, here we just
828 * forcibly return to a dense checkout regardless of initial state.
831 argc
= parse_options(argc
, argv
, NULL
,
832 builtin_sparse_checkout_disable_options
,
833 builtin_sparse_checkout_disable_usage
, 0);
835 repo_read_index(the_repository
);
837 memset(&pl
, 0, sizeof(pl
));
838 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
839 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
840 pl
.use_cone_patterns
= 0;
841 core_apply_sparse_checkout
= 1;
843 strbuf_addstr(&match_all
, "/*");
844 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
846 prepare_repo_settings(the_repository
);
847 the_repository
->settings
.sparse_index
= 0;
849 if (update_working_directory(&pl
))
850 die(_("error while refreshing working directory"));
852 clear_pattern_list(&pl
);
853 return set_config(MODE_NO_PATTERNS
);
856 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
858 static struct option builtin_sparse_checkout_options
[] = {
862 if (argc
== 2 && !strcmp(argv
[1], "-h"))
863 usage_with_options(builtin_sparse_checkout_usage
,
864 builtin_sparse_checkout_options
);
866 argc
= parse_options(argc
, argv
, prefix
,
867 builtin_sparse_checkout_options
,
868 builtin_sparse_checkout_usage
,
869 PARSE_OPT_STOP_AT_NON_OPTION
);
871 git_config(git_default_config
, NULL
);
874 if (!strcmp(argv
[0], "list"))
875 return sparse_checkout_list(argc
, argv
);
876 if (!strcmp(argv
[0], "init"))
877 return sparse_checkout_init(argc
, argv
);
878 if (!strcmp(argv
[0], "set"))
879 return sparse_checkout_set(argc
, argv
, prefix
);
880 if (!strcmp(argv
[0], "add"))
881 return sparse_checkout_add(argc
, argv
, prefix
);
882 if (!strcmp(argv
[0], "reapply"))
883 return sparse_checkout_reapply(argc
, argv
);
884 if (!strcmp(argv
[0], "disable"))
885 return sparse_checkout_disable(argc
, argv
);
888 usage_with_options(builtin_sparse_checkout_usage
,
889 builtin_sparse_checkout_options
);