surround %s with quotes when failed to lookup commit
[git/debian.git] / builtin / sparse-checkout.c
blob40d420f06cb0c58e57b957f322dd0485b7669023
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "object-file.h"
8 #include "object-name.h"
9 #include "parse-options.h"
10 #include "pathspec.h"
11 #include "repository.h"
12 #include "run-command.h"
13 #include "strbuf.h"
14 #include "string-list.h"
15 #include "cache-tree.h"
16 #include "lockfile.h"
17 #include "resolve-undo.h"
18 #include "unpack-trees.h"
19 #include "wt-status.h"
20 #include "quote.h"
21 #include "setup.h"
22 #include "sparse-index.h"
23 #include "worktree.h"
25 static const char *empty_base = "";
27 static char const * const builtin_sparse_checkout_usage[] = {
28 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
29 NULL
32 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
34 int i;
36 for (i = 0; i < pl->nr; i++) {
37 struct path_pattern *p = pl->patterns[i];
39 if (p->flags & PATTERN_FLAG_NEGATIVE)
40 fprintf(fp, "!");
42 fprintf(fp, "%s", p->pattern);
44 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
45 fprintf(fp, "/");
47 fprintf(fp, "\n");
51 static char const * const builtin_sparse_checkout_list_usage[] = {
52 "git sparse-checkout list",
53 NULL
56 static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
58 static struct option builtin_sparse_checkout_list_options[] = {
59 OPT_END(),
61 struct pattern_list pl;
62 char *sparse_filename;
63 int res;
65 setup_work_tree();
66 if (!core_apply_sparse_checkout)
67 die(_("this worktree is not sparse"));
69 argc = parse_options(argc, argv, prefix,
70 builtin_sparse_checkout_list_options,
71 builtin_sparse_checkout_list_usage, 0);
73 memset(&pl, 0, sizeof(pl));
75 pl.use_cone_patterns = core_sparse_checkout_cone;
77 sparse_filename = get_sparse_checkout_filename();
78 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
79 free(sparse_filename);
81 if (res < 0) {
82 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
83 return 0;
86 if (pl.use_cone_patterns) {
87 int i;
88 struct pattern_entry *pe;
89 struct hashmap_iter iter;
90 struct string_list sl = STRING_LIST_INIT_DUP;
92 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
93 /* pe->pattern starts with "/", skip it */
94 string_list_insert(&sl, pe->pattern + 1);
97 string_list_sort(&sl);
99 for (i = 0; i < sl.nr; i++) {
100 quote_c_style(sl.items[i].string, NULL, stdout, 0);
101 printf("\n");
104 return 0;
107 write_patterns_to_file(stdout, &pl);
108 clear_pattern_list(&pl);
110 return 0;
113 static void clean_tracked_sparse_directories(struct repository *r)
115 int i, was_full = 0;
116 struct strbuf path = STRBUF_INIT;
117 size_t pathlen;
118 struct string_list_item *item;
119 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
122 * If we are not using cone mode patterns, then we cannot
123 * delete directories outside of the sparse cone.
125 if (!r || !r->index || !r->worktree)
126 return;
127 if (init_sparse_checkout_patterns(r->index) ||
128 !r->index->sparse_checkout_patterns->use_cone_patterns)
129 return;
132 * Use the sparse index as a data structure to assist finding
133 * directories that are safe to delete. This conversion to a
134 * sparse index will not delete directories that contain
135 * conflicted entries or submodules.
137 if (r->index->sparse_index == INDEX_EXPANDED) {
139 * If something, such as a merge conflict or other concern,
140 * prevents us from converting to a sparse index, then do
141 * not try deleting files.
143 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
144 return;
145 was_full = 1;
148 strbuf_addstr(&path, r->worktree);
149 strbuf_complete(&path, '/');
150 pathlen = path.len;
153 * Collect directories that have gone out of scope but also
154 * exist on disk, so there is some work to be done. We need to
155 * store the entries in a list before exploring, since that might
156 * expand the sparse-index again.
158 for (i = 0; i < r->index->cache_nr; i++) {
159 struct cache_entry *ce = r->index->cache[i];
161 if (S_ISSPARSEDIR(ce->ce_mode) &&
162 repo_file_exists(r, ce->name))
163 string_list_append(&sparse_dirs, ce->name);
166 for_each_string_list_item(item, &sparse_dirs) {
167 struct dir_struct dir = DIR_INIT;
168 struct pathspec p = { 0 };
169 struct strvec s = STRVEC_INIT;
171 strbuf_setlen(&path, pathlen);
172 strbuf_addstr(&path, item->string);
174 dir.flags |= DIR_SHOW_IGNORED_TOO;
176 setup_standard_excludes(&dir);
177 strvec_push(&s, path.buf);
179 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
180 fill_directory(&dir, r->index, &p);
182 if (dir.nr) {
183 warning(_("directory '%s' contains untracked files,"
184 " but is not in the sparse-checkout cone"),
185 item->string);
186 } else if (remove_dir_recursively(&path, 0)) {
188 * Removal is "best effort". If something blocks
189 * the deletion, then continue with a warning.
191 warning(_("failed to remove directory '%s'"),
192 item->string);
195 strvec_clear(&s);
196 clear_pathspec(&p);
197 dir_clear(&dir);
200 string_list_clear(&sparse_dirs, 0);
201 strbuf_release(&path);
203 if (was_full)
204 ensure_full_index(r->index);
207 static int update_working_directory(struct pattern_list *pl)
209 enum update_sparsity_result result;
210 struct unpack_trees_options o;
211 struct lock_file lock_file = LOCK_INIT;
212 struct repository *r = the_repository;
214 /* If no branch has been checked out, there are no updates to make. */
215 if (is_index_unborn(r->index))
216 return UPDATE_SPARSITY_SUCCESS;
218 r->index->sparse_checkout_patterns = pl;
220 memset(&o, 0, sizeof(o));
221 o.verbose_update = isatty(2);
222 o.update = 1;
223 o.head_idx = -1;
224 o.src_index = r->index;
225 o.dst_index = r->index;
226 o.skip_sparse_checkout = 0;
228 setup_work_tree();
230 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
232 setup_unpack_trees_porcelain(&o, "sparse-checkout");
233 result = update_sparsity(&o, pl);
234 clear_unpack_trees_porcelain(&o);
236 if (result == UPDATE_SPARSITY_WARNINGS)
238 * We don't do any special handling of warnings from untracked
239 * files in the way or dirty entries that can't be removed.
241 result = UPDATE_SPARSITY_SUCCESS;
242 if (result == UPDATE_SPARSITY_SUCCESS)
243 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
244 else
245 rollback_lock_file(&lock_file);
247 clean_tracked_sparse_directories(r);
249 r->index->sparse_checkout_patterns = NULL;
250 return result;
253 static char *escaped_pattern(char *pattern)
255 char *p = pattern;
256 struct strbuf final = STRBUF_INIT;
258 while (*p) {
259 if (is_glob_special(*p))
260 strbuf_addch(&final, '\\');
262 strbuf_addch(&final, *p);
263 p++;
266 return strbuf_detach(&final, NULL);
269 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
271 int i;
272 struct pattern_entry *pe;
273 struct hashmap_iter iter;
274 struct string_list sl = STRING_LIST_INIT_DUP;
275 struct strbuf parent_pattern = STRBUF_INIT;
277 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
278 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
279 continue;
281 if (!hashmap_contains_parent(&pl->recursive_hashmap,
282 pe->pattern,
283 &parent_pattern))
284 string_list_insert(&sl, pe->pattern);
287 string_list_sort(&sl);
288 string_list_remove_duplicates(&sl, 0);
290 fprintf(fp, "/*\n!/*/\n");
292 for (i = 0; i < sl.nr; i++) {
293 char *pattern = escaped_pattern(sl.items[i].string);
295 if (strlen(pattern))
296 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
297 free(pattern);
300 string_list_clear(&sl, 0);
302 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
303 if (!hashmap_contains_parent(&pl->recursive_hashmap,
304 pe->pattern,
305 &parent_pattern))
306 string_list_insert(&sl, pe->pattern);
309 strbuf_release(&parent_pattern);
311 string_list_sort(&sl);
312 string_list_remove_duplicates(&sl, 0);
314 for (i = 0; i < sl.nr; i++) {
315 char *pattern = escaped_pattern(sl.items[i].string);
316 fprintf(fp, "%s/\n", pattern);
317 free(pattern);
321 static int write_patterns_and_update(struct pattern_list *pl)
323 char *sparse_filename;
324 FILE *fp;
325 int fd;
326 struct lock_file lk = LOCK_INIT;
327 int result;
329 sparse_filename = get_sparse_checkout_filename();
331 if (safe_create_leading_directories(sparse_filename))
332 die(_("failed to create directory for sparse-checkout file"));
334 fd = hold_lock_file_for_update(&lk, sparse_filename,
335 LOCK_DIE_ON_ERROR);
336 free(sparse_filename);
338 result = update_working_directory(pl);
339 if (result) {
340 rollback_lock_file(&lk);
341 clear_pattern_list(pl);
342 update_working_directory(NULL);
343 return result;
346 fp = xfdopen(fd, "w");
348 if (core_sparse_checkout_cone)
349 write_cone_to_file(fp, pl);
350 else
351 write_patterns_to_file(fp, pl);
353 fflush(fp);
354 commit_lock_file(&lk);
356 clear_pattern_list(pl);
358 return 0;
361 enum sparse_checkout_mode {
362 MODE_NO_PATTERNS = 0,
363 MODE_ALL_PATTERNS = 1,
364 MODE_CONE_PATTERNS = 2,
367 static int set_config(enum sparse_checkout_mode mode)
369 /* Update to use worktree config, if not already. */
370 if (init_worktree_config(the_repository)) {
371 error(_("failed to initialize worktree config"));
372 return 1;
375 if (repo_config_set_worktree_gently(the_repository,
376 "core.sparseCheckout",
377 mode ? "true" : "false") ||
378 repo_config_set_worktree_gently(the_repository,
379 "core.sparseCheckoutCone",
380 mode == MODE_CONE_PATTERNS ?
381 "true" : "false"))
382 return 1;
384 if (mode == MODE_NO_PATTERNS)
385 return set_sparse_index_config(the_repository, 0);
387 return 0;
390 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
391 /* If not specified, use previous definition of cone mode */
392 if (*cone_mode == -1 && core_apply_sparse_checkout)
393 *cone_mode = core_sparse_checkout_cone;
395 /* Set cone/non-cone mode appropriately */
396 core_apply_sparse_checkout = 1;
397 if (*cone_mode == 1 || *cone_mode == -1) {
398 core_sparse_checkout_cone = 1;
399 return MODE_CONE_PATTERNS;
401 core_sparse_checkout_cone = 0;
402 return MODE_ALL_PATTERNS;
405 static int update_modes(int *cone_mode, int *sparse_index)
407 int mode, record_mode;
409 /* Determine if we need to record the mode; ensure sparse checkout on */
410 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
412 mode = update_cone_mode(cone_mode);
413 if (record_mode && set_config(mode))
414 return 1;
416 /* Set sparse-index/non-sparse-index mode if specified */
417 if (*sparse_index >= 0) {
418 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
419 die(_("failed to modify sparse-index config"));
421 /* force an index rewrite */
422 repo_read_index(the_repository);
423 the_repository->index->updated_workdir = 1;
425 if (!*sparse_index)
426 ensure_full_index(the_repository->index);
429 return 0;
432 static char const * const builtin_sparse_checkout_init_usage[] = {
433 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
434 NULL
437 static struct sparse_checkout_init_opts {
438 int cone_mode;
439 int sparse_index;
440 } init_opts;
442 static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
444 struct pattern_list pl;
445 char *sparse_filename;
446 int res;
447 struct object_id oid;
448 struct strbuf pattern = STRBUF_INIT;
450 static struct option builtin_sparse_checkout_init_options[] = {
451 OPT_BOOL(0, "cone", &init_opts.cone_mode,
452 N_("initialize the sparse-checkout in cone mode")),
453 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
454 N_("toggle the use of a sparse index")),
455 OPT_END(),
458 setup_work_tree();
459 repo_read_index(the_repository);
461 init_opts.cone_mode = -1;
462 init_opts.sparse_index = -1;
464 argc = parse_options(argc, argv, prefix,
465 builtin_sparse_checkout_init_options,
466 builtin_sparse_checkout_init_usage, 0);
468 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
469 return 1;
471 memset(&pl, 0, sizeof(pl));
473 sparse_filename = get_sparse_checkout_filename();
474 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
476 /* If we already have a sparse-checkout file, use it. */
477 if (res >= 0) {
478 free(sparse_filename);
479 return update_working_directory(NULL);
482 if (repo_get_oid(the_repository, "HEAD", &oid)) {
483 FILE *fp;
485 /* assume we are in a fresh repo, but update the sparse-checkout file */
486 if (safe_create_leading_directories(sparse_filename))
487 die(_("unable to create leading directories of %s"),
488 sparse_filename);
489 fp = xfopen(sparse_filename, "w");
490 if (!fp)
491 die(_("failed to open '%s'"), sparse_filename);
493 free(sparse_filename);
494 fprintf(fp, "/*\n!/*/\n");
495 fclose(fp);
496 return 0;
499 strbuf_addstr(&pattern, "/*");
500 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
501 strbuf_addstr(&pattern, "!/*/");
502 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
503 pl.use_cone_patterns = init_opts.cone_mode;
505 return write_patterns_and_update(&pl);
508 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
510 struct pattern_entry *e = xmalloc(sizeof(*e));
511 e->patternlen = path->len;
512 e->pattern = strbuf_detach(path, NULL);
513 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
515 hashmap_add(&pl->recursive_hashmap, &e->ent);
517 while (e->patternlen) {
518 char *slash = strrchr(e->pattern, '/');
519 char *oldpattern = e->pattern;
520 size_t newlen;
522 if (!slash || slash == e->pattern)
523 break;
525 newlen = slash - e->pattern;
526 e = xmalloc(sizeof(struct pattern_entry));
527 e->patternlen = newlen;
528 e->pattern = xstrndup(oldpattern, newlen);
529 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
531 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
532 hashmap_add(&pl->parent_hashmap, &e->ent);
536 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
538 strbuf_trim(line);
540 strbuf_trim_trailing_dir_sep(line);
542 if (strbuf_normalize_path(line))
543 die(_("could not normalize path %s"), line->buf);
545 if (!line->len)
546 return;
548 if (line->buf[0] != '/')
549 strbuf_insertstr(line, 0, "/");
551 insert_recursive_pattern(pl, line);
554 static void add_patterns_from_input(struct pattern_list *pl,
555 int argc, const char **argv,
556 FILE *file)
558 int i;
559 if (core_sparse_checkout_cone) {
560 struct strbuf line = STRBUF_INIT;
562 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
563 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
564 pl->use_cone_patterns = 1;
566 if (file) {
567 struct strbuf unquoted = STRBUF_INIT;
568 while (!strbuf_getline(&line, file)) {
569 if (line.buf[0] == '"') {
570 strbuf_reset(&unquoted);
571 if (unquote_c_style(&unquoted, line.buf, NULL))
572 die(_("unable to unquote C-style string '%s'"),
573 line.buf);
575 strbuf_swap(&unquoted, &line);
578 strbuf_to_cone_pattern(&line, pl);
581 strbuf_release(&unquoted);
582 } else {
583 for (i = 0; i < argc; i++) {
584 strbuf_setlen(&line, 0);
585 strbuf_addstr(&line, argv[i]);
586 strbuf_to_cone_pattern(&line, pl);
589 } else {
590 if (file) {
591 struct strbuf line = STRBUF_INIT;
593 while (!strbuf_getline(&line, file)) {
594 size_t len;
595 char *buf = strbuf_detach(&line, &len);
596 add_pattern(buf, empty_base, 0, pl, 0);
598 } else {
599 for (i = 0; i < argc; i++)
600 add_pattern(argv[i], empty_base, 0, pl, 0);
605 enum modify_type {
606 REPLACE,
607 ADD,
610 static void add_patterns_cone_mode(int argc, const char **argv,
611 struct pattern_list *pl,
612 int use_stdin)
614 struct strbuf buffer = STRBUF_INIT;
615 struct pattern_entry *pe;
616 struct hashmap_iter iter;
617 struct pattern_list existing;
618 char *sparse_filename = get_sparse_checkout_filename();
620 add_patterns_from_input(pl, argc, argv,
621 use_stdin ? stdin : NULL);
623 memset(&existing, 0, sizeof(existing));
624 existing.use_cone_patterns = core_sparse_checkout_cone;
626 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
627 &existing, NULL, 0))
628 die(_("unable to load existing sparse-checkout patterns"));
629 free(sparse_filename);
631 if (!existing.use_cone_patterns)
632 die(_("existing sparse-checkout patterns do not use cone mode"));
634 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
635 if (!hashmap_contains_parent(&pl->recursive_hashmap,
636 pe->pattern, &buffer) ||
637 !hashmap_contains_parent(&pl->parent_hashmap,
638 pe->pattern, &buffer)) {
639 strbuf_reset(&buffer);
640 strbuf_addstr(&buffer, pe->pattern);
641 insert_recursive_pattern(pl, &buffer);
645 clear_pattern_list(&existing);
646 strbuf_release(&buffer);
649 static void add_patterns_literal(int argc, const char **argv,
650 struct pattern_list *pl,
651 int use_stdin)
653 char *sparse_filename = get_sparse_checkout_filename();
654 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
655 pl, NULL, 0))
656 die(_("unable to load existing sparse-checkout patterns"));
657 free(sparse_filename);
658 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
661 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
662 enum modify_type m)
664 int result;
665 int changed_config = 0;
666 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
668 switch (m) {
669 case ADD:
670 if (core_sparse_checkout_cone)
671 add_patterns_cone_mode(argc, argv, pl, use_stdin);
672 else
673 add_patterns_literal(argc, argv, pl, use_stdin);
674 break;
676 case REPLACE:
677 add_patterns_from_input(pl, argc, argv,
678 use_stdin ? stdin : NULL);
679 break;
682 if (!core_apply_sparse_checkout) {
683 set_config(MODE_ALL_PATTERNS);
684 core_apply_sparse_checkout = 1;
685 changed_config = 1;
688 result = write_patterns_and_update(pl);
690 if (result && changed_config)
691 set_config(MODE_NO_PATTERNS);
693 clear_pattern_list(pl);
694 free(pl);
695 return result;
698 static void sanitize_paths(int argc, const char **argv,
699 const char *prefix, int skip_checks)
701 int i;
703 if (!argc)
704 return;
706 if (prefix && *prefix && core_sparse_checkout_cone) {
708 * The args are not pathspecs, so unfortunately we
709 * cannot imitate how cmd_add() uses parse_pathspec().
711 int prefix_len = strlen(prefix);
713 for (i = 0; i < argc; i++)
714 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
717 if (skip_checks)
718 return;
720 if (prefix && *prefix && !core_sparse_checkout_cone)
721 die(_("please run from the toplevel directory in non-cone mode"));
723 if (core_sparse_checkout_cone) {
724 for (i = 0; i < argc; i++) {
725 if (argv[i][0] == '/')
726 die(_("specify directories rather than patterns (no leading slash)"));
727 if (argv[i][0] == '!')
728 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
729 if (strpbrk(argv[i], "*?[]"))
730 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
734 for (i = 0; i < argc; i++) {
735 struct cache_entry *ce;
736 struct index_state *index = the_repository->index;
737 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
739 if (pos < 0)
740 continue;
741 ce = index->cache[pos];
742 if (S_ISSPARSEDIR(ce->ce_mode))
743 continue;
745 if (core_sparse_checkout_cone)
746 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
747 else
748 warning(_("pass a leading slash before paths such as '%s' if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)."), argv[i]);
752 static char const * const builtin_sparse_checkout_add_usage[] = {
753 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
754 NULL
757 static struct sparse_checkout_add_opts {
758 int skip_checks;
759 int use_stdin;
760 } add_opts;
762 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
764 static struct option builtin_sparse_checkout_add_options[] = {
765 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
766 N_("skip some sanity checks on the given paths that might give false positives"),
767 PARSE_OPT_NONEG),
768 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
769 N_("read patterns from standard in")),
770 OPT_END(),
773 setup_work_tree();
774 if (!core_apply_sparse_checkout)
775 die(_("no sparse-checkout to add to"));
777 repo_read_index(the_repository);
779 argc = parse_options(argc, argv, prefix,
780 builtin_sparse_checkout_add_options,
781 builtin_sparse_checkout_add_usage,
782 PARSE_OPT_KEEP_UNKNOWN_OPT);
784 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
786 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
789 static char const * const builtin_sparse_checkout_set_usage[] = {
790 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
791 NULL
794 static struct sparse_checkout_set_opts {
795 int cone_mode;
796 int sparse_index;
797 int skip_checks;
798 int use_stdin;
799 } set_opts;
801 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
803 int default_patterns_nr = 2;
804 const char *default_patterns[] = {"/*", "!/*/", NULL};
806 static struct option builtin_sparse_checkout_set_options[] = {
807 OPT_BOOL(0, "cone", &set_opts.cone_mode,
808 N_("initialize the sparse-checkout in cone mode")),
809 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
810 N_("toggle the use of a sparse index")),
811 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
812 N_("skip some sanity checks on the given paths that might give false positives"),
813 PARSE_OPT_NONEG),
814 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
815 N_("read patterns from standard in"),
816 PARSE_OPT_NONEG),
817 OPT_END(),
820 setup_work_tree();
821 repo_read_index(the_repository);
823 set_opts.cone_mode = -1;
824 set_opts.sparse_index = -1;
826 argc = parse_options(argc, argv, prefix,
827 builtin_sparse_checkout_set_options,
828 builtin_sparse_checkout_set_usage,
829 PARSE_OPT_KEEP_UNKNOWN_OPT);
831 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
832 return 1;
835 * Cone mode automatically specifies the toplevel directory. For
836 * non-cone mode, if nothing is specified, manually select just the
837 * top-level directory (much as 'init' would do).
839 if (!core_sparse_checkout_cone && argc == 0) {
840 argv = default_patterns;
841 argc = default_patterns_nr;
842 } else {
843 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
846 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
849 static char const * const builtin_sparse_checkout_reapply_usage[] = {
850 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
851 NULL
854 static struct sparse_checkout_reapply_opts {
855 int cone_mode;
856 int sparse_index;
857 } reapply_opts;
859 static int sparse_checkout_reapply(int argc, const char **argv,
860 const char *prefix)
862 static struct option builtin_sparse_checkout_reapply_options[] = {
863 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
864 N_("initialize the sparse-checkout in cone mode")),
865 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
866 N_("toggle the use of a sparse index")),
867 OPT_END(),
870 setup_work_tree();
871 if (!core_apply_sparse_checkout)
872 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
874 reapply_opts.cone_mode = -1;
875 reapply_opts.sparse_index = -1;
877 argc = parse_options(argc, argv, prefix,
878 builtin_sparse_checkout_reapply_options,
879 builtin_sparse_checkout_reapply_usage, 0);
881 repo_read_index(the_repository);
883 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
884 return 1;
886 return update_working_directory(NULL);
889 static char const * const builtin_sparse_checkout_disable_usage[] = {
890 "git sparse-checkout disable",
891 NULL
894 static int sparse_checkout_disable(int argc, const char **argv,
895 const char *prefix)
897 static struct option builtin_sparse_checkout_disable_options[] = {
898 OPT_END(),
900 struct pattern_list pl;
901 struct strbuf match_all = STRBUF_INIT;
904 * We do not exit early if !core_apply_sparse_checkout; due to the
905 * ability for users to manually muck things up between
906 * direct editing of .git/info/sparse-checkout
907 * running read-tree -m u HEAD or update-index --skip-worktree
908 * direct toggling of config options
909 * users might end up with an index with SKIP_WORKTREE bit set on
910 * some files and not know how to undo it. So, here we just
911 * forcibly return to a dense checkout regardless of initial state.
914 setup_work_tree();
915 argc = parse_options(argc, argv, prefix,
916 builtin_sparse_checkout_disable_options,
917 builtin_sparse_checkout_disable_usage, 0);
919 repo_read_index(the_repository);
921 memset(&pl, 0, sizeof(pl));
922 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
923 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
924 pl.use_cone_patterns = 0;
925 core_apply_sparse_checkout = 1;
927 strbuf_addstr(&match_all, "/*");
928 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
930 prepare_repo_settings(the_repository);
931 the_repository->settings.sparse_index = 0;
933 if (update_working_directory(&pl))
934 die(_("error while refreshing working directory"));
936 clear_pattern_list(&pl);
937 return set_config(MODE_NO_PATTERNS);
940 static char const * const builtin_sparse_checkout_check_rules_usage[] = {
941 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
942 "[--[no-]cone] [--rules-file <file>]"),
943 NULL
946 static struct sparse_checkout_check_rules_opts {
947 int cone_mode;
948 int null_termination;
949 char *rules_file;
950 } check_rules_opts;
952 static int check_rules(struct pattern_list *pl, int null_terminated) {
953 struct strbuf line = STRBUF_INIT;
954 struct strbuf unquoted = STRBUF_INIT;
955 char *path;
956 int line_terminator = null_terminated ? 0 : '\n';
957 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
958 : strbuf_getline;
959 the_repository->index->sparse_checkout_patterns = pl;
960 while (!getline_fn(&line, stdin)) {
961 path = line.buf;
962 if (!null_terminated && line.buf[0] == '"') {
963 strbuf_reset(&unquoted);
964 if (unquote_c_style(&unquoted, line.buf, NULL))
965 die(_("unable to unquote C-style string '%s'"),
966 line.buf);
968 path = unquoted.buf;
971 if (path_in_sparse_checkout(path, the_repository->index))
972 write_name_quoted(path, stdout, line_terminator);
974 strbuf_release(&line);
975 strbuf_release(&unquoted);
977 return 0;
980 static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
982 static struct option builtin_sparse_checkout_check_rules_options[] = {
983 OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
984 N_("terminate input and output files by a NUL character")),
985 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
986 N_("when used with --rules-file interpret patterns as cone mode patterns")),
987 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
988 N_("use patterns in <file> instead of the current ones.")),
989 OPT_END(),
992 FILE *fp;
993 int ret;
994 struct pattern_list pl = {0};
995 char *sparse_filename;
996 check_rules_opts.cone_mode = -1;
998 argc = parse_options(argc, argv, prefix,
999 builtin_sparse_checkout_check_rules_options,
1000 builtin_sparse_checkout_check_rules_usage,
1001 PARSE_OPT_KEEP_UNKNOWN_OPT);
1003 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
1004 check_rules_opts.cone_mode = 1;
1006 update_cone_mode(&check_rules_opts.cone_mode);
1007 pl.use_cone_patterns = core_sparse_checkout_cone;
1008 if (check_rules_opts.rules_file) {
1009 fp = xfopen(check_rules_opts.rules_file, "r");
1010 add_patterns_from_input(&pl, argc, argv, fp);
1011 fclose(fp);
1012 } else {
1013 sparse_filename = get_sparse_checkout_filename();
1014 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1015 NULL, 0))
1016 die(_("unable to load existing sparse-checkout patterns"));
1017 free(sparse_filename);
1020 ret = check_rules(&pl, check_rules_opts.null_termination);
1021 clear_pattern_list(&pl);
1022 return ret;
1025 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
1027 parse_opt_subcommand_fn *fn = NULL;
1028 struct option builtin_sparse_checkout_options[] = {
1029 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1030 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1031 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1032 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1033 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1034 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1035 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
1036 OPT_END(),
1039 argc = parse_options(argc, argv, prefix,
1040 builtin_sparse_checkout_options,
1041 builtin_sparse_checkout_usage, 0);
1043 git_config(git_default_config, NULL);
1045 prepare_repo_settings(the_repository);
1046 the_repository->settings.command_requires_full_index = 0;
1048 return fn(argc, argv, prefix);