Documentation/RelNotes/2.45.0.txt: fix typo
[git.git] / builtin / sparse-checkout.c
blob0f52e252493d0d56fd0bcf5a7b9f92a416e86348
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 "strbuf.h"
12 #include "string-list.h"
13 #include "lockfile.h"
14 #include "unpack-trees.h"
15 #include "quote.h"
16 #include "setup.h"
17 #include "sparse-index.h"
18 #include "worktree.h"
20 static const char *empty_base = "";
22 static char const * const builtin_sparse_checkout_usage[] = {
23 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
24 NULL
27 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
29 int i;
31 for (i = 0; i < pl->nr; i++) {
32 struct path_pattern *p = pl->patterns[i];
34 if (p->flags & PATTERN_FLAG_NEGATIVE)
35 fprintf(fp, "!");
37 fprintf(fp, "%s", p->pattern);
39 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
40 fprintf(fp, "/");
42 fprintf(fp, "\n");
46 static char const * const builtin_sparse_checkout_list_usage[] = {
47 "git sparse-checkout list",
48 NULL
51 static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
53 static struct option builtin_sparse_checkout_list_options[] = {
54 OPT_END(),
56 struct pattern_list pl;
57 char *sparse_filename;
58 int res;
60 setup_work_tree();
61 if (!core_apply_sparse_checkout)
62 die(_("this worktree is not sparse"));
64 argc = parse_options(argc, argv, prefix,
65 builtin_sparse_checkout_list_options,
66 builtin_sparse_checkout_list_usage, 0);
68 memset(&pl, 0, sizeof(pl));
70 pl.use_cone_patterns = core_sparse_checkout_cone;
72 sparse_filename = get_sparse_checkout_filename();
73 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
74 free(sparse_filename);
76 if (res < 0) {
77 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
78 return 0;
81 if (pl.use_cone_patterns) {
82 int i;
83 struct pattern_entry *pe;
84 struct hashmap_iter iter;
85 struct string_list sl = STRING_LIST_INIT_DUP;
87 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
88 /* pe->pattern starts with "/", skip it */
89 string_list_insert(&sl, pe->pattern + 1);
92 string_list_sort(&sl);
94 for (i = 0; i < sl.nr; i++) {
95 quote_c_style(sl.items[i].string, NULL, stdout, 0);
96 printf("\n");
99 return 0;
102 write_patterns_to_file(stdout, &pl);
103 clear_pattern_list(&pl);
105 return 0;
108 static void clean_tracked_sparse_directories(struct repository *r)
110 int i, was_full = 0;
111 struct strbuf path = STRBUF_INIT;
112 size_t pathlen;
113 struct string_list_item *item;
114 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
117 * If we are not using cone mode patterns, then we cannot
118 * delete directories outside of the sparse cone.
120 if (!r || !r->index || !r->worktree)
121 return;
122 if (init_sparse_checkout_patterns(r->index) ||
123 !r->index->sparse_checkout_patterns->use_cone_patterns)
124 return;
127 * Use the sparse index as a data structure to assist finding
128 * directories that are safe to delete. This conversion to a
129 * sparse index will not delete directories that contain
130 * conflicted entries or submodules.
132 if (r->index->sparse_index == INDEX_EXPANDED) {
134 * If something, such as a merge conflict or other concern,
135 * prevents us from converting to a sparse index, then do
136 * not try deleting files.
138 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
139 return;
140 was_full = 1;
143 strbuf_addstr(&path, r->worktree);
144 strbuf_complete(&path, '/');
145 pathlen = path.len;
148 * Collect directories that have gone out of scope but also
149 * exist on disk, so there is some work to be done. We need to
150 * store the entries in a list before exploring, since that might
151 * expand the sparse-index again.
153 for (i = 0; i < r->index->cache_nr; i++) {
154 struct cache_entry *ce = r->index->cache[i];
156 if (S_ISSPARSEDIR(ce->ce_mode) &&
157 repo_file_exists(r, ce->name))
158 string_list_append(&sparse_dirs, ce->name);
161 for_each_string_list_item(item, &sparse_dirs) {
162 struct dir_struct dir = DIR_INIT;
163 struct pathspec p = { 0 };
164 struct strvec s = STRVEC_INIT;
166 strbuf_setlen(&path, pathlen);
167 strbuf_addstr(&path, item->string);
169 dir.flags |= DIR_SHOW_IGNORED_TOO;
171 setup_standard_excludes(&dir);
172 strvec_push(&s, path.buf);
174 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
175 fill_directory(&dir, r->index, &p);
177 if (dir.nr) {
178 warning(_("directory '%s' contains untracked files,"
179 " but is not in the sparse-checkout cone"),
180 item->string);
181 } else if (remove_dir_recursively(&path, 0)) {
183 * Removal is "best effort". If something blocks
184 * the deletion, then continue with a warning.
186 warning(_("failed to remove directory '%s'"),
187 item->string);
190 strvec_clear(&s);
191 clear_pathspec(&p);
192 dir_clear(&dir);
195 string_list_clear(&sparse_dirs, 0);
196 strbuf_release(&path);
198 if (was_full)
199 ensure_full_index(r->index);
202 static int update_working_directory(struct pattern_list *pl)
204 enum update_sparsity_result result;
205 struct unpack_trees_options o;
206 struct lock_file lock_file = LOCK_INIT;
207 struct repository *r = the_repository;
209 /* If no branch has been checked out, there are no updates to make. */
210 if (is_index_unborn(r->index))
211 return UPDATE_SPARSITY_SUCCESS;
213 r->index->sparse_checkout_patterns = pl;
215 memset(&o, 0, sizeof(o));
216 o.verbose_update = isatty(2);
217 o.update = 1;
218 o.head_idx = -1;
219 o.src_index = r->index;
220 o.dst_index = r->index;
221 o.skip_sparse_checkout = 0;
223 setup_work_tree();
225 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
227 setup_unpack_trees_porcelain(&o, "sparse-checkout");
228 result = update_sparsity(&o, pl);
229 clear_unpack_trees_porcelain(&o);
231 if (result == UPDATE_SPARSITY_WARNINGS)
233 * We don't do any special handling of warnings from untracked
234 * files in the way or dirty entries that can't be removed.
236 result = UPDATE_SPARSITY_SUCCESS;
237 if (result == UPDATE_SPARSITY_SUCCESS)
238 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
239 else
240 rollback_lock_file(&lock_file);
242 clean_tracked_sparse_directories(r);
244 r->index->sparse_checkout_patterns = NULL;
245 return result;
248 static char *escaped_pattern(char *pattern)
250 char *p = pattern;
251 struct strbuf final = STRBUF_INIT;
253 while (*p) {
254 if (is_glob_special(*p))
255 strbuf_addch(&final, '\\');
257 strbuf_addch(&final, *p);
258 p++;
261 return strbuf_detach(&final, NULL);
264 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
266 int i;
267 struct pattern_entry *pe;
268 struct hashmap_iter iter;
269 struct string_list sl = STRING_LIST_INIT_DUP;
270 struct strbuf parent_pattern = STRBUF_INIT;
272 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
273 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
274 continue;
276 if (!hashmap_contains_parent(&pl->recursive_hashmap,
277 pe->pattern,
278 &parent_pattern))
279 string_list_insert(&sl, pe->pattern);
282 string_list_sort(&sl);
283 string_list_remove_duplicates(&sl, 0);
285 fprintf(fp, "/*\n!/*/\n");
287 for (i = 0; i < sl.nr; i++) {
288 char *pattern = escaped_pattern(sl.items[i].string);
290 if (strlen(pattern))
291 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
292 free(pattern);
295 string_list_clear(&sl, 0);
297 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
298 if (!hashmap_contains_parent(&pl->recursive_hashmap,
299 pe->pattern,
300 &parent_pattern))
301 string_list_insert(&sl, pe->pattern);
304 strbuf_release(&parent_pattern);
306 string_list_sort(&sl);
307 string_list_remove_duplicates(&sl, 0);
309 for (i = 0; i < sl.nr; i++) {
310 char *pattern = escaped_pattern(sl.items[i].string);
311 fprintf(fp, "%s/\n", pattern);
312 free(pattern);
316 static int write_patterns_and_update(struct pattern_list *pl)
318 char *sparse_filename;
319 FILE *fp;
320 int fd;
321 struct lock_file lk = LOCK_INIT;
322 int result;
324 sparse_filename = get_sparse_checkout_filename();
326 if (safe_create_leading_directories(sparse_filename))
327 die(_("failed to create directory for sparse-checkout file"));
329 fd = hold_lock_file_for_update(&lk, sparse_filename,
330 LOCK_DIE_ON_ERROR);
331 free(sparse_filename);
333 result = update_working_directory(pl);
334 if (result) {
335 rollback_lock_file(&lk);
336 clear_pattern_list(pl);
337 update_working_directory(NULL);
338 return result;
341 fp = xfdopen(fd, "w");
343 if (core_sparse_checkout_cone)
344 write_cone_to_file(fp, pl);
345 else
346 write_patterns_to_file(fp, pl);
348 fflush(fp);
349 commit_lock_file(&lk);
351 clear_pattern_list(pl);
353 return 0;
356 enum sparse_checkout_mode {
357 MODE_NO_PATTERNS = 0,
358 MODE_ALL_PATTERNS = 1,
359 MODE_CONE_PATTERNS = 2,
362 static int set_config(enum sparse_checkout_mode mode)
364 /* Update to use worktree config, if not already. */
365 if (init_worktree_config(the_repository)) {
366 error(_("failed to initialize worktree config"));
367 return 1;
370 if (repo_config_set_worktree_gently(the_repository,
371 "core.sparseCheckout",
372 mode ? "true" : "false") ||
373 repo_config_set_worktree_gently(the_repository,
374 "core.sparseCheckoutCone",
375 mode == MODE_CONE_PATTERNS ?
376 "true" : "false"))
377 return 1;
379 if (mode == MODE_NO_PATTERNS)
380 return set_sparse_index_config(the_repository, 0);
382 return 0;
385 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
386 /* If not specified, use previous definition of cone mode */
387 if (*cone_mode == -1 && core_apply_sparse_checkout)
388 *cone_mode = core_sparse_checkout_cone;
390 /* Set cone/non-cone mode appropriately */
391 core_apply_sparse_checkout = 1;
392 if (*cone_mode == 1 || *cone_mode == -1) {
393 core_sparse_checkout_cone = 1;
394 return MODE_CONE_PATTERNS;
396 core_sparse_checkout_cone = 0;
397 return MODE_ALL_PATTERNS;
400 static int update_modes(int *cone_mode, int *sparse_index)
402 int mode, record_mode;
404 /* Determine if we need to record the mode; ensure sparse checkout on */
405 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
407 mode = update_cone_mode(cone_mode);
408 if (record_mode && set_config(mode))
409 return 1;
411 /* Set sparse-index/non-sparse-index mode if specified */
412 if (*sparse_index >= 0) {
413 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
414 die(_("failed to modify sparse-index config"));
416 /* force an index rewrite */
417 repo_read_index(the_repository);
418 the_repository->index->updated_workdir = 1;
420 if (!*sparse_index)
421 ensure_full_index(the_repository->index);
424 return 0;
427 static char const * const builtin_sparse_checkout_init_usage[] = {
428 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
429 NULL
432 static struct sparse_checkout_init_opts {
433 int cone_mode;
434 int sparse_index;
435 } init_opts;
437 static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
439 struct pattern_list pl;
440 char *sparse_filename;
441 int res;
442 struct object_id oid;
443 struct strbuf pattern = STRBUF_INIT;
445 static struct option builtin_sparse_checkout_init_options[] = {
446 OPT_BOOL(0, "cone", &init_opts.cone_mode,
447 N_("initialize the sparse-checkout in cone mode")),
448 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
449 N_("toggle the use of a sparse index")),
450 OPT_END(),
453 setup_work_tree();
454 repo_read_index(the_repository);
456 init_opts.cone_mode = -1;
457 init_opts.sparse_index = -1;
459 argc = parse_options(argc, argv, prefix,
460 builtin_sparse_checkout_init_options,
461 builtin_sparse_checkout_init_usage, 0);
463 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
464 return 1;
466 memset(&pl, 0, sizeof(pl));
468 sparse_filename = get_sparse_checkout_filename();
469 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
471 /* If we already have a sparse-checkout file, use it. */
472 if (res >= 0) {
473 free(sparse_filename);
474 return update_working_directory(NULL);
477 if (repo_get_oid(the_repository, "HEAD", &oid)) {
478 FILE *fp;
480 /* assume we are in a fresh repo, but update the sparse-checkout file */
481 if (safe_create_leading_directories(sparse_filename))
482 die(_("unable to create leading directories of %s"),
483 sparse_filename);
484 fp = xfopen(sparse_filename, "w");
485 if (!fp)
486 die(_("failed to open '%s'"), sparse_filename);
488 free(sparse_filename);
489 fprintf(fp, "/*\n!/*/\n");
490 fclose(fp);
491 return 0;
494 strbuf_addstr(&pattern, "/*");
495 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
496 strbuf_addstr(&pattern, "!/*/");
497 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
498 pl.use_cone_patterns = init_opts.cone_mode;
500 return write_patterns_and_update(&pl);
503 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
505 struct pattern_entry *e = xmalloc(sizeof(*e));
506 e->patternlen = path->len;
507 e->pattern = strbuf_detach(path, NULL);
508 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
510 hashmap_add(&pl->recursive_hashmap, &e->ent);
512 while (e->patternlen) {
513 char *slash = strrchr(e->pattern, '/');
514 char *oldpattern = e->pattern;
515 size_t newlen;
517 if (!slash || slash == e->pattern)
518 break;
520 newlen = slash - e->pattern;
521 e = xmalloc(sizeof(struct pattern_entry));
522 e->patternlen = newlen;
523 e->pattern = xstrndup(oldpattern, newlen);
524 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
526 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
527 hashmap_add(&pl->parent_hashmap, &e->ent);
531 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
533 strbuf_trim(line);
535 strbuf_trim_trailing_dir_sep(line);
537 if (strbuf_normalize_path(line))
538 die(_("could not normalize path %s"), line->buf);
540 if (!line->len)
541 return;
543 if (line->buf[0] != '/')
544 strbuf_insertstr(line, 0, "/");
546 insert_recursive_pattern(pl, line);
549 static void add_patterns_from_input(struct pattern_list *pl,
550 int argc, const char **argv,
551 FILE *file)
553 int i;
554 if (core_sparse_checkout_cone) {
555 struct strbuf line = STRBUF_INIT;
557 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
558 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
559 pl->use_cone_patterns = 1;
561 if (file) {
562 struct strbuf unquoted = STRBUF_INIT;
563 while (!strbuf_getline(&line, file)) {
564 if (line.buf[0] == '"') {
565 strbuf_reset(&unquoted);
566 if (unquote_c_style(&unquoted, line.buf, NULL))
567 die(_("unable to unquote C-style string '%s'"),
568 line.buf);
570 strbuf_swap(&unquoted, &line);
573 strbuf_to_cone_pattern(&line, pl);
576 strbuf_release(&unquoted);
577 } else {
578 for (i = 0; i < argc; i++) {
579 strbuf_setlen(&line, 0);
580 strbuf_addstr(&line, argv[i]);
581 strbuf_to_cone_pattern(&line, pl);
584 } else {
585 if (file) {
586 struct strbuf line = STRBUF_INIT;
588 while (!strbuf_getline(&line, file)) {
589 size_t len;
590 char *buf = strbuf_detach(&line, &len);
591 add_pattern(buf, empty_base, 0, pl, 0);
593 } else {
594 for (i = 0; i < argc; i++)
595 add_pattern(argv[i], empty_base, 0, pl, 0);
600 enum modify_type {
601 REPLACE,
602 ADD,
605 static void add_patterns_cone_mode(int argc, const char **argv,
606 struct pattern_list *pl,
607 int use_stdin)
609 struct strbuf buffer = STRBUF_INIT;
610 struct pattern_entry *pe;
611 struct hashmap_iter iter;
612 struct pattern_list existing;
613 char *sparse_filename = get_sparse_checkout_filename();
615 add_patterns_from_input(pl, argc, argv,
616 use_stdin ? stdin : NULL);
618 memset(&existing, 0, sizeof(existing));
619 existing.use_cone_patterns = core_sparse_checkout_cone;
621 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
622 &existing, NULL, 0))
623 die(_("unable to load existing sparse-checkout patterns"));
624 free(sparse_filename);
626 if (!existing.use_cone_patterns)
627 die(_("existing sparse-checkout patterns do not use cone mode"));
629 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
630 if (!hashmap_contains_parent(&pl->recursive_hashmap,
631 pe->pattern, &buffer) ||
632 !hashmap_contains_parent(&pl->parent_hashmap,
633 pe->pattern, &buffer)) {
634 strbuf_reset(&buffer);
635 strbuf_addstr(&buffer, pe->pattern);
636 insert_recursive_pattern(pl, &buffer);
640 clear_pattern_list(&existing);
641 strbuf_release(&buffer);
644 static void add_patterns_literal(int argc, const char **argv,
645 struct pattern_list *pl,
646 int use_stdin)
648 char *sparse_filename = get_sparse_checkout_filename();
649 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
650 pl, NULL, 0))
651 die(_("unable to load existing sparse-checkout patterns"));
652 free(sparse_filename);
653 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
656 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
657 enum modify_type m)
659 int result;
660 int changed_config = 0;
661 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
663 switch (m) {
664 case ADD:
665 if (core_sparse_checkout_cone)
666 add_patterns_cone_mode(argc, argv, pl, use_stdin);
667 else
668 add_patterns_literal(argc, argv, pl, use_stdin);
669 break;
671 case REPLACE:
672 add_patterns_from_input(pl, argc, argv,
673 use_stdin ? stdin : NULL);
674 break;
677 if (!core_apply_sparse_checkout) {
678 set_config(MODE_ALL_PATTERNS);
679 core_apply_sparse_checkout = 1;
680 changed_config = 1;
683 result = write_patterns_and_update(pl);
685 if (result && changed_config)
686 set_config(MODE_NO_PATTERNS);
688 clear_pattern_list(pl);
689 free(pl);
690 return result;
693 static void sanitize_paths(int argc, const char **argv,
694 const char *prefix, int skip_checks)
696 int i;
698 if (!argc)
699 return;
701 if (prefix && *prefix && core_sparse_checkout_cone) {
703 * The args are not pathspecs, so unfortunately we
704 * cannot imitate how cmd_add() uses parse_pathspec().
706 int prefix_len = strlen(prefix);
708 for (i = 0; i < argc; i++)
709 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
712 if (skip_checks)
713 return;
715 if (prefix && *prefix && !core_sparse_checkout_cone)
716 die(_("please run from the toplevel directory in non-cone mode"));
718 if (core_sparse_checkout_cone) {
719 for (i = 0; i < argc; i++) {
720 if (argv[i][0] == '/')
721 die(_("specify directories rather than patterns (no leading slash)"));
722 if (argv[i][0] == '!')
723 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
724 if (strpbrk(argv[i], "*?[]"))
725 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
729 for (i = 0; i < argc; i++) {
730 struct cache_entry *ce;
731 struct index_state *index = the_repository->index;
732 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
734 if (pos < 0)
735 continue;
736 ce = index->cache[pos];
737 if (S_ISSPARSEDIR(ce->ce_mode))
738 continue;
740 if (core_sparse_checkout_cone)
741 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
742 else
743 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]);
747 static char const * const builtin_sparse_checkout_add_usage[] = {
748 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
749 NULL
752 static struct sparse_checkout_add_opts {
753 int skip_checks;
754 int use_stdin;
755 } add_opts;
757 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
759 static struct option builtin_sparse_checkout_add_options[] = {
760 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
761 N_("skip some sanity checks on the given paths that might give false positives"),
762 PARSE_OPT_NONEG),
763 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
764 N_("read patterns from standard in")),
765 OPT_END(),
768 setup_work_tree();
769 if (!core_apply_sparse_checkout)
770 die(_("no sparse-checkout to add to"));
772 repo_read_index(the_repository);
774 argc = parse_options(argc, argv, prefix,
775 builtin_sparse_checkout_add_options,
776 builtin_sparse_checkout_add_usage, 0);
778 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
780 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
783 static char const * const builtin_sparse_checkout_set_usage[] = {
784 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
785 NULL
788 static struct sparse_checkout_set_opts {
789 int cone_mode;
790 int sparse_index;
791 int skip_checks;
792 int use_stdin;
793 } set_opts;
795 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
797 int default_patterns_nr = 2;
798 const char *default_patterns[] = {"/*", "!/*/", NULL};
800 static struct option builtin_sparse_checkout_set_options[] = {
801 OPT_BOOL(0, "cone", &set_opts.cone_mode,
802 N_("initialize the sparse-checkout in cone mode")),
803 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
804 N_("toggle the use of a sparse index")),
805 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
806 N_("skip some sanity checks on the given paths that might give false positives"),
807 PARSE_OPT_NONEG),
808 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
809 N_("read patterns from standard in"),
810 PARSE_OPT_NONEG),
811 OPT_END(),
814 setup_work_tree();
815 repo_read_index(the_repository);
817 set_opts.cone_mode = -1;
818 set_opts.sparse_index = -1;
820 argc = parse_options(argc, argv, prefix,
821 builtin_sparse_checkout_set_options,
822 builtin_sparse_checkout_set_usage, 0);
824 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
825 return 1;
828 * Cone mode automatically specifies the toplevel directory. For
829 * non-cone mode, if nothing is specified, manually select just the
830 * top-level directory (much as 'init' would do).
832 if (!core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
833 argv = default_patterns;
834 argc = default_patterns_nr;
835 } else {
836 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
839 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
842 static char const * const builtin_sparse_checkout_reapply_usage[] = {
843 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
844 NULL
847 static struct sparse_checkout_reapply_opts {
848 int cone_mode;
849 int sparse_index;
850 } reapply_opts;
852 static int sparse_checkout_reapply(int argc, const char **argv,
853 const char *prefix)
855 static struct option builtin_sparse_checkout_reapply_options[] = {
856 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
857 N_("initialize the sparse-checkout in cone mode")),
858 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
859 N_("toggle the use of a sparse index")),
860 OPT_END(),
863 setup_work_tree();
864 if (!core_apply_sparse_checkout)
865 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
867 reapply_opts.cone_mode = -1;
868 reapply_opts.sparse_index = -1;
870 argc = parse_options(argc, argv, prefix,
871 builtin_sparse_checkout_reapply_options,
872 builtin_sparse_checkout_reapply_usage, 0);
874 repo_read_index(the_repository);
876 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
877 return 1;
879 return update_working_directory(NULL);
882 static char const * const builtin_sparse_checkout_disable_usage[] = {
883 "git sparse-checkout disable",
884 NULL
887 static int sparse_checkout_disable(int argc, const char **argv,
888 const char *prefix)
890 static struct option builtin_sparse_checkout_disable_options[] = {
891 OPT_END(),
893 struct pattern_list pl;
894 struct strbuf match_all = STRBUF_INIT;
897 * We do not exit early if !core_apply_sparse_checkout; due to the
898 * ability for users to manually muck things up between
899 * direct editing of .git/info/sparse-checkout
900 * running read-tree -m u HEAD or update-index --skip-worktree
901 * direct toggling of config options
902 * users might end up with an index with SKIP_WORKTREE bit set on
903 * some files and not know how to undo it. So, here we just
904 * forcibly return to a dense checkout regardless of initial state.
907 setup_work_tree();
908 argc = parse_options(argc, argv, prefix,
909 builtin_sparse_checkout_disable_options,
910 builtin_sparse_checkout_disable_usage, 0);
912 repo_read_index(the_repository);
914 memset(&pl, 0, sizeof(pl));
915 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
916 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
917 pl.use_cone_patterns = 0;
918 core_apply_sparse_checkout = 1;
920 strbuf_addstr(&match_all, "/*");
921 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
923 prepare_repo_settings(the_repository);
924 the_repository->settings.sparse_index = 0;
926 if (update_working_directory(&pl))
927 die(_("error while refreshing working directory"));
929 clear_pattern_list(&pl);
930 return set_config(MODE_NO_PATTERNS);
933 static char const * const builtin_sparse_checkout_check_rules_usage[] = {
934 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
935 "[--[no-]cone] [--rules-file <file>]"),
936 NULL
939 static struct sparse_checkout_check_rules_opts {
940 int cone_mode;
941 int null_termination;
942 char *rules_file;
943 } check_rules_opts;
945 static int check_rules(struct pattern_list *pl, int null_terminated) {
946 struct strbuf line = STRBUF_INIT;
947 struct strbuf unquoted = STRBUF_INIT;
948 char *path;
949 int line_terminator = null_terminated ? 0 : '\n';
950 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
951 : strbuf_getline;
952 the_repository->index->sparse_checkout_patterns = pl;
953 while (!getline_fn(&line, stdin)) {
954 path = line.buf;
955 if (!null_terminated && line.buf[0] == '"') {
956 strbuf_reset(&unquoted);
957 if (unquote_c_style(&unquoted, line.buf, NULL))
958 die(_("unable to unquote C-style string '%s'"),
959 line.buf);
961 path = unquoted.buf;
964 if (path_in_sparse_checkout(path, the_repository->index))
965 write_name_quoted(path, stdout, line_terminator);
967 strbuf_release(&line);
968 strbuf_release(&unquoted);
970 return 0;
973 static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
975 static struct option builtin_sparse_checkout_check_rules_options[] = {
976 OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
977 N_("terminate input and output files by a NUL character")),
978 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
979 N_("when used with --rules-file interpret patterns as cone mode patterns")),
980 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
981 N_("use patterns in <file> instead of the current ones.")),
982 OPT_END(),
985 FILE *fp;
986 int ret;
987 struct pattern_list pl = {0};
988 char *sparse_filename;
989 check_rules_opts.cone_mode = -1;
991 argc = parse_options(argc, argv, prefix,
992 builtin_sparse_checkout_check_rules_options,
993 builtin_sparse_checkout_check_rules_usage, 0);
995 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
996 check_rules_opts.cone_mode = 1;
998 update_cone_mode(&check_rules_opts.cone_mode);
999 pl.use_cone_patterns = core_sparse_checkout_cone;
1000 if (check_rules_opts.rules_file) {
1001 fp = xfopen(check_rules_opts.rules_file, "r");
1002 add_patterns_from_input(&pl, argc, argv, fp);
1003 fclose(fp);
1004 } else {
1005 sparse_filename = get_sparse_checkout_filename();
1006 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1007 NULL, 0))
1008 die(_("unable to load existing sparse-checkout patterns"));
1009 free(sparse_filename);
1012 ret = check_rules(&pl, check_rules_opts.null_termination);
1013 clear_pattern_list(&pl);
1014 return ret;
1017 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
1019 parse_opt_subcommand_fn *fn = NULL;
1020 struct option builtin_sparse_checkout_options[] = {
1021 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1022 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1023 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1024 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1025 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1026 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1027 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
1028 OPT_END(),
1031 argc = parse_options(argc, argv, prefix,
1032 builtin_sparse_checkout_options,
1033 builtin_sparse_checkout_usage, 0);
1035 git_config(git_default_config, NULL);
1037 prepare_repo_settings(the_repository);
1038 the_repository->settings.command_requires_full_index = 0;
1040 return fn(argc, argv, prefix);