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)
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 result = update_sparsity(&o);
117 if (result == UPDATE_SPARSITY_WARNINGS)
119 * We don't do any special handling of warnings from untracked
120 * files in the way or dirty entries that can't be removed.
122 result = UPDATE_SPARSITY_SUCCESS;
123 if (result == UPDATE_SPARSITY_SUCCESS)
124 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
126 rollback_lock_file(&lock_file);
131 static char *escaped_pattern(char *pattern)
134 struct strbuf final = STRBUF_INIT;
137 if (is_glob_special(*p))
138 strbuf_addch(&final, '\\');
140 strbuf_addch(&final, *p);
144 return strbuf_detach(&final, NULL);
147 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
150 struct pattern_entry *pe;
151 struct hashmap_iter iter;
152 struct string_list sl = STRING_LIST_INIT_DUP;
153 struct strbuf parent_pattern = STRBUF_INIT;
155 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
156 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
159 if (!hashmap_contains_parent(&pl->recursive_hashmap,
162 string_list_insert(&sl, pe->pattern);
165 string_list_sort(&sl);
166 string_list_remove_duplicates(&sl, 0);
168 fprintf(fp, "/*\n!/*/\n");
170 for (i = 0; i < sl.nr; i++) {
171 char *pattern = escaped_pattern(sl.items[i].string);
174 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
178 string_list_clear(&sl, 0);
180 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
181 if (!hashmap_contains_parent(&pl->recursive_hashmap,
184 string_list_insert(&sl, pe->pattern);
187 strbuf_release(&parent_pattern);
189 string_list_sort(&sl);
190 string_list_remove_duplicates(&sl, 0);
192 for (i = 0; i < sl.nr; i++) {
193 char *pattern = escaped_pattern(sl.items[i].string);
194 fprintf(fp, "%s/\n", pattern);
199 static int write_patterns_and_update(struct pattern_list *pl)
201 char *sparse_filename;
204 struct lock_file lk = LOCK_INIT;
207 sparse_filename = get_sparse_checkout_filename();
209 if (safe_create_leading_directories(sparse_filename))
210 die(_("failed to create directory for sparse-checkout file"));
212 fd = hold_lock_file_for_update(&lk, sparse_filename,
215 result = update_working_directory(pl);
217 rollback_lock_file(&lk);
218 free(sparse_filename);
219 clear_pattern_list(pl);
220 update_working_directory(NULL);
224 fp = xfdopen(fd, "w");
226 if (core_sparse_checkout_cone)
227 write_cone_to_file(fp, pl);
229 write_patterns_to_file(fp, pl);
232 commit_lock_file(&lk);
234 free(sparse_filename);
235 clear_pattern_list(pl);
240 enum sparse_checkout_mode {
241 MODE_NO_PATTERNS = 0,
242 MODE_ALL_PATTERNS = 1,
243 MODE_CONE_PATTERNS = 2,
246 static int set_config(enum sparse_checkout_mode mode)
248 const char *config_path;
250 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
251 error(_("failed to set extensions.worktreeConfig setting"));
255 config_path = git_path("config.worktree");
256 git_config_set_in_file_gently(config_path,
257 "core.sparseCheckout",
258 mode ? "true" : NULL);
260 git_config_set_in_file_gently(config_path,
261 "core.sparseCheckoutCone",
262 mode == MODE_CONE_PATTERNS ? "true" : NULL);
267 static char const * const builtin_sparse_checkout_init_usage[] = {
268 N_("git sparse-checkout init [--cone]"),
272 static struct sparse_checkout_init_opts {
276 static int sparse_checkout_init(int argc, const char **argv)
278 struct pattern_list pl;
279 char *sparse_filename;
281 struct object_id oid;
283 struct strbuf pattern = STRBUF_INIT;
285 static struct option builtin_sparse_checkout_init_options[] = {
286 OPT_BOOL(0, "cone", &init_opts.cone_mode,
287 N_("initialize the sparse-checkout in cone mode")),
291 repo_read_index(the_repository);
293 argc = parse_options(argc, argv, NULL,
294 builtin_sparse_checkout_init_options,
295 builtin_sparse_checkout_init_usage, 0);
297 if (init_opts.cone_mode) {
298 mode = MODE_CONE_PATTERNS;
299 core_sparse_checkout_cone = 1;
301 mode = MODE_ALL_PATTERNS;
303 if (set_config(mode))
306 memset(&pl, 0, sizeof(pl));
308 sparse_filename = get_sparse_checkout_filename();
309 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
311 /* If we already have a sparse-checkout file, use it. */
313 free(sparse_filename);
314 core_apply_sparse_checkout = 1;
315 return update_working_directory(NULL);
318 if (get_oid("HEAD", &oid)) {
321 /* assume we are in a fresh repo, but update the sparse-checkout file */
322 fp = xfopen(sparse_filename, "w");
324 die(_("failed to open '%s'"), sparse_filename);
326 free(sparse_filename);
327 fprintf(fp, "/*\n!/*/\n");
332 strbuf_addstr(&pattern, "/*");
333 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
334 strbuf_addstr(&pattern, "!/*/");
335 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
337 return write_patterns_and_update(&pl);
340 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
342 struct pattern_entry *e = xmalloc(sizeof(*e));
343 e->patternlen = path->len;
344 e->pattern = strbuf_detach(path, NULL);
345 hashmap_entry_init(&e->ent,
347 strihash(e->pattern) :
348 strhash(e->pattern));
350 hashmap_add(&pl->recursive_hashmap, &e->ent);
352 while (e->patternlen) {
353 char *slash = strrchr(e->pattern, '/');
354 char *oldpattern = e->pattern;
357 if (slash == e->pattern)
360 newlen = slash - e->pattern;
361 e = xmalloc(sizeof(struct pattern_entry));
362 e->patternlen = newlen;
363 e->pattern = xstrndup(oldpattern, newlen);
364 hashmap_entry_init(&e->ent,
366 strihash(e->pattern) :
367 strhash(e->pattern));
369 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
370 hashmap_add(&pl->parent_hashmap, &e->ent);
374 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
378 strbuf_trim_trailing_dir_sep(line);
380 if (strbuf_normalize_path(line))
381 die(_("could not normalize path %s"), line->buf);
386 if (line->buf[0] != '/')
387 strbuf_insertstr(line, 0, "/");
389 insert_recursive_pattern(pl, line);
392 static char const * const builtin_sparse_checkout_set_usage[] = {
393 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
397 static struct sparse_checkout_set_opts {
401 static void add_patterns_from_input(struct pattern_list *pl,
402 int argc, const char **argv)
405 if (core_sparse_checkout_cone) {
406 struct strbuf line = STRBUF_INIT;
408 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
409 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
410 pl->use_cone_patterns = 1;
412 if (set_opts.use_stdin) {
413 struct strbuf unquoted = STRBUF_INIT;
414 while (!strbuf_getline(&line, stdin)) {
415 if (line.buf[0] == '"') {
416 strbuf_reset(&unquoted);
417 if (unquote_c_style(&unquoted, line.buf, NULL))
418 die(_("unable to unquote C-style string '%s'"),
421 strbuf_swap(&unquoted, &line);
424 strbuf_to_cone_pattern(&line, pl);
427 strbuf_release(&unquoted);
429 for (i = 0; i < argc; i++) {
430 strbuf_setlen(&line, 0);
431 strbuf_addstr(&line, argv[i]);
432 strbuf_to_cone_pattern(&line, pl);
436 if (set_opts.use_stdin) {
437 struct strbuf line = STRBUF_INIT;
439 while (!strbuf_getline(&line, stdin)) {
441 char *buf = strbuf_detach(&line, &len);
442 add_pattern(buf, empty_base, 0, pl, 0);
445 for (i = 0; i < argc; i++)
446 add_pattern(argv[i], empty_base, 0, pl, 0);
456 static void add_patterns_cone_mode(int argc, const char **argv,
457 struct pattern_list *pl)
459 struct strbuf buffer = STRBUF_INIT;
460 struct pattern_entry *pe;
461 struct hashmap_iter iter;
462 struct pattern_list existing;
463 char *sparse_filename = get_sparse_checkout_filename();
465 add_patterns_from_input(pl, argc, argv);
467 memset(&existing, 0, sizeof(existing));
468 existing.use_cone_patterns = core_sparse_checkout_cone;
470 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
472 die(_("unable to load existing sparse-checkout patterns"));
473 free(sparse_filename);
475 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
476 if (!hashmap_contains_parent(&pl->recursive_hashmap,
477 pe->pattern, &buffer) ||
478 !hashmap_contains_parent(&pl->parent_hashmap,
479 pe->pattern, &buffer)) {
480 strbuf_reset(&buffer);
481 strbuf_addstr(&buffer, pe->pattern);
482 insert_recursive_pattern(pl, &buffer);
486 clear_pattern_list(&existing);
487 strbuf_release(&buffer);
490 static void add_patterns_literal(int argc, const char **argv,
491 struct pattern_list *pl)
493 char *sparse_filename = get_sparse_checkout_filename();
494 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
496 die(_("unable to load existing sparse-checkout patterns"));
497 free(sparse_filename);
498 add_patterns_from_input(pl, argc, argv);
501 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
504 int changed_config = 0;
505 struct pattern_list pl;
506 memset(&pl, 0, sizeof(pl));
510 if (core_sparse_checkout_cone)
511 add_patterns_cone_mode(argc, argv, &pl);
513 add_patterns_literal(argc, argv, &pl);
517 add_patterns_from_input(&pl, argc, argv);
521 if (!core_apply_sparse_checkout) {
522 set_config(MODE_ALL_PATTERNS);
523 core_apply_sparse_checkout = 1;
527 result = write_patterns_and_update(&pl);
529 if (result && changed_config)
530 set_config(MODE_NO_PATTERNS);
532 clear_pattern_list(&pl);
536 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
539 static struct option builtin_sparse_checkout_set_options[] = {
540 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
541 N_("read patterns from standard in")),
545 repo_read_index(the_repository);
547 argc = parse_options(argc, argv, prefix,
548 builtin_sparse_checkout_set_options,
549 builtin_sparse_checkout_set_usage,
550 PARSE_OPT_KEEP_UNKNOWN);
552 return modify_pattern_list(argc, argv, m);
555 static int sparse_checkout_disable(int argc, const char **argv)
557 struct pattern_list pl;
558 struct strbuf match_all = STRBUF_INIT;
560 repo_read_index(the_repository);
562 memset(&pl, 0, sizeof(pl));
563 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
564 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
565 pl.use_cone_patterns = 0;
566 core_apply_sparse_checkout = 1;
568 strbuf_addstr(&match_all, "/*");
569 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
571 if (update_working_directory(&pl))
572 die(_("error while refreshing working directory"));
574 clear_pattern_list(&pl);
575 return set_config(MODE_NO_PATTERNS);
578 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
580 static struct option builtin_sparse_checkout_options[] = {
584 if (argc == 2 && !strcmp(argv[1], "-h"))
585 usage_with_options(builtin_sparse_checkout_usage,
586 builtin_sparse_checkout_options);
588 argc = parse_options(argc, argv, prefix,
589 builtin_sparse_checkout_options,
590 builtin_sparse_checkout_usage,
591 PARSE_OPT_STOP_AT_NON_OPTION);
593 git_config(git_default_config, NULL);
596 if (!strcmp(argv[0], "list"))
597 return sparse_checkout_list(argc, argv);
598 if (!strcmp(argv[0], "init"))
599 return sparse_checkout_init(argc, argv);
600 if (!strcmp(argv[0], "set"))
601 return sparse_checkout_set(argc, argv, prefix, REPLACE);
602 if (!strcmp(argv[0], "add"))
603 return sparse_checkout_set(argc, argv, prefix, ADD);
604 if (!strcmp(argv[0], "disable"))
605 return sparse_checkout_disable(argc, argv);
608 usage_with_options(builtin_sparse_checkout_usage,
609 builtin_sparse_checkout_options);