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