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 memset(&o
, 0, sizeof(o
));
103 o
.verbose_update
= isatty(2);
106 o
.src_index
= r
->index
;
107 o
.dst_index
= r
->index
;
108 o
.skip_sparse_checkout
= 0;
113 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
115 setup_unpack_trees_porcelain(&o
, "sparse-checkout");
116 result
= update_sparsity(&o
);
117 clear_unpack_trees_porcelain(&o
);
119 if (result
== UPDATE_SPARSITY_WARNINGS
)
121 * We don't do any special handling of warnings from untracked
122 * files in the way or dirty entries that can't be removed.
124 result
= UPDATE_SPARSITY_SUCCESS
;
125 if (result
== UPDATE_SPARSITY_SUCCESS
)
126 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
128 rollback_lock_file(&lock_file
);
133 static char *escaped_pattern(char *pattern
)
136 struct strbuf final
= STRBUF_INIT
;
139 if (is_glob_special(*p
))
140 strbuf_addch(&final
, '\\');
142 strbuf_addch(&final
, *p
);
146 return strbuf_detach(&final
, NULL
);
149 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
152 struct pattern_entry
*pe
;
153 struct hashmap_iter iter
;
154 struct string_list sl
= STRING_LIST_INIT_DUP
;
155 struct strbuf parent_pattern
= STRBUF_INIT
;
157 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
158 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
161 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
164 string_list_insert(&sl
, pe
->pattern
);
167 string_list_sort(&sl
);
168 string_list_remove_duplicates(&sl
, 0);
170 fprintf(fp
, "/*\n!/*/\n");
172 for (i
= 0; i
< sl
.nr
; i
++) {
173 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
176 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
180 string_list_clear(&sl
, 0);
182 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
183 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
186 string_list_insert(&sl
, pe
->pattern
);
189 strbuf_release(&parent_pattern
);
191 string_list_sort(&sl
);
192 string_list_remove_duplicates(&sl
, 0);
194 for (i
= 0; i
< sl
.nr
; i
++) {
195 char *pattern
= escaped_pattern(sl
.items
[i
].string
);
196 fprintf(fp
, "%s/\n", pattern
);
201 static int write_patterns_and_update(struct pattern_list
*pl
)
203 char *sparse_filename
;
206 struct lock_file lk
= LOCK_INIT
;
209 sparse_filename
= get_sparse_checkout_filename();
211 if (safe_create_leading_directories(sparse_filename
))
212 die(_("failed to create directory for sparse-checkout file"));
214 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
217 result
= update_working_directory(pl
);
219 rollback_lock_file(&lk
);
220 free(sparse_filename
);
221 clear_pattern_list(pl
);
222 update_working_directory(NULL
);
226 fp
= xfdopen(fd
, "w");
228 if (core_sparse_checkout_cone
)
229 write_cone_to_file(fp
, pl
);
231 write_patterns_to_file(fp
, pl
);
234 commit_lock_file(&lk
);
236 free(sparse_filename
);
237 clear_pattern_list(pl
);
242 enum sparse_checkout_mode
{
243 MODE_NO_PATTERNS
= 0,
244 MODE_ALL_PATTERNS
= 1,
245 MODE_CONE_PATTERNS
= 2,
248 static int set_config(enum sparse_checkout_mode mode
)
250 const char *config_path
;
252 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
253 error(_("failed to set extensions.worktreeConfig setting"));
257 config_path
= git_path("config.worktree");
258 git_config_set_in_file_gently(config_path
,
259 "core.sparseCheckout",
260 mode
? "true" : NULL
);
262 git_config_set_in_file_gently(config_path
,
263 "core.sparseCheckoutCone",
264 mode
== MODE_CONE_PATTERNS
? "true" : NULL
);
269 static char const * const builtin_sparse_checkout_init_usage
[] = {
270 N_("git sparse-checkout init [--cone]"),
274 static struct sparse_checkout_init_opts
{
278 static int sparse_checkout_init(int argc
, const char **argv
)
280 struct pattern_list pl
;
281 char *sparse_filename
;
283 struct object_id oid
;
285 struct strbuf pattern
= STRBUF_INIT
;
287 static struct option builtin_sparse_checkout_init_options
[] = {
288 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
289 N_("initialize the sparse-checkout in cone mode")),
293 repo_read_index(the_repository
);
295 argc
= parse_options(argc
, argv
, NULL
,
296 builtin_sparse_checkout_init_options
,
297 builtin_sparse_checkout_init_usage
, 0);
299 if (init_opts
.cone_mode
) {
300 mode
= MODE_CONE_PATTERNS
;
301 core_sparse_checkout_cone
= 1;
303 mode
= MODE_ALL_PATTERNS
;
305 if (set_config(mode
))
308 memset(&pl
, 0, sizeof(pl
));
310 sparse_filename
= get_sparse_checkout_filename();
311 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
313 /* If we already have a sparse-checkout file, use it. */
315 free(sparse_filename
);
316 core_apply_sparse_checkout
= 1;
317 return update_working_directory(NULL
);
320 if (get_oid("HEAD", &oid
)) {
323 /* assume we are in a fresh repo, but update the sparse-checkout file */
324 fp
= xfopen(sparse_filename
, "w");
326 die(_("failed to open '%s'"), sparse_filename
);
328 free(sparse_filename
);
329 fprintf(fp
, "/*\n!/*/\n");
334 strbuf_addstr(&pattern
, "/*");
335 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
336 strbuf_addstr(&pattern
, "!/*/");
337 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
339 return write_patterns_and_update(&pl
);
342 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
344 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
345 e
->patternlen
= path
->len
;
346 e
->pattern
= strbuf_detach(path
, NULL
);
347 hashmap_entry_init(&e
->ent
,
349 strihash(e
->pattern
) :
350 strhash(e
->pattern
));
352 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
354 while (e
->patternlen
) {
355 char *slash
= strrchr(e
->pattern
, '/');
356 char *oldpattern
= e
->pattern
;
359 if (slash
== e
->pattern
)
362 newlen
= slash
- e
->pattern
;
363 e
= xmalloc(sizeof(struct pattern_entry
));
364 e
->patternlen
= newlen
;
365 e
->pattern
= xstrndup(oldpattern
, newlen
);
366 hashmap_entry_init(&e
->ent
,
368 strihash(e
->pattern
) :
369 strhash(e
->pattern
));
371 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
372 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
376 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
380 strbuf_trim_trailing_dir_sep(line
);
382 if (strbuf_normalize_path(line
))
383 die(_("could not normalize path %s"), line
->buf
);
388 if (line
->buf
[0] != '/')
389 strbuf_insertstr(line
, 0, "/");
391 insert_recursive_pattern(pl
, line
);
394 static char const * const builtin_sparse_checkout_set_usage
[] = {
395 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
399 static struct sparse_checkout_set_opts
{
403 static void add_patterns_from_input(struct pattern_list
*pl
,
404 int argc
, const char **argv
)
407 if (core_sparse_checkout_cone
) {
408 struct strbuf line
= STRBUF_INIT
;
410 hashmap_init(&pl
->recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
411 hashmap_init(&pl
->parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
412 pl
->use_cone_patterns
= 1;
414 if (set_opts
.use_stdin
) {
415 struct strbuf unquoted
= STRBUF_INIT
;
416 while (!strbuf_getline(&line
, stdin
)) {
417 if (line
.buf
[0] == '"') {
418 strbuf_reset(&unquoted
);
419 if (unquote_c_style(&unquoted
, line
.buf
, NULL
))
420 die(_("unable to unquote C-style string '%s'"),
423 strbuf_swap(&unquoted
, &line
);
426 strbuf_to_cone_pattern(&line
, pl
);
429 strbuf_release(&unquoted
);
431 for (i
= 0; i
< argc
; i
++) {
432 strbuf_setlen(&line
, 0);
433 strbuf_addstr(&line
, argv
[i
]);
434 strbuf_to_cone_pattern(&line
, pl
);
438 if (set_opts
.use_stdin
) {
439 struct strbuf line
= STRBUF_INIT
;
441 while (!strbuf_getline(&line
, stdin
)) {
443 char *buf
= strbuf_detach(&line
, &len
);
444 add_pattern(buf
, empty_base
, 0, pl
, 0);
447 for (i
= 0; i
< argc
; i
++)
448 add_pattern(argv
[i
], empty_base
, 0, pl
, 0);
458 static void add_patterns_cone_mode(int argc
, const char **argv
,
459 struct pattern_list
*pl
)
461 struct strbuf buffer
= STRBUF_INIT
;
462 struct pattern_entry
*pe
;
463 struct hashmap_iter iter
;
464 struct pattern_list existing
;
465 char *sparse_filename
= get_sparse_checkout_filename();
467 add_patterns_from_input(pl
, argc
, argv
);
469 memset(&existing
, 0, sizeof(existing
));
470 existing
.use_cone_patterns
= core_sparse_checkout_cone
;
472 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
474 die(_("unable to load existing sparse-checkout patterns"));
475 free(sparse_filename
);
477 hashmap_for_each_entry(&existing
.recursive_hashmap
, &iter
, pe
, ent
) {
478 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
479 pe
->pattern
, &buffer
) ||
480 !hashmap_contains_parent(&pl
->parent_hashmap
,
481 pe
->pattern
, &buffer
)) {
482 strbuf_reset(&buffer
);
483 strbuf_addstr(&buffer
, pe
->pattern
);
484 insert_recursive_pattern(pl
, &buffer
);
488 clear_pattern_list(&existing
);
489 strbuf_release(&buffer
);
492 static void add_patterns_literal(int argc
, const char **argv
,
493 struct pattern_list
*pl
)
495 char *sparse_filename
= get_sparse_checkout_filename();
496 if (add_patterns_from_file_to_list(sparse_filename
, "", 0,
498 die(_("unable to load existing sparse-checkout patterns"));
499 free(sparse_filename
);
500 add_patterns_from_input(pl
, argc
, argv
);
503 static int modify_pattern_list(int argc
, const char **argv
, enum modify_type m
)
506 int changed_config
= 0;
507 struct pattern_list pl
;
508 memset(&pl
, 0, sizeof(pl
));
512 if (core_sparse_checkout_cone
)
513 add_patterns_cone_mode(argc
, argv
, &pl
);
515 add_patterns_literal(argc
, argv
, &pl
);
519 add_patterns_from_input(&pl
, argc
, argv
);
523 if (!core_apply_sparse_checkout
) {
524 set_config(MODE_ALL_PATTERNS
);
525 core_apply_sparse_checkout
= 1;
529 result
= write_patterns_and_update(&pl
);
531 if (result
&& changed_config
)
532 set_config(MODE_NO_PATTERNS
);
534 clear_pattern_list(&pl
);
538 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
,
541 static struct option builtin_sparse_checkout_set_options
[] = {
542 OPT_BOOL(0, "stdin", &set_opts
.use_stdin
,
543 N_("read patterns from standard in")),
547 repo_read_index(the_repository
);
549 argc
= parse_options(argc
, argv
, prefix
,
550 builtin_sparse_checkout_set_options
,
551 builtin_sparse_checkout_set_usage
,
552 PARSE_OPT_KEEP_UNKNOWN
);
554 return modify_pattern_list(argc
, argv
, m
);
557 static int sparse_checkout_reapply(int argc
, const char **argv
)
559 repo_read_index(the_repository
);
560 return update_working_directory(NULL
);
563 static int sparse_checkout_disable(int argc
, const char **argv
)
565 struct pattern_list pl
;
566 struct strbuf match_all
= STRBUF_INIT
;
568 repo_read_index(the_repository
);
570 memset(&pl
, 0, sizeof(pl
));
571 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
572 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
573 pl
.use_cone_patterns
= 0;
574 core_apply_sparse_checkout
= 1;
576 strbuf_addstr(&match_all
, "/*");
577 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
579 if (update_working_directory(&pl
))
580 die(_("error while refreshing working directory"));
582 clear_pattern_list(&pl
);
583 return set_config(MODE_NO_PATTERNS
);
586 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
588 static struct option builtin_sparse_checkout_options
[] = {
592 if (argc
== 2 && !strcmp(argv
[1], "-h"))
593 usage_with_options(builtin_sparse_checkout_usage
,
594 builtin_sparse_checkout_options
);
596 argc
= parse_options(argc
, argv
, prefix
,
597 builtin_sparse_checkout_options
,
598 builtin_sparse_checkout_usage
,
599 PARSE_OPT_STOP_AT_NON_OPTION
);
601 git_config(git_default_config
, NULL
);
604 if (!strcmp(argv
[0], "list"))
605 return sparse_checkout_list(argc
, argv
);
606 if (!strcmp(argv
[0], "init"))
607 return sparse_checkout_init(argc
, argv
);
608 if (!strcmp(argv
[0], "set"))
609 return sparse_checkout_set(argc
, argv
, prefix
, REPLACE
);
610 if (!strcmp(argv
[0], "add"))
611 return sparse_checkout_set(argc
, argv
, prefix
, ADD
);
612 if (!strcmp(argv
[0], "reapply"))
613 return sparse_checkout_reapply(argc
, argv
);
614 if (!strcmp(argv
[0], "disable"))
615 return sparse_checkout_disable(argc
, argv
);
618 usage_with_options(builtin_sparse_checkout_usage
,
619 builtin_sparse_checkout_options
);