4 #include "environment.h"
6 #include "object-file.h"
7 #include "object-name.h"
8 #include "parse-options.h"
10 #include "repository.h"
11 #include "run-command.h"
13 #include "string-list.h"
14 #include "cache-tree.h"
16 #include "resolve-undo.h"
17 #include "unpack-trees.h"
18 #include "wt-status.h"
21 #include "sparse-index.h"
24 static const char *empty_base
= "";
26 static char const * const builtin_sparse_checkout_usage
[] = {
27 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
31 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
35 for (i
= 0; i
< pl
->nr
; i
++) {
36 struct path_pattern
*p
= pl
->patterns
[i
];
38 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
41 fprintf(fp
, "%s", p
->pattern
);
43 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
50 static char const * const builtin_sparse_checkout_list_usage
[] = {
51 "git sparse-checkout list",
55 static int sparse_checkout_list(int argc
, const char **argv
, const char *prefix
)
57 static struct option builtin_sparse_checkout_list_options
[] = {
60 struct pattern_list pl
;
61 char *sparse_filename
;
65 if (!core_apply_sparse_checkout
)
66 die(_("this worktree is not sparse"));
68 argc
= parse_options(argc
, argv
, prefix
,
69 builtin_sparse_checkout_list_options
,
70 builtin_sparse_checkout_list_usage
, 0);
72 memset(&pl
, 0, sizeof(pl
));
74 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
76 sparse_filename
= get_sparse_checkout_filename();
77 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
78 free(sparse_filename
);
81 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
85 if (pl
.use_cone_patterns
) {
87 struct pattern_entry
*pe
;
88 struct hashmap_iter iter
;
89 struct string_list sl
= STRING_LIST_INIT_DUP
;
91 hashmap_for_each_entry(&pl
.recursive_hashmap
, &iter
, pe
, ent
) {
92 /* pe->pattern starts with "/", skip it */
93 string_list_insert(&sl
, pe
->pattern
+ 1);
96 string_list_sort(&sl
);
98 for (i
= 0; i
< sl
.nr
; i
++) {
99 quote_c_style(sl
.items
[i
].string
, NULL
, stdout
, 0);
106 write_patterns_to_file(stdout
, &pl
);
107 clear_pattern_list(&pl
);
112 static void clean_tracked_sparse_directories(struct repository
*r
)
115 struct strbuf path
= STRBUF_INIT
;
117 struct string_list_item
*item
;
118 struct string_list sparse_dirs
= STRING_LIST_INIT_DUP
;
121 * If we are not using cone mode patterns, then we cannot
122 * delete directories outside of the sparse cone.
124 if (!r
|| !r
->index
|| !r
->worktree
)
126 if (init_sparse_checkout_patterns(r
->index
) ||
127 !r
->index
->sparse_checkout_patterns
->use_cone_patterns
)
131 * Use the sparse index as a data structure to assist finding
132 * directories that are safe to delete. This conversion to a
133 * sparse index will not delete directories that contain
134 * conflicted entries or submodules.
136 if (r
->index
->sparse_index
== INDEX_EXPANDED
) {
138 * If something, such as a merge conflict or other concern,
139 * prevents us from converting to a sparse index, then do
140 * not try deleting files.
142 if (convert_to_sparse(r
->index
, SPARSE_INDEX_MEMORY_ONLY
))
147 strbuf_addstr(&path
, r
->worktree
);
148 strbuf_complete(&path
, '/');
152 * Collect directories that have gone out of scope but also
153 * exist on disk, so there is some work to be done. We need to
154 * store the entries in a list before exploring, since that might
155 * expand the sparse-index again.
157 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
158 struct cache_entry
*ce
= r
->index
->cache
[i
];
160 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
161 repo_file_exists(r
, ce
->name
))
162 string_list_append(&sparse_dirs
, ce
->name
);
165 for_each_string_list_item(item
, &sparse_dirs
) {
166 struct dir_struct dir
= DIR_INIT
;
167 struct pathspec p
= { 0 };
168 struct strvec s
= STRVEC_INIT
;
170 strbuf_setlen(&path
, pathlen
);
171 strbuf_addstr(&path
, item
->string
);
173 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
175 setup_standard_excludes(&dir
);
176 strvec_push(&s
, path
.buf
);
178 parse_pathspec(&p
, PATHSPEC_GLOB
, 0, NULL
, s
.v
);
179 fill_directory(&dir
, r
->index
, &p
);
182 warning(_("directory '%s' contains untracked files,"
183 " but is not in the sparse-checkout cone"),
185 } else if (remove_dir_recursively(&path
, 0)) {
187 * Removal is "best effort". If something blocks
188 * the deletion, then continue with a warning.
190 warning(_("failed to remove directory '%s'"),
199 string_list_clear(&sparse_dirs
, 0);
200 strbuf_release(&path
);
203 ensure_full_index(r
->index
);
206 static int update_working_directory(struct pattern_list
*pl
)
208 enum update_sparsity_result result
;
209 struct unpack_trees_options o
;
210 struct lock_file lock_file
= LOCK_INIT
;
211 struct repository
*r
= the_repository
;
213 /* If no branch has been checked out, there are no updates to make. */
214 if (is_index_unborn(r
->index
))
215 return UPDATE_SPARSITY_SUCCESS
;
217 r
->index
->sparse_checkout_patterns
= pl
;
219 memset(&o
, 0, sizeof(o
));
220 o
.verbose_update
= isatty(2);
223 o
.src_index
= r
->index
;
224 o
.dst_index
= r
->index
;
225 o
.skip_sparse_checkout
= 0;
229 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
231 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
232 result
= update_sparsity(&o
, pl
);
233 clear_unpack_trees_porcelain(&o
);
235 if (result
== UPDATE_SPARSITY_WARNINGS
)
237 * We don't do any special handling of warnings from untracked
238 * files in the way or dirty entries that can't be removed.
240 result
= UPDATE_SPARSITY_SUCCESS
;
241 if (result
== UPDATE_SPARSITY_SUCCESS
)
242 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
244 rollback_lock_file(&lock_file
);
246 clean_tracked_sparse_directories(r
);
248 r
->index
->sparse_checkout_patterns
= NULL
;
252 static char *escaped_pattern(char *pattern
)
255 struct strbuf final
= STRBUF_INIT
;
258 if (is_glob_special(*p
))
259 strbuf_addch(&final
, '\\');
261 strbuf_addch(&final
, *p
);
265 return strbuf_detach(&final
, NULL
);
268 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
271 struct pattern_entry
*pe
;
272 struct hashmap_iter iter
;
273 struct string_list sl
= STRING_LIST_INIT_DUP
;
274 struct strbuf parent_pattern
= STRBUF_INIT
;
276 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
277 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
280 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
283 string_list_insert(&sl
, pe
->pattern
);
286 string_list_sort(&sl
);
287 string_list_remove_duplicates(&sl
, 0);
289 fprintf(fp
, "/*\n!/*/\n");
291 for (i
= 0; i
< sl
.nr
; i
++) {
292 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
295 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
299 string_list_clear(&sl
, 0);
301 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
302 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
305 string_list_insert(&sl
, pe
->pattern
);
308 strbuf_release(&parent_pattern
);
310 string_list_sort(&sl
);
311 string_list_remove_duplicates(&sl
, 0);
313 for (i
= 0; i
< sl
.nr
; i
++) {
314 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
315 fprintf(fp
, "%s/\n", pattern
);
320 static int write_patterns_and_update(struct pattern_list
*pl
)
322 char *sparse_filename
;
325 struct lock_file lk
= LOCK_INIT
;
328 sparse_filename
= get_sparse_checkout_filename();
330 if (safe_create_leading_directories(sparse_filename
))
331 die(_("failed to create directory for sparse-checkout file"));
333 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
335 free(sparse_filename
);
337 result
= update_working_directory(pl
);
339 rollback_lock_file(&lk
);
340 clear_pattern_list(pl
);
341 update_working_directory(NULL
);
345 fp
= xfdopen(fd
, "w");
347 if (core_sparse_checkout_cone
)
348 write_cone_to_file(fp
, pl
);
350 write_patterns_to_file(fp
, pl
);
353 commit_lock_file(&lk
);
355 clear_pattern_list(pl
);
360 enum sparse_checkout_mode
{
361 MODE_NO_PATTERNS
= 0,
362 MODE_ALL_PATTERNS
= 1,
363 MODE_CONE_PATTERNS
= 2,
366 static int set_config(enum sparse_checkout_mode mode
)
368 /* Update to use worktree config, if not already. */
369 if (init_worktree_config(the_repository
)) {
370 error(_("failed to initialize worktree config"));
374 if (repo_config_set_worktree_gently(the_repository
,
375 "core.sparseCheckout",
376 mode
? "true" : "false") ||
377 repo_config_set_worktree_gently(the_repository
,
378 "core.sparseCheckoutCone",
379 mode
== MODE_CONE_PATTERNS
?
383 if (mode
== MODE_NO_PATTERNS
)
384 return set_sparse_index_config(the_repository
, 0);
389 static enum sparse_checkout_mode
update_cone_mode(int *cone_mode
) {
390 /* If not specified, use previous definition of cone mode */
391 if (*cone_mode
== -1 && core_apply_sparse_checkout
)
392 *cone_mode
= core_sparse_checkout_cone
;
394 /* Set cone/non-cone mode appropriately */
395 core_apply_sparse_checkout
= 1;
396 if (*cone_mode
== 1 || *cone_mode
== -1) {
397 core_sparse_checkout_cone
= 1;
398 return MODE_CONE_PATTERNS
;
400 core_sparse_checkout_cone
= 0;
401 return MODE_ALL_PATTERNS
;
404 static int update_modes(int *cone_mode
, int *sparse_index
)
406 int mode
, record_mode
;
408 /* Determine if we need to record the mode; ensure sparse checkout on */
409 record_mode
= (*cone_mode
!= -1) || !core_apply_sparse_checkout
;
411 mode
= update_cone_mode(cone_mode
);
412 if (record_mode
&& set_config(mode
))
415 /* Set sparse-index/non-sparse-index mode if specified */
416 if (*sparse_index
>= 0) {
417 if (set_sparse_index_config(the_repository
, *sparse_index
) < 0)
418 die(_("failed to modify sparse-index config"));
420 /* force an index rewrite */
421 repo_read_index(the_repository
);
422 the_repository
->index
->updated_workdir
= 1;
425 ensure_full_index(the_repository
->index
);
431 static char const * const builtin_sparse_checkout_init_usage
[] = {
432 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
436 static struct sparse_checkout_init_opts
{
441 static int sparse_checkout_init(int argc
, const char **argv
, const char *prefix
)
443 struct pattern_list pl
;
444 char *sparse_filename
;
446 struct object_id oid
;
447 struct strbuf pattern
= STRBUF_INIT
;
449 static struct option builtin_sparse_checkout_init_options
[] = {
450 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
451 N_("initialize the sparse-checkout in cone mode")),
452 OPT_BOOL(0, "sparse-index", &init_opts
.sparse_index
,
453 N_("toggle the use of a sparse index")),
458 repo_read_index(the_repository
);
460 init_opts
.cone_mode
= -1;
461 init_opts
.sparse_index
= -1;
463 argc
= parse_options(argc
, argv
, prefix
,
464 builtin_sparse_checkout_init_options
,
465 builtin_sparse_checkout_init_usage
, 0);
467 if (update_modes(&init_opts
.cone_mode
, &init_opts
.sparse_index
))
470 memset(&pl
, 0, sizeof(pl
));
472 sparse_filename
= get_sparse_checkout_filename();
473 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
475 /* If we already have a sparse-checkout file, use it. */
477 free(sparse_filename
);
478 return update_working_directory(NULL
);
481 if (repo_get_oid(the_repository
, "HEAD", &oid
)) {
484 /* assume we are in a fresh repo, but update the sparse-checkout file */
485 if (safe_create_leading_directories(sparse_filename
))
486 die(_("unable to create leading directories of %s"),
488 fp
= xfopen(sparse_filename
, "w");
490 die(_("failed to open '%s'"), sparse_filename
);
492 free(sparse_filename
);
493 fprintf(fp
, "/*\n!/*/\n");
498 strbuf_addstr(&pattern
, "/*");
499 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
500 strbuf_addstr(&pattern
, "!/*/");
501 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
502 pl
.use_cone_patterns
= init_opts
.cone_mode
;
504 return write_patterns_and_update(&pl
);
507 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
509 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
510 e
->patternlen
= path
->len
;
511 e
->pattern
= strbuf_detach(path
, NULL
);
512 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
514 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
516 while (e
->patternlen
) {
517 char *slash
= strrchr(e
->pattern
, '/');
518 char *oldpattern
= e
->pattern
;
521 if (!slash
|| slash
== e
->pattern
)
524 newlen
= slash
- e
->pattern
;
525 e
= xmalloc(sizeof(struct pattern_entry
));
526 e
->patternlen
= newlen
;
527 e
->pattern
= xstrndup(oldpattern
, newlen
);
528 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
530 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
531 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
535 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
539 strbuf_trim_trailing_dir_sep(line
);
541 if (strbuf_normalize_path(line
))
542 die(_("could not normalize path %s"), line
->buf
);
547 if (line
->buf
[0] != '/')
548 strbuf_insertstr(line
, 0, "/");
550 insert_recursive_pattern(pl
, line
);
553 static void add_patterns_from_input(struct pattern_list
*pl
,
554 int argc
, const char **argv
,
558 if (core_sparse_checkout_cone
) {
559 struct strbuf line
= STRBUF_INIT
;
561 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
562 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
563 pl
->use_cone_patterns
= 1;
566 struct strbuf unquoted
= STRBUF_INIT
;
567 while (!strbuf_getline(&line
, file
)) {
568 if (line
.buf
[0] == '"') {
569 strbuf_reset(&unquoted
);
570 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
571 die(_("unable to unquote C-style string '%s'"),
574 strbuf_swap(&unquoted
, &line
);
577 strbuf_to_cone_pattern(&line
, pl
);
580 strbuf_release(&unquoted
);
582 for (i
= 0; i
< argc
; i
++) {
583 strbuf_setlen(&line
, 0);
584 strbuf_addstr(&line
, argv
[i
]);
585 strbuf_to_cone_pattern(&line
, pl
);
590 struct strbuf line
= STRBUF_INIT
;
592 while (!strbuf_getline(&line
, file
)) {
594 char *buf
= strbuf_detach(&line
, &len
);
595 add_pattern(buf
, empty_base
, 0, pl
, 0);
598 for (i
= 0; i
< argc
; i
++)
599 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
609 static void add_patterns_cone_mode(int argc
, const char **argv
,
610 struct pattern_list
*pl
,
613 struct strbuf buffer
= STRBUF_INIT
;
614 struct pattern_entry
*pe
;
615 struct hashmap_iter iter
;
616 struct pattern_list existing
;
617 char *sparse_filename
= get_sparse_checkout_filename();
619 add_patterns_from_input(pl
, argc
, argv
,
620 use_stdin
? stdin
: NULL
);
622 memset(&existing
, 0, sizeof(existing
));
623 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
625 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
627 die(_("unable to load existing sparse-checkout patterns"));
628 free(sparse_filename
);
630 if (!existing
.use_cone_patterns
)
631 die(_("existing sparse-checkout patterns do not use cone mode"));
633 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
634 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
635 pe
->pattern
, &buffer
) ||
636 !hashmap_contains_parent(&pl
->parent_hashmap
,
637 pe
->pattern
, &buffer
)) {
638 strbuf_reset(&buffer
);
639 strbuf_addstr(&buffer
, pe
->pattern
);
640 insert_recursive_pattern(pl
, &buffer
);
644 clear_pattern_list(&existing
);
645 strbuf_release(&buffer
);
648 static void add_patterns_literal(int argc
, const char **argv
,
649 struct pattern_list
*pl
,
652 char *sparse_filename
= get_sparse_checkout_filename();
653 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
655 die(_("unable to load existing sparse-checkout patterns"));
656 free(sparse_filename
);
657 add_patterns_from_input(pl
, argc
, argv
, use_stdin
? stdin
: NULL
);
660 static int modify_pattern_list(int argc
, const char **argv
, int use_stdin
,
664 int changed_config
= 0;
665 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
669 if (core_sparse_checkout_cone
)
670 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
672 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
676 add_patterns_from_input(pl
, argc
, argv
,
677 use_stdin
? stdin
: NULL
);
681 if (!core_apply_sparse_checkout
) {
682 set_config(MODE_ALL_PATTERNS
);
683 core_apply_sparse_checkout
= 1;
687 result
= write_patterns_and_update(pl
);
689 if (result
&& changed_config
)
690 set_config(MODE_NO_PATTERNS
);
692 clear_pattern_list(pl
);
697 static void sanitize_paths(int argc
, const char **argv
,
698 const char *prefix
, int skip_checks
)
705 if (prefix
&& *prefix
&& core_sparse_checkout_cone
) {
707 * The args are not pathspecs, so unfortunately we
708 * cannot imitate how cmd_add() uses parse_pathspec().
710 int prefix_len
= strlen(prefix
);
712 for (i
= 0; i
< argc
; i
++)
713 argv
[i
] = prefix_path(prefix
, prefix_len
, argv
[i
]);
719 if (prefix
&& *prefix
&& !core_sparse_checkout_cone
)
720 die(_("please run from the toplevel directory in non-cone mode"));
722 if (core_sparse_checkout_cone
) {
723 for (i
= 0; i
< argc
; i
++) {
724 if (argv
[i
][0] == '/')
725 die(_("specify directories rather than patterns (no leading slash)"));
726 if (argv
[i
][0] == '!')
727 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
728 if (strpbrk(argv
[i
], "*?[]"))
729 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
733 for (i
= 0; i
< argc
; i
++) {
734 struct cache_entry
*ce
;
735 struct index_state
*index
= the_repository
->index
;
736 int pos
= index_name_pos(index
, argv
[i
], strlen(argv
[i
]));
740 ce
= index
->cache
[pos
];
741 if (S_ISSPARSEDIR(ce
->ce_mode
))
744 if (core_sparse_checkout_cone
)
745 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv
[i
]);
747 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
]);
751 static char const * const builtin_sparse_checkout_add_usage
[] = {
752 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
756 static struct sparse_checkout_add_opts
{
761 static int sparse_checkout_add(int argc
, const char **argv
, const char *prefix
)
763 static struct option builtin_sparse_checkout_add_options
[] = {
764 OPT_BOOL_F(0, "skip-checks", &add_opts
.skip_checks
,
765 N_("skip some sanity checks on the given paths that might give false positives"),
767 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
768 N_("read patterns from standard in")),
773 if (!core_apply_sparse_checkout
)
774 die(_("no sparse-checkout to add to"));
776 repo_read_index(the_repository
);
778 argc
= parse_options(argc
, argv
, prefix
,
779 builtin_sparse_checkout_add_options
,
780 builtin_sparse_checkout_add_usage
,
781 PARSE_OPT_KEEP_UNKNOWN_OPT
);
783 sanitize_paths(argc
, argv
, prefix
, add_opts
.skip_checks
);
785 return modify_pattern_list(argc
, argv
, add_opts
.use_stdin
, ADD
);
788 static char const * const builtin_sparse_checkout_set_usage
[] = {
789 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
793 static struct sparse_checkout_set_opts
{
800 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
802 int default_patterns_nr
= 2;
803 const char *default_patterns
[] = {"/*", "!/*/", NULL
};
805 static struct option builtin_sparse_checkout_set_options
[] = {
806 OPT_BOOL(0, "cone", &set_opts
.cone_mode
,
807 N_("initialize the sparse-checkout in cone mode")),
808 OPT_BOOL(0, "sparse-index", &set_opts
.sparse_index
,
809 N_("toggle the use of a sparse index")),
810 OPT_BOOL_F(0, "skip-checks", &set_opts
.skip_checks
,
811 N_("skip some sanity checks on the given paths that might give false positives"),
813 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
814 N_("read patterns from standard in"),
820 repo_read_index(the_repository
);
822 set_opts
.cone_mode
= -1;
823 set_opts
.sparse_index
= -1;
825 argc
= parse_options(argc
, argv
, prefix
,
826 builtin_sparse_checkout_set_options
,
827 builtin_sparse_checkout_set_usage
,
828 PARSE_OPT_KEEP_UNKNOWN_OPT
);
830 if (update_modes(&set_opts
.cone_mode
, &set_opts
.sparse_index
))
834 * Cone mode automatically specifies the toplevel directory. For
835 * non-cone mode, if nothing is specified, manually select just the
836 * top-level directory (much as 'init' would do).
838 if (!core_sparse_checkout_cone
&& argc
== 0) {
839 argv
= default_patterns
;
840 argc
= default_patterns_nr
;
842 sanitize_paths(argc
, argv
, prefix
, set_opts
.skip_checks
);
845 return modify_pattern_list(argc
, argv
, set_opts
.use_stdin
, REPLACE
);
848 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
849 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
853 static struct sparse_checkout_reapply_opts
{
858 static int sparse_checkout_reapply(int argc
, const char **argv
,
861 static struct option builtin_sparse_checkout_reapply_options
[] = {
862 OPT_BOOL(0, "cone", &reapply_opts
.cone_mode
,
863 N_("initialize the sparse-checkout in cone mode")),
864 OPT_BOOL(0, "sparse-index", &reapply_opts
.sparse_index
,
865 N_("toggle the use of a sparse index")),
870 if (!core_apply_sparse_checkout
)
871 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
873 reapply_opts
.cone_mode
= -1;
874 reapply_opts
.sparse_index
= -1;
876 argc
= parse_options(argc
, argv
, prefix
,
877 builtin_sparse_checkout_reapply_options
,
878 builtin_sparse_checkout_reapply_usage
, 0);
880 repo_read_index(the_repository
);
882 if (update_modes(&reapply_opts
.cone_mode
, &reapply_opts
.sparse_index
))
885 return update_working_directory(NULL
);
888 static char const * const builtin_sparse_checkout_disable_usage
[] = {
889 "git sparse-checkout disable",
893 static int sparse_checkout_disable(int argc
, const char **argv
,
896 static struct option builtin_sparse_checkout_disable_options
[] = {
899 struct pattern_list pl
;
900 struct strbuf match_all
= STRBUF_INIT
;
903 * We do not exit early if !core_apply_sparse_checkout; due to the
904 * ability for users to manually muck things up between
905 * direct editing of .git/info/sparse-checkout
906 * running read-tree -m u HEAD or update-index --skip-worktree
907 * direct toggling of config options
908 * users might end up with an index with SKIP_WORKTREE bit set on
909 * some files and not know how to undo it. So, here we just
910 * forcibly return to a dense checkout regardless of initial state.
914 argc
= parse_options(argc
, argv
, prefix
,
915 builtin_sparse_checkout_disable_options
,
916 builtin_sparse_checkout_disable_usage
, 0);
918 repo_read_index(the_repository
);
920 memset(&pl
, 0, sizeof(pl
));
921 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
922 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
923 pl
.use_cone_patterns
= 0;
924 core_apply_sparse_checkout
= 1;
926 strbuf_addstr(&match_all
, "/*");
927 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
929 prepare_repo_settings(the_repository
);
930 the_repository
->settings
.sparse_index
= 0;
932 if (update_working_directory(&pl
))
933 die(_("error while refreshing working directory"));
935 clear_pattern_list(&pl
);
936 return set_config(MODE_NO_PATTERNS
);
939 static char const * const builtin_sparse_checkout_check_rules_usage
[] = {
940 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
941 "[--[no-]cone] [--rules-file <file>]"),
945 static struct sparse_checkout_check_rules_opts
{
947 int null_termination
;
951 static int check_rules(struct pattern_list
*pl
, int null_terminated
) {
952 struct strbuf line
= STRBUF_INIT
;
953 struct strbuf unquoted
= STRBUF_INIT
;
955 int line_terminator
= null_terminated
? 0 : '\n';
956 strbuf_getline_fn getline_fn
= null_terminated
? strbuf_getline_nul
958 the_repository
->index
->sparse_checkout_patterns
= pl
;
959 while (!getline_fn(&line
, stdin
)) {
961 if (!null_terminated
&& line
.buf
[0] == '"') {
962 strbuf_reset(&unquoted
);
963 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
964 die(_("unable to unquote C-style string '%s'"),
970 if (path_in_sparse_checkout(path
, the_repository
->index
))
971 write_name_quoted(path
, stdout
, line_terminator
);
973 strbuf_release(&line
);
974 strbuf_release(&unquoted
);
979 static int sparse_checkout_check_rules(int argc
, const char **argv
, const char *prefix
)
981 static struct option builtin_sparse_checkout_check_rules_options
[] = {
982 OPT_BOOL('z', NULL
, &check_rules_opts
.null_termination
,
983 N_("terminate input and output files by a NUL character")),
984 OPT_BOOL(0, "cone", &check_rules_opts
.cone_mode
,
985 N_("when used with --rules-file interpret patterns as cone mode patterns")),
986 OPT_FILENAME(0, "rules-file", &check_rules_opts
.rules_file
,
987 N_("use patterns in <file> instead of the current ones.")),
993 struct pattern_list pl
= {0};
994 char *sparse_filename
;
995 check_rules_opts
.cone_mode
= -1;
997 argc
= parse_options(argc
, argv
, prefix
,
998 builtin_sparse_checkout_check_rules_options
,
999 builtin_sparse_checkout_check_rules_usage
,
1000 PARSE_OPT_KEEP_UNKNOWN_OPT
);
1002 if (check_rules_opts
.rules_file
&& check_rules_opts
.cone_mode
< 0)
1003 check_rules_opts
.cone_mode
= 1;
1005 update_cone_mode(&check_rules_opts
.cone_mode
);
1006 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
1007 if (check_rules_opts
.rules_file
) {
1008 fp
= xfopen(check_rules_opts
.rules_file
, "r");
1009 add_patterns_from_input(&pl
, argc
, argv
, fp
);
1012 sparse_filename
= get_sparse_checkout_filename();
1013 if (add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
,
1015 die(_("unable to load existing sparse-checkout patterns"));
1016 free(sparse_filename
);
1019 ret
= check_rules(&pl
, check_rules_opts
.null_termination
);
1020 clear_pattern_list(&pl
);
1024 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
1026 parse_opt_subcommand_fn
*fn
= NULL
;
1027 struct option builtin_sparse_checkout_options
[] = {
1028 OPT_SUBCOMMAND("list", &fn
, sparse_checkout_list
),
1029 OPT_SUBCOMMAND("init", &fn
, sparse_checkout_init
),
1030 OPT_SUBCOMMAND("set", &fn
, sparse_checkout_set
),
1031 OPT_SUBCOMMAND("add", &fn
, sparse_checkout_add
),
1032 OPT_SUBCOMMAND("reapply", &fn
, sparse_checkout_reapply
),
1033 OPT_SUBCOMMAND("disable", &fn
, sparse_checkout_disable
),
1034 OPT_SUBCOMMAND("check-rules", &fn
, sparse_checkout_check_rules
),
1038 argc
= parse_options(argc
, argv
, prefix
,
1039 builtin_sparse_checkout_options
,
1040 builtin_sparse_checkout_usage
, 0);
1042 git_config(git_default_config
, NULL
);
1044 prepare_repo_settings(the_repository
);
1045 the_repository
->settings
.command_requires_full_index
= 0;
1047 return fn(argc
, argv
, prefix
);