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 (upgrade_repository_format(1) < 0)
253 die(_("unable to upgrade repository format to enable worktreeConfig"));
254 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
255 error(_("failed to set extensions.worktreeConfig setting"));
259 config_path = git_path("config.worktree");
260 git_config_set_in_file_gently(config_path,
261 "core.sparseCheckout",
262 mode ? "true" : NULL);
264 git_config_set_in_file_gently(config_path,
265 "core.sparseCheckoutCone",
266 mode == MODE_CONE_PATTERNS ? "true" : NULL);
271 static char const * const builtin_sparse_checkout_init_usage[] = {
272 N_("git sparse-checkout init [--cone]"),
276 static struct sparse_checkout_init_opts {
280 static int sparse_checkout_init(int argc, const char **argv)
282 struct pattern_list pl;
283 char *sparse_filename;
285 struct object_id oid;
287 struct strbuf pattern = STRBUF_INIT;
289 static struct option builtin_sparse_checkout_init_options[] = {
290 OPT_BOOL(0, "cone", &init_opts.cone_mode,
291 N_("initialize the sparse-checkout in cone mode")),
295 repo_read_index(the_repository);
297 argc = parse_options(argc, argv, NULL,
298 builtin_sparse_checkout_init_options,
299 builtin_sparse_checkout_init_usage, 0);
301 if (init_opts.cone_mode) {
302 mode = MODE_CONE_PATTERNS;
303 core_sparse_checkout_cone = 1;
305 mode = MODE_ALL_PATTERNS;
307 if (set_config(mode))
310 memset(&pl, 0, sizeof(pl));
312 sparse_filename = get_sparse_checkout_filename();
313 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
315 /* If we already have a sparse-checkout file, use it. */
317 free(sparse_filename);
318 core_apply_sparse_checkout = 1;
319 return update_working_directory(NULL);
322 if (get_oid("HEAD", &oid)) {
325 /* assume we are in a fresh repo, but update the sparse-checkout file */
326 fp = xfopen(sparse_filename, "w");
328 die(_("failed to open '%s'"), sparse_filename);
330 free(sparse_filename);
331 fprintf(fp, "/*\n!/*/\n");
336 strbuf_addstr(&pattern, "/*");
337 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
338 strbuf_addstr(&pattern, "!/*/");
339 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
341 return write_patterns_and_update(&pl);
344 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
346 struct pattern_entry *e = xmalloc(sizeof(*e));
347 e->patternlen = path->len;
348 e->pattern = strbuf_detach(path, NULL);
349 hashmap_entry_init(&e->ent,
351 strihash(e->pattern) :
352 strhash(e->pattern));
354 hashmap_add(&pl->recursive_hashmap, &e->ent);
356 while (e->patternlen) {
357 char *slash = strrchr(e->pattern, '/');
358 char *oldpattern = e->pattern;
361 if (slash == e->pattern)
364 newlen = slash - e->pattern;
365 e = xmalloc(sizeof(struct pattern_entry));
366 e->patternlen = newlen;
367 e->pattern = xstrndup(oldpattern, newlen);
368 hashmap_entry_init(&e->ent,
370 strihash(e->pattern) :
371 strhash(e->pattern));
373 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
374 hashmap_add(&pl->parent_hashmap, &e->ent);
378 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
382 strbuf_trim_trailing_dir_sep(line);
384 if (strbuf_normalize_path(line))
385 die(_("could not normalize path %s"), line->buf);
390 if (line->buf[0] != '/')
391 strbuf_insertstr(line, 0, "/");
393 insert_recursive_pattern(pl, line);
396 static char const * const builtin_sparse_checkout_set_usage[] = {
397 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
401 static struct sparse_checkout_set_opts {
405 static void add_patterns_from_input(struct pattern_list *pl,
406 int argc, const char **argv)
409 if (core_sparse_checkout_cone) {
410 struct strbuf line = STRBUF_INIT;
412 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
413 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
414 pl->use_cone_patterns = 1;
416 if (set_opts.use_stdin) {
417 struct strbuf unquoted = STRBUF_INIT;
418 while (!strbuf_getline(&line, stdin)) {
419 if (line.buf[0] == '"') {
420 strbuf_reset(&unquoted);
421 if (unquote_c_style(&unquoted, line.buf, NULL))
422 die(_("unable to unquote C-style string '%s'"),
425 strbuf_swap(&unquoted, &line);
428 strbuf_to_cone_pattern(&line, pl);
431 strbuf_release(&unquoted);
433 for (i = 0; i < argc; i++) {
434 strbuf_setlen(&line, 0);
435 strbuf_addstr(&line, argv[i]);
436 strbuf_to_cone_pattern(&line, pl);
440 if (set_opts.use_stdin) {
441 struct strbuf line = STRBUF_INIT;
443 while (!strbuf_getline(&line, stdin)) {
445 char *buf = strbuf_detach(&line, &len);
446 add_pattern(buf, empty_base, 0, pl, 0);
449 for (i = 0; i < argc; i++)
450 add_pattern(argv[i], empty_base, 0, pl, 0);
460 static void add_patterns_cone_mode(int argc, const char **argv,
461 struct pattern_list *pl)
463 struct strbuf buffer = STRBUF_INIT;
464 struct pattern_entry *pe;
465 struct hashmap_iter iter;
466 struct pattern_list existing;
467 char *sparse_filename = get_sparse_checkout_filename();
469 add_patterns_from_input(pl, argc, argv);
471 memset(&existing, 0, sizeof(existing));
472 existing.use_cone_patterns = core_sparse_checkout_cone;
474 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
476 die(_("unable to load existing sparse-checkout patterns"));
477 free(sparse_filename);
479 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
480 if (!hashmap_contains_parent(&pl->recursive_hashmap,
481 pe->pattern, &buffer) ||
482 !hashmap_contains_parent(&pl->parent_hashmap,
483 pe->pattern, &buffer)) {
484 strbuf_reset(&buffer);
485 strbuf_addstr(&buffer, pe->pattern);
486 insert_recursive_pattern(pl, &buffer);
490 clear_pattern_list(&existing);
491 strbuf_release(&buffer);
494 static void add_patterns_literal(int argc, const char **argv,
495 struct pattern_list *pl)
497 char *sparse_filename = get_sparse_checkout_filename();
498 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
500 die(_("unable to load existing sparse-checkout patterns"));
501 free(sparse_filename);
502 add_patterns_from_input(pl, argc, argv);
505 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
508 int changed_config = 0;
509 struct pattern_list pl;
510 memset(&pl, 0, sizeof(pl));
514 if (core_sparse_checkout_cone)
515 add_patterns_cone_mode(argc, argv, &pl);
517 add_patterns_literal(argc, argv, &pl);
521 add_patterns_from_input(&pl, argc, argv);
525 if (!core_apply_sparse_checkout) {
526 set_config(MODE_ALL_PATTERNS);
527 core_apply_sparse_checkout = 1;
531 result = write_patterns_and_update(&pl);
533 if (result && changed_config)
534 set_config(MODE_NO_PATTERNS);
536 clear_pattern_list(&pl);
540 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
543 static struct option builtin_sparse_checkout_set_options[] = {
544 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
545 N_("read patterns from standard in")),
549 repo_read_index(the_repository);
551 argc = parse_options(argc, argv, prefix,
552 builtin_sparse_checkout_set_options,
553 builtin_sparse_checkout_set_usage,
554 PARSE_OPT_KEEP_UNKNOWN);
556 return modify_pattern_list(argc, argv, m);
559 static int sparse_checkout_reapply(int argc, const char **argv)
561 repo_read_index(the_repository);
562 return update_working_directory(NULL);
565 static int sparse_checkout_disable(int argc, const char **argv)
567 struct pattern_list pl;
568 struct strbuf match_all = STRBUF_INIT;
570 repo_read_index(the_repository);
572 memset(&pl, 0, sizeof(pl));
573 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
574 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
575 pl.use_cone_patterns = 0;
576 core_apply_sparse_checkout = 1;
578 strbuf_addstr(&match_all, "/*");
579 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
581 if (update_working_directory(&pl))
582 die(_("error while refreshing working directory"));
584 clear_pattern_list(&pl);
585 return set_config(MODE_NO_PATTERNS);
588 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
590 static struct option builtin_sparse_checkout_options[] = {
594 if (argc == 2 && !strcmp(argv[1], "-h"))
595 usage_with_options(builtin_sparse_checkout_usage,
596 builtin_sparse_checkout_options);
598 argc = parse_options(argc, argv, prefix,
599 builtin_sparse_checkout_options,
600 builtin_sparse_checkout_usage,
601 PARSE_OPT_STOP_AT_NON_OPTION);
603 git_config(git_default_config, NULL);
606 if (!strcmp(argv[0], "list"))
607 return sparse_checkout_list(argc, argv);
608 if (!strcmp(argv[0], "init"))
609 return sparse_checkout_init(argc, argv);
610 if (!strcmp(argv[0], "set"))
611 return sparse_checkout_set(argc, argv, prefix, REPLACE);
612 if (!strcmp(argv[0], "add"))
613 return sparse_checkout_set(argc, argv, prefix, ADD);
614 if (!strcmp(argv[0], "reapply"))
615 return sparse_checkout_reapply(argc, argv);
616 if (!strcmp(argv[0], "disable"))
617 return sparse_checkout_disable(argc, argv);
620 usage_with_options(builtin_sparse_checkout_usage,
621 builtin_sparse_checkout_options);