4 #include "environment.h"
6 #include "object-file.h"
7 #include "object-name.h"
8 #include "parse-options.h"
10 #include "repository.h"
12 #include "string-list.h"
14 #include "unpack-trees.h"
17 #include "sparse-index.h"
20 static const char *empty_base
= "";
22 static char const * const builtin_sparse_checkout_usage
[] = {
23 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
27 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
31 for (i
= 0; i
< pl
->nr
; i
++) {
32 struct path_pattern
*p
= pl
->patterns
[i
];
34 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
37 fprintf(fp
, "%s", p
->pattern
);
39 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
46 static char const * const builtin_sparse_checkout_list_usage
[] = {
47 "git sparse-checkout list",
51 static int sparse_checkout_list(int argc
, const char **argv
, const char *prefix
)
53 static struct option builtin_sparse_checkout_list_options
[] = {
56 struct pattern_list pl
;
57 char *sparse_filename
;
61 if (!core_apply_sparse_checkout
)
62 die(_("this worktree is not sparse"));
64 argc
= parse_options(argc
, argv
, prefix
,
65 builtin_sparse_checkout_list_options
,
66 builtin_sparse_checkout_list_usage
, 0);
68 memset(&pl
, 0, sizeof(pl
));
70 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
72 sparse_filename
= get_sparse_checkout_filename();
73 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
74 free(sparse_filename
);
77 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
81 if (pl
.use_cone_patterns
) {
83 struct pattern_entry
*pe
;
84 struct hashmap_iter iter
;
85 struct string_list sl
= STRING_LIST_INIT_DUP
;
87 hashmap_for_each_entry(&pl
.recursive_hashmap
, &iter
, pe
, ent
) {
88 /* pe->pattern starts with "/", skip it */
89 string_list_insert(&sl
, pe
->pattern
+ 1);
92 string_list_sort(&sl
);
94 for (i
= 0; i
< sl
.nr
; i
++) {
95 quote_c_style(sl
.items
[i
].string
, NULL
, stdout
, 0);
99 string_list_clear(&sl
, 0);
101 write_patterns_to_file(stdout
, &pl
);
104 clear_pattern_list(&pl
);
109 static void clean_tracked_sparse_directories(struct repository
*r
)
112 struct strbuf path
= STRBUF_INIT
;
114 struct string_list_item
*item
;
115 struct string_list sparse_dirs
= STRING_LIST_INIT_DUP
;
118 * If we are not using cone mode patterns, then we cannot
119 * delete directories outside of the sparse cone.
121 if (!r
|| !r
->index
|| !r
->worktree
)
123 if (init_sparse_checkout_patterns(r
->index
) ||
124 !r
->index
->sparse_checkout_patterns
->use_cone_patterns
)
128 * Use the sparse index as a data structure to assist finding
129 * directories that are safe to delete. This conversion to a
130 * sparse index will not delete directories that contain
131 * conflicted entries or submodules.
133 if (r
->index
->sparse_index
== INDEX_EXPANDED
) {
135 * If something, such as a merge conflict or other concern,
136 * prevents us from converting to a sparse index, then do
137 * not try deleting files.
139 if (convert_to_sparse(r
->index
, SPARSE_INDEX_MEMORY_ONLY
))
144 strbuf_addstr(&path
, r
->worktree
);
145 strbuf_complete(&path
, '/');
149 * Collect directories that have gone out of scope but also
150 * exist on disk, so there is some work to be done. We need to
151 * store the entries in a list before exploring, since that might
152 * expand the sparse-index again.
154 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
155 struct cache_entry
*ce
= r
->index
->cache
[i
];
157 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
158 repo_file_exists(r
, ce
->name
))
159 string_list_append(&sparse_dirs
, ce
->name
);
162 for_each_string_list_item(item
, &sparse_dirs
) {
163 struct dir_struct dir
= DIR_INIT
;
164 struct pathspec p
= { 0 };
165 struct strvec s
= STRVEC_INIT
;
167 strbuf_setlen(&path
, pathlen
);
168 strbuf_addstr(&path
, item
->string
);
170 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
172 setup_standard_excludes(&dir
);
173 strvec_push(&s
, path
.buf
);
175 parse_pathspec(&p
, PATHSPEC_GLOB
, 0, NULL
, s
.v
);
176 fill_directory(&dir
, r
->index
, &p
);
179 warning(_("directory '%s' contains untracked files,"
180 " but is not in the sparse-checkout cone"),
182 } else if (remove_dir_recursively(&path
, 0)) {
184 * Removal is "best effort". If something blocks
185 * the deletion, then continue with a warning.
187 warning(_("failed to remove directory '%s'"),
196 string_list_clear(&sparse_dirs
, 0);
197 strbuf_release(&path
);
200 ensure_full_index(r
->index
);
203 static int update_working_directory(struct pattern_list
*pl
)
205 enum update_sparsity_result result
;
206 struct unpack_trees_options o
;
207 struct lock_file lock_file
= LOCK_INIT
;
208 struct repository
*r
= the_repository
;
209 struct pattern_list
*old_pl
;
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 old_pl
= r
->index
->sparse_checkout_patterns
;
216 r
->index
->sparse_checkout_patterns
= pl
;
218 memset(&o
, 0, sizeof(o
));
219 o
.verbose_update
= isatty(2);
222 o
.src_index
= r
->index
;
223 o
.dst_index
= r
->index
;
224 o
.skip_sparse_checkout
= 0;
228 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
230 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
231 result
= update_sparsity(&o
, pl
);
232 clear_unpack_trees_porcelain(&o
);
234 if (result
== UPDATE_SPARSITY_WARNINGS
)
236 * We don't do any special handling of warnings from untracked
237 * files in the way or dirty entries that can't be removed.
239 result
= UPDATE_SPARSITY_SUCCESS
;
240 if (result
== UPDATE_SPARSITY_SUCCESS
)
241 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
243 rollback_lock_file(&lock_file
);
245 clean_tracked_sparse_directories(r
);
247 if (r
->index
->sparse_checkout_patterns
!= pl
) {
248 clear_pattern_list(r
->index
->sparse_checkout_patterns
);
249 FREE_AND_NULL(r
->index
->sparse_checkout_patterns
);
251 r
->index
->sparse_checkout_patterns
= old_pl
;
256 static char *escaped_pattern(char *pattern
)
259 struct strbuf final
= STRBUF_INIT
;
262 if (is_glob_special(*p
))
263 strbuf_addch(&final
, '\\');
265 strbuf_addch(&final
, *p
);
269 return strbuf_detach(&final
, NULL
);
272 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
275 struct pattern_entry
*pe
;
276 struct hashmap_iter iter
;
277 struct string_list sl
= STRING_LIST_INIT_DUP
;
278 struct strbuf parent_pattern
= STRBUF_INIT
;
280 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
281 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
284 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
287 string_list_insert(&sl
, pe
->pattern
);
290 string_list_sort(&sl
);
291 string_list_remove_duplicates(&sl
, 0);
293 fprintf(fp
, "/*\n!/*/\n");
295 for (i
= 0; i
< sl
.nr
; i
++) {
296 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
299 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
303 string_list_clear(&sl
, 0);
305 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
306 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
309 string_list_insert(&sl
, pe
->pattern
);
312 strbuf_release(&parent_pattern
);
314 string_list_sort(&sl
);
315 string_list_remove_duplicates(&sl
, 0);
317 for (i
= 0; i
< sl
.nr
; i
++) {
318 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
319 fprintf(fp
, "%s/\n", pattern
);
323 string_list_clear(&sl
, 0);
326 static int write_patterns_and_update(struct pattern_list
*pl
)
328 char *sparse_filename
;
331 struct lock_file lk
= LOCK_INIT
;
334 sparse_filename
= get_sparse_checkout_filename();
336 if (safe_create_leading_directories(sparse_filename
))
337 die(_("failed to create directory for sparse-checkout file"));
339 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
341 free(sparse_filename
);
343 result
= update_working_directory(pl
);
345 rollback_lock_file(&lk
);
346 clear_pattern_list(pl
);
347 update_working_directory(NULL
);
351 fp
= xfdopen(fd
, "w");
353 if (core_sparse_checkout_cone
)
354 write_cone_to_file(fp
, pl
);
356 write_patterns_to_file(fp
, pl
);
359 commit_lock_file(&lk
);
361 clear_pattern_list(pl
);
366 enum sparse_checkout_mode
{
367 MODE_NO_PATTERNS
= 0,
368 MODE_ALL_PATTERNS
= 1,
369 MODE_CONE_PATTERNS
= 2,
372 static int set_config(enum sparse_checkout_mode mode
)
374 /* Update to use worktree config, if not already. */
375 if (init_worktree_config(the_repository
)) {
376 error(_("failed to initialize worktree config"));
380 if (repo_config_set_worktree_gently(the_repository
,
381 "core.sparseCheckout",
382 mode
? "true" : "false") ||
383 repo_config_set_worktree_gently(the_repository
,
384 "core.sparseCheckoutCone",
385 mode
== MODE_CONE_PATTERNS
?
389 if (mode
== MODE_NO_PATTERNS
)
390 return set_sparse_index_config(the_repository
, 0);
395 static enum sparse_checkout_mode
update_cone_mode(int *cone_mode
) {
396 /* If not specified, use previous definition of cone mode */
397 if (*cone_mode
== -1 && core_apply_sparse_checkout
)
398 *cone_mode
= core_sparse_checkout_cone
;
400 /* Set cone/non-cone mode appropriately */
401 core_apply_sparse_checkout
= 1;
402 if (*cone_mode
== 1 || *cone_mode
== -1) {
403 core_sparse_checkout_cone
= 1;
404 return MODE_CONE_PATTERNS
;
406 core_sparse_checkout_cone
= 0;
407 return MODE_ALL_PATTERNS
;
410 static int update_modes(int *cone_mode
, int *sparse_index
)
412 int mode
, record_mode
;
414 /* Determine if we need to record the mode; ensure sparse checkout on */
415 record_mode
= (*cone_mode
!= -1) || !core_apply_sparse_checkout
;
417 mode
= update_cone_mode(cone_mode
);
418 if (record_mode
&& set_config(mode
))
421 /* Set sparse-index/non-sparse-index mode if specified */
422 if (*sparse_index
>= 0) {
423 if (set_sparse_index_config(the_repository
, *sparse_index
) < 0)
424 die(_("failed to modify sparse-index config"));
426 /* force an index rewrite */
427 repo_read_index(the_repository
);
428 the_repository
->index
->updated_workdir
= 1;
431 ensure_full_index(the_repository
->index
);
437 static char const * const builtin_sparse_checkout_init_usage
[] = {
438 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
442 static struct sparse_checkout_init_opts
{
447 static int sparse_checkout_init(int argc
, const char **argv
, const char *prefix
)
449 struct pattern_list pl
;
450 char *sparse_filename
;
452 struct object_id oid
;
454 static struct option builtin_sparse_checkout_init_options
[] = {
455 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
456 N_("initialize the sparse-checkout in cone mode")),
457 OPT_BOOL(0, "sparse-index", &init_opts
.sparse_index
,
458 N_("toggle the use of a sparse index")),
463 repo_read_index(the_repository
);
465 init_opts
.cone_mode
= -1;
466 init_opts
.sparse_index
= -1;
468 argc
= parse_options(argc
, argv
, prefix
,
469 builtin_sparse_checkout_init_options
,
470 builtin_sparse_checkout_init_usage
, 0);
472 if (update_modes(&init_opts
.cone_mode
, &init_opts
.sparse_index
))
475 memset(&pl
, 0, sizeof(pl
));
477 sparse_filename
= get_sparse_checkout_filename();
478 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
480 /* If we already have a sparse-checkout file, use it. */
482 free(sparse_filename
);
483 clear_pattern_list(&pl
);
484 return update_working_directory(NULL
);
487 if (repo_get_oid(the_repository
, "HEAD", &oid
)) {
490 /* assume we are in a fresh repo, but update the sparse-checkout file */
491 if (safe_create_leading_directories(sparse_filename
))
492 die(_("unable to create leading directories of %s"),
494 fp
= xfopen(sparse_filename
, "w");
496 die(_("failed to open '%s'"), sparse_filename
);
498 free(sparse_filename
);
499 fprintf(fp
, "/*\n!/*/\n");
504 free(sparse_filename
);
506 add_pattern("/*", empty_base
, 0, &pl
, 0);
507 add_pattern("!/*/", empty_base
, 0, &pl
, 0);
508 pl
.use_cone_patterns
= init_opts
.cone_mode
;
510 return write_patterns_and_update(&pl
);
513 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
515 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
516 e
->patternlen
= path
->len
;
517 e
->pattern
= strbuf_detach(path
, NULL
);
518 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
520 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
522 while (e
->patternlen
) {
523 char *slash
= strrchr(e
->pattern
, '/');
524 char *oldpattern
= e
->pattern
;
526 struct pattern_entry
*dup
;
528 if (!slash
|| slash
== e
->pattern
)
531 newlen
= slash
- e
->pattern
;
532 e
= xmalloc(sizeof(struct pattern_entry
));
533 e
->patternlen
= newlen
;
534 e
->pattern
= xstrndup(oldpattern
, newlen
);
535 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
537 dup
= hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
);
539 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
548 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
552 strbuf_trim_trailing_dir_sep(line
);
554 if (strbuf_normalize_path(line
))
555 die(_("could not normalize path %s"), line
->buf
);
560 if (line
->buf
[0] != '/')
561 strbuf_insertstr(line
, 0, "/");
563 insert_recursive_pattern(pl
, line
);
566 static void add_patterns_from_input(struct pattern_list
*pl
,
567 int argc
, const char **argv
,
571 if (core_sparse_checkout_cone
) {
572 struct strbuf line
= STRBUF_INIT
;
574 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
575 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
576 pl
->use_cone_patterns
= 1;
579 struct strbuf unquoted
= STRBUF_INIT
;
580 while (!strbuf_getline(&line
, file
)) {
581 if (line
.buf
[0] == '"') {
582 strbuf_reset(&unquoted
);
583 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
584 die(_("unable to unquote C-style string '%s'"),
587 strbuf_swap(&unquoted
, &line
);
590 strbuf_to_cone_pattern(&line
, pl
);
593 strbuf_release(&unquoted
);
595 for (i
= 0; i
< argc
; i
++) {
596 strbuf_setlen(&line
, 0);
597 strbuf_addstr(&line
, argv
[i
]);
598 strbuf_to_cone_pattern(&line
, pl
);
601 strbuf_release(&line
);
604 struct strbuf line
= STRBUF_INIT
;
606 while (!strbuf_getline(&line
, file
))
607 add_pattern(line
.buf
, empty_base
, 0, pl
, 0);
609 strbuf_release(&line
);
611 for (i
= 0; i
< argc
; i
++)
612 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
622 static void add_patterns_cone_mode(int argc
, const char **argv
,
623 struct pattern_list
*pl
,
626 struct strbuf buffer
= STRBUF_INIT
;
627 struct pattern_entry
*pe
;
628 struct hashmap_iter iter
;
629 struct pattern_list existing
;
630 char *sparse_filename
= get_sparse_checkout_filename();
632 add_patterns_from_input(pl
, argc
, argv
,
633 use_stdin
? stdin
: NULL
);
635 memset(&existing
, 0, sizeof(existing
));
636 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
638 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
640 die(_("unable to load existing sparse-checkout patterns"));
641 free(sparse_filename
);
643 if (!existing
.use_cone_patterns
)
644 die(_("existing sparse-checkout patterns do not use cone mode"));
646 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
647 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
648 pe
->pattern
, &buffer
) ||
649 !hashmap_contains_parent(&pl
->parent_hashmap
,
650 pe
->pattern
, &buffer
)) {
651 strbuf_reset(&buffer
);
652 strbuf_addstr(&buffer
, pe
->pattern
);
653 insert_recursive_pattern(pl
, &buffer
);
657 clear_pattern_list(&existing
);
658 strbuf_release(&buffer
);
661 static void add_patterns_literal(int argc
, const char **argv
,
662 struct pattern_list
*pl
,
665 char *sparse_filename
= get_sparse_checkout_filename();
666 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
668 die(_("unable to load existing sparse-checkout patterns"));
669 free(sparse_filename
);
670 add_patterns_from_input(pl
, argc
, argv
, use_stdin
? stdin
: NULL
);
673 static int modify_pattern_list(int argc
, const char **argv
, int use_stdin
,
677 int changed_config
= 0;
678 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
682 if (core_sparse_checkout_cone
)
683 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
685 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
689 add_patterns_from_input(pl
, argc
, argv
,
690 use_stdin
? stdin
: NULL
);
694 if (!core_apply_sparse_checkout
) {
695 set_config(MODE_ALL_PATTERNS
);
696 core_apply_sparse_checkout
= 1;
700 result
= write_patterns_and_update(pl
);
702 if (result
&& changed_config
)
703 set_config(MODE_NO_PATTERNS
);
705 clear_pattern_list(pl
);
710 static void sanitize_paths(int argc
, const char **argv
,
711 const char *prefix
, int skip_checks
)
718 if (prefix
&& *prefix
&& core_sparse_checkout_cone
) {
720 * The args are not pathspecs, so unfortunately we
721 * cannot imitate how cmd_add() uses parse_pathspec().
723 int prefix_len
= strlen(prefix
);
725 for (i
= 0; i
< argc
; i
++)
726 argv
[i
] = prefix_path(prefix
, prefix_len
, argv
[i
]);
732 if (prefix
&& *prefix
&& !core_sparse_checkout_cone
)
733 die(_("please run from the toplevel directory in non-cone mode"));
735 if (core_sparse_checkout_cone
) {
736 for (i
= 0; i
< argc
; i
++) {
737 if (argv
[i
][0] == '/')
738 die(_("specify directories rather than patterns (no leading slash)"));
739 if (argv
[i
][0] == '!')
740 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
741 if (strpbrk(argv
[i
], "*?[]"))
742 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
746 for (i
= 0; i
< argc
; i
++) {
747 struct cache_entry
*ce
;
748 struct index_state
*index
= the_repository
->index
;
749 int pos
= index_name_pos(index
, argv
[i
], strlen(argv
[i
]));
753 ce
= index
->cache
[pos
];
754 if (S_ISSPARSEDIR(ce
->ce_mode
))
757 if (core_sparse_checkout_cone
)
758 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv
[i
]);
760 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
]);
764 static char const * const builtin_sparse_checkout_add_usage
[] = {
765 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
769 static struct sparse_checkout_add_opts
{
774 static int sparse_checkout_add(int argc
, const char **argv
, const char *prefix
)
776 static struct option builtin_sparse_checkout_add_options
[] = {
777 OPT_BOOL_F(0, "skip-checks", &add_opts
.skip_checks
,
778 N_("skip some sanity checks on the given paths that might give false positives"),
780 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
781 N_("read patterns from standard in")),
786 if (!core_apply_sparse_checkout
)
787 die(_("no sparse-checkout to add to"));
789 repo_read_index(the_repository
);
791 argc
= parse_options(argc
, argv
, prefix
,
792 builtin_sparse_checkout_add_options
,
793 builtin_sparse_checkout_add_usage
, 0);
795 sanitize_paths(argc
, argv
, prefix
, add_opts
.skip_checks
);
797 return modify_pattern_list(argc
, argv
, add_opts
.use_stdin
, ADD
);
800 static char const * const builtin_sparse_checkout_set_usage
[] = {
801 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
805 static struct sparse_checkout_set_opts
{
812 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
814 int default_patterns_nr
= 2;
815 const char *default_patterns
[] = {"/*", "!/*/", NULL
};
817 static struct option builtin_sparse_checkout_set_options
[] = {
818 OPT_BOOL(0, "cone", &set_opts
.cone_mode
,
819 N_("initialize the sparse-checkout in cone mode")),
820 OPT_BOOL(0, "sparse-index", &set_opts
.sparse_index
,
821 N_("toggle the use of a sparse index")),
822 OPT_BOOL_F(0, "skip-checks", &set_opts
.skip_checks
,
823 N_("skip some sanity checks on the given paths that might give false positives"),
825 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
826 N_("read patterns from standard in"),
832 repo_read_index(the_repository
);
834 set_opts
.cone_mode
= -1;
835 set_opts
.sparse_index
= -1;
837 argc
= parse_options(argc
, argv
, prefix
,
838 builtin_sparse_checkout_set_options
,
839 builtin_sparse_checkout_set_usage
, 0);
841 if (update_modes(&set_opts
.cone_mode
, &set_opts
.sparse_index
))
845 * Cone mode automatically specifies the toplevel directory. For
846 * non-cone mode, if nothing is specified, manually select just the
847 * top-level directory (much as 'init' would do).
849 if (!core_sparse_checkout_cone
&& !set_opts
.use_stdin
&& argc
== 0) {
850 argv
= default_patterns
;
851 argc
= default_patterns_nr
;
853 sanitize_paths(argc
, argv
, prefix
, set_opts
.skip_checks
);
856 return modify_pattern_list(argc
, argv
, set_opts
.use_stdin
, REPLACE
);
859 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
860 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
864 static struct sparse_checkout_reapply_opts
{
869 static int sparse_checkout_reapply(int argc
, const char **argv
,
872 static struct option builtin_sparse_checkout_reapply_options
[] = {
873 OPT_BOOL(0, "cone", &reapply_opts
.cone_mode
,
874 N_("initialize the sparse-checkout in cone mode")),
875 OPT_BOOL(0, "sparse-index", &reapply_opts
.sparse_index
,
876 N_("toggle the use of a sparse index")),
881 if (!core_apply_sparse_checkout
)
882 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
884 reapply_opts
.cone_mode
= -1;
885 reapply_opts
.sparse_index
= -1;
887 argc
= parse_options(argc
, argv
, prefix
,
888 builtin_sparse_checkout_reapply_options
,
889 builtin_sparse_checkout_reapply_usage
, 0);
891 repo_read_index(the_repository
);
893 if (update_modes(&reapply_opts
.cone_mode
, &reapply_opts
.sparse_index
))
896 return update_working_directory(NULL
);
899 static char const * const builtin_sparse_checkout_disable_usage
[] = {
900 "git sparse-checkout disable",
904 static int sparse_checkout_disable(int argc
, const char **argv
,
907 static struct option builtin_sparse_checkout_disable_options
[] = {
910 struct pattern_list pl
;
913 * We do not exit early if !core_apply_sparse_checkout; due to the
914 * ability for users to manually muck things up between
915 * direct editing of .git/info/sparse-checkout
916 * running read-tree -m u HEAD or update-index --skip-worktree
917 * direct toggling of config options
918 * users might end up with an index with SKIP_WORKTREE bit set on
919 * some files and not know how to undo it. So, here we just
920 * forcibly return to a dense checkout regardless of initial state.
924 argc
= parse_options(argc
, argv
, prefix
,
925 builtin_sparse_checkout_disable_options
,
926 builtin_sparse_checkout_disable_usage
, 0);
928 repo_read_index(the_repository
);
930 memset(&pl
, 0, sizeof(pl
));
931 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
932 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
933 pl
.use_cone_patterns
= 0;
934 core_apply_sparse_checkout
= 1;
936 add_pattern("/*", empty_base
, 0, &pl
, 0);
938 prepare_repo_settings(the_repository
);
939 the_repository
->settings
.sparse_index
= 0;
941 if (update_working_directory(&pl
))
942 die(_("error while refreshing working directory"));
944 clear_pattern_list(&pl
);
945 return set_config(MODE_NO_PATTERNS
);
948 static char const * const builtin_sparse_checkout_check_rules_usage
[] = {
949 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
950 "[--[no-]cone] [--rules-file <file>]"),
954 static struct sparse_checkout_check_rules_opts
{
956 int null_termination
;
960 static int check_rules(struct pattern_list
*pl
, int null_terminated
) {
961 struct strbuf line
= STRBUF_INIT
;
962 struct strbuf unquoted
= STRBUF_INIT
;
964 int line_terminator
= null_terminated
? 0 : '\n';
965 strbuf_getline_fn getline_fn
= null_terminated
? strbuf_getline_nul
967 the_repository
->index
->sparse_checkout_patterns
= pl
;
968 while (!getline_fn(&line
, stdin
)) {
970 if (!null_terminated
&& line
.buf
[0] == '"') {
971 strbuf_reset(&unquoted
);
972 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
973 die(_("unable to unquote C-style string '%s'"),
979 if (path_in_sparse_checkout(path
, the_repository
->index
))
980 write_name_quoted(path
, stdout
, line_terminator
);
982 strbuf_release(&line
);
983 strbuf_release(&unquoted
);
988 static int sparse_checkout_check_rules(int argc
, const char **argv
, const char *prefix
)
990 static struct option builtin_sparse_checkout_check_rules_options
[] = {
991 OPT_BOOL('z', NULL
, &check_rules_opts
.null_termination
,
992 N_("terminate input and output files by a NUL character")),
993 OPT_BOOL(0, "cone", &check_rules_opts
.cone_mode
,
994 N_("when used with --rules-file interpret patterns as cone mode patterns")),
995 OPT_FILENAME(0, "rules-file", &check_rules_opts
.rules_file
,
996 N_("use patterns in <file> instead of the current ones.")),
1002 struct pattern_list pl
= {0};
1003 char *sparse_filename
;
1004 check_rules_opts
.cone_mode
= -1;
1006 argc
= parse_options(argc
, argv
, prefix
,
1007 builtin_sparse_checkout_check_rules_options
,
1008 builtin_sparse_checkout_check_rules_usage
, 0);
1010 if (check_rules_opts
.rules_file
&& check_rules_opts
.cone_mode
< 0)
1011 check_rules_opts
.cone_mode
= 1;
1013 update_cone_mode(&check_rules_opts
.cone_mode
);
1014 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
1015 if (check_rules_opts
.rules_file
) {
1016 fp
= xfopen(check_rules_opts
.rules_file
, "r");
1017 add_patterns_from_input(&pl
, argc
, argv
, fp
);
1020 sparse_filename
= get_sparse_checkout_filename();
1021 if (add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
,
1023 die(_("unable to load existing sparse-checkout patterns"));
1024 free(sparse_filename
);
1027 ret
= check_rules(&pl
, check_rules_opts
.null_termination
);
1028 clear_pattern_list(&pl
);
1032 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
1034 parse_opt_subcommand_fn
*fn
= NULL
;
1035 struct option builtin_sparse_checkout_options
[] = {
1036 OPT_SUBCOMMAND("list", &fn
, sparse_checkout_list
),
1037 OPT_SUBCOMMAND("init", &fn
, sparse_checkout_init
),
1038 OPT_SUBCOMMAND("set", &fn
, sparse_checkout_set
),
1039 OPT_SUBCOMMAND("add", &fn
, sparse_checkout_add
),
1040 OPT_SUBCOMMAND("reapply", &fn
, sparse_checkout_reapply
),
1041 OPT_SUBCOMMAND("disable", &fn
, sparse_checkout_disable
),
1042 OPT_SUBCOMMAND("check-rules", &fn
, sparse_checkout_check_rules
),
1046 argc
= parse_options(argc
, argv
, prefix
,
1047 builtin_sparse_checkout_options
,
1048 builtin_sparse_checkout_usage
, 0);
1050 git_config(git_default_config
, NULL
);
1052 prepare_repo_settings(the_repository
);
1053 the_repository
->settings
.command_requires_full_index
= 0;
1055 return fn(argc
, argv
, prefix
);