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