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"
18 static const char *empty_base
= "";
20 static char const * const builtin_sparse_checkout_usage
[] = {
21 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
25 static char *get_sparse_checkout_filename(void)
27 return git_pathdup("info/sparse-checkout");
30 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
34 for (i
= 0; i
< pl
->nr
; i
++) {
35 struct path_pattern
*p
= pl
->patterns
[i
];
37 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
40 fprintf(fp
, "%s", p
->pattern
);
42 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
49 static char const * const builtin_sparse_checkout_list_usage
[] = {
50 N_("git sparse-checkout list"),
54 static int sparse_checkout_list(int argc
, const char **argv
)
56 static struct option builtin_sparse_checkout_list_options
[] = {
59 struct pattern_list pl
;
60 char *sparse_filename
;
63 argc
= parse_options(argc
, argv
, NULL
,
64 builtin_sparse_checkout_list_options
,
65 builtin_sparse_checkout_list_usage
, 0);
67 memset(&pl
, 0, sizeof(pl
));
69 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
71 sparse_filename
= get_sparse_checkout_filename();
72 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
73 free(sparse_filename
);
76 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
80 if (pl
.use_cone_patterns
) {
82 struct pattern_entry
*pe
;
83 struct hashmap_iter iter
;
84 struct string_list sl
= STRING_LIST_INIT_DUP
;
86 hashmap_for_each_entry(&pl
.recursive_hashmap
, &iter
, pe
, ent
) {
87 /* pe->pattern starts with "/", skip it */
88 string_list_insert(&sl
, pe
->pattern
+ 1);
91 string_list_sort(&sl
);
93 for (i
= 0; i
< sl
.nr
; i
++) {
94 quote_c_style(sl
.items
[i
].string
, NULL
, stdout
, 0);
101 write_patterns_to_file(stdout
, &pl
);
102 clear_pattern_list(&pl
);
107 static int update_working_directory(struct pattern_list
*pl
)
109 enum update_sparsity_result result
;
110 struct unpack_trees_options o
;
111 struct lock_file lock_file
= LOCK_INIT
;
112 struct repository
*r
= the_repository
;
114 /* If no branch has been checked out, there are no updates to make. */
115 if (is_index_unborn(r
->index
))
116 return UPDATE_SPARSITY_SUCCESS
;
118 memset(&o
, 0, sizeof(o
));
119 o
.verbose_update
= isatty(2);
122 o
.src_index
= r
->index
;
123 o
.dst_index
= r
->index
;
124 o
.skip_sparse_checkout
= 0;
129 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
131 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
132 result
= update_sparsity(&o
);
133 clear_unpack_trees_porcelain(&o
);
135 if (result
== UPDATE_SPARSITY_WARNINGS
)
137 * We don't do any special handling of warnings from untracked
138 * files in the way or dirty entries that can't be removed.
140 result
= UPDATE_SPARSITY_SUCCESS
;
141 if (result
== UPDATE_SPARSITY_SUCCESS
)
142 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
144 rollback_lock_file(&lock_file
);
149 static char *escaped_pattern(char *pattern
)
152 struct strbuf final
= STRBUF_INIT
;
155 if (is_glob_special(*p
))
156 strbuf_addch(&final
, '\\');
158 strbuf_addch(&final
, *p
);
162 return strbuf_detach(&final
, NULL
);
165 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
168 struct pattern_entry
*pe
;
169 struct hashmap_iter iter
;
170 struct string_list sl
= STRING_LIST_INIT_DUP
;
171 struct strbuf parent_pattern
= STRBUF_INIT
;
173 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
174 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
177 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
180 string_list_insert(&sl
, pe
->pattern
);
183 string_list_sort(&sl
);
184 string_list_remove_duplicates(&sl
, 0);
186 fprintf(fp
, "/*\n!/*/\n");
188 for (i
= 0; i
< sl
.nr
; i
++) {
189 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
192 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
196 string_list_clear(&sl
, 0);
198 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
199 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
202 string_list_insert(&sl
, pe
->pattern
);
205 strbuf_release(&parent_pattern
);
207 string_list_sort(&sl
);
208 string_list_remove_duplicates(&sl
, 0);
210 for (i
= 0; i
< sl
.nr
; i
++) {
211 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
212 fprintf(fp
, "%s/\n", pattern
);
217 static int write_patterns_and_update(struct pattern_list
*pl
)
219 char *sparse_filename
;
222 struct lock_file lk
= LOCK_INIT
;
225 sparse_filename
= get_sparse_checkout_filename();
227 if (safe_create_leading_directories(sparse_filename
))
228 die(_("failed to create directory for sparse-checkout file"));
230 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
233 result
= update_working_directory(pl
);
235 rollback_lock_file(&lk
);
236 free(sparse_filename
);
237 clear_pattern_list(pl
);
238 update_working_directory(NULL
);
242 fp
= xfdopen(fd
, "w");
244 if (core_sparse_checkout_cone
)
245 write_cone_to_file(fp
, pl
);
247 write_patterns_to_file(fp
, pl
);
250 commit_lock_file(&lk
);
252 free(sparse_filename
);
253 clear_pattern_list(pl
);
258 enum sparse_checkout_mode
{
259 MODE_NO_PATTERNS
= 0,
260 MODE_ALL_PATTERNS
= 1,
261 MODE_CONE_PATTERNS
= 2,
264 static int set_config(enum sparse_checkout_mode mode
)
266 const char *config_path
;
268 if (upgrade_repository_format(1) < 0)
269 die(_("unable to upgrade repository format to enable worktreeConfig"));
270 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
271 error(_("failed to set extensions.worktreeConfig setting"));
275 config_path
= git_path("config.worktree");
276 git_config_set_in_file_gently(config_path
,
277 "core.sparseCheckout",
278 mode
? "true" : NULL
);
280 git_config_set_in_file_gently(config_path
,
281 "core.sparseCheckoutCone",
282 mode
== MODE_CONE_PATTERNS
? "true" : NULL
);
287 static char const * const builtin_sparse_checkout_init_usage
[] = {
288 N_("git sparse-checkout init [--cone]"),
292 static struct sparse_checkout_init_opts
{
296 static int sparse_checkout_init(int argc
, const char **argv
)
298 struct pattern_list pl
;
299 char *sparse_filename
;
301 struct object_id oid
;
303 struct strbuf pattern
= STRBUF_INIT
;
305 static struct option builtin_sparse_checkout_init_options
[] = {
306 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
307 N_("initialize the sparse-checkout in cone mode")),
311 repo_read_index(the_repository
);
313 argc
= parse_options(argc
, argv
, NULL
,
314 builtin_sparse_checkout_init_options
,
315 builtin_sparse_checkout_init_usage
, 0);
317 if (init_opts
.cone_mode
) {
318 mode
= MODE_CONE_PATTERNS
;
319 core_sparse_checkout_cone
= 1;
321 mode
= MODE_ALL_PATTERNS
;
323 if (set_config(mode
))
326 memset(&pl
, 0, sizeof(pl
));
328 sparse_filename
= get_sparse_checkout_filename();
329 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
331 /* If we already have a sparse-checkout file, use it. */
333 free(sparse_filename
);
334 core_apply_sparse_checkout
= 1;
335 return update_working_directory(NULL
);
338 if (get_oid("HEAD", &oid
)) {
341 /* assume we are in a fresh repo, but update the sparse-checkout file */
342 fp
= xfopen(sparse_filename
, "w");
344 die(_("failed to open '%s'"), sparse_filename
);
346 free(sparse_filename
);
347 fprintf(fp
, "/*\n!/*/\n");
352 strbuf_addstr(&pattern
, "/*");
353 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
354 strbuf_addstr(&pattern
, "!/*/");
355 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
357 return write_patterns_and_update(&pl
);
360 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
362 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
363 e
->patternlen
= path
->len
;
364 e
->pattern
= strbuf_detach(path
, NULL
);
365 hashmap_entry_init(&e
->ent
,
367 strihash(e
->pattern
) :
368 strhash(e
->pattern
));
370 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
372 while (e
->patternlen
) {
373 char *slash
= strrchr(e
->pattern
, '/');
374 char *oldpattern
= e
->pattern
;
377 if (slash
== e
->pattern
)
380 newlen
= slash
- e
->pattern
;
381 e
= xmalloc(sizeof(struct pattern_entry
));
382 e
->patternlen
= newlen
;
383 e
->pattern
= xstrndup(oldpattern
, newlen
);
384 hashmap_entry_init(&e
->ent
,
386 strihash(e
->pattern
) :
387 strhash(e
->pattern
));
389 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
390 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
394 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
398 strbuf_trim_trailing_dir_sep(line
);
400 if (strbuf_normalize_path(line
))
401 die(_("could not normalize path %s"), line
->buf
);
406 if (line
->buf
[0] != '/')
407 strbuf_insertstr(line
, 0, "/");
409 insert_recursive_pattern(pl
, line
);
412 static char const * const builtin_sparse_checkout_set_usage
[] = {
413 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
417 static struct sparse_checkout_set_opts
{
421 static void add_patterns_from_input(struct pattern_list
*pl
,
422 int argc
, const char **argv
)
425 if (core_sparse_checkout_cone
) {
426 struct strbuf line
= STRBUF_INIT
;
428 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
429 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
430 pl
->use_cone_patterns
= 1;
432 if (set_opts
.use_stdin
) {
433 struct strbuf unquoted
= STRBUF_INIT
;
434 while (!strbuf_getline(&line
, stdin
)) {
435 if (line
.buf
[0] == '"') {
436 strbuf_reset(&unquoted
);
437 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
438 die(_("unable to unquote C-style string '%s'"),
441 strbuf_swap(&unquoted
, &line
);
444 strbuf_to_cone_pattern(&line
, pl
);
447 strbuf_release(&unquoted
);
449 for (i
= 0; i
< argc
; i
++) {
450 strbuf_setlen(&line
, 0);
451 strbuf_addstr(&line
, argv
[i
]);
452 strbuf_to_cone_pattern(&line
, pl
);
456 if (set_opts
.use_stdin
) {
457 struct strbuf line
= STRBUF_INIT
;
459 while (!strbuf_getline(&line
, stdin
)) {
461 char *buf
= strbuf_detach(&line
, &len
);
462 add_pattern(buf
, empty_base
, 0, pl
, 0);
465 for (i
= 0; i
< argc
; i
++)
466 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
476 static void add_patterns_cone_mode(int argc
, const char **argv
,
477 struct pattern_list
*pl
)
479 struct strbuf buffer
= STRBUF_INIT
;
480 struct pattern_entry
*pe
;
481 struct hashmap_iter iter
;
482 struct pattern_list existing
;
483 char *sparse_filename
= get_sparse_checkout_filename();
485 add_patterns_from_input(pl
, argc
, argv
);
487 memset(&existing
, 0, sizeof(existing
));
488 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
490 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
492 die(_("unable to load existing sparse-checkout patterns"));
493 free(sparse_filename
);
495 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
496 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
497 pe
->pattern
, &buffer
) ||
498 !hashmap_contains_parent(&pl
->parent_hashmap
,
499 pe
->pattern
, &buffer
)) {
500 strbuf_reset(&buffer
);
501 strbuf_addstr(&buffer
, pe
->pattern
);
502 insert_recursive_pattern(pl
, &buffer
);
506 clear_pattern_list(&existing
);
507 strbuf_release(&buffer
);
510 static void add_patterns_literal(int argc
, const char **argv
,
511 struct pattern_list
*pl
)
513 char *sparse_filename
= get_sparse_checkout_filename();
514 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
516 die(_("unable to load existing sparse-checkout patterns"));
517 free(sparse_filename
);
518 add_patterns_from_input(pl
, argc
, argv
);
521 static int modify_pattern_list(int argc
, const char **argv
, enum modify_type m
)
524 int changed_config
= 0;
525 struct pattern_list pl
;
526 memset(&pl
, 0, sizeof(pl
));
530 if (core_sparse_checkout_cone
)
531 add_patterns_cone_mode(argc
, argv
, &pl
);
533 add_patterns_literal(argc
, argv
, &pl
);
537 add_patterns_from_input(&pl
, argc
, argv
);
541 if (!core_apply_sparse_checkout
) {
542 set_config(MODE_ALL_PATTERNS
);
543 core_apply_sparse_checkout
= 1;
547 result
= write_patterns_and_update(&pl
);
549 if (result
&& changed_config
)
550 set_config(MODE_NO_PATTERNS
);
552 clear_pattern_list(&pl
);
556 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
,
559 static struct option builtin_sparse_checkout_set_options
[] = {
560 OPT_BOOL(0, "stdin", &set_opts
.use_stdin
,
561 N_("read patterns from standard in")),
565 repo_read_index(the_repository
);
567 argc
= parse_options(argc
, argv
, prefix
,
568 builtin_sparse_checkout_set_options
,
569 builtin_sparse_checkout_set_usage
,
570 PARSE_OPT_KEEP_UNKNOWN
);
572 return modify_pattern_list(argc
, argv
, m
);
575 static char const * const builtin_sparse_checkout_reapply_usage
[] = {
576 N_("git sparse-checkout reapply"),
580 static int sparse_checkout_reapply(int argc
, const char **argv
)
582 static struct option builtin_sparse_checkout_reapply_options
[] = {
586 argc
= parse_options(argc
, argv
, NULL
,
587 builtin_sparse_checkout_reapply_options
,
588 builtin_sparse_checkout_reapply_usage
, 0);
590 repo_read_index(the_repository
);
591 return update_working_directory(NULL
);
594 static char const * const builtin_sparse_checkout_disable_usage
[] = {
595 N_("git sparse-checkout disable"),
599 static int sparse_checkout_disable(int argc
, const char **argv
)
601 static struct option builtin_sparse_checkout_disable_options
[] = {
604 struct pattern_list pl
;
605 struct strbuf match_all
= STRBUF_INIT
;
607 argc
= parse_options(argc
, argv
, NULL
,
608 builtin_sparse_checkout_disable_options
,
609 builtin_sparse_checkout_disable_usage
, 0);
611 repo_read_index(the_repository
);
613 memset(&pl
, 0, sizeof(pl
));
614 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
615 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
616 pl
.use_cone_patterns
= 0;
617 core_apply_sparse_checkout
= 1;
619 strbuf_addstr(&match_all
, "/*");
620 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
622 if (update_working_directory(&pl
))
623 die(_("error while refreshing working directory"));
625 clear_pattern_list(&pl
);
626 return set_config(MODE_NO_PATTERNS
);
629 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
631 static struct option builtin_sparse_checkout_options
[] = {
635 if (argc
== 2 && !strcmp(argv
[1], "-h"))
636 usage_with_options(builtin_sparse_checkout_usage
,
637 builtin_sparse_checkout_options
);
639 argc
= parse_options(argc
, argv
, prefix
,
640 builtin_sparse_checkout_options
,
641 builtin_sparse_checkout_usage
,
642 PARSE_OPT_STOP_AT_NON_OPTION
);
644 git_config(git_default_config
, NULL
);
647 if (!strcmp(argv
[0], "list"))
648 return sparse_checkout_list(argc
, argv
);
649 if (!strcmp(argv
[0], "init"))
650 return sparse_checkout_init(argc
, argv
);
651 if (!strcmp(argv
[0], "set"))
652 return sparse_checkout_set(argc
, argv
, prefix
, REPLACE
);
653 if (!strcmp(argv
[0], "add"))
654 return sparse_checkout_set(argc
, argv
, prefix
, ADD
);
655 if (!strcmp(argv
[0], "reapply"))
656 return sparse_checkout_reapply(argc
, argv
);
657 if (!strcmp(argv
[0], "disable"))
658 return sparse_checkout_disable(argc
, argv
);
661 usage_with_options(builtin_sparse_checkout_usage
,
662 builtin_sparse_checkout_options
);