4 #include "parse-options.h"
6 #include "repository.h"
7 #include "run-command.h"
9 #include "string-list.h"
11 #include "cache-tree.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 #include "wt-status.h"
17 #include "sparse-index.h"
19 static const char *empty_base
= "";
21 static char const * const builtin_sparse_checkout_usage
[] = {
22 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
26 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
30 for (i
= 0; i
< pl
->nr
; i
++) {
31 struct path_pattern
*p
= pl
->patterns
[i
];
33 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
36 fprintf(fp
, "%s", p
->pattern
);
38 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
45 static char const * const builtin_sparse_checkout_list_usage
[] = {
46 N_("git sparse-checkout list"),
50 static int sparse_checkout_list(int argc
, const char **argv
)
52 static struct option builtin_sparse_checkout_list_options
[] = {
55 struct pattern_list pl
;
56 char *sparse_filename
;
59 argc
= parse_options(argc
, argv
, NULL
,
60 builtin_sparse_checkout_list_options
,
61 builtin_sparse_checkout_list_usage
, 0);
63 memset(&pl
, 0, sizeof(pl
));
65 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
67 sparse_filename
= get_sparse_checkout_filename();
68 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
69 free(sparse_filename
);
72 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
76 if (pl
.use_cone_patterns
) {
78 struct pattern_entry
*pe
;
79 struct hashmap_iter iter
;
80 struct string_list sl
= STRING_LIST_INIT_DUP
;
82 hashmap_for_each_entry(&pl
.recursive_hashmap
, &iter
, pe
, ent
) {
83 /* pe->pattern starts with "/", skip it */
84 string_list_insert(&sl
, pe
->pattern
+ 1);
87 string_list_sort(&sl
);
89 for (i
= 0; i
< sl
.nr
; i
++) {
90 quote_c_style(sl
.items
[i
].string
, NULL
, stdout
, 0);
97 write_patterns_to_file(stdout
, &pl
);
98 clear_pattern_list(&pl
);
103 static int update_working_directory(struct pattern_list
*pl
)
105 enum update_sparsity_result result
;
106 struct unpack_trees_options o
;
107 struct lock_file lock_file
= LOCK_INIT
;
108 struct repository
*r
= the_repository
;
110 /* If no branch has been checked out, there are no updates to make. */
111 if (is_index_unborn(r
->index
))
112 return UPDATE_SPARSITY_SUCCESS
;
114 r
->index
->sparse_checkout_patterns
= pl
;
116 memset(&o
, 0, sizeof(o
));
117 o
.verbose_update
= isatty(2);
120 o
.src_index
= r
->index
;
121 o
.dst_index
= r
->index
;
122 o
.skip_sparse_checkout
= 0;
127 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
129 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
130 result
= update_sparsity(&o
);
131 clear_unpack_trees_porcelain(&o
);
133 if (result
== UPDATE_SPARSITY_WARNINGS
)
135 * We don't do any special handling of warnings from untracked
136 * files in the way or dirty entries that can't be removed.
138 result
= UPDATE_SPARSITY_SUCCESS
;
139 if (result
== UPDATE_SPARSITY_SUCCESS
)
140 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
142 rollback_lock_file(&lock_file
);
144 r
->index
->sparse_checkout_patterns
= NULL
;
148 static char *escaped_pattern(char *pattern
)
151 struct strbuf final
= STRBUF_INIT
;
154 if (is_glob_special(*p
))
155 strbuf_addch(&final
, '\\');
157 strbuf_addch(&final
, *p
);
161 return strbuf_detach(&final
, NULL
);
164 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
167 struct pattern_entry
*pe
;
168 struct hashmap_iter iter
;
169 struct string_list sl
= STRING_LIST_INIT_DUP
;
170 struct strbuf parent_pattern
= STRBUF_INIT
;
172 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
173 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
176 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
179 string_list_insert(&sl
, pe
->pattern
);
182 string_list_sort(&sl
);
183 string_list_remove_duplicates(&sl
, 0);
185 fprintf(fp
, "/*\n!/*/\n");
187 for (i
= 0; i
< sl
.nr
; i
++) {
188 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
191 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
195 string_list_clear(&sl
, 0);
197 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
198 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
201 string_list_insert(&sl
, pe
->pattern
);
204 strbuf_release(&parent_pattern
);
206 string_list_sort(&sl
);
207 string_list_remove_duplicates(&sl
, 0);
209 for (i
= 0; i
< sl
.nr
; i
++) {
210 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
211 fprintf(fp
, "%s/\n", pattern
);
216 static int write_patterns_and_update(struct pattern_list
*pl
)
218 char *sparse_filename
;
221 struct lock_file lk
= LOCK_INIT
;
224 sparse_filename
= get_sparse_checkout_filename();
226 if (safe_create_leading_directories(sparse_filename
))
227 die(_("failed to create directory for sparse-checkout file"));
229 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
232 result
= update_working_directory(pl
);
234 rollback_lock_file(&lk
);
235 free(sparse_filename
);
236 clear_pattern_list(pl
);
237 update_working_directory(NULL
);
241 fp
= xfdopen(fd
, "w");
243 if (core_sparse_checkout_cone
)
244 write_cone_to_file(fp
, pl
);
246 write_patterns_to_file(fp
, pl
);
249 commit_lock_file(&lk
);
251 free(sparse_filename
);
252 clear_pattern_list(pl
);
257 enum sparse_checkout_mode
{
258 MODE_NO_PATTERNS
= 0,
259 MODE_ALL_PATTERNS
= 1,
260 MODE_CONE_PATTERNS
= 2,
263 static int set_config(enum sparse_checkout_mode mode
)
265 const char *config_path
;
267 if (upgrade_repository_format(1) < 0)
268 die(_("unable to upgrade repository format to enable worktreeConfig"));
269 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
270 error(_("failed to set extensions.worktreeConfig setting"));
274 config_path
= git_path("config.worktree");
275 git_config_set_in_file_gently(config_path
,
276 "core.sparseCheckout",
277 mode
? "true" : NULL
);
279 git_config_set_in_file_gently(config_path
,
280 "core.sparseCheckoutCone",
281 mode
== MODE_CONE_PATTERNS
? "true" : NULL
);
283 if (mode
== MODE_NO_PATTERNS
)
284 set_sparse_index_config(the_repository
, 0);
289 static char const * const builtin_sparse_checkout_init_usage
[] = {
290 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
294 static struct sparse_checkout_init_opts
{
299 static int sparse_checkout_init(int argc
, const char **argv
)
301 struct pattern_list pl
;
302 char *sparse_filename
;
304 struct object_id oid
;
306 struct strbuf pattern
= STRBUF_INIT
;
308 static struct option builtin_sparse_checkout_init_options
[] = {
309 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
310 N_("initialize the sparse-checkout in cone mode")),
311 OPT_BOOL(0, "sparse-index", &init_opts
.sparse_index
,
312 N_("toggle the use of a sparse index")),
316 repo_read_index(the_repository
);
318 init_opts
.sparse_index
= -1;
320 argc
= parse_options(argc
, argv
, NULL
,
321 builtin_sparse_checkout_init_options
,
322 builtin_sparse_checkout_init_usage
, 0);
324 if (init_opts
.cone_mode
) {
325 mode
= MODE_CONE_PATTERNS
;
326 core_sparse_checkout_cone
= 1;
328 mode
= MODE_ALL_PATTERNS
;
330 if (set_config(mode
))
333 memset(&pl
, 0, sizeof(pl
));
335 sparse_filename
= get_sparse_checkout_filename();
336 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
, 0);
338 if (init_opts
.sparse_index
>= 0) {
339 if (set_sparse_index_config(the_repository
, init_opts
.sparse_index
) < 0)
340 die(_("failed to modify sparse-index config"));
342 /* force an index rewrite */
343 repo_read_index(the_repository
);
344 the_repository
->index
->updated_workdir
= 1;
347 core_apply_sparse_checkout
= 1;
349 /* If we already have a sparse-checkout file, use it. */
351 free(sparse_filename
);
352 return update_working_directory(NULL
);
355 if (get_oid("HEAD", &oid
)) {
358 /* assume we are in a fresh repo, but update the sparse-checkout file */
359 fp
= xfopen(sparse_filename
, "w");
361 die(_("failed to open '%s'"), sparse_filename
);
363 free(sparse_filename
);
364 fprintf(fp
, "/*\n!/*/\n");
369 strbuf_addstr(&pattern
, "/*");
370 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
371 strbuf_addstr(&pattern
, "!/*/");
372 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
373 pl
.use_cone_patterns
= init_opts
.cone_mode
;
375 return write_patterns_and_update(&pl
);
378 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
380 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
381 e
->patternlen
= path
->len
;
382 e
->pattern
= strbuf_detach(path
, NULL
);
383 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
385 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
387 while (e
->patternlen
) {
388 char *slash
= strrchr(e
->pattern
, '/');
389 char *oldpattern
= e
->pattern
;
392 if (slash
== e
->pattern
)
395 newlen
= slash
- e
->pattern
;
396 e
= xmalloc(sizeof(struct pattern_entry
));
397 e
->patternlen
= newlen
;
398 e
->pattern
= xstrndup(oldpattern
, newlen
);
399 hashmap_entry_init(&e
->ent
, fspathhash(e
->pattern
));
401 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
402 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
406 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
410 strbuf_trim_trailing_dir_sep(line
);
412 if (strbuf_normalize_path(line
))
413 die(_("could not normalize path %s"), line
->buf
);
418 if (line
->buf
[0] != '/')
419 strbuf_insertstr(line
, 0, "/");
421 insert_recursive_pattern(pl
, line
);
424 static char const * const builtin_sparse_checkout_set_usage
[] = {
425 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
429 static struct sparse_checkout_set_opts
{
433 static void add_patterns_from_input(struct pattern_list
*pl
,
434 int argc
, const char **argv
)
437 if (core_sparse_checkout_cone
) {
438 struct strbuf line
= STRBUF_INIT
;
440 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
441 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
442 pl
->use_cone_patterns
= 1;
444 if (set_opts
.use_stdin
) {
445 struct strbuf unquoted
= STRBUF_INIT
;
446 while (!strbuf_getline(&line
, stdin
)) {
447 if (line
.buf
[0] == '"') {
448 strbuf_reset(&unquoted
);
449 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
450 die(_("unable to unquote C-style string '%s'"),
453 strbuf_swap(&unquoted
, &line
);
456 strbuf_to_cone_pattern(&line
, pl
);
459 strbuf_release(&unquoted
);
461 for (i
= 0; i
< argc
; i
++) {
462 strbuf_setlen(&line
, 0);
463 strbuf_addstr(&line
, argv
[i
]);
464 strbuf_to_cone_pattern(&line
, pl
);
468 if (set_opts
.use_stdin
) {
469 struct strbuf line
= STRBUF_INIT
;
471 while (!strbuf_getline(&line
, stdin
)) {
473 char *buf
= strbuf_detach(&line
, &len
);
474 add_pattern(buf
, empty_base
, 0, pl
, 0);
477 for (i
= 0; i
< argc
; i
++)
478 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
488 static void add_patterns_cone_mode(int argc
, const char **argv
,
489 struct pattern_list
*pl
)
491 struct strbuf buffer
= STRBUF_INIT
;
492 struct pattern_entry
*pe
;
493 struct hashmap_iter iter
;
494 struct pattern_list existing
;
495 char *sparse_filename
= get_sparse_checkout_filename();
497 add_patterns_from_input(pl
, argc
, argv
);
499 memset(&existing
, 0, sizeof(existing
));
500 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
502 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
504 die(_("unable to load existing sparse-checkout patterns"));
505 free(sparse_filename
);
507 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
508 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
509 pe
->pattern
, &buffer
) ||
510 !hashmap_contains_parent(&pl
->parent_hashmap
,
511 pe
->pattern
, &buffer
)) {
512 strbuf_reset(&buffer
);
513 strbuf_addstr(&buffer
, pe
->pattern
);
514 insert_recursive_pattern(pl
, &buffer
);
518 clear_pattern_list(&existing
);
519 strbuf_release(&buffer
);
522 static void add_patterns_literal(int argc
, const char **argv
,
523 struct pattern_list
*pl
)
525 char *sparse_filename
= get_sparse_checkout_filename();
526 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
528 die(_("unable to load existing sparse-checkout patterns"));
529 free(sparse_filename
);
530 add_patterns_from_input(pl
, argc
, argv
);
533 static int modify_pattern_list(int argc
, const char **argv
, enum modify_type m
)
536 int changed_config
= 0;
537 struct pattern_list
*pl
= xcalloc(1, sizeof(*pl
));
541 if (core_sparse_checkout_cone
)
542 add_patterns_cone_mode(argc
, argv
, pl
);
544 add_patterns_literal(argc
, argv
, pl
);
548 add_patterns_from_input(pl
, argc
, argv
);
552 if (!core_apply_sparse_checkout
) {
553 set_config(MODE_ALL_PATTERNS
);
554 core_apply_sparse_checkout
= 1;
558 result
= write_patterns_and_update(pl
);
560 if (result
&& changed_config
)
561 set_config(MODE_NO_PATTERNS
);
563 clear_pattern_list(pl
);
568 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
,
571 static struct option builtin_sparse_checkout_set_options
[] = {
572 OPT_BOOL(0, "stdin", &set_opts
.use_stdin
,
573 N_("read patterns from standard in")),
577 repo_read_index(the_repository
);
579 argc
= parse_options(argc
, argv
, prefix
,
580 builtin_sparse_checkout_set_options
,
581 builtin_sparse_checkout_set_usage
,
582 PARSE_OPT_KEEP_UNKNOWN
);
584 return modify_pattern_list(argc
, argv
, m
);
587 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
588 N_("git sparse-checkout reapply"),
592 static int sparse_checkout_reapply(int argc
, const char **argv
)
594 static struct option builtin_sparse_checkout_reapply_options
[] = {
598 argc
= parse_options(argc
, argv
, NULL
,
599 builtin_sparse_checkout_reapply_options
,
600 builtin_sparse_checkout_reapply_usage
, 0);
602 repo_read_index(the_repository
);
603 return update_working_directory(NULL
);
606 static char const * const builtin_sparse_checkout_disable_usage
[] = {
607 N_("git sparse-checkout disable"),
611 static int sparse_checkout_disable(int argc
, const char **argv
)
613 static struct option builtin_sparse_checkout_disable_options
[] = {
616 struct pattern_list pl
;
617 struct strbuf match_all
= STRBUF_INIT
;
619 argc
= parse_options(argc
, argv
, NULL
,
620 builtin_sparse_checkout_disable_options
,
621 builtin_sparse_checkout_disable_usage
, 0);
623 repo_read_index(the_repository
);
625 memset(&pl
, 0, sizeof(pl
));
626 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
627 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
628 pl
.use_cone_patterns
= 0;
629 core_apply_sparse_checkout
= 1;
631 strbuf_addstr(&match_all
, "/*");
632 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
634 prepare_repo_settings(the_repository
);
635 the_repository
->settings
.sparse_index
= 0;
637 if (update_working_directory(&pl
))
638 die(_("error while refreshing working directory"));
640 clear_pattern_list(&pl
);
641 return set_config(MODE_NO_PATTERNS
);
644 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
646 static struct option builtin_sparse_checkout_options
[] = {
650 if (argc
== 2 && !strcmp(argv
[1], "-h"))
651 usage_with_options(builtin_sparse_checkout_usage
,
652 builtin_sparse_checkout_options
);
654 argc
= parse_options(argc
, argv
, prefix
,
655 builtin_sparse_checkout_options
,
656 builtin_sparse_checkout_usage
,
657 PARSE_OPT_STOP_AT_NON_OPTION
);
659 git_config(git_default_config
, NULL
);
662 if (!strcmp(argv
[0], "list"))
663 return sparse_checkout_list(argc
, argv
);
664 if (!strcmp(argv
[0], "init"))
665 return sparse_checkout_init(argc
, argv
);
666 if (!strcmp(argv
[0], "set"))
667 return sparse_checkout_set(argc
, argv
, prefix
, REPLACE
);
668 if (!strcmp(argv
[0], "add"))
669 return sparse_checkout_set(argc
, argv
, prefix
, ADD
);
670 if (!strcmp(argv
[0], "reapply"))
671 return sparse_checkout_reapply(argc
, argv
);
672 if (!strcmp(argv
[0], "disable"))
673 return sparse_checkout_disable(argc
, argv
);
676 usage_with_options(builtin_sparse_checkout_usage
,
677 builtin_sparse_checkout_options
);