rebase -i: remove patch file after conflict resolution
[git.git] / builtin / sparse-checkout.c
blob5e917ead7bc304956faa789a60b8a6663d1d6123
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 "parse-options.h"
8 #include "pathspec.h"
9 #include "repository.h"
10 #include "run-command.h"
11 #include "strbuf.h"
12 #include "string-list.h"
13 #include "cache-tree.h"
14 #include "lockfile.h"
15 #include "resolve-undo.h"
16 #include "unpack-trees.h"
17 #include "wt-status.h"
18 #include "quote.h"
19 #include "setup.h"
20 #include "sparse-index.h"
21 #include "worktree.h"
23 static const char *empty_base = "";
25 static char const * const builtin_sparse_checkout_usage[] = {
26 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
27 NULL
30 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
32 int i;
34 for (i = 0; i < pl->nr; i++) {
35 struct path_pattern *p = pl->patterns[i];
37 if (p->flags & PATTERN_FLAG_NEGATIVE)
38 fprintf(fp, "!");
40 fprintf(fp, "%s", p->pattern);
42 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
43 fprintf(fp, "/");
45 fprintf(fp, "\n");
49 static char const * const builtin_sparse_checkout_list_usage[] = {
50 "git sparse-checkout list",
51 NULL
54 static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
56 static struct option builtin_sparse_checkout_list_options[] = {
57 OPT_END(),
59 struct pattern_list pl;
60 char *sparse_filename;
61 int res;
63 setup_work_tree();
64 if (!core_apply_sparse_checkout)
65 die(_("this worktree is not sparse"));
67 argc = parse_options(argc, argv, prefix,
68 builtin_sparse_checkout_list_options,
69 builtin_sparse_checkout_list_usage, 0);
71 memset(&pl, 0, sizeof(pl));
73 pl.use_cone_patterns = core_sparse_checkout_cone;
75 sparse_filename = get_sparse_checkout_filename();
76 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
77 free(sparse_filename);
79 if (res < 0) {
80 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
81 return 0;
84 if (pl.use_cone_patterns) {
85 int i;
86 struct pattern_entry *pe;
87 struct hashmap_iter iter;
88 struct string_list sl = STRING_LIST_INIT_DUP;
90 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
91 /* pe->pattern starts with "/", skip it */
92 string_list_insert(&sl, pe->pattern + 1);
95 string_list_sort(&sl);
97 for (i = 0; i < sl.nr; i++) {
98 quote_c_style(sl.items[i].string, NULL, stdout, 0);
99 printf("\n");
102 return 0;
105 write_patterns_to_file(stdout, &pl);
106 clear_pattern_list(&pl);
108 return 0;
111 static void clean_tracked_sparse_directories(struct repository *r)
113 int i, was_full = 0;
114 struct strbuf path = STRBUF_INIT;
115 size_t pathlen;
116 struct string_list_item *item;
117 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
120 * If we are not using cone mode patterns, then we cannot
121 * delete directories outside of the sparse cone.
123 if (!r || !r->index || !r->worktree)
124 return;
125 if (init_sparse_checkout_patterns(r->index) ||
126 !r->index->sparse_checkout_patterns->use_cone_patterns)
127 return;
130 * Use the sparse index as a data structure to assist finding
131 * directories that are safe to delete. This conversion to a
132 * sparse index will not delete directories that contain
133 * conflicted entries or submodules.
135 if (r->index->sparse_index == INDEX_EXPANDED) {
137 * If something, such as a merge conflict or other concern,
138 * prevents us from converting to a sparse index, then do
139 * not try deleting files.
141 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
142 return;
143 was_full = 1;
146 strbuf_addstr(&path, r->worktree);
147 strbuf_complete(&path, '/');
148 pathlen = path.len;
151 * Collect directories that have gone out of scope but also
152 * exist on disk, so there is some work to be done. We need to
153 * store the entries in a list before exploring, since that might
154 * expand the sparse-index again.
156 for (i = 0; i < r->index->cache_nr; i++) {
157 struct cache_entry *ce = r->index->cache[i];
159 if (S_ISSPARSEDIR(ce->ce_mode) &&
160 repo_file_exists(r, ce->name))
161 string_list_append(&sparse_dirs, ce->name);
164 for_each_string_list_item(item, &sparse_dirs) {
165 struct dir_struct dir = DIR_INIT;
166 struct pathspec p = { 0 };
167 struct strvec s = STRVEC_INIT;
169 strbuf_setlen(&path, pathlen);
170 strbuf_addstr(&path, item->string);
172 dir.flags |= DIR_SHOW_IGNORED_TOO;
174 setup_standard_excludes(&dir);
175 strvec_push(&s, path.buf);
177 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
178 fill_directory(&dir, r->index, &p);
180 if (dir.nr) {
181 warning(_("directory '%s' contains untracked files,"
182 " but is not in the sparse-checkout cone"),
183 item->string);
184 } else if (remove_dir_recursively(&path, 0)) {
186 * Removal is "best effort". If something blocks
187 * the deletion, then continue with a warning.
189 warning(_("failed to remove directory '%s'"),
190 item->string);
193 strvec_clear(&s);
194 clear_pathspec(&p);
195 dir_clear(&dir);
198 string_list_clear(&sparse_dirs, 0);
199 strbuf_release(&path);
201 if (was_full)
202 ensure_full_index(r->index);
205 static int update_working_directory(struct pattern_list *pl)
207 enum update_sparsity_result result;
208 struct unpack_trees_options o;
209 struct lock_file lock_file = LOCK_INIT;
210 struct repository *r = the_repository;
212 /* If no branch has been checked out, there are no updates to make. */
213 if (is_index_unborn(r->index))
214 return UPDATE_SPARSITY_SUCCESS;
216 r->index->sparse_checkout_patterns = pl;
218 memset(&o, 0, sizeof(o));
219 o.verbose_update = isatty(2);
220 o.update = 1;
221 o.head_idx = -1;
222 o.src_index = r->index;
223 o.dst_index = r->index;
224 o.skip_sparse_checkout = 0;
226 setup_work_tree();
228 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
230 setup_unpack_trees_porcelain(&o, "sparse-checkout");
231 result = update_sparsity(&o, pl);
232 clear_unpack_trees_porcelain(&o);
234 if (result == UPDATE_SPARSITY_WARNINGS)
236 * We don't do any special handling of warnings from untracked
237 * files in the way or dirty entries that can't be removed.
239 result = UPDATE_SPARSITY_SUCCESS;
240 if (result == UPDATE_SPARSITY_SUCCESS)
241 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
242 else
243 rollback_lock_file(&lock_file);
245 clean_tracked_sparse_directories(r);
247 r->index->sparse_checkout_patterns = NULL;
248 return result;
251 static char *escaped_pattern(char *pattern)
253 char *p = pattern;
254 struct strbuf final = STRBUF_INIT;
256 while (*p) {
257 if (is_glob_special(*p))
258 strbuf_addch(&final, '\\');
260 strbuf_addch(&final, *p);
261 p++;
264 return strbuf_detach(&final, NULL);
267 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
269 int i;
270 struct pattern_entry *pe;
271 struct hashmap_iter iter;
272 struct string_list sl = STRING_LIST_INIT_DUP;
273 struct strbuf parent_pattern = STRBUF_INIT;
275 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
276 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
277 continue;
279 if (!hashmap_contains_parent(&pl->recursive_hashmap,
280 pe->pattern,
281 &parent_pattern))
282 string_list_insert(&sl, pe->pattern);
285 string_list_sort(&sl);
286 string_list_remove_duplicates(&sl, 0);
288 fprintf(fp, "/*\n!/*/\n");
290 for (i = 0; i < sl.nr; i++) {
291 char *pattern = escaped_pattern(sl.items[i].string);
293 if (strlen(pattern))
294 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
295 free(pattern);
298 string_list_clear(&sl, 0);
300 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
301 if (!hashmap_contains_parent(&pl->recursive_hashmap,
302 pe->pattern,
303 &parent_pattern))
304 string_list_insert(&sl, pe->pattern);
307 strbuf_release(&parent_pattern);
309 string_list_sort(&sl);
310 string_list_remove_duplicates(&sl, 0);
312 for (i = 0; i < sl.nr; i++) {
313 char *pattern = escaped_pattern(sl.items[i].string);
314 fprintf(fp, "%s/\n", pattern);
315 free(pattern);
319 static int write_patterns_and_update(struct pattern_list *pl)
321 char *sparse_filename;
322 FILE *fp;
323 int fd;
324 struct lock_file lk = LOCK_INIT;
325 int result;
327 sparse_filename = get_sparse_checkout_filename();
329 if (safe_create_leading_directories(sparse_filename))
330 die(_("failed to create directory for sparse-checkout file"));
332 fd = hold_lock_file_for_update(&lk, sparse_filename,
333 LOCK_DIE_ON_ERROR);
334 free(sparse_filename);
336 result = update_working_directory(pl);
337 if (result) {
338 rollback_lock_file(&lk);
339 clear_pattern_list(pl);
340 update_working_directory(NULL);
341 return result;
344 fp = xfdopen(fd, "w");
346 if (core_sparse_checkout_cone)
347 write_cone_to_file(fp, pl);
348 else
349 write_patterns_to_file(fp, pl);
351 fflush(fp);
352 commit_lock_file(&lk);
354 clear_pattern_list(pl);
356 return 0;
359 enum sparse_checkout_mode {
360 MODE_NO_PATTERNS = 0,
361 MODE_ALL_PATTERNS = 1,
362 MODE_CONE_PATTERNS = 2,
365 static int set_config(enum sparse_checkout_mode mode)
367 /* Update to use worktree config, if not already. */
368 if (init_worktree_config(the_repository)) {
369 error(_("failed to initialize worktree config"));
370 return 1;
373 if (repo_config_set_worktree_gently(the_repository,
374 "core.sparseCheckout",
375 mode ? "true" : "false") ||
376 repo_config_set_worktree_gently(the_repository,
377 "core.sparseCheckoutCone",
378 mode == MODE_CONE_PATTERNS ?
379 "true" : "false"))
380 return 1;
382 if (mode == MODE_NO_PATTERNS)
383 return set_sparse_index_config(the_repository, 0);
385 return 0;
388 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
389 /* If not specified, use previous definition of cone mode */
390 if (*cone_mode == -1 && core_apply_sparse_checkout)
391 *cone_mode = core_sparse_checkout_cone;
393 /* Set cone/non-cone mode appropriately */
394 core_apply_sparse_checkout = 1;
395 if (*cone_mode == 1 || *cone_mode == -1) {
396 core_sparse_checkout_cone = 1;
397 return MODE_CONE_PATTERNS;
399 core_sparse_checkout_cone = 0;
400 return MODE_ALL_PATTERNS;
403 static int update_modes(int *cone_mode, int *sparse_index)
405 int mode, record_mode;
407 /* Determine if we need to record the mode; ensure sparse checkout on */
408 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
410 mode = update_cone_mode(cone_mode);
411 if (record_mode && set_config(mode))
412 return 1;
414 /* Set sparse-index/non-sparse-index mode if specified */
415 if (*sparse_index >= 0) {
416 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
417 die(_("failed to modify sparse-index config"));
419 /* force an index rewrite */
420 repo_read_index(the_repository);
421 the_repository->index->updated_workdir = 1;
423 if (!*sparse_index)
424 ensure_full_index(the_repository->index);
427 return 0;
430 static char const * const builtin_sparse_checkout_init_usage[] = {
431 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
432 NULL
435 static struct sparse_checkout_init_opts {
436 int cone_mode;
437 int sparse_index;
438 } init_opts;
440 static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
442 struct pattern_list pl;
443 char *sparse_filename;
444 int res;
445 struct object_id oid;
446 struct strbuf pattern = STRBUF_INIT;
448 static struct option builtin_sparse_checkout_init_options[] = {
449 OPT_BOOL(0, "cone", &init_opts.cone_mode,
450 N_("initialize the sparse-checkout in cone mode")),
451 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
452 N_("toggle the use of a sparse index")),
453 OPT_END(),
456 setup_work_tree();
457 repo_read_index(the_repository);
459 init_opts.cone_mode = -1;
460 init_opts.sparse_index = -1;
462 argc = parse_options(argc, argv, prefix,
463 builtin_sparse_checkout_init_options,
464 builtin_sparse_checkout_init_usage, 0);
466 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
467 return 1;
469 memset(&pl, 0, sizeof(pl));
471 sparse_filename = get_sparse_checkout_filename();
472 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
474 /* If we already have a sparse-checkout file, use it. */
475 if (res >= 0) {
476 free(sparse_filename);
477 return update_working_directory(NULL);
480 if (repo_get_oid(the_repository, "HEAD", &oid)) {
481 FILE *fp;
483 /* assume we are in a fresh repo, but update the sparse-checkout file */
484 if (safe_create_leading_directories(sparse_filename))
485 die(_("unable to create leading directories of %s"),
486 sparse_filename);
487 fp = xfopen(sparse_filename, "w");
488 if (!fp)
489 die(_("failed to open '%s'"), sparse_filename);
491 free(sparse_filename);
492 fprintf(fp, "/*\n!/*/\n");
493 fclose(fp);
494 return 0;
497 strbuf_addstr(&pattern, "/*");
498 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
499 strbuf_addstr(&pattern, "!/*/");
500 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
501 pl.use_cone_patterns = init_opts.cone_mode;
503 return write_patterns_and_update(&pl);
506 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
508 struct pattern_entry *e = xmalloc(sizeof(*e));
509 e->patternlen = path->len;
510 e->pattern = strbuf_detach(path, NULL);
511 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
513 hashmap_add(&pl->recursive_hashmap, &e->ent);
515 while (e->patternlen) {
516 char *slash = strrchr(e->pattern, '/');
517 char *oldpattern = e->pattern;
518 size_t newlen;
520 if (!slash || slash == e->pattern)
521 break;
523 newlen = slash - e->pattern;
524 e = xmalloc(sizeof(struct pattern_entry));
525 e->patternlen = newlen;
526 e->pattern = xstrndup(oldpattern, newlen);
527 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
529 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
530 hashmap_add(&pl->parent_hashmap, &e->ent);
534 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
536 strbuf_trim(line);
538 strbuf_trim_trailing_dir_sep(line);
540 if (strbuf_normalize_path(line))
541 die(_("could not normalize path %s"), line->buf);
543 if (!line->len)
544 return;
546 if (line->buf[0] != '/')
547 strbuf_insertstr(line, 0, "/");
549 insert_recursive_pattern(pl, line);
552 static void add_patterns_from_input(struct pattern_list *pl,
553 int argc, const char **argv,
554 FILE *file)
556 int i;
557 if (core_sparse_checkout_cone) {
558 struct strbuf line = STRBUF_INIT;
560 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
561 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
562 pl->use_cone_patterns = 1;
564 if (file) {
565 struct strbuf unquoted = STRBUF_INIT;
566 while (!strbuf_getline(&line, file)) {
567 if (line.buf[0] == '"') {
568 strbuf_reset(&unquoted);
569 if (unquote_c_style(&unquoted, line.buf, NULL))
570 die(_("unable to unquote C-style string '%s'"),
571 line.buf);
573 strbuf_swap(&unquoted, &line);
576 strbuf_to_cone_pattern(&line, pl);
579 strbuf_release(&unquoted);
580 } else {
581 for (i = 0; i < argc; i++) {
582 strbuf_setlen(&line, 0);
583 strbuf_addstr(&line, argv[i]);
584 strbuf_to_cone_pattern(&line, pl);
587 } else {
588 if (file) {
589 struct strbuf line = STRBUF_INIT;
591 while (!strbuf_getline(&line, file)) {
592 size_t len;
593 char *buf = strbuf_detach(&line, &len);
594 add_pattern(buf, empty_base, 0, pl, 0);
596 } else {
597 for (i = 0; i < argc; i++)
598 add_pattern(argv[i], empty_base, 0, pl, 0);
603 enum modify_type {
604 REPLACE,
605 ADD,
608 static void add_patterns_cone_mode(int argc, const char **argv,
609 struct pattern_list *pl,
610 int use_stdin)
612 struct strbuf buffer = STRBUF_INIT;
613 struct pattern_entry *pe;
614 struct hashmap_iter iter;
615 struct pattern_list existing;
616 char *sparse_filename = get_sparse_checkout_filename();
618 add_patterns_from_input(pl, argc, argv,
619 use_stdin ? stdin : NULL);
621 memset(&existing, 0, sizeof(existing));
622 existing.use_cone_patterns = core_sparse_checkout_cone;
624 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
625 &existing, NULL, 0))
626 die(_("unable to load existing sparse-checkout patterns"));
627 free(sparse_filename);
629 if (!existing.use_cone_patterns)
630 die(_("existing sparse-checkout patterns do not use cone mode"));
632 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
633 if (!hashmap_contains_parent(&pl->recursive_hashmap,
634 pe->pattern, &buffer) ||
635 !hashmap_contains_parent(&pl->parent_hashmap,
636 pe->pattern, &buffer)) {
637 strbuf_reset(&buffer);
638 strbuf_addstr(&buffer, pe->pattern);
639 insert_recursive_pattern(pl, &buffer);
643 clear_pattern_list(&existing);
644 strbuf_release(&buffer);
647 static void add_patterns_literal(int argc, const char **argv,
648 struct pattern_list *pl,
649 int use_stdin)
651 char *sparse_filename = get_sparse_checkout_filename();
652 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
653 pl, NULL, 0))
654 die(_("unable to load existing sparse-checkout patterns"));
655 free(sparse_filename);
656 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
659 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
660 enum modify_type m)
662 int result;
663 int changed_config = 0;
664 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
666 switch (m) {
667 case ADD:
668 if (core_sparse_checkout_cone)
669 add_patterns_cone_mode(argc, argv, pl, use_stdin);
670 else
671 add_patterns_literal(argc, argv, pl, use_stdin);
672 break;
674 case REPLACE:
675 add_patterns_from_input(pl, argc, argv,
676 use_stdin ? stdin : NULL);
677 break;
680 if (!core_apply_sparse_checkout) {
681 set_config(MODE_ALL_PATTERNS);
682 core_apply_sparse_checkout = 1;
683 changed_config = 1;
686 result = write_patterns_and_update(pl);
688 if (result && changed_config)
689 set_config(MODE_NO_PATTERNS);
691 clear_pattern_list(pl);
692 free(pl);
693 return result;
696 static void sanitize_paths(int argc, const char **argv,
697 const char *prefix, int skip_checks)
699 int i;
701 if (!argc)
702 return;
704 if (prefix && *prefix && core_sparse_checkout_cone) {
706 * The args are not pathspecs, so unfortunately we
707 * cannot imitate how cmd_add() uses parse_pathspec().
709 int prefix_len = strlen(prefix);
711 for (i = 0; i < argc; i++)
712 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
715 if (skip_checks)
716 return;
718 if (prefix && *prefix && !core_sparse_checkout_cone)
719 die(_("please run from the toplevel directory in non-cone mode"));
721 if (core_sparse_checkout_cone) {
722 for (i = 0; i < argc; i++) {
723 if (argv[i][0] == '/')
724 die(_("specify directories rather than patterns (no leading slash)"));
725 if (argv[i][0] == '!')
726 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
727 if (strpbrk(argv[i], "*?[]"))
728 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
732 for (i = 0; i < argc; i++) {
733 struct cache_entry *ce;
734 struct index_state *index = the_repository->index;
735 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
737 if (pos < 0)
738 continue;
739 ce = index->cache[pos];
740 if (S_ISSPARSEDIR(ce->ce_mode))
741 continue;
743 if (core_sparse_checkout_cone)
744 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
745 else
746 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]);
750 static char const * const builtin_sparse_checkout_add_usage[] = {
751 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
752 NULL
755 static struct sparse_checkout_add_opts {
756 int skip_checks;
757 int use_stdin;
758 } add_opts;
760 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
762 static struct option builtin_sparse_checkout_add_options[] = {
763 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
764 N_("skip some sanity checks on the given paths that might give false positives"),
765 PARSE_OPT_NONEG),
766 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
767 N_("read patterns from standard in")),
768 OPT_END(),
771 setup_work_tree();
772 if (!core_apply_sparse_checkout)
773 die(_("no sparse-checkout to add to"));
775 repo_read_index(the_repository);
777 argc = parse_options(argc, argv, prefix,
778 builtin_sparse_checkout_add_options,
779 builtin_sparse_checkout_add_usage,
780 PARSE_OPT_KEEP_UNKNOWN_OPT);
782 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
784 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
787 static char const * const builtin_sparse_checkout_set_usage[] = {
788 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
789 NULL
792 static struct sparse_checkout_set_opts {
793 int cone_mode;
794 int sparse_index;
795 int skip_checks;
796 int use_stdin;
797 } set_opts;
799 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
801 int default_patterns_nr = 2;
802 const char *default_patterns[] = {"/*", "!/*/", NULL};
804 static struct option builtin_sparse_checkout_set_options[] = {
805 OPT_BOOL(0, "cone", &set_opts.cone_mode,
806 N_("initialize the sparse-checkout in cone mode")),
807 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
808 N_("toggle the use of a sparse index")),
809 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
810 N_("skip some sanity checks on the given paths that might give false positives"),
811 PARSE_OPT_NONEG),
812 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
813 N_("read patterns from standard in"),
814 PARSE_OPT_NONEG),
815 OPT_END(),
818 setup_work_tree();
819 repo_read_index(the_repository);
821 set_opts.cone_mode = -1;
822 set_opts.sparse_index = -1;
824 argc = parse_options(argc, argv, prefix,
825 builtin_sparse_checkout_set_options,
826 builtin_sparse_checkout_set_usage,
827 PARSE_OPT_KEEP_UNKNOWN_OPT);
829 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
830 return 1;
833 * Cone mode automatically specifies the toplevel directory. For
834 * non-cone mode, if nothing is specified, manually select just the
835 * top-level directory (much as 'init' would do).
837 if (!core_sparse_checkout_cone && argc == 0) {
838 argv = default_patterns;
839 argc = default_patterns_nr;
840 } else {
841 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
844 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
847 static char const * const builtin_sparse_checkout_reapply_usage[] = {
848 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
849 NULL
852 static struct sparse_checkout_reapply_opts {
853 int cone_mode;
854 int sparse_index;
855 } reapply_opts;
857 static int sparse_checkout_reapply(int argc, const char **argv,
858 const char *prefix)
860 static struct option builtin_sparse_checkout_reapply_options[] = {
861 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
862 N_("initialize the sparse-checkout in cone mode")),
863 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
864 N_("toggle the use of a sparse index")),
865 OPT_END(),
868 setup_work_tree();
869 if (!core_apply_sparse_checkout)
870 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
872 reapply_opts.cone_mode = -1;
873 reapply_opts.sparse_index = -1;
875 argc = parse_options(argc, argv, prefix,
876 builtin_sparse_checkout_reapply_options,
877 builtin_sparse_checkout_reapply_usage, 0);
879 repo_read_index(the_repository);
881 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
882 return 1;
884 return update_working_directory(NULL);
887 static char const * const builtin_sparse_checkout_disable_usage[] = {
888 "git sparse-checkout disable",
889 NULL
892 static int sparse_checkout_disable(int argc, const char **argv,
893 const char *prefix)
895 static struct option builtin_sparse_checkout_disable_options[] = {
896 OPT_END(),
898 struct pattern_list pl;
899 struct strbuf match_all = STRBUF_INIT;
902 * We do not exit early if !core_apply_sparse_checkout; due to the
903 * ability for users to manually muck things up between
904 * direct editing of .git/info/sparse-checkout
905 * running read-tree -m u HEAD or update-index --skip-worktree
906 * direct toggling of config options
907 * users might end up with an index with SKIP_WORKTREE bit set on
908 * some files and not know how to undo it. So, here we just
909 * forcibly return to a dense checkout regardless of initial state.
912 setup_work_tree();
913 argc = parse_options(argc, argv, prefix,
914 builtin_sparse_checkout_disable_options,
915 builtin_sparse_checkout_disable_usage, 0);
917 repo_read_index(the_repository);
919 memset(&pl, 0, sizeof(pl));
920 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
921 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
922 pl.use_cone_patterns = 0;
923 core_apply_sparse_checkout = 1;
925 strbuf_addstr(&match_all, "/*");
926 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
928 prepare_repo_settings(the_repository);
929 the_repository->settings.sparse_index = 0;
931 if (update_working_directory(&pl))
932 die(_("error while refreshing working directory"));
934 clear_pattern_list(&pl);
935 return set_config(MODE_NO_PATTERNS);
938 static char const * const builtin_sparse_checkout_check_rules_usage[] = {
939 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
940 "[--[no-]cone] [--rules-file <file>]"),
941 NULL
944 static struct sparse_checkout_check_rules_opts {
945 int cone_mode;
946 int null_termination;
947 char *rules_file;
948 } check_rules_opts;
950 static int check_rules(struct pattern_list *pl, int null_terminated) {
951 struct strbuf line = STRBUF_INIT;
952 struct strbuf unquoted = STRBUF_INIT;
953 char *path;
954 int line_terminator = null_terminated ? 0 : '\n';
955 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
956 : strbuf_getline;
957 the_repository->index->sparse_checkout_patterns = pl;
958 while (!getline_fn(&line, stdin)) {
959 path = line.buf;
960 if (!null_terminated && line.buf[0] == '"') {
961 strbuf_reset(&unquoted);
962 if (unquote_c_style(&unquoted, line.buf, NULL))
963 die(_("unable to unquote C-style string '%s'"),
964 line.buf);
966 path = unquoted.buf;
969 if (path_in_sparse_checkout(path, the_repository->index))
970 write_name_quoted(path, stdout, line_terminator);
972 strbuf_release(&line);
973 strbuf_release(&unquoted);
975 return 0;
978 static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
980 static struct option builtin_sparse_checkout_check_rules_options[] = {
981 OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
982 N_("terminate input and output files by a NUL character")),
983 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
984 N_("when used with --rules-file interpret patterns as cone mode patterns")),
985 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
986 N_("use patterns in <file> instead of the current ones.")),
987 OPT_END(),
990 FILE *fp;
991 int ret;
992 struct pattern_list pl = {0};
993 char *sparse_filename;
994 check_rules_opts.cone_mode = -1;
996 argc = parse_options(argc, argv, prefix,
997 builtin_sparse_checkout_check_rules_options,
998 builtin_sparse_checkout_check_rules_usage,
999 PARSE_OPT_KEEP_UNKNOWN_OPT);
1001 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
1002 check_rules_opts.cone_mode = 1;
1004 update_cone_mode(&check_rules_opts.cone_mode);
1005 pl.use_cone_patterns = core_sparse_checkout_cone;
1006 if (check_rules_opts.rules_file) {
1007 fp = xfopen(check_rules_opts.rules_file, "r");
1008 add_patterns_from_input(&pl, argc, argv, fp);
1009 fclose(fp);
1010 } else {
1011 sparse_filename = get_sparse_checkout_filename();
1012 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1013 NULL, 0))
1014 die(_("unable to load existing sparse-checkout patterns"));
1015 free(sparse_filename);
1018 ret = check_rules(&pl, check_rules_opts.null_termination);
1019 clear_pattern_list(&pl);
1020 return ret;
1023 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
1025 parse_opt_subcommand_fn *fn = NULL;
1026 struct option builtin_sparse_checkout_options[] = {
1027 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1028 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1029 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1030 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1031 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1032 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1033 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
1034 OPT_END(),
1037 argc = parse_options(argc, argv, prefix,
1038 builtin_sparse_checkout_options,
1039 builtin_sparse_checkout_usage, 0);
1041 git_config(git_default_config, NULL);
1043 prepare_repo_settings(the_repository);
1044 the_repository->settings.command_requires_full_index = 0;
1046 return fn(argc, argv, prefix);