documentation: fix apostrophe usage
[git.git] / builtin / sparse-checkout.c
blob5c8ffb1f7598b056fadcdd8fb98182d34928e6f3
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "object-file.h"
7 #include "object-name.h"
8 #include "parse-options.h"
9 #include "pathspec.h"
10 #include "repository.h"
11 #include "run-command.h"
12 #include "strbuf.h"
13 #include "string-list.h"
14 #include "cache-tree.h"
15 #include "lockfile.h"
16 #include "resolve-undo.h"
17 #include "unpack-trees.h"
18 #include "wt-status.h"
19 #include "quote.h"
20 #include "setup.h"
21 #include "sparse-index.h"
22 #include "worktree.h"
24 static const char *empty_base = "";
26 static char const * const builtin_sparse_checkout_usage[] = {
27 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
28 NULL
31 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
33 int i;
35 for (i = 0; i < pl->nr; i++) {
36 struct path_pattern *p = pl->patterns[i];
38 if (p->flags & PATTERN_FLAG_NEGATIVE)
39 fprintf(fp, "!");
41 fprintf(fp, "%s", p->pattern);
43 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
44 fprintf(fp, "/");
46 fprintf(fp, "\n");
50 static char const * const builtin_sparse_checkout_list_usage[] = {
51 "git sparse-checkout list",
52 NULL
55 static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
57 static struct option builtin_sparse_checkout_list_options[] = {
58 OPT_END(),
60 struct pattern_list pl;
61 char *sparse_filename;
62 int res;
64 setup_work_tree();
65 if (!core_apply_sparse_checkout)
66 die(_("this worktree is not sparse"));
68 argc = parse_options(argc, argv, prefix,
69 builtin_sparse_checkout_list_options,
70 builtin_sparse_checkout_list_usage, 0);
72 memset(&pl, 0, sizeof(pl));
74 pl.use_cone_patterns = core_sparse_checkout_cone;
76 sparse_filename = get_sparse_checkout_filename();
77 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
78 free(sparse_filename);
80 if (res < 0) {
81 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
82 return 0;
85 if (pl.use_cone_patterns) {
86 int i;
87 struct pattern_entry *pe;
88 struct hashmap_iter iter;
89 struct string_list sl = STRING_LIST_INIT_DUP;
91 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
92 /* pe->pattern starts with "/", skip it */
93 string_list_insert(&sl, pe->pattern + 1);
96 string_list_sort(&sl);
98 for (i = 0; i < sl.nr; i++) {
99 quote_c_style(sl.items[i].string, NULL, stdout, 0);
100 printf("\n");
103 return 0;
106 write_patterns_to_file(stdout, &pl);
107 clear_pattern_list(&pl);
109 return 0;
112 static void clean_tracked_sparse_directories(struct repository *r)
114 int i, was_full = 0;
115 struct strbuf path = STRBUF_INIT;
116 size_t pathlen;
117 struct string_list_item *item;
118 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
121 * If we are not using cone mode patterns, then we cannot
122 * delete directories outside of the sparse cone.
124 if (!r || !r->index || !r->worktree)
125 return;
126 if (init_sparse_checkout_patterns(r->index) ||
127 !r->index->sparse_checkout_patterns->use_cone_patterns)
128 return;
131 * Use the sparse index as a data structure to assist finding
132 * directories that are safe to delete. This conversion to a
133 * sparse index will not delete directories that contain
134 * conflicted entries or submodules.
136 if (r->index->sparse_index == INDEX_EXPANDED) {
138 * If something, such as a merge conflict or other concern,
139 * prevents us from converting to a sparse index, then do
140 * not try deleting files.
142 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
143 return;
144 was_full = 1;
147 strbuf_addstr(&path, r->worktree);
148 strbuf_complete(&path, '/');
149 pathlen = path.len;
152 * Collect directories that have gone out of scope but also
153 * exist on disk, so there is some work to be done. We need to
154 * store the entries in a list before exploring, since that might
155 * expand the sparse-index again.
157 for (i = 0; i < r->index->cache_nr; i++) {
158 struct cache_entry *ce = r->index->cache[i];
160 if (S_ISSPARSEDIR(ce->ce_mode) &&
161 repo_file_exists(r, ce->name))
162 string_list_append(&sparse_dirs, ce->name);
165 for_each_string_list_item(item, &sparse_dirs) {
166 struct dir_struct dir = DIR_INIT;
167 struct pathspec p = { 0 };
168 struct strvec s = STRVEC_INIT;
170 strbuf_setlen(&path, pathlen);
171 strbuf_addstr(&path, item->string);
173 dir.flags |= DIR_SHOW_IGNORED_TOO;
175 setup_standard_excludes(&dir);
176 strvec_push(&s, path.buf);
178 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
179 fill_directory(&dir, r->index, &p);
181 if (dir.nr) {
182 warning(_("directory '%s' contains untracked files,"
183 " but is not in the sparse-checkout cone"),
184 item->string);
185 } else if (remove_dir_recursively(&path, 0)) {
187 * Removal is "best effort". If something blocks
188 * the deletion, then continue with a warning.
190 warning(_("failed to remove directory '%s'"),
191 item->string);
194 strvec_clear(&s);
195 clear_pathspec(&p);
196 dir_clear(&dir);
199 string_list_clear(&sparse_dirs, 0);
200 strbuf_release(&path);
202 if (was_full)
203 ensure_full_index(r->index);
206 static int update_working_directory(struct pattern_list *pl)
208 enum update_sparsity_result result;
209 struct unpack_trees_options o;
210 struct lock_file lock_file = LOCK_INIT;
211 struct repository *r = the_repository;
213 /* If no branch has been checked out, there are no updates to make. */
214 if (is_index_unborn(r->index))
215 return UPDATE_SPARSITY_SUCCESS;
217 r->index->sparse_checkout_patterns = pl;
219 memset(&o, 0, sizeof(o));
220 o.verbose_update = isatty(2);
221 o.update = 1;
222 o.head_idx = -1;
223 o.src_index = r->index;
224 o.dst_index = r->index;
225 o.skip_sparse_checkout = 0;
227 setup_work_tree();
229 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
231 setup_unpack_trees_porcelain(&o, "sparse-checkout");
232 result = update_sparsity(&o, pl);
233 clear_unpack_trees_porcelain(&o);
235 if (result == UPDATE_SPARSITY_WARNINGS)
237 * We don't do any special handling of warnings from untracked
238 * files in the way or dirty entries that can't be removed.
240 result = UPDATE_SPARSITY_SUCCESS;
241 if (result == UPDATE_SPARSITY_SUCCESS)
242 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
243 else
244 rollback_lock_file(&lock_file);
246 clean_tracked_sparse_directories(r);
248 r->index->sparse_checkout_patterns = NULL;
249 return result;
252 static char *escaped_pattern(char *pattern)
254 char *p = pattern;
255 struct strbuf final = STRBUF_INIT;
257 while (*p) {
258 if (is_glob_special(*p))
259 strbuf_addch(&final, '\\');
261 strbuf_addch(&final, *p);
262 p++;
265 return strbuf_detach(&final, NULL);
268 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
270 int i;
271 struct pattern_entry *pe;
272 struct hashmap_iter iter;
273 struct string_list sl = STRING_LIST_INIT_DUP;
274 struct strbuf parent_pattern = STRBUF_INIT;
276 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
277 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
278 continue;
280 if (!hashmap_contains_parent(&pl->recursive_hashmap,
281 pe->pattern,
282 &parent_pattern))
283 string_list_insert(&sl, pe->pattern);
286 string_list_sort(&sl);
287 string_list_remove_duplicates(&sl, 0);
289 fprintf(fp, "/*\n!/*/\n");
291 for (i = 0; i < sl.nr; i++) {
292 char *pattern = escaped_pattern(sl.items[i].string);
294 if (strlen(pattern))
295 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
296 free(pattern);
299 string_list_clear(&sl, 0);
301 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
302 if (!hashmap_contains_parent(&pl->recursive_hashmap,
303 pe->pattern,
304 &parent_pattern))
305 string_list_insert(&sl, pe->pattern);
308 strbuf_release(&parent_pattern);
310 string_list_sort(&sl);
311 string_list_remove_duplicates(&sl, 0);
313 for (i = 0; i < sl.nr; i++) {
314 char *pattern = escaped_pattern(sl.items[i].string);
315 fprintf(fp, "%s/\n", pattern);
316 free(pattern);
320 static int write_patterns_and_update(struct pattern_list *pl)
322 char *sparse_filename;
323 FILE *fp;
324 int fd;
325 struct lock_file lk = LOCK_INIT;
326 int result;
328 sparse_filename = get_sparse_checkout_filename();
330 if (safe_create_leading_directories(sparse_filename))
331 die(_("failed to create directory for sparse-checkout file"));
333 fd = hold_lock_file_for_update(&lk, sparse_filename,
334 LOCK_DIE_ON_ERROR);
335 free(sparse_filename);
337 result = update_working_directory(pl);
338 if (result) {
339 rollback_lock_file(&lk);
340 clear_pattern_list(pl);
341 update_working_directory(NULL);
342 return result;
345 fp = xfdopen(fd, "w");
347 if (core_sparse_checkout_cone)
348 write_cone_to_file(fp, pl);
349 else
350 write_patterns_to_file(fp, pl);
352 fflush(fp);
353 commit_lock_file(&lk);
355 clear_pattern_list(pl);
357 return 0;
360 enum sparse_checkout_mode {
361 MODE_NO_PATTERNS = 0,
362 MODE_ALL_PATTERNS = 1,
363 MODE_CONE_PATTERNS = 2,
366 static int set_config(enum sparse_checkout_mode mode)
368 /* Update to use worktree config, if not already. */
369 if (init_worktree_config(the_repository)) {
370 error(_("failed to initialize worktree config"));
371 return 1;
374 if (repo_config_set_worktree_gently(the_repository,
375 "core.sparseCheckout",
376 mode ? "true" : "false") ||
377 repo_config_set_worktree_gently(the_repository,
378 "core.sparseCheckoutCone",
379 mode == MODE_CONE_PATTERNS ?
380 "true" : "false"))
381 return 1;
383 if (mode == MODE_NO_PATTERNS)
384 return set_sparse_index_config(the_repository, 0);
386 return 0;
389 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
390 /* If not specified, use previous definition of cone mode */
391 if (*cone_mode == -1 && core_apply_sparse_checkout)
392 *cone_mode = core_sparse_checkout_cone;
394 /* Set cone/non-cone mode appropriately */
395 core_apply_sparse_checkout = 1;
396 if (*cone_mode == 1 || *cone_mode == -1) {
397 core_sparse_checkout_cone = 1;
398 return MODE_CONE_PATTERNS;
400 core_sparse_checkout_cone = 0;
401 return MODE_ALL_PATTERNS;
404 static int update_modes(int *cone_mode, int *sparse_index)
406 int mode, record_mode;
408 /* Determine if we need to record the mode; ensure sparse checkout on */
409 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
411 mode = update_cone_mode(cone_mode);
412 if (record_mode && set_config(mode))
413 return 1;
415 /* Set sparse-index/non-sparse-index mode if specified */
416 if (*sparse_index >= 0) {
417 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
418 die(_("failed to modify sparse-index config"));
420 /* force an index rewrite */
421 repo_read_index(the_repository);
422 the_repository->index->updated_workdir = 1;
424 if (!*sparse_index)
425 ensure_full_index(the_repository->index);
428 return 0;
431 static char const * const builtin_sparse_checkout_init_usage[] = {
432 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
433 NULL
436 static struct sparse_checkout_init_opts {
437 int cone_mode;
438 int sparse_index;
439 } init_opts;
441 static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
443 struct pattern_list pl;
444 char *sparse_filename;
445 int res;
446 struct object_id oid;
447 struct strbuf pattern = STRBUF_INIT;
449 static struct option builtin_sparse_checkout_init_options[] = {
450 OPT_BOOL(0, "cone", &init_opts.cone_mode,
451 N_("initialize the sparse-checkout in cone mode")),
452 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
453 N_("toggle the use of a sparse index")),
454 OPT_END(),
457 setup_work_tree();
458 repo_read_index(the_repository);
460 init_opts.cone_mode = -1;
461 init_opts.sparse_index = -1;
463 argc = parse_options(argc, argv, prefix,
464 builtin_sparse_checkout_init_options,
465 builtin_sparse_checkout_init_usage, 0);
467 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
468 return 1;
470 memset(&pl, 0, sizeof(pl));
472 sparse_filename = get_sparse_checkout_filename();
473 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
475 /* If we already have a sparse-checkout file, use it. */
476 if (res >= 0) {
477 free(sparse_filename);
478 return update_working_directory(NULL);
481 if (repo_get_oid(the_repository, "HEAD", &oid)) {
482 FILE *fp;
484 /* assume we are in a fresh repo, but update the sparse-checkout file */
485 if (safe_create_leading_directories(sparse_filename))
486 die(_("unable to create leading directories of %s"),
487 sparse_filename);
488 fp = xfopen(sparse_filename, "w");
489 if (!fp)
490 die(_("failed to open '%s'"), sparse_filename);
492 free(sparse_filename);
493 fprintf(fp, "/*\n!/*/\n");
494 fclose(fp);
495 return 0;
498 strbuf_addstr(&pattern, "/*");
499 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
500 strbuf_addstr(&pattern, "!/*/");
501 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
502 pl.use_cone_patterns = init_opts.cone_mode;
504 return write_patterns_and_update(&pl);
507 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
509 struct pattern_entry *e = xmalloc(sizeof(*e));
510 e->patternlen = path->len;
511 e->pattern = strbuf_detach(path, NULL);
512 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
514 hashmap_add(&pl->recursive_hashmap, &e->ent);
516 while (e->patternlen) {
517 char *slash = strrchr(e->pattern, '/');
518 char *oldpattern = e->pattern;
519 size_t newlen;
521 if (!slash || slash == e->pattern)
522 break;
524 newlen = slash - e->pattern;
525 e = xmalloc(sizeof(struct pattern_entry));
526 e->patternlen = newlen;
527 e->pattern = xstrndup(oldpattern, newlen);
528 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
530 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
531 hashmap_add(&pl->parent_hashmap, &e->ent);
535 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
537 strbuf_trim(line);
539 strbuf_trim_trailing_dir_sep(line);
541 if (strbuf_normalize_path(line))
542 die(_("could not normalize path %s"), line->buf);
544 if (!line->len)
545 return;
547 if (line->buf[0] != '/')
548 strbuf_insertstr(line, 0, "/");
550 insert_recursive_pattern(pl, line);
553 static void add_patterns_from_input(struct pattern_list *pl,
554 int argc, const char **argv,
555 FILE *file)
557 int i;
558 if (core_sparse_checkout_cone) {
559 struct strbuf line = STRBUF_INIT;
561 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
562 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
563 pl->use_cone_patterns = 1;
565 if (file) {
566 struct strbuf unquoted = STRBUF_INIT;
567 while (!strbuf_getline(&line, file)) {
568 if (line.buf[0] == '"') {
569 strbuf_reset(&unquoted);
570 if (unquote_c_style(&unquoted, line.buf, NULL))
571 die(_("unable to unquote C-style string '%s'"),
572 line.buf);
574 strbuf_swap(&unquoted, &line);
577 strbuf_to_cone_pattern(&line, pl);
580 strbuf_release(&unquoted);
581 } else {
582 for (i = 0; i < argc; i++) {
583 strbuf_setlen(&line, 0);
584 strbuf_addstr(&line, argv[i]);
585 strbuf_to_cone_pattern(&line, pl);
588 } else {
589 if (file) {
590 struct strbuf line = STRBUF_INIT;
592 while (!strbuf_getline(&line, file)) {
593 size_t len;
594 char *buf = strbuf_detach(&line, &len);
595 add_pattern(buf, empty_base, 0, pl, 0);
597 } else {
598 for (i = 0; i < argc; i++)
599 add_pattern(argv[i], empty_base, 0, pl, 0);
604 enum modify_type {
605 REPLACE,
606 ADD,
609 static void add_patterns_cone_mode(int argc, const char **argv,
610 struct pattern_list *pl,
611 int use_stdin)
613 struct strbuf buffer = STRBUF_INIT;
614 struct pattern_entry *pe;
615 struct hashmap_iter iter;
616 struct pattern_list existing;
617 char *sparse_filename = get_sparse_checkout_filename();
619 add_patterns_from_input(pl, argc, argv,
620 use_stdin ? stdin : NULL);
622 memset(&existing, 0, sizeof(existing));
623 existing.use_cone_patterns = core_sparse_checkout_cone;
625 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
626 &existing, NULL, 0))
627 die(_("unable to load existing sparse-checkout patterns"));
628 free(sparse_filename);
630 if (!existing.use_cone_patterns)
631 die(_("existing sparse-checkout patterns do not use cone mode"));
633 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
634 if (!hashmap_contains_parent(&pl->recursive_hashmap,
635 pe->pattern, &buffer) ||
636 !hashmap_contains_parent(&pl->parent_hashmap,
637 pe->pattern, &buffer)) {
638 strbuf_reset(&buffer);
639 strbuf_addstr(&buffer, pe->pattern);
640 insert_recursive_pattern(pl, &buffer);
644 clear_pattern_list(&existing);
645 strbuf_release(&buffer);
648 static void add_patterns_literal(int argc, const char **argv,
649 struct pattern_list *pl,
650 int use_stdin)
652 char *sparse_filename = get_sparse_checkout_filename();
653 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
654 pl, NULL, 0))
655 die(_("unable to load existing sparse-checkout patterns"));
656 free(sparse_filename);
657 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
660 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
661 enum modify_type m)
663 int result;
664 int changed_config = 0;
665 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
667 switch (m) {
668 case ADD:
669 if (core_sparse_checkout_cone)
670 add_patterns_cone_mode(argc, argv, pl, use_stdin);
671 else
672 add_patterns_literal(argc, argv, pl, use_stdin);
673 break;
675 case REPLACE:
676 add_patterns_from_input(pl, argc, argv,
677 use_stdin ? stdin : NULL);
678 break;
681 if (!core_apply_sparse_checkout) {
682 set_config(MODE_ALL_PATTERNS);
683 core_apply_sparse_checkout = 1;
684 changed_config = 1;
687 result = write_patterns_and_update(pl);
689 if (result && changed_config)
690 set_config(MODE_NO_PATTERNS);
692 clear_pattern_list(pl);
693 free(pl);
694 return result;
697 static void sanitize_paths(int argc, const char **argv,
698 const char *prefix, int skip_checks)
700 int i;
702 if (!argc)
703 return;
705 if (prefix && *prefix && core_sparse_checkout_cone) {
707 * The args are not pathspecs, so unfortunately we
708 * cannot imitate how cmd_add() uses parse_pathspec().
710 int prefix_len = strlen(prefix);
712 for (i = 0; i < argc; i++)
713 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
716 if (skip_checks)
717 return;
719 if (prefix && *prefix && !core_sparse_checkout_cone)
720 die(_("please run from the toplevel directory in non-cone mode"));
722 if (core_sparse_checkout_cone) {
723 for (i = 0; i < argc; i++) {
724 if (argv[i][0] == '/')
725 die(_("specify directories rather than patterns (no leading slash)"));
726 if (argv[i][0] == '!')
727 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
728 if (strpbrk(argv[i], "*?[]"))
729 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
733 for (i = 0; i < argc; i++) {
734 struct cache_entry *ce;
735 struct index_state *index = the_repository->index;
736 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
738 if (pos < 0)
739 continue;
740 ce = index->cache[pos];
741 if (S_ISSPARSEDIR(ce->ce_mode))
742 continue;
744 if (core_sparse_checkout_cone)
745 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
746 else
747 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]);
751 static char const * const builtin_sparse_checkout_add_usage[] = {
752 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
753 NULL
756 static struct sparse_checkout_add_opts {
757 int skip_checks;
758 int use_stdin;
759 } add_opts;
761 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
763 static struct option builtin_sparse_checkout_add_options[] = {
764 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
765 N_("skip some sanity checks on the given paths that might give false positives"),
766 PARSE_OPT_NONEG),
767 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
768 N_("read patterns from standard in")),
769 OPT_END(),
772 setup_work_tree();
773 if (!core_apply_sparse_checkout)
774 die(_("no sparse-checkout to add to"));
776 repo_read_index(the_repository);
778 argc = parse_options(argc, argv, prefix,
779 builtin_sparse_checkout_add_options,
780 builtin_sparse_checkout_add_usage,
781 PARSE_OPT_KEEP_UNKNOWN_OPT);
783 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
785 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
788 static char const * const builtin_sparse_checkout_set_usage[] = {
789 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
790 NULL
793 static struct sparse_checkout_set_opts {
794 int cone_mode;
795 int sparse_index;
796 int skip_checks;
797 int use_stdin;
798 } set_opts;
800 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
802 int default_patterns_nr = 2;
803 const char *default_patterns[] = {"/*", "!/*/", NULL};
805 static struct option builtin_sparse_checkout_set_options[] = {
806 OPT_BOOL(0, "cone", &set_opts.cone_mode,
807 N_("initialize the sparse-checkout in cone mode")),
808 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
809 N_("toggle the use of a sparse index")),
810 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
811 N_("skip some sanity checks on the given paths that might give false positives"),
812 PARSE_OPT_NONEG),
813 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
814 N_("read patterns from standard in"),
815 PARSE_OPT_NONEG),
816 OPT_END(),
819 setup_work_tree();
820 repo_read_index(the_repository);
822 set_opts.cone_mode = -1;
823 set_opts.sparse_index = -1;
825 argc = parse_options(argc, argv, prefix,
826 builtin_sparse_checkout_set_options,
827 builtin_sparse_checkout_set_usage,
828 PARSE_OPT_KEEP_UNKNOWN_OPT);
830 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
831 return 1;
834 * Cone mode automatically specifies the toplevel directory. For
835 * non-cone mode, if nothing is specified, manually select just the
836 * top-level directory (much as 'init' would do).
838 if (!core_sparse_checkout_cone && argc == 0) {
839 argv = default_patterns;
840 argc = default_patterns_nr;
841 } else {
842 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
845 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
848 static char const * const builtin_sparse_checkout_reapply_usage[] = {
849 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
850 NULL
853 static struct sparse_checkout_reapply_opts {
854 int cone_mode;
855 int sparse_index;
856 } reapply_opts;
858 static int sparse_checkout_reapply(int argc, const char **argv,
859 const char *prefix)
861 static struct option builtin_sparse_checkout_reapply_options[] = {
862 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
863 N_("initialize the sparse-checkout in cone mode")),
864 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
865 N_("toggle the use of a sparse index")),
866 OPT_END(),
869 setup_work_tree();
870 if (!core_apply_sparse_checkout)
871 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
873 reapply_opts.cone_mode = -1;
874 reapply_opts.sparse_index = -1;
876 argc = parse_options(argc, argv, prefix,
877 builtin_sparse_checkout_reapply_options,
878 builtin_sparse_checkout_reapply_usage, 0);
880 repo_read_index(the_repository);
882 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
883 return 1;
885 return update_working_directory(NULL);
888 static char const * const builtin_sparse_checkout_disable_usage[] = {
889 "git sparse-checkout disable",
890 NULL
893 static int sparse_checkout_disable(int argc, const char **argv,
894 const char *prefix)
896 static struct option builtin_sparse_checkout_disable_options[] = {
897 OPT_END(),
899 struct pattern_list pl;
900 struct strbuf match_all = STRBUF_INIT;
903 * We do not exit early if !core_apply_sparse_checkout; due to the
904 * ability for users to manually muck things up between
905 * direct editing of .git/info/sparse-checkout
906 * running read-tree -m u HEAD or update-index --skip-worktree
907 * direct toggling of config options
908 * users might end up with an index with SKIP_WORKTREE bit set on
909 * some files and not know how to undo it. So, here we just
910 * forcibly return to a dense checkout regardless of initial state.
913 setup_work_tree();
914 argc = parse_options(argc, argv, prefix,
915 builtin_sparse_checkout_disable_options,
916 builtin_sparse_checkout_disable_usage, 0);
918 repo_read_index(the_repository);
920 memset(&pl, 0, sizeof(pl));
921 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
922 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
923 pl.use_cone_patterns = 0;
924 core_apply_sparse_checkout = 1;
926 strbuf_addstr(&match_all, "/*");
927 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
929 prepare_repo_settings(the_repository);
930 the_repository->settings.sparse_index = 0;
932 if (update_working_directory(&pl))
933 die(_("error while refreshing working directory"));
935 clear_pattern_list(&pl);
936 return set_config(MODE_NO_PATTERNS);
939 static char const * const builtin_sparse_checkout_check_rules_usage[] = {
940 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
941 "[--[no-]cone] [--rules-file <file>]"),
942 NULL
945 static struct sparse_checkout_check_rules_opts {
946 int cone_mode;
947 int null_termination;
948 char *rules_file;
949 } check_rules_opts;
951 static int check_rules(struct pattern_list *pl, int null_terminated) {
952 struct strbuf line = STRBUF_INIT;
953 struct strbuf unquoted = STRBUF_INIT;
954 char *path;
955 int line_terminator = null_terminated ? 0 : '\n';
956 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
957 : strbuf_getline;
958 the_repository->index->sparse_checkout_patterns = pl;
959 while (!getline_fn(&line, stdin)) {
960 path = line.buf;
961 if (!null_terminated && line.buf[0] == '"') {
962 strbuf_reset(&unquoted);
963 if (unquote_c_style(&unquoted, line.buf, NULL))
964 die(_("unable to unquote C-style string '%s'"),
965 line.buf);
967 path = unquoted.buf;
970 if (path_in_sparse_checkout(path, the_repository->index))
971 write_name_quoted(path, stdout, line_terminator);
973 strbuf_release(&line);
974 strbuf_release(&unquoted);
976 return 0;
979 static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
981 static struct option builtin_sparse_checkout_check_rules_options[] = {
982 OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
983 N_("terminate input and output files by a NUL character")),
984 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
985 N_("when used with --rules-file interpret patterns as cone mode patterns")),
986 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
987 N_("use patterns in <file> instead of the current ones.")),
988 OPT_END(),
991 FILE *fp;
992 int ret;
993 struct pattern_list pl = {0};
994 char *sparse_filename;
995 check_rules_opts.cone_mode = -1;
997 argc = parse_options(argc, argv, prefix,
998 builtin_sparse_checkout_check_rules_options,
999 builtin_sparse_checkout_check_rules_usage,
1000 PARSE_OPT_KEEP_UNKNOWN_OPT);
1002 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
1003 check_rules_opts.cone_mode = 1;
1005 update_cone_mode(&check_rules_opts.cone_mode);
1006 pl.use_cone_patterns = core_sparse_checkout_cone;
1007 if (check_rules_opts.rules_file) {
1008 fp = xfopen(check_rules_opts.rules_file, "r");
1009 add_patterns_from_input(&pl, argc, argv, fp);
1010 fclose(fp);
1011 } else {
1012 sparse_filename = get_sparse_checkout_filename();
1013 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1014 NULL, 0))
1015 die(_("unable to load existing sparse-checkout patterns"));
1016 free(sparse_filename);
1019 ret = check_rules(&pl, check_rules_opts.null_termination);
1020 clear_pattern_list(&pl);
1021 return ret;
1024 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
1026 parse_opt_subcommand_fn *fn = NULL;
1027 struct option builtin_sparse_checkout_options[] = {
1028 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1029 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1030 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1031 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1032 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1033 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1034 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
1035 OPT_END(),
1038 argc = parse_options(argc, argv, prefix,
1039 builtin_sparse_checkout_options,
1040 builtin_sparse_checkout_usage, 0);
1042 git_config(git_default_config, NULL);
1044 prepare_repo_settings(the_repository);
1045 the_repository->settings.command_requires_full_index = 0;
1047 return fn(argc, argv, prefix);