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);
102 write_patterns_to_file(stdout
, &pl
);
103 clear_pattern_list(&pl
);
108 static void clean_tracked_sparse_directories(struct repository
*r
)
111 struct strbuf path
= STRBUF_INIT
;
113 struct string_list_item
*item
;
114 struct string_list sparse_dirs
= STRING_LIST_INIT_DUP
;
117 * If we are not using cone mode patterns, then we cannot
118 * delete directories outside of the sparse cone.
120 if (!r
|| !r
->index
|| !r
->worktree
)
122 if (init_sparse_checkout_patterns(r
->index
) ||
123 !r
->index
->sparse_checkout_patterns
->use_cone_patterns
)
127 * Use the sparse index as a data structure to assist finding
128 * directories that are safe to delete. This conversion to a
129 * sparse index will not delete directories that contain
130 * conflicted entries or submodules.
132 if (r
->index
->sparse_index
== INDEX_EXPANDED
) {
134 * If something, such as a merge conflict or other concern,
135 * prevents us from converting to a sparse index, then do
136 * not try deleting files.
138 if (convert_to_sparse(r
->index
, SPARSE_INDEX_MEMORY_ONLY
))
143 strbuf_addstr(&path
, r
->worktree
);
144 strbuf_complete(&path
, '/');
148 * Collect directories that have gone out of scope but also
149 * exist on disk, so there is some work to be done. We need to
150 * store the entries in a list before exploring, since that might
151 * expand the sparse-index again.
153 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
154 struct cache_entry
*ce
= r
->index
->cache
[i
];
156 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
157 repo_file_exists(r
, ce
->name
))
158 string_list_append(&sparse_dirs
, ce
->name
);
161 for_each_string_list_item(item
, &sparse_dirs
) {
162 struct dir_struct dir
= DIR_INIT
;
163 struct pathspec p
= { 0 };
164 struct strvec s
= STRVEC_INIT
;
166 strbuf_setlen(&path
, pathlen
);
167 strbuf_addstr(&path
, item
->string
);
169 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
171 setup_standard_excludes(&dir
);
172 strvec_push(&s
, path
.buf
);
174 parse_pathspec(&p
, PATHSPEC_GLOB
, 0, NULL
, s
.v
);
175 fill_directory(&dir
, r
->index
, &p
);
178 warning(_("directory '%s' contains untracked files,"
179 " but is not in the sparse-checkout cone"),
181 } else if (remove_dir_recursively(&path
, 0)) {
183 * Removal is "best effort". If something blocks
184 * the deletion, then continue with a warning.
186 warning(_("failed to remove directory '%s'"),
195 string_list_clear(&sparse_dirs
, 0);
196 strbuf_release(&path
);
199 ensure_full_index(r
->index
);
202 static int update_working_directory(struct pattern_list
*pl
)
204 enum update_sparsity_result result
;
205 struct unpack_trees_options o
;
206 struct lock_file lock_file
= LOCK_INIT
;
207 struct repository
*r
= the_repository
;
209 /* If no branch has been checked out, there are no updates to make. */
210 if (is_index_unborn(r
->index
))
211 return UPDATE_SPARSITY_SUCCESS
;
213 r
->index
->sparse_checkout_patterns
= pl
;
215 memset(&o
, 0, sizeof(o
));
216 o
.verbose_update
= isatty(2);
219 o
.src_index
= r
->index
;
220 o
.dst_index
= r
->index
;
221 o
.skip_sparse_checkout
= 0;
225 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
227 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
228 result
= update_sparsity(&o
, pl
);
229 clear_unpack_trees_porcelain(&o
);
231 if (result
== UPDATE_SPARSITY_WARNINGS
)
233 * We don't do any special handling of warnings from untracked
234 * files in the way or dirty entries that can't be removed.
236 result
= UPDATE_SPARSITY_SUCCESS
;
237 if (result
== UPDATE_SPARSITY_SUCCESS
)
238 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
240 rollback_lock_file(&lock_file
);
242 clean_tracked_sparse_directories(r
);
244 r
->index
->sparse_checkout_patterns
= NULL
;
248 static char *escaped_pattern(char *pattern
)
251 struct strbuf final
= STRBUF_INIT
;
254 if (is_glob_special(*p
))
255 strbuf_addch(&final
, '\\');
257 strbuf_addch(&final
, *p
);
261 return strbuf_detach(&final
, NULL
);
264 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
267 struct pattern_entry
*pe
;
268 struct hashmap_iter iter
;
269 struct string_list sl
= STRING_LIST_INIT_DUP
;
270 struct strbuf parent_pattern
= STRBUF_INIT
;
272 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
273 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
276 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
279 string_list_insert(&sl
, pe
->pattern
);
282 string_list_sort(&sl
);
283 string_list_remove_duplicates(&sl
, 0);
285 fprintf(fp
, "/*\n!/*/\n");
287 for (i
= 0; i
< sl
.nr
; i
++) {
288 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
291 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
295 string_list_clear(&sl
, 0);
297 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
298 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
301 string_list_insert(&sl
, pe
->pattern
);
304 strbuf_release(&parent_pattern
);
306 string_list_sort(&sl
);
307 string_list_remove_duplicates(&sl
, 0);
309 for (i
= 0; i
< sl
.nr
; i
++) {
310 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
311 fprintf(fp
, "%s/\n", pattern
);
316 static int write_patterns_and_update(struct pattern_list
*pl
)
318 char *sparse_filename
;
321 struct lock_file lk
= LOCK_INIT
;
324 sparse_filename
= get_sparse_checkout_filename();
326 if (safe_create_leading_directories(sparse_filename
))
327 die(_("failed to create directory for sparse-checkout file"));
329 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
331 free(sparse_filename
);
333 result
= update_working_directory(pl
);
335 rollback_lock_file(&lk
);
336 clear_pattern_list(pl
);
337 update_working_directory(NULL
);
341 fp
= xfdopen(fd
, "w");
343 if (core_sparse_checkout_cone
)
344 write_cone_to_file(fp
, pl
);
346 write_patterns_to_file(fp
, pl
);
349 commit_lock_file(&lk
);
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 /* Update to use worktree config, if not already. */
365 if (init_worktree_config(the_repository
)) {
366 error(_("failed to initialize worktree config"));
370 if (repo_config_set_worktree_gently(the_repository
,
371 "core.sparseCheckout",
372 mode
? "true" : "false") ||
373 repo_config_set_worktree_gently(the_repository
,
374 "core.sparseCheckoutCone",
375 mode
== MODE_CONE_PATTERNS
?
379 if (mode
== MODE_NO_PATTERNS
)
380 return set_sparse_index_config(the_repository
, 0);
385 static enum sparse_checkout_mode
update_cone_mode(int *cone_mode
) {
386 /* If not specified, use previous definition of cone mode */
387 if (*cone_mode
== -1 && core_apply_sparse_checkout
)
388 *cone_mode
= core_sparse_checkout_cone
;
390 /* Set cone/non-cone mode appropriately */
391 core_apply_sparse_checkout
= 1;
392 if (*cone_mode
== 1 || *cone_mode
== -1) {
393 core_sparse_checkout_cone
= 1;
394 return MODE_CONE_PATTERNS
;
396 core_sparse_checkout_cone
= 0;
397 return MODE_ALL_PATTERNS
;
400 static int update_modes(int *cone_mode
, int *sparse_index
)
402 int mode
, record_mode
;
404 /* Determine if we need to record the mode; ensure sparse checkout on */
405 record_mode
= (*cone_mode
!= -1) || !core_apply_sparse_checkout
;
407 mode
= update_cone_mode(cone_mode
);
408 if (record_mode
&& set_config(mode
))
411 /* Set sparse-index/non-sparse-index mode if specified */
412 if (*sparse_index
>= 0) {
413 if (set_sparse_index_config(the_repository
, *sparse_index
) < 0)
414 die(_("failed to modify sparse-index config"));
416 /* force an index rewrite */
417 repo_read_index(the_repository
);
418 the_repository
->index
->updated_workdir
= 1;
421 ensure_full_index(the_repository
->index
);
427 static char const * const builtin_sparse_checkout_init_usage
[] = {
428 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
432 static struct sparse_checkout_init_opts
{
437 static int sparse_checkout_init(int argc
, const char **argv
, const char *prefix
)
439 struct pattern_list pl
;
440 char *sparse_filename
;
442 struct object_id oid
;
443 struct strbuf pattern
= STRBUF_INIT
;
445 static struct option builtin_sparse_checkout_init_options
[] = {
446 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
447 N_("initialize the sparse-checkout in cone mode")),
448 OPT_BOOL(0, "sparse-index", &init_opts
.sparse_index
,
449 N_("toggle the use of a sparse index")),
454 repo_read_index(the_repository
);
456 init_opts
.cone_mode
= -1;
457 init_opts
.sparse_index
= -1;
459 argc
= parse_options(argc
, argv
, prefix
,
460 builtin_sparse_checkout_init_options
,
461 builtin_sparse_checkout_init_usage
, 0);
463 if (update_modes(&init_opts
.cone_mode
, &init_opts
.sparse_index
))
466 memset(&pl
, 0, sizeof(pl
));
468 sparse_filename
= get_sparse_checkout_filename();
469 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
471 /* If we already have a sparse-checkout file, use it. */
473 free(sparse_filename
);
474 return update_working_directory(NULL
);
477 if (repo_get_oid(the_repository
, "HEAD", &oid
)) {
480 /* assume we are in a fresh repo, but update the sparse-checkout file */
481 if (safe_create_leading_directories(sparse_filename
))
482 die(_("unable to create leading directories of %s"),
484 fp
= xfopen(sparse_filename
, "w");
486 die(_("failed to open '%s'"), sparse_filename
);
488 free(sparse_filename
);
489 fprintf(fp
, "/*\n!/*/\n");
494 strbuf_addstr(&pattern
, "/*");
495 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
496 strbuf_addstr(&pattern
, "!/*/");
497 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
498 pl
.use_cone_patterns
= init_opts
.cone_mode
;
500 return write_patterns_and_update(&pl
);
503 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
505 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
506 e
->patternlen
= path
->len
;
507 e
->pattern
= strbuf_detach(path
, NULL
);
508 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
510 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
512 while (e
->patternlen
) {
513 char *slash
= strrchr(e
->pattern
, '/');
514 char *oldpattern
= e
->pattern
;
517 if (!slash
|| slash
== e
->pattern
)
520 newlen
= slash
- e
->pattern
;
521 e
= xmalloc(sizeof(struct pattern_entry
));
522 e
->patternlen
= newlen
;
523 e
->pattern
= xstrndup(oldpattern
, newlen
);
524 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
526 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
527 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
531 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
535 strbuf_trim_trailing_dir_sep(line
);
537 if (strbuf_normalize_path(line
))
538 die(_("could not normalize path %s"), line
->buf
);
543 if (line
->buf
[0] != '/')
544 strbuf_insertstr(line
, 0, "/");
546 insert_recursive_pattern(pl
, line
);
549 static void add_patterns_from_input(struct pattern_list
*pl
,
550 int argc
, const char **argv
,
554 if (core_sparse_checkout_cone
) {
555 struct strbuf line
= STRBUF_INIT
;
557 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
558 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
559 pl
->use_cone_patterns
= 1;
562 struct strbuf unquoted
= STRBUF_INIT
;
563 while (!strbuf_getline(&line
, file
)) {
564 if (line
.buf
[0] == '"') {
565 strbuf_reset(&unquoted
);
566 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
567 die(_("unable to unquote C-style string '%s'"),
570 strbuf_swap(&unquoted
, &line
);
573 strbuf_to_cone_pattern(&line
, pl
);
576 strbuf_release(&unquoted
);
578 for (i
= 0; i
< argc
; i
++) {
579 strbuf_setlen(&line
, 0);
580 strbuf_addstr(&line
, argv
[i
]);
581 strbuf_to_cone_pattern(&line
, pl
);
586 struct strbuf line
= STRBUF_INIT
;
588 while (!strbuf_getline(&line
, file
)) {
590 char *buf
= strbuf_detach(&line
, &len
);
591 add_pattern(buf
, empty_base
, 0, pl
, 0);
594 for (i
= 0; i
< argc
; i
++)
595 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
605 static void add_patterns_cone_mode(int argc
, const char **argv
,
606 struct pattern_list
*pl
,
609 struct strbuf buffer
= STRBUF_INIT
;
610 struct pattern_entry
*pe
;
611 struct hashmap_iter iter
;
612 struct pattern_list existing
;
613 char *sparse_filename
= get_sparse_checkout_filename();
615 add_patterns_from_input(pl
, argc
, argv
,
616 use_stdin
? stdin
: NULL
);
618 memset(&existing
, 0, sizeof(existing
));
619 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
621 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
623 die(_("unable to load existing sparse-checkout patterns"));
624 free(sparse_filename
);
626 if (!existing
.use_cone_patterns
)
627 die(_("existing sparse-checkout patterns do not use cone mode"));
629 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
630 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
631 pe
->pattern
, &buffer
) ||
632 !hashmap_contains_parent(&pl
->parent_hashmap
,
633 pe
->pattern
, &buffer
)) {
634 strbuf_reset(&buffer
);
635 strbuf_addstr(&buffer
, pe
->pattern
);
636 insert_recursive_pattern(pl
, &buffer
);
640 clear_pattern_list(&existing
);
641 strbuf_release(&buffer
);
644 static void add_patterns_literal(int argc
, const char **argv
,
645 struct pattern_list
*pl
,
648 char *sparse_filename
= get_sparse_checkout_filename();
649 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
651 die(_("unable to load existing sparse-checkout patterns"));
652 free(sparse_filename
);
653 add_patterns_from_input(pl
, argc
, argv
, use_stdin
? stdin
: NULL
);
656 static int modify_pattern_list(int argc
, const char **argv
, int use_stdin
,
660 int changed_config
= 0;
661 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
665 if (core_sparse_checkout_cone
)
666 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
668 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
672 add_patterns_from_input(pl
, argc
, argv
,
673 use_stdin
? stdin
: NULL
);
677 if (!core_apply_sparse_checkout
) {
678 set_config(MODE_ALL_PATTERNS
);
679 core_apply_sparse_checkout
= 1;
683 result
= write_patterns_and_update(pl
);
685 if (result
&& changed_config
)
686 set_config(MODE_NO_PATTERNS
);
688 clear_pattern_list(pl
);
693 static void sanitize_paths(int argc
, const char **argv
,
694 const char *prefix
, int skip_checks
)
701 if (prefix
&& *prefix
&& core_sparse_checkout_cone
) {
703 * The args are not pathspecs, so unfortunately we
704 * cannot imitate how cmd_add() uses parse_pathspec().
706 int prefix_len
= strlen(prefix
);
708 for (i
= 0; i
< argc
; i
++)
709 argv
[i
] = prefix_path(prefix
, prefix_len
, argv
[i
]);
715 if (prefix
&& *prefix
&& !core_sparse_checkout_cone
)
716 die(_("please run from the toplevel directory in non-cone mode"));
718 if (core_sparse_checkout_cone
) {
719 for (i
= 0; i
< argc
; i
++) {
720 if (argv
[i
][0] == '/')
721 die(_("specify directories rather than patterns (no leading slash)"));
722 if (argv
[i
][0] == '!')
723 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
724 if (strpbrk(argv
[i
], "*?[]"))
725 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
729 for (i
= 0; i
< argc
; i
++) {
730 struct cache_entry
*ce
;
731 struct index_state
*index
= the_repository
->index
;
732 int pos
= index_name_pos(index
, argv
[i
], strlen(argv
[i
]));
736 ce
= index
->cache
[pos
];
737 if (S_ISSPARSEDIR(ce
->ce_mode
))
740 if (core_sparse_checkout_cone
)
741 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv
[i
]);
743 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
]);
747 static char const * const builtin_sparse_checkout_add_usage
[] = {
748 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
752 static struct sparse_checkout_add_opts
{
757 static int sparse_checkout_add(int argc
, const char **argv
, const char *prefix
)
759 static struct option builtin_sparse_checkout_add_options
[] = {
760 OPT_BOOL_F(0, "skip-checks", &add_opts
.skip_checks
,
761 N_("skip some sanity checks on the given paths that might give false positives"),
763 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
764 N_("read patterns from standard in")),
769 if (!core_apply_sparse_checkout
)
770 die(_("no sparse-checkout to add to"));
772 repo_read_index(the_repository
);
774 argc
= parse_options(argc
, argv
, prefix
,
775 builtin_sparse_checkout_add_options
,
776 builtin_sparse_checkout_add_usage
, 0);
778 sanitize_paths(argc
, argv
, prefix
, add_opts
.skip_checks
);
780 return modify_pattern_list(argc
, argv
, add_opts
.use_stdin
, ADD
);
783 static char const * const builtin_sparse_checkout_set_usage
[] = {
784 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
788 static struct sparse_checkout_set_opts
{
795 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
797 int default_patterns_nr
= 2;
798 const char *default_patterns
[] = {"/*", "!/*/", NULL
};
800 static struct option builtin_sparse_checkout_set_options
[] = {
801 OPT_BOOL(0, "cone", &set_opts
.cone_mode
,
802 N_("initialize the sparse-checkout in cone mode")),
803 OPT_BOOL(0, "sparse-index", &set_opts
.sparse_index
,
804 N_("toggle the use of a sparse index")),
805 OPT_BOOL_F(0, "skip-checks", &set_opts
.skip_checks
,
806 N_("skip some sanity checks on the given paths that might give false positives"),
808 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
809 N_("read patterns from standard in"),
815 repo_read_index(the_repository
);
817 set_opts
.cone_mode
= -1;
818 set_opts
.sparse_index
= -1;
820 argc
= parse_options(argc
, argv
, prefix
,
821 builtin_sparse_checkout_set_options
,
822 builtin_sparse_checkout_set_usage
, 0);
824 if (update_modes(&set_opts
.cone_mode
, &set_opts
.sparse_index
))
828 * Cone mode automatically specifies the toplevel directory. For
829 * non-cone mode, if nothing is specified, manually select just the
830 * top-level directory (much as 'init' would do).
832 if (!core_sparse_checkout_cone
&& !set_opts
.use_stdin
&& argc
== 0) {
833 argv
= default_patterns
;
834 argc
= default_patterns_nr
;
836 sanitize_paths(argc
, argv
, prefix
, set_opts
.skip_checks
);
839 return modify_pattern_list(argc
, argv
, set_opts
.use_stdin
, REPLACE
);
842 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
843 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
847 static struct sparse_checkout_reapply_opts
{
852 static int sparse_checkout_reapply(int argc
, const char **argv
,
855 static struct option builtin_sparse_checkout_reapply_options
[] = {
856 OPT_BOOL(0, "cone", &reapply_opts
.cone_mode
,
857 N_("initialize the sparse-checkout in cone mode")),
858 OPT_BOOL(0, "sparse-index", &reapply_opts
.sparse_index
,
859 N_("toggle the use of a sparse index")),
864 if (!core_apply_sparse_checkout
)
865 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
867 reapply_opts
.cone_mode
= -1;
868 reapply_opts
.sparse_index
= -1;
870 argc
= parse_options(argc
, argv
, prefix
,
871 builtin_sparse_checkout_reapply_options
,
872 builtin_sparse_checkout_reapply_usage
, 0);
874 repo_read_index(the_repository
);
876 if (update_modes(&reapply_opts
.cone_mode
, &reapply_opts
.sparse_index
))
879 return update_working_directory(NULL
);
882 static char const * const builtin_sparse_checkout_disable_usage
[] = {
883 "git sparse-checkout disable",
887 static int sparse_checkout_disable(int argc
, const char **argv
,
890 static struct option builtin_sparse_checkout_disable_options
[] = {
893 struct pattern_list pl
;
894 struct strbuf match_all
= STRBUF_INIT
;
897 * We do not exit early if !core_apply_sparse_checkout; due to the
898 * ability for users to manually muck things up between
899 * direct editing of .git/info/sparse-checkout
900 * running read-tree -m u HEAD or update-index --skip-worktree
901 * direct toggling of config options
902 * users might end up with an index with SKIP_WORKTREE bit set on
903 * some files and not know how to undo it. So, here we just
904 * forcibly return to a dense checkout regardless of initial state.
908 argc
= parse_options(argc
, argv
, prefix
,
909 builtin_sparse_checkout_disable_options
,
910 builtin_sparse_checkout_disable_usage
, 0);
912 repo_read_index(the_repository
);
914 memset(&pl
, 0, sizeof(pl
));
915 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
916 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
917 pl
.use_cone_patterns
= 0;
918 core_apply_sparse_checkout
= 1;
920 strbuf_addstr(&match_all
, "/*");
921 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
923 prepare_repo_settings(the_repository
);
924 the_repository
->settings
.sparse_index
= 0;
926 if (update_working_directory(&pl
))
927 die(_("error while refreshing working directory"));
929 clear_pattern_list(&pl
);
930 return set_config(MODE_NO_PATTERNS
);
933 static char const * const builtin_sparse_checkout_check_rules_usage
[] = {
934 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
935 "[--[no-]cone] [--rules-file <file>]"),
939 static struct sparse_checkout_check_rules_opts
{
941 int null_termination
;
945 static int check_rules(struct pattern_list
*pl
, int null_terminated
) {
946 struct strbuf line
= STRBUF_INIT
;
947 struct strbuf unquoted
= STRBUF_INIT
;
949 int line_terminator
= null_terminated
? 0 : '\n';
950 strbuf_getline_fn getline_fn
= null_terminated
? strbuf_getline_nul
952 the_repository
->index
->sparse_checkout_patterns
= pl
;
953 while (!getline_fn(&line
, stdin
)) {
955 if (!null_terminated
&& line
.buf
[0] == '"') {
956 strbuf_reset(&unquoted
);
957 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
958 die(_("unable to unquote C-style string '%s'"),
964 if (path_in_sparse_checkout(path
, the_repository
->index
))
965 write_name_quoted(path
, stdout
, line_terminator
);
967 strbuf_release(&line
);
968 strbuf_release(&unquoted
);
973 static int sparse_checkout_check_rules(int argc
, const char **argv
, const char *prefix
)
975 static struct option builtin_sparse_checkout_check_rules_options
[] = {
976 OPT_BOOL('z', NULL
, &check_rules_opts
.null_termination
,
977 N_("terminate input and output files by a NUL character")),
978 OPT_BOOL(0, "cone", &check_rules_opts
.cone_mode
,
979 N_("when used with --rules-file interpret patterns as cone mode patterns")),
980 OPT_FILENAME(0, "rules-file", &check_rules_opts
.rules_file
,
981 N_("use patterns in <file> instead of the current ones.")),
987 struct pattern_list pl
= {0};
988 char *sparse_filename
;
989 check_rules_opts
.cone_mode
= -1;
991 argc
= parse_options(argc
, argv
, prefix
,
992 builtin_sparse_checkout_check_rules_options
,
993 builtin_sparse_checkout_check_rules_usage
, 0);
995 if (check_rules_opts
.rules_file
&& check_rules_opts
.cone_mode
< 0)
996 check_rules_opts
.cone_mode
= 1;
998 update_cone_mode(&check_rules_opts
.cone_mode
);
999 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
1000 if (check_rules_opts
.rules_file
) {
1001 fp
= xfopen(check_rules_opts
.rules_file
, "r");
1002 add_patterns_from_input(&pl
, argc
, argv
, fp
);
1005 sparse_filename
= get_sparse_checkout_filename();
1006 if (add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
,
1008 die(_("unable to load existing sparse-checkout patterns"));
1009 free(sparse_filename
);
1012 ret
= check_rules(&pl
, check_rules_opts
.null_termination
);
1013 clear_pattern_list(&pl
);
1017 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
1019 parse_opt_subcommand_fn
*fn
= NULL
;
1020 struct option builtin_sparse_checkout_options
[] = {
1021 OPT_SUBCOMMAND("list", &fn
, sparse_checkout_list
),
1022 OPT_SUBCOMMAND("init", &fn
, sparse_checkout_init
),
1023 OPT_SUBCOMMAND("set", &fn
, sparse_checkout_set
),
1024 OPT_SUBCOMMAND("add", &fn
, sparse_checkout_add
),
1025 OPT_SUBCOMMAND("reapply", &fn
, sparse_checkout_reapply
),
1026 OPT_SUBCOMMAND("disable", &fn
, sparse_checkout_disable
),
1027 OPT_SUBCOMMAND("check-rules", &fn
, sparse_checkout_check_rules
),
1031 argc
= parse_options(argc
, argv
, prefix
,
1032 builtin_sparse_checkout_options
,
1033 builtin_sparse_checkout_usage
, 0);
1035 git_config(git_default_config
, NULL
);
1037 prepare_repo_settings(the_repository
);
1038 the_repository
->settings
.command_requires_full_index
= 0;
1040 return fn(argc
, argv
, prefix
);