]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/sparse-checkout.c
Merge branch 'gc/branch-recurse-submodules-fix'
[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[] = {
5644ca28 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
94c0956b
DS
51static int sparse_checkout_list(int argc, const char **argv)
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
75d3bee1
JK
63 argc = parse_options(argc, argv, NULL,
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 */
131 if (!r->index->sparse_index) {
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;
220 o.skip_sparse_checkout = 0;
221 o.pl = pl;
e091228e 222
e091228e
DS
223 setup_work_tree();
224
e091228e
DS
225 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
226
4ee5d50f 227 setup_unpack_trees_porcelain(&o, "sparse-checkout");
f56f31af 228 result = update_sparsity(&o);
4ee5d50f 229 clear_unpack_trees_porcelain(&o);
e091228e 230
f56f31af
EN
231 if (result == UPDATE_SPARSITY_WARNINGS)
232 /*
233 * We don't do any special handling of warnings from untracked
234 * files in the way or dirty entries that can't be removed.
235 */
236 result = UPDATE_SPARSITY_SUCCESS;
237 if (result == UPDATE_SPARSITY_SUCCESS)
e091228e 238 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
f56f31af 239 else
e091228e 240 rollback_lock_file(&lock_file);
bab3c359 241
55dfcf95
DS
242 clean_tracked_sparse_directories(r);
243
836e25c5 244 r->index->sparse_checkout_patterns = NULL;
bab3c359
DS
245 return result;
246}
247
d585f0e7
DS
248static char *escaped_pattern(char *pattern)
249{
250 char *p = pattern;
251 struct strbuf final = STRBUF_INIT;
252
253 while (*p) {
e53ffe27 254 if (is_glob_special(*p))
d585f0e7
DS
255 strbuf_addch(&final, '\\');
256
257 strbuf_addch(&final, *p);
258 p++;
259 }
260
261 return strbuf_detach(&final, NULL);
262}
263
af09ce24
DS
264static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
265{
266 int i;
267 struct pattern_entry *pe;
268 struct hashmap_iter iter;
269 struct string_list sl = STRING_LIST_INIT_DUP;
e9de487a 270 struct strbuf parent_pattern = STRBUF_INIT;
af09ce24 271
e9de487a
DS
272 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
273 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
274 continue;
275
276 if (!hashmap_contains_parent(&pl->recursive_hashmap,
277 pe->pattern,
278 &parent_pattern))
279 string_list_insert(&sl, pe->pattern);
280 }
af09ce24
DS
281
282 string_list_sort(&sl);
283 string_list_remove_duplicates(&sl, 0);
284
285 fprintf(fp, "/*\n!/*/\n");
286
287 for (i = 0; i < sl.nr; i++) {
d585f0e7 288 char *pattern = escaped_pattern(sl.items[i].string);
af09ce24
DS
289
290 if (strlen(pattern))
291 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
d585f0e7 292 free(pattern);
af09ce24
DS
293 }
294
295 string_list_clear(&sl, 0);
296
e9de487a
DS
297 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
298 if (!hashmap_contains_parent(&pl->recursive_hashmap,
299 pe->pattern,
300 &parent_pattern))
301 string_list_insert(&sl, pe->pattern);
302 }
303
304 strbuf_release(&parent_pattern);
af09ce24
DS
305
306 string_list_sort(&sl);
307 string_list_remove_duplicates(&sl, 0);
308
309 for (i = 0; i < sl.nr; i++) {
d585f0e7 310 char *pattern = escaped_pattern(sl.items[i].string);
af09ce24 311 fprintf(fp, "%s/\n", pattern);
d585f0e7 312 free(pattern);
af09ce24
DS
313 }
314}
315
316static int write_patterns_and_update(struct pattern_list *pl)
317{
318 char *sparse_filename;
319 FILE *fp;
fb10ca5b
DS
320 int fd;
321 struct lock_file lk = LOCK_INIT;
e091228e
DS
322 int result;
323
fb10ca5b 324 sparse_filename = get_sparse_checkout_filename();
3c754067
DS
325
326 if (safe_create_leading_directories(sparse_filename))
327 die(_("failed to create directory for sparse-checkout file"));
328
fb10ca5b
DS
329 fd = hold_lock_file_for_update(&lk, sparse_filename,
330 LOCK_DIE_ON_ERROR);
ef3fe214 331 free(sparse_filename);
e091228e 332
fb10ca5b 333 result = update_working_directory(pl);
e091228e 334 if (result) {
fb10ca5b 335 rollback_lock_file(&lk);
e091228e
DS
336 clear_pattern_list(pl);
337 update_working_directory(NULL);
338 return result;
339 }
af09ce24 340
fb10ca5b 341 fp = xfdopen(fd, "w");
af09ce24
DS
342
343 if (core_sparse_checkout_cone)
344 write_cone_to_file(fp, pl);
345 else
346 write_patterns_to_file(fp, pl);
347
fb10ca5b
DS
348 fflush(fp);
349 commit_lock_file(&lk);
e091228e 350
e091228e 351 clear_pattern_list(pl);
af09ce24 352
e091228e 353 return 0;
af09ce24
DS
354}
355
bab3c359
DS
356enum sparse_checkout_mode {
357 MODE_NO_PATTERNS = 0,
358 MODE_ALL_PATTERNS = 1,
af09ce24 359 MODE_CONE_PATTERNS = 2,
bab3c359
DS
360};
361
362static int set_config(enum sparse_checkout_mode mode)
363{
7316dc5f
DS
364 /* Update to use worktree config, if not already. */
365 if (init_worktree_config(the_repository)) {
366 error(_("failed to initialize worktree config"));
bab3c359
DS
367 return 1;
368 }
369
7316dc5f
DS
370 if (repo_config_set_worktree_gently(the_repository,
371 "core.sparseCheckout",
372 mode ? "true" : "false") ||
373 repo_config_set_worktree_gently(the_repository,
374 "core.sparseCheckoutCone",
375 mode == MODE_CONE_PATTERNS ?
376 "true" : "false"))
377 return 1;
af09ce24 378
dcc5fd5f 379 if (mode == MODE_NO_PATTERNS)
7316dc5f 380 return set_sparse_index_config(the_repository, 0);
dcc5fd5f 381
bab3c359
DS
382 return 0;
383}
384
be61fd11
EN
385static int update_modes(int *cone_mode, int *sparse_index)
386{
387 int mode, record_mode;
388
389 /* Determine if we need to record the mode; ensure sparse checkout on */
390 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
391
392 /* If not specified, use previous definition of cone mode */
393 if (*cone_mode == -1 && core_apply_sparse_checkout)
394 *cone_mode = core_sparse_checkout_cone;
395
396 /* Set cone/non-cone mode appropriately */
397 core_apply_sparse_checkout = 1;
398 if (*cone_mode == 1) {
399 mode = MODE_CONE_PATTERNS;
400 core_sparse_checkout_cone = 1;
401 } else {
402 mode = MODE_ALL_PATTERNS;
d526b4db 403 core_sparse_checkout_cone = 0;
be61fd11
EN
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[] = {
959d670d 422 "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 473 /* assume we are in a fresh repo, but update the sparse-checkout file */
7f44842a
JT
474 if (safe_create_leading_directories(sparse_filename))
475 die(_("unable to create leading directories of %s"),
476 sparse_filename);
416adc87
DS
477 fp = xfopen(sparse_filename, "w");
478 if (!fp)
479 die(_("failed to open '%s'"), sparse_filename);
bab3c359 480
416adc87
DS
481 free(sparse_filename);
482 fprintf(fp, "/*\n!/*/\n");
483 fclose(fp);
d89f09c8
DS
484 return 0;
485 }
486
416adc87
DS
487 strbuf_addstr(&pattern, "/*");
488 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
489 strbuf_addstr(&pattern, "!/*/");
490 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
dcc5fd5f 491 pl.use_cone_patterns = init_opts.cone_mode;
416adc87
DS
492
493 return write_patterns_and_update(&pl);
bab3c359
DS
494}
495
af09ce24 496static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
f6039a94 497{
af09ce24
DS
498 struct pattern_entry *e = xmalloc(sizeof(*e));
499 e->patternlen = path->len;
500 e->pattern = strbuf_detach(path, NULL);
74318423 501 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
f6039a94 502
af09ce24 503 hashmap_add(&pl->recursive_hashmap, &e->ent);
f6039a94 504
af09ce24
DS
505 while (e->patternlen) {
506 char *slash = strrchr(e->pattern, '/');
507 char *oldpattern = e->pattern;
508 size_t newlen;
509
391c3a10 510 if (!slash || slash == e->pattern)
af09ce24
DS
511 break;
512
513 newlen = slash - e->pattern;
514 e = xmalloc(sizeof(struct pattern_entry));
515 e->patternlen = newlen;
516 e->pattern = xstrndup(oldpattern, newlen);
74318423 517 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
af09ce24
DS
518
519 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
520 hashmap_add(&pl->parent_hashmap, &e->ent);
521 }
522}
523
524static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
525{
526 strbuf_trim(line);
527
528 strbuf_trim_trailing_dir_sep(line);
529
ef076599
DS
530 if (strbuf_normalize_path(line))
531 die(_("could not normalize path %s"), line->buf);
532
af09ce24
DS
533 if (!line->len)
534 return;
535
536 if (line->buf[0] != '/')
a91cc7fa 537 strbuf_insertstr(line, 0, "/");
af09ce24
DS
538
539 insert_recursive_pattern(pl, line);
f6039a94
DS
540}
541
6fb705ab 542static void add_patterns_from_input(struct pattern_list *pl,
1530ff35
EN
543 int argc, const char **argv,
544 int use_stdin)
f6039a94 545{
f6039a94 546 int i;
af09ce24 547 if (core_sparse_checkout_cone) {
7bffca95
DS
548 struct strbuf line = STRBUF_INIT;
549
6fb705ab
DS
550 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
551 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
552 pl->use_cone_patterns = 1;
af09ce24 553
1530ff35 554 if (use_stdin) {
bd64de42
DS
555 struct strbuf unquoted = STRBUF_INIT;
556 while (!strbuf_getline(&line, stdin)) {
557 if (line.buf[0] == '"') {
558 strbuf_reset(&unquoted);
559 if (unquote_c_style(&unquoted, line.buf, NULL))
560 die(_("unable to unquote C-style string '%s'"),
561 line.buf);
562
563 strbuf_swap(&unquoted, &line);
564 }
565
6fb705ab 566 strbuf_to_cone_pattern(&line, pl);
bd64de42
DS
567 }
568
569 strbuf_release(&unquoted);
af09ce24
DS
570 } else {
571 for (i = 0; i < argc; i++) {
572 strbuf_setlen(&line, 0);
573 strbuf_addstr(&line, argv[i]);
6fb705ab 574 strbuf_to_cone_pattern(&line, pl);
af09ce24 575 }
7bffca95
DS
576 }
577 } else {
1530ff35 578 if (use_stdin) {
af09ce24
DS
579 struct strbuf line = STRBUF_INIT;
580
581 while (!strbuf_getline(&line, stdin)) {
582 size_t len;
583 char *buf = strbuf_detach(&line, &len);
6fb705ab 584 add_pattern(buf, empty_base, 0, pl, 0);
af09ce24
DS
585 }
586 } else {
587 for (i = 0; i < argc; i++)
6fb705ab 588 add_pattern(argv[i], empty_base, 0, pl, 0);
af09ce24 589 }
7bffca95 590 }
6fb705ab
DS
591}
592
4bf0c06c
DS
593enum modify_type {
594 REPLACE,
2631dc87 595 ADD,
4bf0c06c
DS
596};
597
2631dc87 598static void add_patterns_cone_mode(int argc, const char **argv,
1530ff35
EN
599 struct pattern_list *pl,
600 int use_stdin)
2631dc87
DS
601{
602 struct strbuf buffer = STRBUF_INIT;
603 struct pattern_entry *pe;
604 struct hashmap_iter iter;
605 struct pattern_list existing;
606 char *sparse_filename = get_sparse_checkout_filename();
607
1530ff35 608 add_patterns_from_input(pl, argc, argv, use_stdin);
2631dc87
DS
609
610 memset(&existing, 0, sizeof(existing));
611 existing.use_cone_patterns = core_sparse_checkout_cone;
612
613 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
1679d60b 614 &existing, NULL, 0))
2631dc87
DS
615 die(_("unable to load existing sparse-checkout patterns"));
616 free(sparse_filename);
617
a3eca584
DS
618 if (!existing.use_cone_patterns)
619 die(_("existing sparse-checkout patterns do not use cone mode"));
620
2631dc87
DS
621 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
622 if (!hashmap_contains_parent(&pl->recursive_hashmap,
623 pe->pattern, &buffer) ||
624 !hashmap_contains_parent(&pl->parent_hashmap,
625 pe->pattern, &buffer)) {
626 strbuf_reset(&buffer);
627 strbuf_addstr(&buffer, pe->pattern);
628 insert_recursive_pattern(pl, &buffer);
629 }
630 }
631
632 clear_pattern_list(&existing);
633 strbuf_release(&buffer);
634}
635
636static void add_patterns_literal(int argc, const char **argv,
1530ff35
EN
637 struct pattern_list *pl,
638 int use_stdin)
2631dc87
DS
639{
640 char *sparse_filename = get_sparse_checkout_filename();
641 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
1679d60b 642 pl, NULL, 0))
2631dc87
DS
643 die(_("unable to load existing sparse-checkout patterns"));
644 free(sparse_filename);
1530ff35 645 add_patterns_from_input(pl, argc, argv, use_stdin);
2631dc87
DS
646}
647
1530ff35
EN
648static int modify_pattern_list(int argc, const char **argv, int use_stdin,
649 enum modify_type m)
6fb705ab 650{
6fb705ab
DS
651 int result;
652 int changed_config = 0;
836e25c5 653 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
6fb705ab 654
2631dc87
DS
655 switch (m) {
656 case ADD:
657 if (core_sparse_checkout_cone)
1530ff35 658 add_patterns_cone_mode(argc, argv, pl, use_stdin);
2631dc87 659 else
1530ff35 660 add_patterns_literal(argc, argv, pl, use_stdin);
2631dc87
DS
661 break;
662
663 case REPLACE:
1530ff35 664 add_patterns_from_input(pl, argc, argv, use_stdin);
2631dc87
DS
665 break;
666 }
f6039a94
DS
667
668 if (!core_apply_sparse_checkout) {
669 set_config(MODE_ALL_PATTERNS);
670 core_apply_sparse_checkout = 1;
671 changed_config = 1;
672 }
673
836e25c5 674 result = write_patterns_and_update(pl);
f6039a94
DS
675
676 if (result && changed_config)
677 set_config(MODE_NO_PATTERNS);
678
836e25c5
DS
679 clear_pattern_list(pl);
680 free(pl);
f6039a94
DS
681 return result;
682}
683
4ce50436
EN
684static void sanitize_paths(int argc, const char **argv,
685 const char *prefix, int skip_checks)
bb8b5e9a 686{
4ce50436
EN
687 int i;
688
bb8b5e9a
EN
689 if (!argc)
690 return;
691
692 if (prefix && *prefix && core_sparse_checkout_cone) {
693 /*
694 * The args are not pathspecs, so unfortunately we
695 * cannot imitate how cmd_add() uses parse_pathspec().
696 */
bb8b5e9a
EN
697 int prefix_len = strlen(prefix);
698
699 for (i = 0; i < argc; i++)
700 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
701 }
702
4ce50436
EN
703 if (skip_checks)
704 return;
705
bb8b5e9a
EN
706 if (prefix && *prefix && !core_sparse_checkout_cone)
707 die(_("please run from the toplevel directory in non-cone mode"));
708
8dd7c473
EN
709 if (core_sparse_checkout_cone) {
710 for (i = 0; i < argc; i++) {
711 if (argv[i][0] == '/')
712 die(_("specify directories rather than patterns (no leading slash)"));
713 if (argv[i][0] == '!')
714 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
715 if (strpbrk(argv[i], "*?[]"))
716 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
717 }
718 }
719
4ce50436
EN
720 for (i = 0; i < argc; i++) {
721 struct cache_entry *ce;
722 struct index_state *index = the_repository->index;
723 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
724
725 if (pos < 0)
726 continue;
727 ce = index->cache[pos];
728 if (S_ISSPARSEDIR(ce->ce_mode))
729 continue;
730
731 if (core_sparse_checkout_cone)
732 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
733 else
734 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]);
735 }
bb8b5e9a
EN
736}
737
0b624e03 738static char const * const builtin_sparse_checkout_add_usage[] = {
4ce50436 739 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
0b624e03
EN
740 NULL
741};
742
743static struct sparse_checkout_add_opts {
4ce50436 744 int skip_checks;
0b624e03
EN
745 int use_stdin;
746} add_opts;
747
748static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
749{
750 static struct option builtin_sparse_checkout_add_options[] = {
4ce50436
EN
751 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
752 N_("skip some sanity checks on the given paths that might give false positives"),
753 PARSE_OPT_NONEG),
0b624e03
EN
754 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
755 N_("read patterns from standard in")),
756 OPT_END(),
757 };
758
45c5e470
EN
759 if (!core_apply_sparse_checkout)
760 die(_("no sparse-checkout to add to"));
761
0b624e03
EN
762 repo_read_index(the_repository);
763
764 argc = parse_options(argc, argv, prefix,
765 builtin_sparse_checkout_add_options,
766 builtin_sparse_checkout_add_usage,
767 PARSE_OPT_KEEP_UNKNOWN);
768
4ce50436 769 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
bb8b5e9a 770
0b624e03
EN
771 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
772}
773
774static char const * const builtin_sparse_checkout_set_usage[] = {
4ce50436 775 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
0b624e03
EN
776 NULL
777};
778
779static struct sparse_checkout_set_opts {
f2e3a218
EN
780 int cone_mode;
781 int sparse_index;
4ce50436 782 int skip_checks;
0b624e03
EN
783 int use_stdin;
784} set_opts;
785
786static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
4bf0c06c 787{
f2e3a218
EN
788 int default_patterns_nr = 2;
789 const char *default_patterns[] = {"/*", "!/*/", NULL};
790
4bf0c06c 791 static struct option builtin_sparse_checkout_set_options[] = {
f2e3a218
EN
792 OPT_BOOL(0, "cone", &set_opts.cone_mode,
793 N_("initialize the sparse-checkout in cone mode")),
794 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
795 N_("toggle the use of a sparse index")),
4ce50436
EN
796 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
797 N_("skip some sanity checks on the given paths that might give false positives"),
798 PARSE_OPT_NONEG),
f85751a1
EN
799 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
800 N_("read patterns from standard in"),
801 PARSE_OPT_NONEG),
4bf0c06c
DS
802 OPT_END(),
803 };
804
805 repo_read_index(the_repository);
4bf0c06c 806
f2e3a218
EN
807 set_opts.cone_mode = -1;
808 set_opts.sparse_index = -1;
809
4bf0c06c
DS
810 argc = parse_options(argc, argv, prefix,
811 builtin_sparse_checkout_set_options,
812 builtin_sparse_checkout_set_usage,
813 PARSE_OPT_KEEP_UNKNOWN);
814
f2e3a218
EN
815 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
816 return 1;
817
818 /*
819 * Cone mode automatically specifies the toplevel directory. For
820 * non-cone mode, if nothing is specified, manually select just the
821 * top-level directory (much as 'init' would do).
822 */
823 if (!core_sparse_checkout_cone && argc == 0) {
824 argv = default_patterns;
825 argc = default_patterns_nr;
bb8b5e9a 826 } else {
4ce50436 827 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
f2e3a218
EN
828 }
829
0b624e03 830 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
4bf0c06c
DS
831}
832
75d3bee1 833static char const * const builtin_sparse_checkout_reapply_usage[] = {
959d670d 834 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
75d3bee1
JK
835 NULL
836};
837
4e256731
EN
838static struct sparse_checkout_reapply_opts {
839 int cone_mode;
840 int sparse_index;
841} reapply_opts;
842
5644ca28
EN
843static int sparse_checkout_reapply(int argc, const char **argv)
844{
75d3bee1 845 static struct option builtin_sparse_checkout_reapply_options[] = {
4e256731
EN
846 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
847 N_("initialize the sparse-checkout in cone mode")),
848 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
849 N_("toggle the use of a sparse index")),
75d3bee1
JK
850 OPT_END(),
851 };
852
45c5e470
EN
853 if (!core_apply_sparse_checkout)
854 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
855
f748012e
EN
856 reapply_opts.cone_mode = -1;
857 reapply_opts.sparse_index = -1;
858
75d3bee1
JK
859 argc = parse_options(argc, argv, NULL,
860 builtin_sparse_checkout_reapply_options,
861 builtin_sparse_checkout_reapply_usage, 0);
862
5644ca28 863 repo_read_index(the_repository);
4e256731 864
4e256731
EN
865 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
866 return 1;
867
5644ca28
EN
868 return update_working_directory(NULL);
869}
870
75d3bee1 871static char const * const builtin_sparse_checkout_disable_usage[] = {
959d670d 872 "git sparse-checkout disable",
75d3bee1
JK
873 NULL
874};
875
72918c1a
DS
876static int sparse_checkout_disable(int argc, const char **argv)
877{
75d3bee1
JK
878 static struct option builtin_sparse_checkout_disable_options[] = {
879 OPT_END(),
880 };
99dfa6f9
DS
881 struct pattern_list pl;
882 struct strbuf match_all = STRBUF_INIT;
72918c1a 883
45c5e470
EN
884 /*
885 * We do not exit early if !core_apply_sparse_checkout; due to the
886 * ability for users to manually muck things up between
887 * direct editing of .git/info/sparse-checkout
888 * running read-tree -m u HEAD or update-index --skip-worktree
889 * direct toggling of config options
890 * users might end up with an index with SKIP_WORKTREE bit set on
891 * some files and not know how to undo it. So, here we just
892 * forcibly return to a dense checkout regardless of initial state.
893 */
894
75d3bee1
JK
895 argc = parse_options(argc, argv, NULL,
896 builtin_sparse_checkout_disable_options,
897 builtin_sparse_checkout_disable_usage, 0);
898
cff4e913 899 repo_read_index(the_repository);
cff4e913 900
99dfa6f9
DS
901 memset(&pl, 0, sizeof(pl));
902 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
903 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
904 pl.use_cone_patterns = 0;
905 core_apply_sparse_checkout = 1;
72918c1a 906
99dfa6f9
DS
907 strbuf_addstr(&match_all, "/*");
908 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
72918c1a 909
dcc5fd5f
DS
910 prepare_repo_settings(the_repository);
911 the_repository->settings.sparse_index = 0;
912
99dfa6f9 913 if (update_working_directory(&pl))
72918c1a
DS
914 die(_("error while refreshing working directory"));
915
99dfa6f9 916 clear_pattern_list(&pl);
72918c1a
DS
917 return set_config(MODE_NO_PATTERNS);
918}
919
94c0956b
DS
920int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
921{
922 static struct option builtin_sparse_checkout_options[] = {
923 OPT_END(),
924 };
925
926 if (argc == 2 && !strcmp(argv[1], "-h"))
927 usage_with_options(builtin_sparse_checkout_usage,
928 builtin_sparse_checkout_options);
929
930 argc = parse_options(argc, argv, prefix,
931 builtin_sparse_checkout_options,
932 builtin_sparse_checkout_usage,
933 PARSE_OPT_STOP_AT_NON_OPTION);
934
935 git_config(git_default_config, NULL);
936
937 if (argc > 0) {
938 if (!strcmp(argv[0], "list"))
939 return sparse_checkout_list(argc, argv);
bab3c359
DS
940 if (!strcmp(argv[0], "init"))
941 return sparse_checkout_init(argc, argv);
f6039a94 942 if (!strcmp(argv[0], "set"))
0b624e03 943 return sparse_checkout_set(argc, argv, prefix);
2631dc87 944 if (!strcmp(argv[0], "add"))
0b624e03 945 return sparse_checkout_add(argc, argv, prefix);
5644ca28
EN
946 if (!strcmp(argv[0], "reapply"))
947 return sparse_checkout_reapply(argc, argv);
72918c1a
DS
948 if (!strcmp(argv[0], "disable"))
949 return sparse_checkout_disable(argc, argv);
94c0956b
DS
950 }
951
952 usage_with_options(builtin_sparse_checkout_usage,
953 builtin_sparse_checkout_options);
954}