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 int sparse_checkout_list(int argc
, const char **argv
)
51 struct pattern_list pl
;
52 char *sparse_filename
;
55 memset(&pl
, 0, sizeof(pl
));
57 pl
.use_cone_patterns
= core_sparse_checkout_cone
;
59 sparse_filename
= get_sparse_checkout_filename();
60 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
61 free(sparse_filename
);
64 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
68 if (pl
.use_cone_patterns
) {
70 struct pattern_entry
*pe
;
71 struct hashmap_iter iter
;
72 struct string_list sl
= STRING_LIST_INIT_DUP
;
74 hashmap_for_each_entry(&pl
.recursive_hashmap
, &iter
, pe
, ent
) {
75 /* pe->pattern starts with "/", skip it */
76 string_list_insert(&sl
, pe
->pattern
+ 1);
79 string_list_sort(&sl
);
81 for (i
= 0; i
< sl
.nr
; i
++) {
82 quote_c_style(sl
.items
[i
].string
, NULL
, stdout
, 0);
89 write_patterns_to_file(stdout
, &pl
);
90 clear_pattern_list(&pl
);
95 static int update_working_directory(struct pattern_list
*pl
)
97 enum update_sparsity_result result
;
98 struct unpack_trees_options o
;
99 struct lock_file lock_file
= LOCK_INIT
;
100 struct repository
*r
= the_repository
;
102 /* If no branch has been checked out, there are no updates to make. */
103 if (is_index_unborn(r
->index
))
104 return UPDATE_SPARSITY_SUCCESS
;
106 memset(&o
, 0, sizeof(o
));
107 o
.verbose_update
= isatty(2);
110 o
.src_index
= r
->index
;
111 o
.dst_index
= r
->index
;
112 o
.skip_sparse_checkout
= 0;
117 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
119 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
120 result
= update_sparsity(&o
);
121 clear_unpack_trees_porcelain(&o
);
123 if (result
== UPDATE_SPARSITY_WARNINGS
)
125 * We don't do any special handling of warnings from untracked
126 * files in the way or dirty entries that can't be removed.
128 result
= UPDATE_SPARSITY_SUCCESS
;
129 if (result
== UPDATE_SPARSITY_SUCCESS
)
130 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
132 rollback_lock_file(&lock_file
);
137 static char *escaped_pattern(char *pattern
)
140 struct strbuf final
= STRBUF_INIT
;
143 if (is_glob_special(*p
))
144 strbuf_addch(&final
, '\\');
146 strbuf_addch(&final
, *p
);
150 return strbuf_detach(&final
, NULL
);
153 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
156 struct pattern_entry
*pe
;
157 struct hashmap_iter iter
;
158 struct string_list sl
= STRING_LIST_INIT_DUP
;
159 struct strbuf parent_pattern
= STRBUF_INIT
;
161 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
162 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
165 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
168 string_list_insert(&sl
, pe
->pattern
);
171 string_list_sort(&sl
);
172 string_list_remove_duplicates(&sl
, 0);
174 fprintf(fp
, "/*\n!/*/\n");
176 for (i
= 0; i
< sl
.nr
; i
++) {
177 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
180 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
184 string_list_clear(&sl
, 0);
186 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
187 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
190 string_list_insert(&sl
, pe
->pattern
);
193 strbuf_release(&parent_pattern
);
195 string_list_sort(&sl
);
196 string_list_remove_duplicates(&sl
, 0);
198 for (i
= 0; i
< sl
.nr
; i
++) {
199 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
200 fprintf(fp
, "%s/\n", pattern
);
205 static int write_patterns_and_update(struct pattern_list
*pl
)
207 char *sparse_filename
;
210 struct lock_file lk
= LOCK_INIT
;
213 sparse_filename
= get_sparse_checkout_filename();
215 if (safe_create_leading_directories(sparse_filename
))
216 die(_("failed to create directory for sparse-checkout file"));
218 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
221 result
= update_working_directory(pl
);
223 rollback_lock_file(&lk
);
224 free(sparse_filename
);
225 clear_pattern_list(pl
);
226 update_working_directory(NULL
);
230 fp
= xfdopen(fd
, "w");
232 if (core_sparse_checkout_cone
)
233 write_cone_to_file(fp
, pl
);
235 write_patterns_to_file(fp
, pl
);
238 commit_lock_file(&lk
);
240 free(sparse_filename
);
241 clear_pattern_list(pl
);
246 enum sparse_checkout_mode
{
247 MODE_NO_PATTERNS
= 0,
248 MODE_ALL_PATTERNS
= 1,
249 MODE_CONE_PATTERNS
= 2,
252 static int set_config(enum sparse_checkout_mode mode
)
254 const char *config_path
;
256 if (upgrade_repository_format(1) < 0)
257 die(_("unable to upgrade repository format to enable worktreeConfig"));
258 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
259 error(_("failed to set extensions.worktreeConfig setting"));
263 config_path
= git_path("config.worktree");
264 git_config_set_in_file_gently(config_path
,
265 "core.sparseCheckout",
266 mode
? "true" : NULL
);
268 git_config_set_in_file_gently(config_path
,
269 "core.sparseCheckoutCone",
270 mode
== MODE_CONE_PATTERNS
? "true" : NULL
);
275 static char const * const builtin_sparse_checkout_init_usage
[] = {
276 N_("git sparse-checkout init [--cone]"),
280 static struct sparse_checkout_init_opts
{
284 static int sparse_checkout_init(int argc
, const char **argv
)
286 struct pattern_list pl
;
287 char *sparse_filename
;
289 struct object_id oid
;
291 struct strbuf pattern
= STRBUF_INIT
;
293 static struct option builtin_sparse_checkout_init_options
[] = {
294 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
295 N_("initialize the sparse-checkout in cone mode")),
299 repo_read_index(the_repository
);
301 argc
= parse_options(argc
, argv
, NULL
,
302 builtin_sparse_checkout_init_options
,
303 builtin_sparse_checkout_init_usage
, 0);
305 if (init_opts
.cone_mode
) {
306 mode
= MODE_CONE_PATTERNS
;
307 core_sparse_checkout_cone
= 1;
309 mode
= MODE_ALL_PATTERNS
;
311 if (set_config(mode
))
314 memset(&pl
, 0, sizeof(pl
));
316 sparse_filename
= get_sparse_checkout_filename();
317 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
319 /* If we already have a sparse-checkout file, use it. */
321 free(sparse_filename
);
322 core_apply_sparse_checkout
= 1;
323 return update_working_directory(NULL
);
326 if (get_oid("HEAD", &oid
)) {
329 /* assume we are in a fresh repo, but update the sparse-checkout file */
330 fp
= xfopen(sparse_filename
, "w");
332 die(_("failed to open '%s'"), sparse_filename
);
334 free(sparse_filename
);
335 fprintf(fp
, "/*\n!/*/\n");
340 strbuf_addstr(&pattern
, "/*");
341 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
342 strbuf_addstr(&pattern
, "!/*/");
343 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
345 return write_patterns_and_update(&pl
);
348 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
350 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
351 e
->patternlen
= path
->len
;
352 e
->pattern
= strbuf_detach(path
, NULL
);
353 hashmap_entry_init(&e
->ent
,
355 strihash(e
->pattern
) :
356 strhash(e
->pattern
));
358 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
360 while (e
->patternlen
) {
361 char *slash
= strrchr(e
->pattern
, '/');
362 char *oldpattern
= e
->pattern
;
365 if (slash
== e
->pattern
)
368 newlen
= slash
- e
->pattern
;
369 e
= xmalloc(sizeof(struct pattern_entry
));
370 e
->patternlen
= newlen
;
371 e
->pattern
= xstrndup(oldpattern
, newlen
);
372 hashmap_entry_init(&e
->ent
,
374 strihash(e
->pattern
) :
375 strhash(e
->pattern
));
377 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
378 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
382 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
386 strbuf_trim_trailing_dir_sep(line
);
388 if (strbuf_normalize_path(line
))
389 die(_("could not normalize path %s"), line
->buf
);
394 if (line
->buf
[0] != '/')
395 strbuf_insertstr(line
, 0, "/");
397 insert_recursive_pattern(pl
, line
);
400 static char const * const builtin_sparse_checkout_set_usage
[] = {
401 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
405 static struct sparse_checkout_set_opts
{
409 static void add_patterns_from_input(struct pattern_list
*pl
,
410 int argc
, const char **argv
)
413 if (core_sparse_checkout_cone
) {
414 struct strbuf line
= STRBUF_INIT
;
416 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
417 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
418 pl
->use_cone_patterns
= 1;
420 if (set_opts
.use_stdin
) {
421 struct strbuf unquoted
= STRBUF_INIT
;
422 while (!strbuf_getline(&line
, stdin
)) {
423 if (line
.buf
[0] == '"') {
424 strbuf_reset(&unquoted
);
425 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
426 die(_("unable to unquote C-style string '%s'"),
429 strbuf_swap(&unquoted
, &line
);
432 strbuf_to_cone_pattern(&line
, pl
);
435 strbuf_release(&unquoted
);
437 for (i
= 0; i
< argc
; i
++) {
438 strbuf_setlen(&line
, 0);
439 strbuf_addstr(&line
, argv
[i
]);
440 strbuf_to_cone_pattern(&line
, pl
);
444 if (set_opts
.use_stdin
) {
445 struct strbuf line
= STRBUF_INIT
;
447 while (!strbuf_getline(&line
, stdin
)) {
449 char *buf
= strbuf_detach(&line
, &len
);
450 add_pattern(buf
, empty_base
, 0, pl
, 0);
453 for (i
= 0; i
< argc
; i
++)
454 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
464 static void add_patterns_cone_mode(int argc
, const char **argv
,
465 struct pattern_list
*pl
)
467 struct strbuf buffer
= STRBUF_INIT
;
468 struct pattern_entry
*pe
;
469 struct hashmap_iter iter
;
470 struct pattern_list existing
;
471 char *sparse_filename
= get_sparse_checkout_filename();
473 add_patterns_from_input(pl
, argc
, argv
);
475 memset(&existing
, 0, sizeof(existing
));
476 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
478 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
480 die(_("unable to load existing sparse-checkout patterns"));
481 free(sparse_filename
);
483 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
484 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
485 pe
->pattern
, &buffer
) ||
486 !hashmap_contains_parent(&pl
->parent_hashmap
,
487 pe
->pattern
, &buffer
)) {
488 strbuf_reset(&buffer
);
489 strbuf_addstr(&buffer
, pe
->pattern
);
490 insert_recursive_pattern(pl
, &buffer
);
494 clear_pattern_list(&existing
);
495 strbuf_release(&buffer
);
498 static void add_patterns_literal(int argc
, const char **argv
,
499 struct pattern_list
*pl
)
501 char *sparse_filename
= get_sparse_checkout_filename();
502 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
504 die(_("unable to load existing sparse-checkout patterns"));
505 free(sparse_filename
);
506 add_patterns_from_input(pl
, argc
, argv
);
509 static int modify_pattern_list(int argc
, const char **argv
, enum modify_type m
)
512 int changed_config
= 0;
513 struct pattern_list pl
;
514 memset(&pl
, 0, sizeof(pl
));
518 if (core_sparse_checkout_cone
)
519 add_patterns_cone_mode(argc
, argv
, &pl
);
521 add_patterns_literal(argc
, argv
, &pl
);
525 add_patterns_from_input(&pl
, argc
, argv
);
529 if (!core_apply_sparse_checkout
) {
530 set_config(MODE_ALL_PATTERNS
);
531 core_apply_sparse_checkout
= 1;
535 result
= write_patterns_and_update(&pl
);
537 if (result
&& changed_config
)
538 set_config(MODE_NO_PATTERNS
);
540 clear_pattern_list(&pl
);
544 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
,
547 static struct option builtin_sparse_checkout_set_options
[] = {
548 OPT_BOOL(0, "stdin", &set_opts
.use_stdin
,
549 N_("read patterns from standard in")),
553 repo_read_index(the_repository
);
555 argc
= parse_options(argc
, argv
, prefix
,
556 builtin_sparse_checkout_set_options
,
557 builtin_sparse_checkout_set_usage
,
558 PARSE_OPT_KEEP_UNKNOWN
);
560 return modify_pattern_list(argc
, argv
, m
);
563 static int sparse_checkout_reapply(int argc
, const char **argv
)
565 repo_read_index(the_repository
);
566 return update_working_directory(NULL
);
569 static int sparse_checkout_disable(int argc
, const char **argv
)
571 struct pattern_list pl
;
572 struct strbuf match_all
= STRBUF_INIT
;
574 repo_read_index(the_repository
);
576 memset(&pl
, 0, sizeof(pl
));
577 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
578 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
579 pl
.use_cone_patterns
= 0;
580 core_apply_sparse_checkout
= 1;
582 strbuf_addstr(&match_all
, "/*");
583 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
585 if (update_working_directory(&pl
))
586 die(_("error while refreshing working directory"));
588 clear_pattern_list(&pl
);
589 return set_config(MODE_NO_PATTERNS
);
592 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
594 static struct option builtin_sparse_checkout_options
[] = {
598 if (argc
== 2 && !strcmp(argv
[1], "-h"))
599 usage_with_options(builtin_sparse_checkout_usage
,
600 builtin_sparse_checkout_options
);
602 argc
= parse_options(argc
, argv
, prefix
,
603 builtin_sparse_checkout_options
,
604 builtin_sparse_checkout_usage
,
605 PARSE_OPT_STOP_AT_NON_OPTION
);
607 git_config(git_default_config
, NULL
);
610 if (!strcmp(argv
[0], "list"))
611 return sparse_checkout_list(argc
, argv
);
612 if (!strcmp(argv
[0], "init"))
613 return sparse_checkout_init(argc
, argv
);
614 if (!strcmp(argv
[0], "set"))
615 return sparse_checkout_set(argc
, argv
, prefix
, REPLACE
);
616 if (!strcmp(argv
[0], "add"))
617 return sparse_checkout_set(argc
, argv
, prefix
, ADD
);
618 if (!strcmp(argv
[0], "reapply"))
619 return sparse_checkout_reapply(argc
, argv
);
620 if (!strcmp(argv
[0], "disable"))
621 return sparse_checkout_disable(argc
, argv
);
624 usage_with_options(builtin_sparse_checkout_usage
,
625 builtin_sparse_checkout_options
);