1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "environment.h"
7 #include "object-file.h"
8 #include "object-name.h"
9 #include "parse-options.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
;
330 struct lock_file lk
= LOCK_INIT
;
333 sparse_filename
= get_sparse_checkout_filename();
335 if (safe_create_leading_directories(sparse_filename
))
336 die(_("failed to create directory for sparse-checkout file"));
338 hold_lock_file_for_update(&lk
, sparse_filename
, LOCK_DIE_ON_ERROR
);
340 result
= update_working_directory(pl
);
342 rollback_lock_file(&lk
);
343 update_working_directory(NULL
);
347 fp
= fdopen_lock_file(&lk
, "w");
349 die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk
));
351 if (core_sparse_checkout_cone
)
352 write_cone_to_file(fp
, pl
);
354 write_patterns_to_file(fp
, pl
);
356 if (commit_lock_file(&lk
))
357 die_errno(_("unable to write %s"), sparse_filename
);
360 clear_pattern_list(pl
);
361 free(sparse_filename
);
365 enum sparse_checkout_mode
{
366 MODE_NO_PATTERNS
= 0,
367 MODE_ALL_PATTERNS
= 1,
368 MODE_CONE_PATTERNS
= 2,
371 static int set_config(enum sparse_checkout_mode mode
)
373 /* Update to use worktree config, if not already. */
374 if (init_worktree_config(the_repository
)) {
375 error(_("failed to initialize worktree config"));
379 if (repo_config_set_worktree_gently(the_repository
,
380 "core.sparseCheckout",
381 mode
? "true" : "false") ||
382 repo_config_set_worktree_gently(the_repository
,
383 "core.sparseCheckoutCone",
384 mode
== MODE_CONE_PATTERNS
?
388 if (mode
== MODE_NO_PATTERNS
)
389 return set_sparse_index_config(the_repository
, 0);
394 static enum sparse_checkout_mode
update_cone_mode(int *cone_mode
) {
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 || *cone_mode
== -1) {
402 core_sparse_checkout_cone
= 1;
403 return MODE_CONE_PATTERNS
;
405 core_sparse_checkout_cone
= 0;
406 return MODE_ALL_PATTERNS
;
409 static int update_modes(int *cone_mode
, int *sparse_index
)
411 int mode
, record_mode
;
413 /* Determine if we need to record the mode; ensure sparse checkout on */
414 record_mode
= (*cone_mode
!= -1) || !core_apply_sparse_checkout
;
416 mode
= update_cone_mode(cone_mode
);
417 if (record_mode
&& set_config(mode
))
420 /* Set sparse-index/non-sparse-index mode if specified */
421 if (*sparse_index
>= 0) {
422 if (set_sparse_index_config(the_repository
, *sparse_index
) < 0)
423 die(_("failed to modify sparse-index config"));
425 /* force an index rewrite */
426 repo_read_index(the_repository
);
427 the_repository
->index
->updated_workdir
= 1;
430 ensure_full_index(the_repository
->index
);
436 static char const * const builtin_sparse_checkout_init_usage
[] = {
437 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
441 static struct sparse_checkout_init_opts
{
446 static int sparse_checkout_init(int argc
, const char **argv
, const char *prefix
)
448 struct pattern_list pl
;
449 char *sparse_filename
;
451 struct object_id oid
;
453 static struct option builtin_sparse_checkout_init_options
[] = {
454 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
455 N_("initialize the sparse-checkout in cone mode")),
456 OPT_BOOL(0, "sparse-index", &init_opts
.sparse_index
,
457 N_("toggle the use of a sparse index")),
462 repo_read_index(the_repository
);
464 init_opts
.cone_mode
= -1;
465 init_opts
.sparse_index
= -1;
467 argc
= parse_options(argc
, argv
, prefix
,
468 builtin_sparse_checkout_init_options
,
469 builtin_sparse_checkout_init_usage
, 0);
471 if (update_modes(&init_opts
.cone_mode
, &init_opts
.sparse_index
))
474 memset(&pl
, 0, sizeof(pl
));
476 sparse_filename
= get_sparse_checkout_filename();
477 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
479 /* If we already have a sparse-checkout file, use it. */
481 free(sparse_filename
);
482 clear_pattern_list(&pl
);
483 return update_working_directory(NULL
);
486 if (repo_get_oid(the_repository
, "HEAD", &oid
)) {
489 /* assume we are in a fresh repo, but update the sparse-checkout file */
490 if (safe_create_leading_directories(sparse_filename
))
491 die(_("unable to create leading directories of %s"),
493 fp
= xfopen(sparse_filename
, "w");
495 die(_("failed to open '%s'"), sparse_filename
);
497 free(sparse_filename
);
498 fprintf(fp
, "/*\n!/*/\n");
503 free(sparse_filename
);
505 add_pattern("/*", empty_base
, 0, &pl
, 0);
506 add_pattern("!/*/", empty_base
, 0, &pl
, 0);
507 pl
.use_cone_patterns
= init_opts
.cone_mode
;
509 return write_patterns_and_update(&pl
);
512 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
514 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
515 e
->patternlen
= path
->len
;
516 e
->pattern
= strbuf_detach(path
, NULL
);
517 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
519 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
521 while (e
->patternlen
) {
522 char *slash
= strrchr(e
->pattern
, '/');
523 char *oldpattern
= e
->pattern
;
525 struct pattern_entry
*dup
;
527 if (!slash
|| slash
== e
->pattern
)
530 newlen
= slash
- e
->pattern
;
531 e
= xmalloc(sizeof(struct pattern_entry
));
532 e
->patternlen
= newlen
;
533 e
->pattern
= xstrndup(oldpattern
, newlen
);
534 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
536 dup
= hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
);
538 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
547 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
551 strbuf_trim_trailing_dir_sep(line
);
553 if (strbuf_normalize_path(line
))
554 die(_("could not normalize path %s"), line
->buf
);
559 if (line
->buf
[0] != '/')
560 strbuf_insertstr(line
, 0, "/");
562 insert_recursive_pattern(pl
, line
);
565 static void add_patterns_from_input(struct pattern_list
*pl
,
566 int argc
, const char **argv
,
570 if (core_sparse_checkout_cone
) {
571 struct strbuf line
= STRBUF_INIT
;
573 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
574 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
575 pl
->use_cone_patterns
= 1;
578 struct strbuf unquoted
= STRBUF_INIT
;
579 while (!strbuf_getline(&line
, file
)) {
580 if (line
.buf
[0] == '"') {
581 strbuf_reset(&unquoted
);
582 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
583 die(_("unable to unquote C-style string '%s'"),
586 strbuf_swap(&unquoted
, &line
);
589 strbuf_to_cone_pattern(&line
, pl
);
592 strbuf_release(&unquoted
);
594 for (i
= 0; i
< argc
; i
++) {
595 strbuf_setlen(&line
, 0);
596 strbuf_addstr(&line
, argv
[i
]);
597 strbuf_to_cone_pattern(&line
, pl
);
600 strbuf_release(&line
);
603 struct strbuf line
= STRBUF_INIT
;
605 while (!strbuf_getline(&line
, file
))
606 add_pattern(line
.buf
, empty_base
, 0, pl
, 0);
608 strbuf_release(&line
);
610 for (i
= 0; i
< argc
; i
++)
611 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
621 static void add_patterns_cone_mode(int argc
, const char **argv
,
622 struct pattern_list
*pl
,
625 struct strbuf buffer
= STRBUF_INIT
;
626 struct pattern_entry
*pe
;
627 struct hashmap_iter iter
;
628 struct pattern_list existing
;
629 char *sparse_filename
= get_sparse_checkout_filename();
631 add_patterns_from_input(pl
, argc
, argv
,
632 use_stdin
? stdin
: NULL
);
634 memset(&existing
, 0, sizeof(existing
));
635 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
637 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
639 die(_("unable to load existing sparse-checkout patterns"));
640 free(sparse_filename
);
642 if (!existing
.use_cone_patterns
)
643 die(_("existing sparse-checkout patterns do not use cone mode"));
645 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
646 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
647 pe
->pattern
, &buffer
) ||
648 !hashmap_contains_parent(&pl
->parent_hashmap
,
649 pe
->pattern
, &buffer
)) {
650 strbuf_reset(&buffer
);
651 strbuf_addstr(&buffer
, pe
->pattern
);
652 insert_recursive_pattern(pl
, &buffer
);
656 clear_pattern_list(&existing
);
657 strbuf_release(&buffer
);
660 static void add_patterns_literal(int argc
, const char **argv
,
661 struct pattern_list
*pl
,
664 char *sparse_filename
= get_sparse_checkout_filename();
665 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
667 die(_("unable to load existing sparse-checkout patterns"));
668 free(sparse_filename
);
669 add_patterns_from_input(pl
, argc
, argv
, use_stdin
? stdin
: NULL
);
672 static int modify_pattern_list(int argc
, const char **argv
, int use_stdin
,
676 int changed_config
= 0;
677 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
681 if (core_sparse_checkout_cone
)
682 add_patterns_cone_mode(argc
, argv
, pl
, use_stdin
);
684 add_patterns_literal(argc
, argv
, pl
, use_stdin
);
688 add_patterns_from_input(pl
, argc
, argv
,
689 use_stdin
? stdin
: NULL
);
693 if (!core_apply_sparse_checkout
) {
694 set_config(MODE_ALL_PATTERNS
);
695 core_apply_sparse_checkout
= 1;
699 result
= write_patterns_and_update(pl
);
701 if (result
&& changed_config
)
702 set_config(MODE_NO_PATTERNS
);
704 clear_pattern_list(pl
);
709 static void sanitize_paths(int argc
, const char **argv
,
710 const char *prefix
, int skip_checks
)
717 if (prefix
&& *prefix
&& core_sparse_checkout_cone
) {
719 * The args are not pathspecs, so unfortunately we
720 * cannot imitate how cmd_add() uses parse_pathspec().
722 int prefix_len
= strlen(prefix
);
724 for (i
= 0; i
< argc
; i
++)
725 argv
[i
] = prefix_path(prefix
, prefix_len
, argv
[i
]);
731 if (prefix
&& *prefix
&& !core_sparse_checkout_cone
)
732 die(_("please run from the toplevel directory in non-cone mode"));
734 if (core_sparse_checkout_cone
) {
735 for (i
= 0; i
< argc
; i
++) {
736 if (argv
[i
][0] == '/')
737 die(_("specify directories rather than patterns (no leading slash)"));
738 if (argv
[i
][0] == '!')
739 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
740 if (strpbrk(argv
[i
], "*?[]"))
741 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
745 for (i
= 0; i
< argc
; i
++) {
746 struct cache_entry
*ce
;
747 struct index_state
*index
= the_repository
->index
;
748 int pos
= index_name_pos(index
, argv
[i
], strlen(argv
[i
]));
752 ce
= index
->cache
[pos
];
753 if (S_ISSPARSEDIR(ce
->ce_mode
))
756 if (core_sparse_checkout_cone
)
757 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv
[i
]);
759 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
]);
763 static char const * const builtin_sparse_checkout_add_usage
[] = {
764 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
768 static struct sparse_checkout_add_opts
{
773 static int sparse_checkout_add(int argc
, const char **argv
, const char *prefix
)
775 static struct option builtin_sparse_checkout_add_options
[] = {
776 OPT_BOOL_F(0, "skip-checks", &add_opts
.skip_checks
,
777 N_("skip some sanity checks on the given paths that might give false positives"),
779 OPT_BOOL(0, "stdin", &add_opts
.use_stdin
,
780 N_("read patterns from standard in")),
785 if (!core_apply_sparse_checkout
)
786 die(_("no sparse-checkout to add to"));
788 repo_read_index(the_repository
);
790 argc
= parse_options(argc
, argv
, prefix
,
791 builtin_sparse_checkout_add_options
,
792 builtin_sparse_checkout_add_usage
, 0);
794 sanitize_paths(argc
, argv
, prefix
, add_opts
.skip_checks
);
796 return modify_pattern_list(argc
, argv
, add_opts
.use_stdin
, ADD
);
799 static char const * const builtin_sparse_checkout_set_usage
[] = {
800 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
804 static struct sparse_checkout_set_opts
{
811 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
813 int default_patterns_nr
= 2;
814 const char *default_patterns
[] = {"/*", "!/*/", NULL
};
816 static struct option builtin_sparse_checkout_set_options
[] = {
817 OPT_BOOL(0, "cone", &set_opts
.cone_mode
,
818 N_("initialize the sparse-checkout in cone mode")),
819 OPT_BOOL(0, "sparse-index", &set_opts
.sparse_index
,
820 N_("toggle the use of a sparse index")),
821 OPT_BOOL_F(0, "skip-checks", &set_opts
.skip_checks
,
822 N_("skip some sanity checks on the given paths that might give false positives"),
824 OPT_BOOL_F(0, "stdin", &set_opts
.use_stdin
,
825 N_("read patterns from standard in"),
831 repo_read_index(the_repository
);
833 set_opts
.cone_mode
= -1;
834 set_opts
.sparse_index
= -1;
836 argc
= parse_options(argc
, argv
, prefix
,
837 builtin_sparse_checkout_set_options
,
838 builtin_sparse_checkout_set_usage
, 0);
840 if (update_modes(&set_opts
.cone_mode
, &set_opts
.sparse_index
))
844 * Cone mode automatically specifies the toplevel directory. For
845 * non-cone mode, if nothing is specified, manually select just the
846 * top-level directory (much as 'init' would do).
848 if (!core_sparse_checkout_cone
&& !set_opts
.use_stdin
&& argc
== 0) {
849 argv
= default_patterns
;
850 argc
= default_patterns_nr
;
852 sanitize_paths(argc
, argv
, prefix
, set_opts
.skip_checks
);
855 return modify_pattern_list(argc
, argv
, set_opts
.use_stdin
, REPLACE
);
858 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
859 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
863 static struct sparse_checkout_reapply_opts
{
868 static int sparse_checkout_reapply(int argc
, const char **argv
,
871 static struct option builtin_sparse_checkout_reapply_options
[] = {
872 OPT_BOOL(0, "cone", &reapply_opts
.cone_mode
,
873 N_("initialize the sparse-checkout in cone mode")),
874 OPT_BOOL(0, "sparse-index", &reapply_opts
.sparse_index
,
875 N_("toggle the use of a sparse index")),
880 if (!core_apply_sparse_checkout
)
881 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
883 reapply_opts
.cone_mode
= -1;
884 reapply_opts
.sparse_index
= -1;
886 argc
= parse_options(argc
, argv
, prefix
,
887 builtin_sparse_checkout_reapply_options
,
888 builtin_sparse_checkout_reapply_usage
, 0);
890 repo_read_index(the_repository
);
892 if (update_modes(&reapply_opts
.cone_mode
, &reapply_opts
.sparse_index
))
895 return update_working_directory(NULL
);
898 static char const * const builtin_sparse_checkout_disable_usage
[] = {
899 "git sparse-checkout disable",
903 static int sparse_checkout_disable(int argc
, const char **argv
,
906 static struct option builtin_sparse_checkout_disable_options
[] = {
909 struct pattern_list pl
;
912 * We do not exit early if !core_apply_sparse_checkout; due to the
913 * ability for users to manually muck things up between
914 * direct editing of .git/info/sparse-checkout
915 * running read-tree -m u HEAD or update-index --skip-worktree
916 * direct toggling of config options
917 * users might end up with an index with SKIP_WORKTREE bit set on
918 * some files and not know how to undo it. So, here we just
919 * forcibly return to a dense checkout regardless of initial state.
923 argc
= parse_options(argc
, argv
, prefix
,
924 builtin_sparse_checkout_disable_options
,
925 builtin_sparse_checkout_disable_usage
, 0);
928 * Disable the advice message for expanding a sparse index, as we
929 * are expecting to do that when disabling sparse-checkout.
931 give_advice_on_expansion
= 0;
932 repo_read_index(the_repository
);
934 memset(&pl
, 0, sizeof(pl
));
935 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
936 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
937 pl
.use_cone_patterns
= 0;
938 core_apply_sparse_checkout
= 1;
940 add_pattern("/*", empty_base
, 0, &pl
, 0);
942 prepare_repo_settings(the_repository
);
943 the_repository
->settings
.sparse_index
= 0;
945 if (update_working_directory(&pl
))
946 die(_("error while refreshing working directory"));
948 clear_pattern_list(&pl
);
949 return set_config(MODE_NO_PATTERNS
);
952 static char const * const builtin_sparse_checkout_check_rules_usage
[] = {
953 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
954 "[--[no-]cone] [--rules-file <file>]"),
958 static struct sparse_checkout_check_rules_opts
{
960 int null_termination
;
964 static int check_rules(struct pattern_list
*pl
, int null_terminated
) {
965 struct strbuf line
= STRBUF_INIT
;
966 struct strbuf unquoted
= STRBUF_INIT
;
968 int line_terminator
= null_terminated
? 0 : '\n';
969 strbuf_getline_fn getline_fn
= null_terminated
? strbuf_getline_nul
971 the_repository
->index
->sparse_checkout_patterns
= pl
;
972 while (!getline_fn(&line
, stdin
)) {
974 if (!null_terminated
&& line
.buf
[0] == '"') {
975 strbuf_reset(&unquoted
);
976 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
977 die(_("unable to unquote C-style string '%s'"),
983 if (path_in_sparse_checkout(path
, the_repository
->index
))
984 write_name_quoted(path
, stdout
, line_terminator
);
986 strbuf_release(&line
);
987 strbuf_release(&unquoted
);
992 static int sparse_checkout_check_rules(int argc
, const char **argv
, const char *prefix
)
994 static struct option builtin_sparse_checkout_check_rules_options
[] = {
995 OPT_BOOL('z', NULL
, &check_rules_opts
.null_termination
,
996 N_("terminate input and output files by a NUL character")),
997 OPT_BOOL(0, "cone", &check_rules_opts
.cone_mode
,
998 N_("when used with --rules-file interpret patterns as cone mode patterns")),
999 OPT_FILENAME(0, "rules-file", &check_rules_opts
.rules_file
,
1000 N_("use patterns in <file> instead of the current ones.")),
1006 struct pattern_list pl
= {0};
1007 char *sparse_filename
;
1008 check_rules_opts
.cone_mode
= -1;
1010 argc
= parse_options(argc
, argv
, prefix
,
1011 builtin_sparse_checkout_check_rules_options
,
1012 builtin_sparse_checkout_check_rules_usage
, 0);
1014 if (check_rules_opts
.rules_file
&& check_rules_opts
.cone_mode
< 0)
1015 check_rules_opts
.cone_mode
= 1;
1017 update_cone_mode(&check_rules_opts
.cone_mode
);
1018 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
1019 if (check_rules_opts
.rules_file
) {
1020 fp
= xfopen(check_rules_opts
.rules_file
, "r");
1021 add_patterns_from_input(&pl
, argc
, argv
, fp
);
1024 sparse_filename
= get_sparse_checkout_filename();
1025 if (add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
,
1027 die(_("unable to load existing sparse-checkout patterns"));
1028 free(sparse_filename
);
1031 ret
= check_rules(&pl
, check_rules_opts
.null_termination
);
1032 clear_pattern_list(&pl
);
1033 free(check_rules_opts
.rules_file
);
1037 int cmd_sparse_checkout(int argc
,
1040 struct repository
*repo UNUSED
)
1042 parse_opt_subcommand_fn
*fn
= NULL
;
1043 struct option builtin_sparse_checkout_options
[] = {
1044 OPT_SUBCOMMAND("list", &fn
, sparse_checkout_list
),
1045 OPT_SUBCOMMAND("init", &fn
, sparse_checkout_init
),
1046 OPT_SUBCOMMAND("set", &fn
, sparse_checkout_set
),
1047 OPT_SUBCOMMAND("add", &fn
, sparse_checkout_add
),
1048 OPT_SUBCOMMAND("reapply", &fn
, sparse_checkout_reapply
),
1049 OPT_SUBCOMMAND("disable", &fn
, sparse_checkout_disable
),
1050 OPT_SUBCOMMAND("check-rules", &fn
, sparse_checkout_check_rules
),
1054 argc
= parse_options(argc
, argv
, prefix
,
1055 builtin_sparse_checkout_options
,
1056 builtin_sparse_checkout_usage
, 0);
1058 git_config(git_default_config
, NULL
);
1060 prepare_repo_settings(the_repository
);
1061 the_repository
->settings
.command_requires_full_index
= 0;
1063 return fn(argc
, argv
, prefix
);