]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/sparse-checkout.c
sparse-checkout: fix OOM error with mixed patterns
[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
75d3bee1
JK
59 argc = parse_options(argc, argv, NULL,
60 builtin_sparse_checkout_list_options,
61 builtin_sparse_checkout_list_usage, 0);
62
94c0956b
DS
63 memset(&pl, 0, sizeof(pl));
64
de11951b
DS
65 pl.use_cone_patterns = core_sparse_checkout_cone;
66
94c0956b 67 sparse_filename = get_sparse_checkout_filename();
1679d60b 68 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
94c0956b
DS
69 free(sparse_filename);
70
71 if (res < 0) {
72 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
73 return 0;
74 }
75
de11951b
DS
76 if (pl.use_cone_patterns) {
77 int i;
78 struct pattern_entry *pe;
79 struct hashmap_iter iter;
80 struct string_list sl = STRING_LIST_INIT_DUP;
81
82 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
83 /* pe->pattern starts with "/", skip it */
84 string_list_insert(&sl, pe->pattern + 1);
85 }
86
87 string_list_sort(&sl);
88
e55682ea
DS
89 for (i = 0; i < sl.nr; i++) {
90 quote_c_style(sl.items[i].string, NULL, stdout, 0);
91 printf("\n");
92 }
de11951b
DS
93
94 return 0;
95 }
96
94c0956b
DS
97 write_patterns_to_file(stdout, &pl);
98 clear_pattern_list(&pl);
99
100 return 0;
101}
102
55dfcf95
DS
103static void clean_tracked_sparse_directories(struct repository *r)
104{
105 int i, was_full = 0;
106 struct strbuf path = STRBUF_INIT;
107 size_t pathlen;
108 struct string_list_item *item;
109 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
110
111 /*
112 * If we are not using cone mode patterns, then we cannot
113 * delete directories outside of the sparse cone.
114 */
115 if (!r || !r->index || !r->worktree)
116 return;
117 if (init_sparse_checkout_patterns(r->index) ||
118 !r->index->sparse_checkout_patterns->use_cone_patterns)
119 return;
120
121 /*
122 * Use the sparse index as a data structure to assist finding
123 * directories that are safe to delete. This conversion to a
124 * sparse index will not delete directories that contain
125 * conflicted entries or submodules.
126 */
127 if (!r->index->sparse_index) {
128 /*
129 * If something, such as a merge conflict or other concern,
130 * prevents us from converting to a sparse index, then do
131 * not try deleting files.
132 */
133 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
134 return;
135 was_full = 1;
136 }
137
138 strbuf_addstr(&path, r->worktree);
139 strbuf_complete(&path, '/');
140 pathlen = path.len;
141
142 /*
143 * Collect directories that have gone out of scope but also
144 * exist on disk, so there is some work to be done. We need to
145 * store the entries in a list before exploring, since that might
146 * expand the sparse-index again.
147 */
148 for (i = 0; i < r->index->cache_nr; i++) {
149 struct cache_entry *ce = r->index->cache[i];
150
151 if (S_ISSPARSEDIR(ce->ce_mode) &&
152 repo_file_exists(r, ce->name))
153 string_list_append(&sparse_dirs, ce->name);
154 }
155
156 for_each_string_list_item(item, &sparse_dirs) {
157 struct dir_struct dir = DIR_INIT;
158 struct pathspec p = { 0 };
159 struct strvec s = STRVEC_INIT;
160
161 strbuf_setlen(&path, pathlen);
162 strbuf_addstr(&path, item->string);
163
164 dir.flags |= DIR_SHOW_IGNORED_TOO;
165
166 setup_standard_excludes(&dir);
167 strvec_push(&s, path.buf);
168
169 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
170 fill_directory(&dir, r->index, &p);
171
172 if (dir.nr) {
173 warning(_("directory '%s' contains untracked files,"
174 " but is not in the sparse-checkout cone"),
175 item->string);
176 } else if (remove_dir_recursively(&path, 0)) {
177 /*
178 * Removal is "best effort". If something blocks
179 * the deletion, then continue with a warning.
180 */
181 warning(_("failed to remove directory '%s'"),
182 item->string);
183 }
184
185 dir_clear(&dir);
186 }
187
188 string_list_clear(&sparse_dirs, 0);
189 strbuf_release(&path);
190
191 if (was_full)
192 ensure_full_index(r->index);
193}
194
e091228e 195static int update_working_directory(struct pattern_list *pl)
bab3c359 196{
f56f31af 197 enum update_sparsity_result result;
e091228e
DS
198 struct unpack_trees_options o;
199 struct lock_file lock_file = LOCK_INIT;
e091228e 200 struct repository *r = the_repository;
bab3c359 201
b5bfc08a
EN
202 /* If no branch has been checked out, there are no updates to make. */
203 if (is_index_unborn(r->index))
204 return UPDATE_SPARSITY_SUCCESS;
205
836e25c5
DS
206 r->index->sparse_checkout_patterns = pl;
207
e091228e
DS
208 memset(&o, 0, sizeof(o));
209 o.verbose_update = isatty(2);
e091228e 210 o.update = 1;
e091228e
DS
211 o.head_idx = -1;
212 o.src_index = r->index;
213 o.dst_index = r->index;
214 o.skip_sparse_checkout = 0;
215 o.pl = pl;
e091228e 216
e091228e
DS
217 setup_work_tree();
218
e091228e
DS
219 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
220
4ee5d50f 221 setup_unpack_trees_porcelain(&o, "sparse-checkout");
f56f31af 222 result = update_sparsity(&o);
4ee5d50f 223 clear_unpack_trees_porcelain(&o);
e091228e 224
f56f31af
EN
225 if (result == UPDATE_SPARSITY_WARNINGS)
226 /*
227 * We don't do any special handling of warnings from untracked
228 * files in the way or dirty entries that can't be removed.
229 */
230 result = UPDATE_SPARSITY_SUCCESS;
231 if (result == UPDATE_SPARSITY_SUCCESS)
e091228e 232 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
f56f31af 233 else
e091228e 234 rollback_lock_file(&lock_file);
bab3c359 235
55dfcf95
DS
236 clean_tracked_sparse_directories(r);
237
836e25c5 238 r->index->sparse_checkout_patterns = NULL;
bab3c359
DS
239 return result;
240}
241
d585f0e7
DS
242static char *escaped_pattern(char *pattern)
243{
244 char *p = pattern;
245 struct strbuf final = STRBUF_INIT;
246
247 while (*p) {
e53ffe27 248 if (is_glob_special(*p))
d585f0e7
DS
249 strbuf_addch(&final, '\\');
250
251 strbuf_addch(&final, *p);
252 p++;
253 }
254
255 return strbuf_detach(&final, NULL);
256}
257
af09ce24
DS
258static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
259{
260 int i;
261 struct pattern_entry *pe;
262 struct hashmap_iter iter;
263 struct string_list sl = STRING_LIST_INIT_DUP;
e9de487a 264 struct strbuf parent_pattern = STRBUF_INIT;
af09ce24 265
e9de487a
DS
266 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
267 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
268 continue;
269
270 if (!hashmap_contains_parent(&pl->recursive_hashmap,
271 pe->pattern,
272 &parent_pattern))
273 string_list_insert(&sl, pe->pattern);
274 }
af09ce24
DS
275
276 string_list_sort(&sl);
277 string_list_remove_duplicates(&sl, 0);
278
279 fprintf(fp, "/*\n!/*/\n");
280
281 for (i = 0; i < sl.nr; i++) {
d585f0e7 282 char *pattern = escaped_pattern(sl.items[i].string);
af09ce24
DS
283
284 if (strlen(pattern))
285 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
d585f0e7 286 free(pattern);
af09ce24
DS
287 }
288
289 string_list_clear(&sl, 0);
290
e9de487a
DS
291 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
292 if (!hashmap_contains_parent(&pl->recursive_hashmap,
293 pe->pattern,
294 &parent_pattern))
295 string_list_insert(&sl, pe->pattern);
296 }
297
298 strbuf_release(&parent_pattern);
af09ce24
DS
299
300 string_list_sort(&sl);
301 string_list_remove_duplicates(&sl, 0);
302
303 for (i = 0; i < sl.nr; i++) {
d585f0e7 304 char *pattern = escaped_pattern(sl.items[i].string);
af09ce24 305 fprintf(fp, "%s/\n", pattern);
d585f0e7 306 free(pattern);
af09ce24
DS
307 }
308}
309
310static int write_patterns_and_update(struct pattern_list *pl)
311{
312 char *sparse_filename;
313 FILE *fp;
fb10ca5b
DS
314 int fd;
315 struct lock_file lk = LOCK_INIT;
e091228e
DS
316 int result;
317
fb10ca5b 318 sparse_filename = get_sparse_checkout_filename();
3c754067
DS
319
320 if (safe_create_leading_directories(sparse_filename))
321 die(_("failed to create directory for sparse-checkout file"));
322
fb10ca5b
DS
323 fd = hold_lock_file_for_update(&lk, sparse_filename,
324 LOCK_DIE_ON_ERROR);
e091228e 325
fb10ca5b 326 result = update_working_directory(pl);
e091228e 327 if (result) {
fb10ca5b
DS
328 rollback_lock_file(&lk);
329 free(sparse_filename);
e091228e
DS
330 clear_pattern_list(pl);
331 update_working_directory(NULL);
332 return result;
333 }
af09ce24 334
fb10ca5b 335 fp = xfdopen(fd, "w");
af09ce24
DS
336
337 if (core_sparse_checkout_cone)
338 write_cone_to_file(fp, pl);
339 else
340 write_patterns_to_file(fp, pl);
341
fb10ca5b
DS
342 fflush(fp);
343 commit_lock_file(&lk);
e091228e 344
af09ce24 345 free(sparse_filename);
e091228e 346 clear_pattern_list(pl);
af09ce24 347
e091228e 348 return 0;
af09ce24
DS
349}
350
bab3c359
DS
351enum sparse_checkout_mode {
352 MODE_NO_PATTERNS = 0,
353 MODE_ALL_PATTERNS = 1,
af09ce24 354 MODE_CONE_PATTERNS = 2,
bab3c359
DS
355};
356
357static int set_config(enum sparse_checkout_mode mode)
358{
359 const char *config_path;
360
98564d80
XL
361 if (upgrade_repository_format(1) < 0)
362 die(_("unable to upgrade repository format to enable worktreeConfig"));
bab3c359
DS
363 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
364 error(_("failed to set extensions.worktreeConfig setting"));
365 return 1;
366 }
367
368 config_path = git_path("config.worktree");
369 git_config_set_in_file_gently(config_path,
370 "core.sparseCheckout",
371 mode ? "true" : NULL);
372
af09ce24
DS
373 git_config_set_in_file_gently(config_path,
374 "core.sparseCheckoutCone",
375 mode == MODE_CONE_PATTERNS ? "true" : NULL);
376
dcc5fd5f
DS
377 if (mode == MODE_NO_PATTERNS)
378 set_sparse_index_config(the_repository, 0);
379
bab3c359
DS
380 return 0;
381}
382
af09ce24 383static char const * const builtin_sparse_checkout_init_usage[] = {
122ba1f7 384 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
af09ce24
DS
385 NULL
386};
387
388static struct sparse_checkout_init_opts {
389 int cone_mode;
122ba1f7 390 int sparse_index;
af09ce24
DS
391} init_opts;
392
bab3c359
DS
393static int sparse_checkout_init(int argc, const char **argv)
394{
395 struct pattern_list pl;
396 char *sparse_filename;
bab3c359 397 int res;
d89f09c8 398 struct object_id oid;
af09ce24 399 int mode;
416adc87 400 struct strbuf pattern = STRBUF_INIT;
bab3c359 401
af09ce24
DS
402 static struct option builtin_sparse_checkout_init_options[] = {
403 OPT_BOOL(0, "cone", &init_opts.cone_mode,
404 N_("initialize the sparse-checkout in cone mode")),
122ba1f7
DS
405 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
406 N_("toggle the use of a sparse index")),
af09ce24
DS
407 OPT_END(),
408 };
409
cff4e913 410 repo_read_index(the_repository);
cff4e913 411
122ba1f7
DS
412 init_opts.sparse_index = -1;
413
af09ce24
DS
414 argc = parse_options(argc, argv, NULL,
415 builtin_sparse_checkout_init_options,
416 builtin_sparse_checkout_init_usage, 0);
417
e091228e
DS
418 if (init_opts.cone_mode) {
419 mode = MODE_CONE_PATTERNS;
420 core_sparse_checkout_cone = 1;
421 } else
422 mode = MODE_ALL_PATTERNS;
af09ce24
DS
423
424 if (set_config(mode))
bab3c359
DS
425 return 1;
426
427 memset(&pl, 0, sizeof(pl));
428
429 sparse_filename = get_sparse_checkout_filename();
1679d60b 430 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
bab3c359 431
122ba1f7
DS
432 if (init_opts.sparse_index >= 0) {
433 if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0)
434 die(_("failed to modify sparse-index config"));
435
436 /* force an index rewrite */
437 repo_read_index(the_repository);
438 the_repository->index->updated_workdir = 1;
439 }
440
dcc5fd5f
DS
441 core_apply_sparse_checkout = 1;
442
bab3c359
DS
443 /* If we already have a sparse-checkout file, use it. */
444 if (res >= 0) {
445 free(sparse_filename);
416adc87 446 return update_working_directory(NULL);
bab3c359
DS
447 }
448
416adc87
DS
449 if (get_oid("HEAD", &oid)) {
450 FILE *fp;
bab3c359 451
416adc87
DS
452 /* assume we are in a fresh repo, but update the sparse-checkout file */
453 fp = xfopen(sparse_filename, "w");
454 if (!fp)
455 die(_("failed to open '%s'"), sparse_filename);
bab3c359 456
416adc87
DS
457 free(sparse_filename);
458 fprintf(fp, "/*\n!/*/\n");
459 fclose(fp);
d89f09c8
DS
460 return 0;
461 }
462
416adc87
DS
463 strbuf_addstr(&pattern, "/*");
464 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
465 strbuf_addstr(&pattern, "!/*/");
466 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
dcc5fd5f 467 pl.use_cone_patterns = init_opts.cone_mode;
416adc87
DS
468
469 return write_patterns_and_update(&pl);
bab3c359
DS
470}
471
af09ce24 472static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
f6039a94 473{
af09ce24
DS
474 struct pattern_entry *e = xmalloc(sizeof(*e));
475 e->patternlen = path->len;
476 e->pattern = strbuf_detach(path, NULL);
74318423 477 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
f6039a94 478
af09ce24 479 hashmap_add(&pl->recursive_hashmap, &e->ent);
f6039a94 480
af09ce24
DS
481 while (e->patternlen) {
482 char *slash = strrchr(e->pattern, '/');
483 char *oldpattern = e->pattern;
484 size_t newlen;
485
391c3a10 486 if (!slash || slash == e->pattern)
af09ce24
DS
487 break;
488
489 newlen = slash - e->pattern;
490 e = xmalloc(sizeof(struct pattern_entry));
491 e->patternlen = newlen;
492 e->pattern = xstrndup(oldpattern, newlen);
74318423 493 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
af09ce24
DS
494
495 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
496 hashmap_add(&pl->parent_hashmap, &e->ent);
497 }
498}
499
500static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
501{
502 strbuf_trim(line);
503
504 strbuf_trim_trailing_dir_sep(line);
505
ef076599
DS
506 if (strbuf_normalize_path(line))
507 die(_("could not normalize path %s"), line->buf);
508
af09ce24
DS
509 if (!line->len)
510 return;
511
512 if (line->buf[0] != '/')
a91cc7fa 513 strbuf_insertstr(line, 0, "/");
af09ce24
DS
514
515 insert_recursive_pattern(pl, line);
f6039a94
DS
516}
517
7bffca95 518static char const * const builtin_sparse_checkout_set_usage[] = {
2631dc87 519 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
7bffca95
DS
520 NULL
521};
522
523static struct sparse_checkout_set_opts {
524 int use_stdin;
525} set_opts;
526
6fb705ab
DS
527static void add_patterns_from_input(struct pattern_list *pl,
528 int argc, const char **argv)
f6039a94 529{
f6039a94 530 int i;
af09ce24 531 if (core_sparse_checkout_cone) {
7bffca95
DS
532 struct strbuf line = STRBUF_INIT;
533
6fb705ab
DS
534 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
535 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
536 pl->use_cone_patterns = 1;
af09ce24
DS
537
538 if (set_opts.use_stdin) {
bd64de42
DS
539 struct strbuf unquoted = STRBUF_INIT;
540 while (!strbuf_getline(&line, stdin)) {
541 if (line.buf[0] == '"') {
542 strbuf_reset(&unquoted);
543 if (unquote_c_style(&unquoted, line.buf, NULL))
544 die(_("unable to unquote C-style string '%s'"),
545 line.buf);
546
547 strbuf_swap(&unquoted, &line);
548 }
549
6fb705ab 550 strbuf_to_cone_pattern(&line, pl);
bd64de42
DS
551 }
552
553 strbuf_release(&unquoted);
af09ce24
DS
554 } else {
555 for (i = 0; i < argc; i++) {
556 strbuf_setlen(&line, 0);
557 strbuf_addstr(&line, argv[i]);
6fb705ab 558 strbuf_to_cone_pattern(&line, pl);
af09ce24 559 }
7bffca95
DS
560 }
561 } else {
af09ce24
DS
562 if (set_opts.use_stdin) {
563 struct strbuf line = STRBUF_INIT;
564
565 while (!strbuf_getline(&line, stdin)) {
566 size_t len;
567 char *buf = strbuf_detach(&line, &len);
6fb705ab 568 add_pattern(buf, empty_base, 0, pl, 0);
af09ce24
DS
569 }
570 } else {
571 for (i = 0; i < argc; i++)
6fb705ab 572 add_pattern(argv[i], empty_base, 0, pl, 0);
af09ce24 573 }
7bffca95 574 }
6fb705ab
DS
575}
576
4bf0c06c
DS
577enum modify_type {
578 REPLACE,
2631dc87 579 ADD,
4bf0c06c
DS
580};
581
2631dc87
DS
582static void add_patterns_cone_mode(int argc, const char **argv,
583 struct pattern_list *pl)
584{
585 struct strbuf buffer = STRBUF_INIT;
586 struct pattern_entry *pe;
587 struct hashmap_iter iter;
588 struct pattern_list existing;
589 char *sparse_filename = get_sparse_checkout_filename();
590
591 add_patterns_from_input(pl, argc, argv);
592
593 memset(&existing, 0, sizeof(existing));
594 existing.use_cone_patterns = core_sparse_checkout_cone;
595
596 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
1679d60b 597 &existing, NULL, 0))
2631dc87
DS
598 die(_("unable to load existing sparse-checkout patterns"));
599 free(sparse_filename);
600
601 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
602 if (!hashmap_contains_parent(&pl->recursive_hashmap,
603 pe->pattern, &buffer) ||
604 !hashmap_contains_parent(&pl->parent_hashmap,
605 pe->pattern, &buffer)) {
606 strbuf_reset(&buffer);
607 strbuf_addstr(&buffer, pe->pattern);
608 insert_recursive_pattern(pl, &buffer);
609 }
610 }
611
612 clear_pattern_list(&existing);
613 strbuf_release(&buffer);
614}
615
616static void add_patterns_literal(int argc, const char **argv,
617 struct pattern_list *pl)
618{
619 char *sparse_filename = get_sparse_checkout_filename();
620 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
1679d60b 621 pl, NULL, 0))
2631dc87
DS
622 die(_("unable to load existing sparse-checkout patterns"));
623 free(sparse_filename);
624 add_patterns_from_input(pl, argc, argv);
625}
626
4bf0c06c 627static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
6fb705ab 628{
6fb705ab
DS
629 int result;
630 int changed_config = 0;
836e25c5 631 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
6fb705ab 632
2631dc87
DS
633 switch (m) {
634 case ADD:
635 if (core_sparse_checkout_cone)
836e25c5 636 add_patterns_cone_mode(argc, argv, pl);
2631dc87 637 else
836e25c5 638 add_patterns_literal(argc, argv, pl);
2631dc87
DS
639 break;
640
641 case REPLACE:
836e25c5 642 add_patterns_from_input(pl, argc, argv);
2631dc87
DS
643 break;
644 }
f6039a94
DS
645
646 if (!core_apply_sparse_checkout) {
647 set_config(MODE_ALL_PATTERNS);
648 core_apply_sparse_checkout = 1;
649 changed_config = 1;
650 }
651
836e25c5 652 result = write_patterns_and_update(pl);
f6039a94
DS
653
654 if (result && changed_config)
655 set_config(MODE_NO_PATTERNS);
656
836e25c5
DS
657 clear_pattern_list(pl);
658 free(pl);
f6039a94
DS
659 return result;
660}
661
2631dc87
DS
662static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
663 enum modify_type m)
4bf0c06c
DS
664{
665 static struct option builtin_sparse_checkout_set_options[] = {
666 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
667 N_("read patterns from standard in")),
668 OPT_END(),
669 };
670
671 repo_read_index(the_repository);
4bf0c06c
DS
672
673 argc = parse_options(argc, argv, prefix,
674 builtin_sparse_checkout_set_options,
675 builtin_sparse_checkout_set_usage,
676 PARSE_OPT_KEEP_UNKNOWN);
677
2631dc87 678 return modify_pattern_list(argc, argv, m);
4bf0c06c
DS
679}
680
75d3bee1
JK
681static char const * const builtin_sparse_checkout_reapply_usage[] = {
682 N_("git sparse-checkout reapply"),
683 NULL
684};
685
5644ca28
EN
686static int sparse_checkout_reapply(int argc, const char **argv)
687{
75d3bee1
JK
688 static struct option builtin_sparse_checkout_reapply_options[] = {
689 OPT_END(),
690 };
691
692 argc = parse_options(argc, argv, NULL,
693 builtin_sparse_checkout_reapply_options,
694 builtin_sparse_checkout_reapply_usage, 0);
695
5644ca28
EN
696 repo_read_index(the_repository);
697 return update_working_directory(NULL);
698}
699
75d3bee1
JK
700static char const * const builtin_sparse_checkout_disable_usage[] = {
701 N_("git sparse-checkout disable"),
702 NULL
703};
704
72918c1a
DS
705static int sparse_checkout_disable(int argc, const char **argv)
706{
75d3bee1
JK
707 static struct option builtin_sparse_checkout_disable_options[] = {
708 OPT_END(),
709 };
99dfa6f9
DS
710 struct pattern_list pl;
711 struct strbuf match_all = STRBUF_INIT;
72918c1a 712
75d3bee1
JK
713 argc = parse_options(argc, argv, NULL,
714 builtin_sparse_checkout_disable_options,
715 builtin_sparse_checkout_disable_usage, 0);
716
cff4e913 717 repo_read_index(the_repository);
cff4e913 718
99dfa6f9
DS
719 memset(&pl, 0, sizeof(pl));
720 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
721 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
722 pl.use_cone_patterns = 0;
723 core_apply_sparse_checkout = 1;
72918c1a 724
99dfa6f9
DS
725 strbuf_addstr(&match_all, "/*");
726 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
72918c1a 727
dcc5fd5f
DS
728 prepare_repo_settings(the_repository);
729 the_repository->settings.sparse_index = 0;
730
99dfa6f9 731 if (update_working_directory(&pl))
72918c1a
DS
732 die(_("error while refreshing working directory"));
733
99dfa6f9 734 clear_pattern_list(&pl);
72918c1a
DS
735 return set_config(MODE_NO_PATTERNS);
736}
737
94c0956b
DS
738int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
739{
740 static struct option builtin_sparse_checkout_options[] = {
741 OPT_END(),
742 };
743
744 if (argc == 2 && !strcmp(argv[1], "-h"))
745 usage_with_options(builtin_sparse_checkout_usage,
746 builtin_sparse_checkout_options);
747
748 argc = parse_options(argc, argv, prefix,
749 builtin_sparse_checkout_options,
750 builtin_sparse_checkout_usage,
751 PARSE_OPT_STOP_AT_NON_OPTION);
752
753 git_config(git_default_config, NULL);
754
755 if (argc > 0) {
756 if (!strcmp(argv[0], "list"))
757 return sparse_checkout_list(argc, argv);
bab3c359
DS
758 if (!strcmp(argv[0], "init"))
759 return sparse_checkout_init(argc, argv);
f6039a94 760 if (!strcmp(argv[0], "set"))
2631dc87
DS
761 return sparse_checkout_set(argc, argv, prefix, REPLACE);
762 if (!strcmp(argv[0], "add"))
763 return sparse_checkout_set(argc, argv, prefix, ADD);
5644ca28
EN
764 if (!strcmp(argv[0], "reapply"))
765 return sparse_checkout_reapply(argc, argv);
72918c1a
DS
766 if (!strcmp(argv[0], "disable"))
767 return sparse_checkout_disable(argc, argv);
94c0956b
DS
768 }
769
770 usage_with_options(builtin_sparse_checkout_usage,
771 builtin_sparse_checkout_options);
772}