]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/sparse-checkout.c
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / builtin / sparse-checkout.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 #include "pathspec.h"
6 #include "repository.h"
7 #include "run-command.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10 #include "cache.h"
11 #include "cache-tree.h"
12 #include "lockfile.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 #include "wt-status.h"
16 #include "quote.h"
17 #include "sparse-index.h"
18
19 static const char *empty_base = "";
20
21 static char const * const builtin_sparse_checkout_usage[] = {
22 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
23 NULL
24 };
25
26 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
27 {
28 int i;
29
30 for (i = 0; i < pl->nr; i++) {
31 struct path_pattern *p = pl->patterns[i];
32
33 if (p->flags & PATTERN_FLAG_NEGATIVE)
34 fprintf(fp, "!");
35
36 fprintf(fp, "%s", p->pattern);
37
38 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
39 fprintf(fp, "/");
40
41 fprintf(fp, "\n");
42 }
43 }
44
45 static char const * const builtin_sparse_checkout_list_usage[] = {
46 N_("git sparse-checkout list"),
47 NULL
48 };
49
50 static int sparse_checkout_list(int argc, const char **argv)
51 {
52 static struct option builtin_sparse_checkout_list_options[] = {
53 OPT_END(),
54 };
55 struct pattern_list pl;
56 char *sparse_filename;
57 int res;
58
59 if (!core_apply_sparse_checkout)
60 die(_("this worktree is not sparse"));
61
62 argc = parse_options(argc, argv, NULL,
63 builtin_sparse_checkout_list_options,
64 builtin_sparse_checkout_list_usage, 0);
65
66 memset(&pl, 0, sizeof(pl));
67
68 pl.use_cone_patterns = core_sparse_checkout_cone;
69
70 sparse_filename = get_sparse_checkout_filename();
71 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
72 free(sparse_filename);
73
74 if (res < 0) {
75 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
76 return 0;
77 }
78
79 if (pl.use_cone_patterns) {
80 int i;
81 struct pattern_entry *pe;
82 struct hashmap_iter iter;
83 struct string_list sl = STRING_LIST_INIT_DUP;
84
85 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
86 /* pe->pattern starts with "/", skip it */
87 string_list_insert(&sl, pe->pattern + 1);
88 }
89
90 string_list_sort(&sl);
91
92 for (i = 0; i < sl.nr; i++) {
93 quote_c_style(sl.items[i].string, NULL, stdout, 0);
94 printf("\n");
95 }
96
97 return 0;
98 }
99
100 write_patterns_to_file(stdout, &pl);
101 clear_pattern_list(&pl);
102
103 return 0;
104 }
105
106 static void clean_tracked_sparse_directories(struct repository *r)
107 {
108 int i, was_full = 0;
109 struct strbuf path = STRBUF_INIT;
110 size_t pathlen;
111 struct string_list_item *item;
112 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
113
114 /*
115 * If we are not using cone mode patterns, then we cannot
116 * delete directories outside of the sparse cone.
117 */
118 if (!r || !r->index || !r->worktree)
119 return;
120 if (init_sparse_checkout_patterns(r->index) ||
121 !r->index->sparse_checkout_patterns->use_cone_patterns)
122 return;
123
124 /*
125 * Use the sparse index as a data structure to assist finding
126 * directories that are safe to delete. This conversion to a
127 * sparse index will not delete directories that contain
128 * conflicted entries or submodules.
129 */
130 if (!r->index->sparse_index) {
131 /*
132 * If something, such as a merge conflict or other concern,
133 * prevents us from converting to a sparse index, then do
134 * not try deleting files.
135 */
136 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
137 return;
138 was_full = 1;
139 }
140
141 strbuf_addstr(&path, r->worktree);
142 strbuf_complete(&path, '/');
143 pathlen = path.len;
144
145 /*
146 * Collect directories that have gone out of scope but also
147 * exist on disk, so there is some work to be done. We need to
148 * store the entries in a list before exploring, since that might
149 * expand the sparse-index again.
150 */
151 for (i = 0; i < r->index->cache_nr; i++) {
152 struct cache_entry *ce = r->index->cache[i];
153
154 if (S_ISSPARSEDIR(ce->ce_mode) &&
155 repo_file_exists(r, ce->name))
156 string_list_append(&sparse_dirs, ce->name);
157 }
158
159 for_each_string_list_item(item, &sparse_dirs) {
160 struct dir_struct dir = DIR_INIT;
161 struct pathspec p = { 0 };
162 struct strvec s = STRVEC_INIT;
163
164 strbuf_setlen(&path, pathlen);
165 strbuf_addstr(&path, item->string);
166
167 dir.flags |= DIR_SHOW_IGNORED_TOO;
168
169 setup_standard_excludes(&dir);
170 strvec_push(&s, path.buf);
171
172 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
173 fill_directory(&dir, r->index, &p);
174
175 if (dir.nr) {
176 warning(_("directory '%s' contains untracked files,"
177 " but is not in the sparse-checkout cone"),
178 item->string);
179 } else if (remove_dir_recursively(&path, 0)) {
180 /*
181 * Removal is "best effort". If something blocks
182 * the deletion, then continue with a warning.
183 */
184 warning(_("failed to remove directory '%s'"),
185 item->string);
186 }
187
188 strvec_clear(&s);
189 clear_pathspec(&p);
190 dir_clear(&dir);
191 }
192
193 string_list_clear(&sparse_dirs, 0);
194 strbuf_release(&path);
195
196 if (was_full)
197 ensure_full_index(r->index);
198 }
199
200 static int update_working_directory(struct pattern_list *pl)
201 {
202 enum update_sparsity_result result;
203 struct unpack_trees_options o;
204 struct lock_file lock_file = LOCK_INIT;
205 struct repository *r = the_repository;
206
207 /* If no branch has been checked out, there are no updates to make. */
208 if (is_index_unborn(r->index))
209 return UPDATE_SPARSITY_SUCCESS;
210
211 r->index->sparse_checkout_patterns = pl;
212
213 memset(&o, 0, sizeof(o));
214 o.verbose_update = isatty(2);
215 o.update = 1;
216 o.head_idx = -1;
217 o.src_index = r->index;
218 o.dst_index = r->index;
219 o.skip_sparse_checkout = 0;
220 o.pl = pl;
221
222 setup_work_tree();
223
224 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
225
226 setup_unpack_trees_porcelain(&o, "sparse-checkout");
227 result = update_sparsity(&o);
228 clear_unpack_trees_porcelain(&o);
229
230 if (result == UPDATE_SPARSITY_WARNINGS)
231 /*
232 * We don't do any special handling of warnings from untracked
233 * files in the way or dirty entries that can't be removed.
234 */
235 result = UPDATE_SPARSITY_SUCCESS;
236 if (result == UPDATE_SPARSITY_SUCCESS)
237 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
238 else
239 rollback_lock_file(&lock_file);
240
241 clean_tracked_sparse_directories(r);
242
243 r->index->sparse_checkout_patterns = NULL;
244 return result;
245 }
246
247 static char *escaped_pattern(char *pattern)
248 {
249 char *p = pattern;
250 struct strbuf final = STRBUF_INIT;
251
252 while (*p) {
253 if (is_glob_special(*p))
254 strbuf_addch(&final, '\\');
255
256 strbuf_addch(&final, *p);
257 p++;
258 }
259
260 return strbuf_detach(&final, NULL);
261 }
262
263 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
264 {
265 int i;
266 struct pattern_entry *pe;
267 struct hashmap_iter iter;
268 struct string_list sl = STRING_LIST_INIT_DUP;
269 struct strbuf parent_pattern = STRBUF_INIT;
270
271 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
272 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
273 continue;
274
275 if (!hashmap_contains_parent(&pl->recursive_hashmap,
276 pe->pattern,
277 &parent_pattern))
278 string_list_insert(&sl, pe->pattern);
279 }
280
281 string_list_sort(&sl);
282 string_list_remove_duplicates(&sl, 0);
283
284 fprintf(fp, "/*\n!/*/\n");
285
286 for (i = 0; i < sl.nr; i++) {
287 char *pattern = escaped_pattern(sl.items[i].string);
288
289 if (strlen(pattern))
290 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
291 free(pattern);
292 }
293
294 string_list_clear(&sl, 0);
295
296 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
297 if (!hashmap_contains_parent(&pl->recursive_hashmap,
298 pe->pattern,
299 &parent_pattern))
300 string_list_insert(&sl, pe->pattern);
301 }
302
303 strbuf_release(&parent_pattern);
304
305 string_list_sort(&sl);
306 string_list_remove_duplicates(&sl, 0);
307
308 for (i = 0; i < sl.nr; i++) {
309 char *pattern = escaped_pattern(sl.items[i].string);
310 fprintf(fp, "%s/\n", pattern);
311 free(pattern);
312 }
313 }
314
315 static int write_patterns_and_update(struct pattern_list *pl)
316 {
317 char *sparse_filename;
318 FILE *fp;
319 int fd;
320 struct lock_file lk = LOCK_INIT;
321 int result;
322
323 sparse_filename = get_sparse_checkout_filename();
324
325 if (safe_create_leading_directories(sparse_filename))
326 die(_("failed to create directory for sparse-checkout file"));
327
328 fd = hold_lock_file_for_update(&lk, sparse_filename,
329 LOCK_DIE_ON_ERROR);
330
331 result = update_working_directory(pl);
332 if (result) {
333 rollback_lock_file(&lk);
334 free(sparse_filename);
335 clear_pattern_list(pl);
336 update_working_directory(NULL);
337 return result;
338 }
339
340 fp = xfdopen(fd, "w");
341
342 if (core_sparse_checkout_cone)
343 write_cone_to_file(fp, pl);
344 else
345 write_patterns_to_file(fp, pl);
346
347 fflush(fp);
348 commit_lock_file(&lk);
349
350 free(sparse_filename);
351 clear_pattern_list(pl);
352
353 return 0;
354 }
355
356 enum sparse_checkout_mode {
357 MODE_NO_PATTERNS = 0,
358 MODE_ALL_PATTERNS = 1,
359 MODE_CONE_PATTERNS = 2,
360 };
361
362 static int set_config(enum sparse_checkout_mode mode)
363 {
364 const char *config_path;
365
366 if (upgrade_repository_format(1) < 0)
367 die(_("unable to upgrade repository format to enable worktreeConfig"));
368 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
369 error(_("failed to set extensions.worktreeConfig setting"));
370 return 1;
371 }
372
373 config_path = git_path("config.worktree");
374 git_config_set_in_file_gently(config_path,
375 "core.sparseCheckout",
376 mode ? "true" : NULL);
377
378 git_config_set_in_file_gently(config_path,
379 "core.sparseCheckoutCone",
380 mode == MODE_CONE_PATTERNS ? "true" : NULL);
381
382 if (mode == MODE_NO_PATTERNS)
383 set_sparse_index_config(the_repository, 0);
384
385 return 0;
386 }
387
388 static int update_modes(int *cone_mode, int *sparse_index)
389 {
390 int mode, record_mode;
391
392 /* Determine if we need to record the mode; ensure sparse checkout on */
393 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
394
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;
398
399 /* Set cone/non-cone mode appropriately */
400 core_apply_sparse_checkout = 1;
401 if (*cone_mode == 1) {
402 mode = MODE_CONE_PATTERNS;
403 core_sparse_checkout_cone = 1;
404 } else {
405 mode = MODE_ALL_PATTERNS;
406 }
407 if (record_mode && set_config(mode))
408 return 1;
409
410 /* Set sparse-index/non-sparse-index mode if specified */
411 if (*sparse_index >= 0) {
412 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
413 die(_("failed to modify sparse-index config"));
414
415 /* force an index rewrite */
416 repo_read_index(the_repository);
417 the_repository->index->updated_workdir = 1;
418 }
419
420 return 0;
421 }
422
423 static char const * const builtin_sparse_checkout_init_usage[] = {
424 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
425 NULL
426 };
427
428 static struct sparse_checkout_init_opts {
429 int cone_mode;
430 int sparse_index;
431 } init_opts;
432
433 static int sparse_checkout_init(int argc, const char **argv)
434 {
435 struct pattern_list pl;
436 char *sparse_filename;
437 int res;
438 struct object_id oid;
439 struct strbuf pattern = STRBUF_INIT;
440
441 static struct option builtin_sparse_checkout_init_options[] = {
442 OPT_BOOL(0, "cone", &init_opts.cone_mode,
443 N_("initialize the sparse-checkout in cone mode")),
444 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
445 N_("toggle the use of a sparse index")),
446 OPT_END(),
447 };
448
449 repo_read_index(the_repository);
450
451 init_opts.cone_mode = -1;
452 init_opts.sparse_index = -1;
453
454 argc = parse_options(argc, argv, NULL,
455 builtin_sparse_checkout_init_options,
456 builtin_sparse_checkout_init_usage, 0);
457
458 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
459 return 1;
460
461 memset(&pl, 0, sizeof(pl));
462
463 sparse_filename = get_sparse_checkout_filename();
464 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
465
466 /* If we already have a sparse-checkout file, use it. */
467 if (res >= 0) {
468 free(sparse_filename);
469 return update_working_directory(NULL);
470 }
471
472 if (get_oid("HEAD", &oid)) {
473 FILE *fp;
474
475 /* assume we are in a fresh repo, but update the sparse-checkout file */
476 if (safe_create_leading_directories(sparse_filename))
477 die(_("unable to create leading directories of %s"),
478 sparse_filename);
479 fp = xfopen(sparse_filename, "w");
480 if (!fp)
481 die(_("failed to open '%s'"), sparse_filename);
482
483 free(sparse_filename);
484 fprintf(fp, "/*\n!/*/\n");
485 fclose(fp);
486 return 0;
487 }
488
489 strbuf_addstr(&pattern, "/*");
490 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
491 strbuf_addstr(&pattern, "!/*/");
492 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
493 pl.use_cone_patterns = init_opts.cone_mode;
494
495 return write_patterns_and_update(&pl);
496 }
497
498 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
499 {
500 struct pattern_entry *e = xmalloc(sizeof(*e));
501 e->patternlen = path->len;
502 e->pattern = strbuf_detach(path, NULL);
503 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
504
505 hashmap_add(&pl->recursive_hashmap, &e->ent);
506
507 while (e->patternlen) {
508 char *slash = strrchr(e->pattern, '/');
509 char *oldpattern = e->pattern;
510 size_t newlen;
511
512 if (!slash || slash == e->pattern)
513 break;
514
515 newlen = slash - e->pattern;
516 e = xmalloc(sizeof(struct pattern_entry));
517 e->patternlen = newlen;
518 e->pattern = xstrndup(oldpattern, newlen);
519 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
520
521 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
522 hashmap_add(&pl->parent_hashmap, &e->ent);
523 }
524 }
525
526 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
527 {
528 strbuf_trim(line);
529
530 strbuf_trim_trailing_dir_sep(line);
531
532 if (strbuf_normalize_path(line))
533 die(_("could not normalize path %s"), line->buf);
534
535 if (!line->len)
536 return;
537
538 if (line->buf[0] != '/')
539 strbuf_insertstr(line, 0, "/");
540
541 insert_recursive_pattern(pl, line);
542 }
543
544 static void add_patterns_from_input(struct pattern_list *pl,
545 int argc, const char **argv,
546 int use_stdin)
547 {
548 int i;
549 if (core_sparse_checkout_cone) {
550 struct strbuf line = STRBUF_INIT;
551
552 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
553 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
554 pl->use_cone_patterns = 1;
555
556 if (use_stdin) {
557 struct strbuf unquoted = STRBUF_INIT;
558 while (!strbuf_getline(&line, stdin)) {
559 if (line.buf[0] == '"') {
560 strbuf_reset(&unquoted);
561 if (unquote_c_style(&unquoted, line.buf, NULL))
562 die(_("unable to unquote C-style string '%s'"),
563 line.buf);
564
565 strbuf_swap(&unquoted, &line);
566 }
567
568 strbuf_to_cone_pattern(&line, pl);
569 }
570
571 strbuf_release(&unquoted);
572 } else {
573 for (i = 0; i < argc; i++) {
574 strbuf_setlen(&line, 0);
575 strbuf_addstr(&line, argv[i]);
576 strbuf_to_cone_pattern(&line, pl);
577 }
578 }
579 } else {
580 if (use_stdin) {
581 struct strbuf line = STRBUF_INIT;
582
583 while (!strbuf_getline(&line, stdin)) {
584 size_t len;
585 char *buf = strbuf_detach(&line, &len);
586 add_pattern(buf, empty_base, 0, pl, 0);
587 }
588 } else {
589 for (i = 0; i < argc; i++)
590 add_pattern(argv[i], empty_base, 0, pl, 0);
591 }
592 }
593 }
594
595 enum modify_type {
596 REPLACE,
597 ADD,
598 };
599
600 static void add_patterns_cone_mode(int argc, const char **argv,
601 struct pattern_list *pl,
602 int use_stdin)
603 {
604 struct strbuf buffer = STRBUF_INIT;
605 struct pattern_entry *pe;
606 struct hashmap_iter iter;
607 struct pattern_list existing;
608 char *sparse_filename = get_sparse_checkout_filename();
609
610 add_patterns_from_input(pl, argc, argv, use_stdin);
611
612 memset(&existing, 0, sizeof(existing));
613 existing.use_cone_patterns = core_sparse_checkout_cone;
614
615 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
616 &existing, NULL, 0))
617 die(_("unable to load existing sparse-checkout patterns"));
618 free(sparse_filename);
619
620 if (!existing.use_cone_patterns)
621 die(_("existing sparse-checkout patterns do not use cone mode"));
622
623 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
624 if (!hashmap_contains_parent(&pl->recursive_hashmap,
625 pe->pattern, &buffer) ||
626 !hashmap_contains_parent(&pl->parent_hashmap,
627 pe->pattern, &buffer)) {
628 strbuf_reset(&buffer);
629 strbuf_addstr(&buffer, pe->pattern);
630 insert_recursive_pattern(pl, &buffer);
631 }
632 }
633
634 clear_pattern_list(&existing);
635 strbuf_release(&buffer);
636 }
637
638 static void add_patterns_literal(int argc, const char **argv,
639 struct pattern_list *pl,
640 int use_stdin)
641 {
642 char *sparse_filename = get_sparse_checkout_filename();
643 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
644 pl, NULL, 0))
645 die(_("unable to load existing sparse-checkout patterns"));
646 free(sparse_filename);
647 add_patterns_from_input(pl, argc, argv, use_stdin);
648 }
649
650 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
651 enum modify_type m)
652 {
653 int result;
654 int changed_config = 0;
655 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
656
657 switch (m) {
658 case ADD:
659 if (core_sparse_checkout_cone)
660 add_patterns_cone_mode(argc, argv, pl, use_stdin);
661 else
662 add_patterns_literal(argc, argv, pl, use_stdin);
663 break;
664
665 case REPLACE:
666 add_patterns_from_input(pl, argc, argv, use_stdin);
667 break;
668 }
669
670 if (!core_apply_sparse_checkout) {
671 set_config(MODE_ALL_PATTERNS);
672 core_apply_sparse_checkout = 1;
673 changed_config = 1;
674 }
675
676 result = write_patterns_and_update(pl);
677
678 if (result && changed_config)
679 set_config(MODE_NO_PATTERNS);
680
681 clear_pattern_list(pl);
682 free(pl);
683 return result;
684 }
685
686 static char const * const builtin_sparse_checkout_add_usage[] = {
687 N_("git sparse-checkout add (--stdin | <patterns>)"),
688 NULL
689 };
690
691 static struct sparse_checkout_add_opts {
692 int use_stdin;
693 } add_opts;
694
695 static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
696 {
697 static struct option builtin_sparse_checkout_add_options[] = {
698 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
699 N_("read patterns from standard in")),
700 OPT_END(),
701 };
702
703 if (!core_apply_sparse_checkout)
704 die(_("no sparse-checkout to add to"));
705
706 repo_read_index(the_repository);
707
708 argc = parse_options(argc, argv, prefix,
709 builtin_sparse_checkout_add_options,
710 builtin_sparse_checkout_add_usage,
711 PARSE_OPT_KEEP_UNKNOWN);
712
713 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
714 }
715
716 static char const * const builtin_sparse_checkout_set_usage[] = {
717 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | <patterns>)"),
718 NULL
719 };
720
721 static struct sparse_checkout_set_opts {
722 int cone_mode;
723 int sparse_index;
724 int use_stdin;
725 } set_opts;
726
727 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
728 {
729 int default_patterns_nr = 2;
730 const char *default_patterns[] = {"/*", "!/*/", NULL};
731
732 static struct option builtin_sparse_checkout_set_options[] = {
733 OPT_BOOL(0, "cone", &set_opts.cone_mode,
734 N_("initialize the sparse-checkout in cone mode")),
735 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
736 N_("toggle the use of a sparse index")),
737 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
738 N_("read patterns from standard in"),
739 PARSE_OPT_NONEG),
740 OPT_END(),
741 };
742
743 repo_read_index(the_repository);
744
745 set_opts.cone_mode = -1;
746 set_opts.sparse_index = -1;
747
748 argc = parse_options(argc, argv, prefix,
749 builtin_sparse_checkout_set_options,
750 builtin_sparse_checkout_set_usage,
751 PARSE_OPT_KEEP_UNKNOWN);
752
753 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
754 return 1;
755
756 /*
757 * Cone mode automatically specifies the toplevel directory. For
758 * non-cone mode, if nothing is specified, manually select just the
759 * top-level directory (much as 'init' would do).
760 */
761 if (!core_sparse_checkout_cone && argc == 0) {
762 argv = default_patterns;
763 argc = default_patterns_nr;
764 }
765
766 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
767 }
768
769 static char const * const builtin_sparse_checkout_reapply_usage[] = {
770 N_("git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]"),
771 NULL
772 };
773
774 static struct sparse_checkout_reapply_opts {
775 int cone_mode;
776 int sparse_index;
777 } reapply_opts;
778
779 static int sparse_checkout_reapply(int argc, const char **argv)
780 {
781 static struct option builtin_sparse_checkout_reapply_options[] = {
782 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
783 N_("initialize the sparse-checkout in cone mode")),
784 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
785 N_("toggle the use of a sparse index")),
786 OPT_END(),
787 };
788
789 if (!core_apply_sparse_checkout)
790 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
791
792 argc = parse_options(argc, argv, NULL,
793 builtin_sparse_checkout_reapply_options,
794 builtin_sparse_checkout_reapply_usage, 0);
795
796 repo_read_index(the_repository);
797
798 reapply_opts.cone_mode = -1;
799 reapply_opts.sparse_index = -1;
800
801 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
802 return 1;
803
804 return update_working_directory(NULL);
805 }
806
807 static char const * const builtin_sparse_checkout_disable_usage[] = {
808 N_("git sparse-checkout disable"),
809 NULL
810 };
811
812 static int sparse_checkout_disable(int argc, const char **argv)
813 {
814 static struct option builtin_sparse_checkout_disable_options[] = {
815 OPT_END(),
816 };
817 struct pattern_list pl;
818 struct strbuf match_all = STRBUF_INIT;
819
820 /*
821 * We do not exit early if !core_apply_sparse_checkout; due to the
822 * ability for users to manually muck things up between
823 * direct editing of .git/info/sparse-checkout
824 * running read-tree -m u HEAD or update-index --skip-worktree
825 * direct toggling of config options
826 * users might end up with an index with SKIP_WORKTREE bit set on
827 * some files and not know how to undo it. So, here we just
828 * forcibly return to a dense checkout regardless of initial state.
829 */
830
831 argc = parse_options(argc, argv, NULL,
832 builtin_sparse_checkout_disable_options,
833 builtin_sparse_checkout_disable_usage, 0);
834
835 repo_read_index(the_repository);
836
837 memset(&pl, 0, sizeof(pl));
838 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
839 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
840 pl.use_cone_patterns = 0;
841 core_apply_sparse_checkout = 1;
842
843 strbuf_addstr(&match_all, "/*");
844 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
845
846 prepare_repo_settings(the_repository);
847 the_repository->settings.sparse_index = 0;
848
849 if (update_working_directory(&pl))
850 die(_("error while refreshing working directory"));
851
852 clear_pattern_list(&pl);
853 return set_config(MODE_NO_PATTERNS);
854 }
855
856 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
857 {
858 static struct option builtin_sparse_checkout_options[] = {
859 OPT_END(),
860 };
861
862 if (argc == 2 && !strcmp(argv[1], "-h"))
863 usage_with_options(builtin_sparse_checkout_usage,
864 builtin_sparse_checkout_options);
865
866 argc = parse_options(argc, argv, prefix,
867 builtin_sparse_checkout_options,
868 builtin_sparse_checkout_usage,
869 PARSE_OPT_STOP_AT_NON_OPTION);
870
871 git_config(git_default_config, NULL);
872
873 if (argc > 0) {
874 if (!strcmp(argv[0], "list"))
875 return sparse_checkout_list(argc, argv);
876 if (!strcmp(argv[0], "init"))
877 return sparse_checkout_init(argc, argv);
878 if (!strcmp(argv[0], "set"))
879 return sparse_checkout_set(argc, argv, prefix);
880 if (!strcmp(argv[0], "add"))
881 return sparse_checkout_add(argc, argv, prefix);
882 if (!strcmp(argv[0], "reapply"))
883 return sparse_checkout_reapply(argc, argv);
884 if (!strcmp(argv[0], "disable"))
885 return sparse_checkout_disable(argc, argv);
886 }
887
888 usage_with_options(builtin_sparse_checkout_usage,
889 builtin_sparse_checkout_options);
890 }