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|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)
98 struct unpack_trees_options o;
99 struct lock_file lock_file = LOCK_INIT;
100 struct object_id oid;
103 struct repository *r = the_repository;
105 if (repo_read_index_unmerged(r))
106 die(_("you need to resolve your current index first"));
108 if (get_oid("HEAD", &oid))
111 tree = parse_tree_indirect(&oid);
113 init_tree_desc(&t, tree->buffer, tree->size);
115 memset(&o, 0, sizeof(o));
116 o.verbose_update = isatty(2);
121 o.src_index = r->index;
122 o.dst_index = r->index;
123 o.skip_sparse_checkout = 0;
126 resolve_undo_clear_index(r->index);
129 cache_tree_free(&r->index->cache_tree);
131 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
133 core_apply_sparse_checkout = 1;
134 result = unpack_trees(1, &t, &o);
137 prime_cache_tree(r, r->index, tree);
138 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
140 rollback_lock_file(&lock_file);
145 static char *escaped_pattern(char *pattern)
148 struct strbuf final = STRBUF_INIT;
151 if (is_glob_special(*p))
152 strbuf_addch(&final, '\\');
154 strbuf_addch(&final, *p);
158 return strbuf_detach(&final, NULL);
161 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
164 struct pattern_entry *pe;
165 struct hashmap_iter iter;
166 struct string_list sl = STRING_LIST_INIT_DUP;
167 struct strbuf parent_pattern = STRBUF_INIT;
169 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
170 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
173 if (!hashmap_contains_parent(&pl->recursive_hashmap,
176 string_list_insert(&sl, pe->pattern);
179 string_list_sort(&sl);
180 string_list_remove_duplicates(&sl, 0);
182 fprintf(fp, "/*\n!/*/\n");
184 for (i = 0; i < sl.nr; i++) {
185 char *pattern = escaped_pattern(sl.items[i].string);
188 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
192 string_list_clear(&sl, 0);
194 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
195 if (!hashmap_contains_parent(&pl->recursive_hashmap,
198 string_list_insert(&sl, pe->pattern);
201 strbuf_release(&parent_pattern);
203 string_list_sort(&sl);
204 string_list_remove_duplicates(&sl, 0);
206 for (i = 0; i < sl.nr; i++) {
207 char *pattern = escaped_pattern(sl.items[i].string);
208 fprintf(fp, "%s/\n", pattern);
213 static int write_patterns_and_update(struct pattern_list *pl)
215 char *sparse_filename;
218 struct lock_file lk = LOCK_INIT;
221 sparse_filename = get_sparse_checkout_filename();
223 if (safe_create_leading_directories(sparse_filename))
224 die(_("failed to create directory for sparse-checkout file"));
226 fd = hold_lock_file_for_update(&lk, sparse_filename,
229 result = update_working_directory(pl);
231 rollback_lock_file(&lk);
232 free(sparse_filename);
233 clear_pattern_list(pl);
234 update_working_directory(NULL);
238 fp = xfdopen(fd, "w");
240 if (core_sparse_checkout_cone)
241 write_cone_to_file(fp, pl);
243 write_patterns_to_file(fp, pl);
246 commit_lock_file(&lk);
248 free(sparse_filename);
249 clear_pattern_list(pl);
254 enum sparse_checkout_mode {
255 MODE_NO_PATTERNS = 0,
256 MODE_ALL_PATTERNS = 1,
257 MODE_CONE_PATTERNS = 2,
260 static int set_config(enum sparse_checkout_mode mode)
262 const char *config_path;
264 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
265 error(_("failed to set extensions.worktreeConfig setting"));
269 config_path = git_path("config.worktree");
270 git_config_set_in_file_gently(config_path,
271 "core.sparseCheckout",
272 mode ? "true" : NULL);
274 git_config_set_in_file_gently(config_path,
275 "core.sparseCheckoutCone",
276 mode == MODE_CONE_PATTERNS ? "true" : NULL);
281 static char const * const builtin_sparse_checkout_init_usage[] = {
282 N_("git sparse-checkout init [--cone]"),
286 static struct sparse_checkout_init_opts {
290 static int sparse_checkout_init(int argc, const char **argv)
292 struct pattern_list pl;
293 char *sparse_filename;
295 struct object_id oid;
297 struct strbuf pattern = STRBUF_INIT;
299 static struct option builtin_sparse_checkout_init_options[] = {
300 OPT_BOOL(0, "cone", &init_opts.cone_mode,
301 N_("initialize the sparse-checkout in cone mode")),
305 repo_read_index(the_repository);
306 require_clean_work_tree(the_repository,
307 N_("initialize sparse-checkout"), NULL, 1, 0);
309 argc = parse_options(argc, argv, NULL,
310 builtin_sparse_checkout_init_options,
311 builtin_sparse_checkout_init_usage, 0);
313 if (init_opts.cone_mode) {
314 mode = MODE_CONE_PATTERNS;
315 core_sparse_checkout_cone = 1;
317 mode = MODE_ALL_PATTERNS;
319 if (set_config(mode))
322 memset(&pl, 0, sizeof(pl));
324 sparse_filename = get_sparse_checkout_filename();
325 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
327 /* If we already have a sparse-checkout file, use it. */
329 free(sparse_filename);
330 core_apply_sparse_checkout = 1;
331 return update_working_directory(NULL);
334 if (get_oid("HEAD", &oid)) {
337 /* assume we are in a fresh repo, but update the sparse-checkout file */
338 fp = xfopen(sparse_filename, "w");
340 die(_("failed to open '%s'"), sparse_filename);
342 free(sparse_filename);
343 fprintf(fp, "/*\n!/*/\n");
348 strbuf_addstr(&pattern, "/*");
349 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
350 strbuf_addstr(&pattern, "!/*/");
351 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
353 return write_patterns_and_update(&pl);
356 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
358 struct pattern_entry *e = xmalloc(sizeof(*e));
359 e->patternlen = path->len;
360 e->pattern = strbuf_detach(path, NULL);
361 hashmap_entry_init(&e->ent,
363 strihash(e->pattern) :
364 strhash(e->pattern));
366 hashmap_add(&pl->recursive_hashmap, &e->ent);
368 while (e->patternlen) {
369 char *slash = strrchr(e->pattern, '/');
370 char *oldpattern = e->pattern;
373 if (slash == e->pattern)
376 newlen = slash - e->pattern;
377 e = xmalloc(sizeof(struct pattern_entry));
378 e->patternlen = newlen;
379 e->pattern = xstrndup(oldpattern, newlen);
380 hashmap_entry_init(&e->ent,
382 strihash(e->pattern) :
383 strhash(e->pattern));
385 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
386 hashmap_add(&pl->parent_hashmap, &e->ent);
390 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
394 strbuf_trim_trailing_dir_sep(line);
396 if (strbuf_normalize_path(line))
397 die(_("could not normalize path %s"), line->buf);
402 if (line->buf[0] != '/')
403 strbuf_insertstr(line, 0, "/");
405 insert_recursive_pattern(pl, line);
408 static char const * const builtin_sparse_checkout_set_usage[] = {
409 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
413 static struct sparse_checkout_set_opts {
417 static void add_patterns_from_input(struct pattern_list *pl,
418 int argc, const char **argv)
421 if (core_sparse_checkout_cone) {
422 struct strbuf line = STRBUF_INIT;
424 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
425 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
426 pl->use_cone_patterns = 1;
428 if (set_opts.use_stdin) {
429 struct strbuf unquoted = STRBUF_INIT;
430 while (!strbuf_getline(&line, stdin)) {
431 if (line.buf[0] == '"') {
432 strbuf_reset(&unquoted);
433 if (unquote_c_style(&unquoted, line.buf, NULL))
434 die(_("unable to unquote C-style string '%s'"),
437 strbuf_swap(&unquoted, &line);
440 strbuf_to_cone_pattern(&line, pl);
443 strbuf_release(&unquoted);
445 for (i = 0; i < argc; i++) {
446 strbuf_setlen(&line, 0);
447 strbuf_addstr(&line, argv[i]);
448 strbuf_to_cone_pattern(&line, pl);
452 if (set_opts.use_stdin) {
453 struct strbuf line = STRBUF_INIT;
455 while (!strbuf_getline(&line, stdin)) {
457 char *buf = strbuf_detach(&line, &len);
458 add_pattern(buf, empty_base, 0, pl, 0);
461 for (i = 0; i < argc; i++)
462 add_pattern(argv[i], empty_base, 0, pl, 0);
472 static void add_patterns_cone_mode(int argc, const char **argv,
473 struct pattern_list *pl)
475 struct strbuf buffer = STRBUF_INIT;
476 struct pattern_entry *pe;
477 struct hashmap_iter iter;
478 struct pattern_list existing;
479 char *sparse_filename = get_sparse_checkout_filename();
481 add_patterns_from_input(pl, argc, argv);
483 memset(&existing, 0, sizeof(existing));
484 existing.use_cone_patterns = core_sparse_checkout_cone;
486 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
488 die(_("unable to load existing sparse-checkout patterns"));
489 free(sparse_filename);
491 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
492 if (!hashmap_contains_parent(&pl->recursive_hashmap,
493 pe->pattern, &buffer) ||
494 !hashmap_contains_parent(&pl->parent_hashmap,
495 pe->pattern, &buffer)) {
496 strbuf_reset(&buffer);
497 strbuf_addstr(&buffer, pe->pattern);
498 insert_recursive_pattern(pl, &buffer);
502 clear_pattern_list(&existing);
503 strbuf_release(&buffer);
506 static void add_patterns_literal(int argc, const char **argv,
507 struct pattern_list *pl)
509 char *sparse_filename = get_sparse_checkout_filename();
510 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
512 die(_("unable to load existing sparse-checkout patterns"));
513 free(sparse_filename);
514 add_patterns_from_input(pl, argc, argv);
517 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
520 int changed_config = 0;
521 struct pattern_list pl;
522 memset(&pl, 0, sizeof(pl));
526 if (core_sparse_checkout_cone)
527 add_patterns_cone_mode(argc, argv, &pl);
529 add_patterns_literal(argc, argv, &pl);
533 add_patterns_from_input(&pl, argc, argv);
537 if (!core_apply_sparse_checkout) {
538 set_config(MODE_ALL_PATTERNS);
539 core_apply_sparse_checkout = 1;
543 result = write_patterns_and_update(&pl);
545 if (result && changed_config)
546 set_config(MODE_NO_PATTERNS);
548 clear_pattern_list(&pl);
552 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
555 static struct option builtin_sparse_checkout_set_options[] = {
556 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
557 N_("read patterns from standard in")),
561 repo_read_index(the_repository);
562 require_clean_work_tree(the_repository,
563 N_("set sparse-checkout patterns"), NULL, 1, 0);
565 argc = parse_options(argc, argv, prefix,
566 builtin_sparse_checkout_set_options,
567 builtin_sparse_checkout_set_usage,
568 PARSE_OPT_KEEP_UNKNOWN);
570 return modify_pattern_list(argc, argv, m);
573 static int sparse_checkout_disable(int argc, const char **argv)
575 struct pattern_list pl;
576 struct strbuf match_all = STRBUF_INIT;
578 repo_read_index(the_repository);
579 require_clean_work_tree(the_repository,
580 N_("disable sparse-checkout"), NULL, 1, 0);
582 memset(&pl, 0, sizeof(pl));
583 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
584 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
585 pl.use_cone_patterns = 0;
586 core_apply_sparse_checkout = 1;
588 strbuf_addstr(&match_all, "/*");
589 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
591 if (update_working_directory(&pl))
592 die(_("error while refreshing working directory"));
594 clear_pattern_list(&pl);
595 return set_config(MODE_NO_PATTERNS);
598 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
600 static struct option builtin_sparse_checkout_options[] = {
604 if (argc == 2 && !strcmp(argv[1], "-h"))
605 usage_with_options(builtin_sparse_checkout_usage,
606 builtin_sparse_checkout_options);
608 argc = parse_options(argc, argv, prefix,
609 builtin_sparse_checkout_options,
610 builtin_sparse_checkout_usage,
611 PARSE_OPT_STOP_AT_NON_OPTION);
613 git_config(git_default_config, NULL);
616 if (!strcmp(argv[0], "list"))
617 return sparse_checkout_list(argc, argv);
618 if (!strcmp(argv[0], "init"))
619 return sparse_checkout_init(argc, argv);
620 if (!strcmp(argv[0], "set"))
621 return sparse_checkout_set(argc, argv, prefix, REPLACE);
622 if (!strcmp(argv[0], "add"))
623 return sparse_checkout_set(argc, argv, prefix, ADD);
624 if (!strcmp(argv[0], "disable"))
625 return sparse_checkout_disable(argc, argv);
628 usage_with_options(builtin_sparse_checkout_usage,
629 builtin_sparse_checkout_options);