t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / builtin / sparse-checkout.c
blob49aedc1de81a17b8b491cded7fa71b384e0e8be9
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.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 "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 string_list_clear(&sl, 0);
100 } else {
101 write_patterns_to_file(stdout, &pl);
104 clear_pattern_list(&pl);
106 return 0;
109 static void clean_tracked_sparse_directories(struct repository *r)
111 int i, was_full = 0;
112 struct strbuf path = STRBUF_INIT;
113 size_t pathlen;
114 struct string_list_item *item;
115 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
118 * If we are not using cone mode patterns, then we cannot
119 * delete directories outside of the sparse cone.
121 if (!r || !r->index || !r->worktree)
122 return;
123 if (init_sparse_checkout_patterns(r->index) ||
124 !r->index->sparse_checkout_patterns->use_cone_patterns)
125 return;
128 * Use the sparse index as a data structure to assist finding
129 * directories that are safe to delete. This conversion to a
130 * sparse index will not delete directories that contain
131 * conflicted entries or submodules.
133 if (r->index->sparse_index == INDEX_EXPANDED) {
135 * If something, such as a merge conflict or other concern,
136 * prevents us from converting to a sparse index, then do
137 * not try deleting files.
139 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
140 return;
141 was_full = 1;
144 strbuf_addstr(&path, r->worktree);
145 strbuf_complete(&path, '/');
146 pathlen = path.len;
149 * Collect directories that have gone out of scope but also
150 * exist on disk, so there is some work to be done. We need to
151 * store the entries in a list before exploring, since that might
152 * expand the sparse-index again.
154 for (i = 0; i < r->index->cache_nr; i++) {
155 struct cache_entry *ce = r->index->cache[i];
157 if (S_ISSPARSEDIR(ce->ce_mode) &&
158 repo_file_exists(r, ce->name))
159 string_list_append(&sparse_dirs, ce->name);
162 for_each_string_list_item(item, &sparse_dirs) {
163 struct dir_struct dir = DIR_INIT;
164 struct pathspec p = { 0 };
165 struct strvec s = STRVEC_INIT;
167 strbuf_setlen(&path, pathlen);
168 strbuf_addstr(&path, item->string);
170 dir.flags |= DIR_SHOW_IGNORED_TOO;
172 setup_standard_excludes(&dir);
173 strvec_push(&s, path.buf);
175 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
176 fill_directory(&dir, r->index, &p);
178 if (dir.nr) {
179 warning(_("directory '%s' contains untracked files,"
180 " but is not in the sparse-checkout cone"),
181 item->string);
182 } else if (remove_dir_recursively(&path, 0)) {
184 * Removal is "best effort". If something blocks
185 * the deletion, then continue with a warning.
187 warning(_("failed to remove directory '%s'"),
188 item->string);
191 strvec_clear(&s);
192 clear_pathspec(&p);
193 dir_clear(&dir);
196 string_list_clear(&sparse_dirs, 0);
197 strbuf_release(&path);
199 if (was_full)
200 ensure_full_index(r->index);
203 static int update_working_directory(struct pattern_list *pl)
205 enum update_sparsity_result result;
206 struct unpack_trees_options o;
207 struct lock_file lock_file = LOCK_INIT;
208 struct repository *r = the_repository;
209 struct pattern_list *old_pl;
211 /* If no branch has been checked out, there are no updates to make. */
212 if (is_index_unborn(r->index))
213 return UPDATE_SPARSITY_SUCCESS;
215 old_pl = r->index->sparse_checkout_patterns;
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 if (r->index->sparse_checkout_patterns != pl) {
248 clear_pattern_list(r->index->sparse_checkout_patterns);
249 FREE_AND_NULL(r->index->sparse_checkout_patterns);
251 r->index->sparse_checkout_patterns = old_pl;
253 return result;
256 static char *escaped_pattern(char *pattern)
258 char *p = pattern;
259 struct strbuf final = STRBUF_INIT;
261 while (*p) {
262 if (is_glob_special(*p))
263 strbuf_addch(&final, '\\');
265 strbuf_addch(&final, *p);
266 p++;
269 return strbuf_detach(&final, NULL);
272 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
274 int i;
275 struct pattern_entry *pe;
276 struct hashmap_iter iter;
277 struct string_list sl = STRING_LIST_INIT_DUP;
278 struct strbuf parent_pattern = STRBUF_INIT;
280 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
281 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
282 continue;
284 if (!hashmap_contains_parent(&pl->recursive_hashmap,
285 pe->pattern,
286 &parent_pattern))
287 string_list_insert(&sl, pe->pattern);
290 string_list_sort(&sl);
291 string_list_remove_duplicates(&sl, 0);
293 fprintf(fp, "/*\n!/*/\n");
295 for (i = 0; i < sl.nr; i++) {
296 char *pattern = escaped_pattern(sl.items[i].string);
298 if (strlen(pattern))
299 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
300 free(pattern);
303 string_list_clear(&sl, 0);
305 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
306 if (!hashmap_contains_parent(&pl->recursive_hashmap,
307 pe->pattern,
308 &parent_pattern))
309 string_list_insert(&sl, pe->pattern);
312 strbuf_release(&parent_pattern);
314 string_list_sort(&sl);
315 string_list_remove_duplicates(&sl, 0);
317 for (i = 0; i < sl.nr; i++) {
318 char *pattern = escaped_pattern(sl.items[i].string);
319 fprintf(fp, "%s/\n", pattern);
320 free(pattern);
323 string_list_clear(&sl, 0);
326 static int write_patterns_and_update(struct pattern_list *pl)
328 char *sparse_filename;
329 FILE *fp;
330 struct lock_file lk = LOCK_INIT;
331 int result;
333 sparse_filename = get_sparse_checkout_filename();
335 if (safe_create_leading_directories(sparse_filename))
336 die(_("failed to create directory for sparse-checkout file"));
338 hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR);
340 result = update_working_directory(pl);
341 if (result) {
342 rollback_lock_file(&lk);
343 update_working_directory(NULL);
344 goto out;
347 fp = fdopen_lock_file(&lk, "w");
348 if (!fp)
349 die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk));
351 if (core_sparse_checkout_cone)
352 write_cone_to_file(fp, pl);
353 else
354 write_patterns_to_file(fp, pl);
356 if (commit_lock_file(&lk))
357 die_errno(_("unable to write %s"), sparse_filename);
359 out:
360 clear_pattern_list(pl);
361 free(sparse_filename);
362 return result;
365 enum sparse_checkout_mode {
366 MODE_NO_PATTERNS = 0,
367 MODE_ALL_PATTERNS = 1,
368 MODE_CONE_PATTERNS = 2,
371 static int set_config(enum sparse_checkout_mode mode)
373 /* Update to use worktree config, if not already. */
374 if (init_worktree_config(the_repository)) {
375 error(_("failed to initialize worktree config"));
376 return 1;
379 if (repo_config_set_worktree_gently(the_repository,
380 "core.sparseCheckout",
381 mode ? "true" : "false") ||
382 repo_config_set_worktree_gently(the_repository,
383 "core.sparseCheckoutCone",
384 mode == MODE_CONE_PATTERNS ?
385 "true" : "false"))
386 return 1;
388 if (mode == MODE_NO_PATTERNS)
389 return set_sparse_index_config(the_repository, 0);
391 return 0;
394 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
395 /* If not specified, use previous definition of cone mode */
396 if (*cone_mode == -1 && core_apply_sparse_checkout)
397 *cone_mode = core_sparse_checkout_cone;
399 /* Set cone/non-cone mode appropriately */
400 core_apply_sparse_checkout = 1;
401 if (*cone_mode == 1 || *cone_mode == -1) {
402 core_sparse_checkout_cone = 1;
403 return MODE_CONE_PATTERNS;
405 core_sparse_checkout_cone = 0;
406 return MODE_ALL_PATTERNS;
409 static int update_modes(int *cone_mode, int *sparse_index)
411 int mode, record_mode;
413 /* Determine if we need to record the mode; ensure sparse checkout on */
414 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
416 mode = update_cone_mode(cone_mode);
417 if (record_mode && set_config(mode))
418 return 1;
420 /* Set sparse-index/non-sparse-index mode if specified */
421 if (*sparse_index >= 0) {
422 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
423 die(_("failed to modify sparse-index config"));
425 /* force an index rewrite */
426 repo_read_index(the_repository);
427 the_repository->index->updated_workdir = 1;
429 if (!*sparse_index)
430 ensure_full_index(the_repository->index);
433 return 0;
436 static char const * const builtin_sparse_checkout_init_usage[] = {
437 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
438 NULL
441 static struct sparse_checkout_init_opts {
442 int cone_mode;
443 int sparse_index;
444 } init_opts;
446 static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
448 struct pattern_list pl;
449 char *sparse_filename;
450 int res;
451 struct object_id oid;
453 static struct option builtin_sparse_checkout_init_options[] = {
454 OPT_BOOL(0, "cone", &init_opts.cone_mode,
455 N_("initialize the sparse-checkout in cone mode")),
456 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
457 N_("toggle the use of a sparse index")),
458 OPT_END(),
461 setup_work_tree();
462 repo_read_index(the_repository);
464 init_opts.cone_mode = -1;
465 init_opts.sparse_index = -1;
467 argc = parse_options(argc, argv, prefix,
468 builtin_sparse_checkout_init_options,
469 builtin_sparse_checkout_init_usage, 0);
471 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
472 return 1;
474 memset(&pl, 0, sizeof(pl));
476 sparse_filename = get_sparse_checkout_filename();
477 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
479 /* If we already have a sparse-checkout file, use it. */
480 if (res >= 0) {
481 free(sparse_filename);
482 clear_pattern_list(&pl);
483 return update_working_directory(NULL);
486 if (repo_get_oid(the_repository, "HEAD", &oid)) {
487 FILE *fp;
489 /* assume we are in a fresh repo, but update the sparse-checkout file */
490 if (safe_create_leading_directories(sparse_filename))
491 die(_("unable to create leading directories of %s"),
492 sparse_filename);
493 fp = xfopen(sparse_filename, "w");
494 if (!fp)
495 die(_("failed to open '%s'"), sparse_filename);
497 free(sparse_filename);
498 fprintf(fp, "/*\n!/*/\n");
499 fclose(fp);
500 return 0;
503 free(sparse_filename);
505 add_pattern("/*", empty_base, 0, &pl, 0);
506 add_pattern("!/*/", empty_base, 0, &pl, 0);
507 pl.use_cone_patterns = init_opts.cone_mode;
509 return write_patterns_and_update(&pl);
512 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
514 struct pattern_entry *e = xmalloc(sizeof(*e));
515 e->patternlen = path->len;
516 e->pattern = strbuf_detach(path, NULL);
517 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
519 hashmap_add(&pl->recursive_hashmap, &e->ent);
521 while (e->patternlen) {
522 char *slash = strrchr(e->pattern, '/');
523 char *oldpattern = e->pattern;
524 size_t newlen;
525 struct pattern_entry *dup;
527 if (!slash || slash == e->pattern)
528 break;
530 newlen = slash - e->pattern;
531 e = xmalloc(sizeof(struct pattern_entry));
532 e->patternlen = newlen;
533 e->pattern = xstrndup(oldpattern, newlen);
534 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
536 dup = hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL);
537 if (!dup) {
538 hashmap_add(&pl->parent_hashmap, &e->ent);
539 } else {
540 free(e->pattern);
541 free(e);
542 e = dup;
547 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
549 strbuf_trim(line);
551 strbuf_trim_trailing_dir_sep(line);
553 if (strbuf_normalize_path(line))
554 die(_("could not normalize path %s"), line->buf);
556 if (!line->len)
557 return;
559 if (line->buf[0] != '/')
560 strbuf_insertstr(line, 0, "/");
562 insert_recursive_pattern(pl, line);
565 static void add_patterns_from_input(struct pattern_list *pl,
566 int argc, const char **argv,
567 FILE *file)
569 int i;
570 if (core_sparse_checkout_cone) {
571 struct strbuf line = STRBUF_INIT;
573 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
574 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
575 pl->use_cone_patterns = 1;
577 if (file) {
578 struct strbuf unquoted = STRBUF_INIT;
579 while (!strbuf_getline(&line, file)) {
580 if (line.buf[0] == '"') {
581 strbuf_reset(&unquoted);
582 if (unquote_c_style(&unquoted, line.buf, NULL))
583 die(_("unable to unquote C-style string '%s'"),
584 line.buf);
586 strbuf_swap(&unquoted, &line);
589 strbuf_to_cone_pattern(&line, pl);
592 strbuf_release(&unquoted);
593 } else {
594 for (i = 0; i < argc; i++) {
595 strbuf_setlen(&line, 0);
596 strbuf_addstr(&line, argv[i]);
597 strbuf_to_cone_pattern(&line, pl);
600 strbuf_release(&line);
601 } else {
602 if (file) {
603 struct strbuf line = STRBUF_INIT;
605 while (!strbuf_getline(&line, file))
606 add_pattern(line.buf, empty_base, 0, pl, 0);
608 strbuf_release(&line);
609 } else {
610 for (i = 0; i < argc; i++)
611 add_pattern(argv[i], empty_base, 0, pl, 0);
616 enum modify_type {
617 REPLACE,
618 ADD,
621 static void add_patterns_cone_mode(int argc, const char **argv,
622 struct pattern_list *pl,
623 int use_stdin)
625 struct strbuf buffer = STRBUF_INIT;
626 struct pattern_entry *pe;
627 struct hashmap_iter iter;
628 struct pattern_list existing;
629 char *sparse_filename = get_sparse_checkout_filename();
631 add_patterns_from_input(pl, argc, argv,
632 use_stdin ? stdin : NULL);
634 memset(&existing, 0, sizeof(existing));
635 existing.use_cone_patterns = core_sparse_checkout_cone;
637 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
638 &existing, NULL, 0))
639 die(_("unable to load existing sparse-checkout patterns"));
640 free(sparse_filename);
642 if (!existing.use_cone_patterns)
643 die(_("existing sparse-checkout patterns do not use cone mode"));
645 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
646 if (!hashmap_contains_parent(&pl->recursive_hashmap,
647 pe->pattern, &buffer) ||
648 !hashmap_contains_parent(&pl->parent_hashmap,
649 pe->pattern, &buffer)) {
650 strbuf_reset(&buffer);
651 strbuf_addstr(&buffer, pe->pattern);
652 insert_recursive_pattern(pl, &buffer);
656 clear_pattern_list(&existing);
657 strbuf_release(&buffer);
660 static void add_patterns_literal(int argc, const char **argv,
661 struct pattern_list *pl,
662 int use_stdin)
664 char *sparse_filename = get_sparse_checkout_filename();
665 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
666 pl, NULL, 0))
667 die(_("unable to load existing sparse-checkout patterns"));
668 free(sparse_filename);
669 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
672 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
673 enum modify_type m)
675 int result;
676 int changed_config = 0;
677 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
679 switch (m) {
680 case ADD:
681 if (core_sparse_checkout_cone)
682 add_patterns_cone_mode(argc, argv, pl, use_stdin);
683 else
684 add_patterns_literal(argc, argv, pl, use_stdin);
685 break;
687 case REPLACE:
688 add_patterns_from_input(pl, argc, argv,
689 use_stdin ? stdin : NULL);
690 break;
693 if (!core_apply_sparse_checkout) {
694 set_config(MODE_ALL_PATTERNS);
695 core_apply_sparse_checkout = 1;
696 changed_config = 1;
699 result = write_patterns_and_update(pl);
701 if (result && changed_config)
702 set_config(MODE_NO_PATTERNS);
704 clear_pattern_list(pl);
705 free(pl);
706 return result;
709 static void sanitize_paths(int argc, const char **argv,
710 const char *prefix, int skip_checks)
712 int i;
714 if (!argc)
715 return;
717 if (prefix && *prefix && core_sparse_checkout_cone) {
719 * The args are not pathspecs, so unfortunately we
720 * cannot imitate how cmd_add() uses parse_pathspec().
722 int prefix_len = strlen(prefix);
724 for (i = 0; i < argc; i++)
725 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
728 if (skip_checks)
729 return;
731 if (prefix && *prefix && !core_sparse_checkout_cone)
732 die(_("please run from the toplevel directory in non-cone mode"));
734 if (core_sparse_checkout_cone) {
735 for (i = 0; i < argc; i++) {
736 if (argv[i][0] == '/')
737 die(_("specify directories rather than patterns (no leading slash)"));
738 if (argv[i][0] == '!')
739 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
740 if (strpbrk(argv[i], "*?[]"))
741 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
745 for (i = 0; i < argc; i++) {
746 struct cache_entry *ce;
747 struct index_state *index = the_repository->index;
748 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
750 if (pos < 0)
751 continue;
752 ce = index->cache[pos];
753 if (S_ISSPARSEDIR(ce->ce_mode))
754 continue;
756 if (core_sparse_checkout_cone)
757 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
758 else
759 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]);
763 static char const * const builtin_sparse_checkout_add_usage[] = {
764 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
765 NULL
768 static struct sparse_checkout_add_opts {
769 int skip_checks;
770 int use_stdin;
771 } add_opts;
773 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
775 static struct option builtin_sparse_checkout_add_options[] = {
776 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
777 N_("skip some sanity checks on the given paths that might give false positives"),
778 PARSE_OPT_NONEG),
779 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
780 N_("read patterns from standard in")),
781 OPT_END(),
784 setup_work_tree();
785 if (!core_apply_sparse_checkout)
786 die(_("no sparse-checkout to add to"));
788 repo_read_index(the_repository);
790 argc = parse_options(argc, argv, prefix,
791 builtin_sparse_checkout_add_options,
792 builtin_sparse_checkout_add_usage, 0);
794 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
796 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
799 static char const * const builtin_sparse_checkout_set_usage[] = {
800 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
801 NULL
804 static struct sparse_checkout_set_opts {
805 int cone_mode;
806 int sparse_index;
807 int skip_checks;
808 int use_stdin;
809 } set_opts;
811 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
813 int default_patterns_nr = 2;
814 const char *default_patterns[] = {"/*", "!/*/", NULL};
816 static struct option builtin_sparse_checkout_set_options[] = {
817 OPT_BOOL(0, "cone", &set_opts.cone_mode,
818 N_("initialize the sparse-checkout in cone mode")),
819 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
820 N_("toggle the use of a sparse index")),
821 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
822 N_("skip some sanity checks on the given paths that might give false positives"),
823 PARSE_OPT_NONEG),
824 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
825 N_("read patterns from standard in"),
826 PARSE_OPT_NONEG),
827 OPT_END(),
830 setup_work_tree();
831 repo_read_index(the_repository);
833 set_opts.cone_mode = -1;
834 set_opts.sparse_index = -1;
836 argc = parse_options(argc, argv, prefix,
837 builtin_sparse_checkout_set_options,
838 builtin_sparse_checkout_set_usage, 0);
840 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
841 return 1;
844 * Cone mode automatically specifies the toplevel directory. For
845 * non-cone mode, if nothing is specified, manually select just the
846 * top-level directory (much as 'init' would do).
848 if (!core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
849 argv = default_patterns;
850 argc = default_patterns_nr;
851 } else {
852 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
855 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
858 static char const * const builtin_sparse_checkout_reapply_usage[] = {
859 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
860 NULL
863 static struct sparse_checkout_reapply_opts {
864 int cone_mode;
865 int sparse_index;
866 } reapply_opts;
868 static int sparse_checkout_reapply(int argc, const char **argv,
869 const char *prefix)
871 static struct option builtin_sparse_checkout_reapply_options[] = {
872 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
873 N_("initialize the sparse-checkout in cone mode")),
874 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
875 N_("toggle the use of a sparse index")),
876 OPT_END(),
879 setup_work_tree();
880 if (!core_apply_sparse_checkout)
881 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
883 reapply_opts.cone_mode = -1;
884 reapply_opts.sparse_index = -1;
886 argc = parse_options(argc, argv, prefix,
887 builtin_sparse_checkout_reapply_options,
888 builtin_sparse_checkout_reapply_usage, 0);
890 repo_read_index(the_repository);
892 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
893 return 1;
895 return update_working_directory(NULL);
898 static char const * const builtin_sparse_checkout_disable_usage[] = {
899 "git sparse-checkout disable",
900 NULL
903 static int sparse_checkout_disable(int argc, const char **argv,
904 const char *prefix)
906 static struct option builtin_sparse_checkout_disable_options[] = {
907 OPT_END(),
909 struct pattern_list pl;
912 * We do not exit early if !core_apply_sparse_checkout; due to the
913 * ability for users to manually muck things up between
914 * direct editing of .git/info/sparse-checkout
915 * running read-tree -m u HEAD or update-index --skip-worktree
916 * direct toggling of config options
917 * users might end up with an index with SKIP_WORKTREE bit set on
918 * some files and not know how to undo it. So, here we just
919 * forcibly return to a dense checkout regardless of initial state.
922 setup_work_tree();
923 argc = parse_options(argc, argv, prefix,
924 builtin_sparse_checkout_disable_options,
925 builtin_sparse_checkout_disable_usage, 0);
928 * Disable the advice message for expanding a sparse index, as we
929 * are expecting to do that when disabling sparse-checkout.
931 give_advice_on_expansion = 0;
932 repo_read_index(the_repository);
934 memset(&pl, 0, sizeof(pl));
935 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
936 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
937 pl.use_cone_patterns = 0;
938 core_apply_sparse_checkout = 1;
940 add_pattern("/*", empty_base, 0, &pl, 0);
942 prepare_repo_settings(the_repository);
943 the_repository->settings.sparse_index = 0;
945 if (update_working_directory(&pl))
946 die(_("error while refreshing working directory"));
948 clear_pattern_list(&pl);
949 return set_config(MODE_NO_PATTERNS);
952 static char const * const builtin_sparse_checkout_check_rules_usage[] = {
953 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
954 "[--[no-]cone] [--rules-file <file>]"),
955 NULL
958 static struct sparse_checkout_check_rules_opts {
959 int cone_mode;
960 int null_termination;
961 char *rules_file;
962 } check_rules_opts;
964 static int check_rules(struct pattern_list *pl, int null_terminated) {
965 struct strbuf line = STRBUF_INIT;
966 struct strbuf unquoted = STRBUF_INIT;
967 char *path;
968 int line_terminator = null_terminated ? 0 : '\n';
969 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
970 : strbuf_getline;
971 the_repository->index->sparse_checkout_patterns = pl;
972 while (!getline_fn(&line, stdin)) {
973 path = line.buf;
974 if (!null_terminated && line.buf[0] == '"') {
975 strbuf_reset(&unquoted);
976 if (unquote_c_style(&unquoted, line.buf, NULL))
977 die(_("unable to unquote C-style string '%s'"),
978 line.buf);
980 path = unquoted.buf;
983 if (path_in_sparse_checkout(path, the_repository->index))
984 write_name_quoted(path, stdout, line_terminator);
986 strbuf_release(&line);
987 strbuf_release(&unquoted);
989 return 0;
992 static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
994 static struct option builtin_sparse_checkout_check_rules_options[] = {
995 OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
996 N_("terminate input and output files by a NUL character")),
997 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
998 N_("when used with --rules-file interpret patterns as cone mode patterns")),
999 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
1000 N_("use patterns in <file> instead of the current ones.")),
1001 OPT_END(),
1004 FILE *fp;
1005 int ret;
1006 struct pattern_list pl = {0};
1007 char *sparse_filename;
1008 check_rules_opts.cone_mode = -1;
1010 argc = parse_options(argc, argv, prefix,
1011 builtin_sparse_checkout_check_rules_options,
1012 builtin_sparse_checkout_check_rules_usage, 0);
1014 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
1015 check_rules_opts.cone_mode = 1;
1017 update_cone_mode(&check_rules_opts.cone_mode);
1018 pl.use_cone_patterns = core_sparse_checkout_cone;
1019 if (check_rules_opts.rules_file) {
1020 fp = xfopen(check_rules_opts.rules_file, "r");
1021 add_patterns_from_input(&pl, argc, argv, fp);
1022 fclose(fp);
1023 } else {
1024 sparse_filename = get_sparse_checkout_filename();
1025 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1026 NULL, 0))
1027 die(_("unable to load existing sparse-checkout patterns"));
1028 free(sparse_filename);
1031 ret = check_rules(&pl, check_rules_opts.null_termination);
1032 clear_pattern_list(&pl);
1033 free(check_rules_opts.rules_file);
1034 return ret;
1037 int cmd_sparse_checkout(int argc,
1038 const char **argv,
1039 const char *prefix,
1040 struct repository *repo UNUSED)
1042 parse_opt_subcommand_fn *fn = NULL;
1043 struct option builtin_sparse_checkout_options[] = {
1044 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1045 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1046 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1047 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1048 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1049 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1050 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
1051 OPT_END(),
1054 argc = parse_options(argc, argv, prefix,
1055 builtin_sparse_checkout_options,
1056 builtin_sparse_checkout_usage, 0);
1058 git_config(git_default_config, NULL);
1060 prepare_repo_settings(the_repository);
1061 the_repository->settings.command_requires_full_index = 0;
1063 return fn(argc, argv, prefix);