]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/sparse-checkout.c
fast-import: tighten path unquoting
[thirdparty/git.git] / builtin / sparse-checkout.c
CommitLineData
94c0956b
DS
1#include "builtin.h"
2#include "config.h"
3#include "dir.h"
32a8f510 4#include "environment.h"
f394e093 5#include "gettext.h"
87bed179 6#include "object-file.h"
dabab1d6 7#include "object-name.h"
94c0956b
DS
8#include "parse-options.h"
9#include "pathspec.h"
10#include "repository.h"
94c0956b 11#include "strbuf.h"
af09ce24 12#include "string-list.h"
e091228e 13#include "lockfile.h"
e091228e 14#include "unpack-trees.h"
d585f0e7 15#include "quote.h"
e38da487 16#include "setup.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[] = {
00408ade 23 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<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
24fc2cde 60 setup_work_tree();
45c5e470
EN
61 if (!core_apply_sparse_checkout)
62 die(_("this worktree is not sparse"));
63
ecd2d3ef 64 argc = parse_options(argc, argv, prefix,
75d3bee1
JK
65 builtin_sparse_checkout_list_options,
66 builtin_sparse_checkout_list_usage, 0);
67
94c0956b
DS
68 memset(&pl, 0, sizeof(pl));
69
de11951b
DS
70 pl.use_cone_patterns = core_sparse_checkout_cone;
71
94c0956b 72 sparse_filename = get_sparse_checkout_filename();
1679d60b 73 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
94c0956b
DS
74 free(sparse_filename);
75
76 if (res < 0) {
77 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
78 return 0;
79 }
80
de11951b
DS
81 if (pl.use_cone_patterns) {
82 int i;
83 struct pattern_entry *pe;
84 struct hashmap_iter iter;
85 struct string_list sl = STRING_LIST_INIT_DUP;
86
87 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
88 /* pe->pattern starts with "/", skip it */
89 string_list_insert(&sl, pe->pattern + 1);
90 }
91
92 string_list_sort(&sl);
93
e55682ea
DS
94 for (i = 0; i < sl.nr; i++) {
95 quote_c_style(sl.items[i].string, NULL, stdout, 0);
96 printf("\n");
97 }
de11951b
DS
98
99 return 0;
100 }
101
94c0956b
DS
102 write_patterns_to_file(stdout, &pl);
103 clear_pattern_list(&pl);
104
105 return 0;
106}
107
55dfcf95
DS
108static void clean_tracked_sparse_directories(struct repository *r)
109{
110 int i, was_full = 0;
111 struct strbuf path = STRBUF_INIT;
112 size_t pathlen;
113 struct string_list_item *item;
114 struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
115
116 /*
117 * If we are not using cone mode patterns, then we cannot
118 * delete directories outside of the sparse cone.
119 */
120 if (!r || !r->index || !r->worktree)
121 return;
122 if (init_sparse_checkout_patterns(r->index) ||
123 !r->index->sparse_checkout_patterns->use_cone_patterns)
124 return;
125
126 /*
127 * Use the sparse index as a data structure to assist finding
128 * directories that are safe to delete. This conversion to a
129 * sparse index will not delete directories that contain
130 * conflicted entries or submodules.
131 */
9fadb373 132 if (r->index->sparse_index == INDEX_EXPANDED) {
55dfcf95
DS
133 /*
134 * If something, such as a merge conflict or other concern,
135 * prevents us from converting to a sparse index, then do
136 * not try deleting files.
137 */
138 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
139 return;
140 was_full = 1;
141 }
142
143 strbuf_addstr(&path, r->worktree);
144 strbuf_complete(&path, '/');
145 pathlen = path.len;
146
147 /*
148 * Collect directories that have gone out of scope but also
149 * exist on disk, so there is some work to be done. We need to
150 * store the entries in a list before exploring, since that might
151 * expand the sparse-index again.
152 */
153 for (i = 0; i < r->index->cache_nr; i++) {
154 struct cache_entry *ce = r->index->cache[i];
155
156 if (S_ISSPARSEDIR(ce->ce_mode) &&
157 repo_file_exists(r, ce->name))
158 string_list_append(&sparse_dirs, ce->name);
159 }
160
161 for_each_string_list_item(item, &sparse_dirs) {
162 struct dir_struct dir = DIR_INIT;
163 struct pathspec p = { 0 };
164 struct strvec s = STRVEC_INIT;
165
166 strbuf_setlen(&path, pathlen);
167 strbuf_addstr(&path, item->string);
168
169 dir.flags |= DIR_SHOW_IGNORED_TOO;
170
171 setup_standard_excludes(&dir);
172 strvec_push(&s, path.buf);
173
174 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
175 fill_directory(&dir, r->index, &p);
176
177 if (dir.nr) {
178 warning(_("directory '%s' contains untracked files,"
179 " but is not in the sparse-checkout cone"),
180 item->string);
181 } else if (remove_dir_recursively(&path, 0)) {
182 /*
183 * Removal is "best effort". If something blocks
184 * the deletion, then continue with a warning.
185 */
186 warning(_("failed to remove directory '%s'"),
187 item->string);
188 }
189
0f03f04c
EN
190 strvec_clear(&s);
191 clear_pathspec(&p);
55dfcf95
DS
192 dir_clear(&dir);
193 }
194
195 string_list_clear(&sparse_dirs, 0);
196 strbuf_release(&path);
197
198 if (was_full)
199 ensure_full_index(r->index);
200}
201
e091228e 202static int update_working_directory(struct pattern_list *pl)
bab3c359 203{
f56f31af 204 enum update_sparsity_result result;
e091228e
DS
205 struct unpack_trees_options o;
206 struct lock_file lock_file = LOCK_INIT;
e091228e 207 struct repository *r = the_repository;
bab3c359 208
b5bfc08a
EN
209 /* If no branch has been checked out, there are no updates to make. */
210 if (is_index_unborn(r->index))
211 return UPDATE_SPARSITY_SUCCESS;
212
836e25c5
DS
213 r->index->sparse_checkout_patterns = pl;
214
e091228e
DS
215 memset(&o, 0, sizeof(o));
216 o.verbose_update = isatty(2);
e091228e 217 o.update = 1;
e091228e
DS
218 o.head_idx = -1;
219 o.src_index = r->index;
220 o.dst_index = r->index;
221 o.skip_sparse_checkout = 0;
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");
1147c56f 228 result = update_sparsity(&o, pl);
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
00408ade 385static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
be61fd11
EN
386 /* If not specified, use previous definition of cone mode */
387 if (*cone_mode == -1 && core_apply_sparse_checkout)
388 *cone_mode = core_sparse_checkout_cone;
389
390 /* Set cone/non-cone mode appropriately */
391 core_apply_sparse_checkout = 1;
2d95707a 392 if (*cone_mode == 1 || *cone_mode == -1) {
be61fd11 393 core_sparse_checkout_cone = 1;
00408ade 394 return MODE_CONE_PATTERNS;
be61fd11 395 }
00408ade
WS
396 core_sparse_checkout_cone = 0;
397 return MODE_ALL_PATTERNS;
398}
399
400static int update_modes(int *cone_mode, int *sparse_index)
401{
402 int mode, record_mode;
403
404 /* Determine if we need to record the mode; ensure sparse checkout on */
405 record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
406
407 mode = update_cone_mode(cone_mode);
be61fd11
EN
408 if (record_mode && set_config(mode))
409 return 1;
410
411 /* Set sparse-index/non-sparse-index mode if specified */
412 if (*sparse_index >= 0) {
413 if (set_sparse_index_config(the_repository, *sparse_index) < 0)
414 die(_("failed to modify sparse-index config"));
415
416 /* force an index rewrite */
417 repo_read_index(the_repository);
418 the_repository->index->updated_workdir = 1;
2d443389
DS
419
420 if (!*sparse_index)
421 ensure_full_index(the_repository->index);
be61fd11
EN
422 }
423
424 return 0;
425}
426
af09ce24 427static char const * const builtin_sparse_checkout_init_usage[] = {
959d670d 428 "git sparse-checkout init [--cone] [--[no-]sparse-index]",
af09ce24
DS
429 NULL
430};
431
432static struct sparse_checkout_init_opts {
433 int cone_mode;
122ba1f7 434 int sparse_index;
af09ce24
DS
435} init_opts;
436
1c3502b1 437static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
bab3c359
DS
438{
439 struct pattern_list pl;
440 char *sparse_filename;
bab3c359 441 int res;
d89f09c8 442 struct object_id oid;
416adc87 443 struct strbuf pattern = STRBUF_INIT;
bab3c359 444
af09ce24
DS
445 static struct option builtin_sparse_checkout_init_options[] = {
446 OPT_BOOL(0, "cone", &init_opts.cone_mode,
447 N_("initialize the sparse-checkout in cone mode")),
122ba1f7
DS
448 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
449 N_("toggle the use of a sparse index")),
af09ce24
DS
450 OPT_END(),
451 };
452
24fc2cde 453 setup_work_tree();
cff4e913 454 repo_read_index(the_repository);
cff4e913 455
be61fd11 456 init_opts.cone_mode = -1;
122ba1f7
DS
457 init_opts.sparse_index = -1;
458
ecd2d3ef 459 argc = parse_options(argc, argv, prefix,
af09ce24
DS
460 builtin_sparse_checkout_init_options,
461 builtin_sparse_checkout_init_usage, 0);
462
be61fd11 463 if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
bab3c359
DS
464 return 1;
465
466 memset(&pl, 0, sizeof(pl));
467
468 sparse_filename = get_sparse_checkout_filename();
1679d60b 469 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
bab3c359
DS
470
471 /* If we already have a sparse-checkout file, use it. */
472 if (res >= 0) {
473 free(sparse_filename);
416adc87 474 return update_working_directory(NULL);
bab3c359
DS
475 }
476
d850b7a5 477 if (repo_get_oid(the_repository, "HEAD", &oid)) {
416adc87 478 FILE *fp;
bab3c359 479
416adc87 480 /* assume we are in a fresh repo, but update the sparse-checkout file */
7f44842a
JT
481 if (safe_create_leading_directories(sparse_filename))
482 die(_("unable to create leading directories of %s"),
483 sparse_filename);
416adc87
DS
484 fp = xfopen(sparse_filename, "w");
485 if (!fp)
486 die(_("failed to open '%s'"), sparse_filename);
bab3c359 487
416adc87
DS
488 free(sparse_filename);
489 fprintf(fp, "/*\n!/*/\n");
490 fclose(fp);
d89f09c8
DS
491 return 0;
492 }
493
416adc87
DS
494 strbuf_addstr(&pattern, "/*");
495 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
496 strbuf_addstr(&pattern, "!/*/");
497 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
dcc5fd5f 498 pl.use_cone_patterns = init_opts.cone_mode;
416adc87
DS
499
500 return write_patterns_and_update(&pl);
bab3c359
DS
501}
502
af09ce24 503static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
f6039a94 504{
af09ce24
DS
505 struct pattern_entry *e = xmalloc(sizeof(*e));
506 e->patternlen = path->len;
507 e->pattern = strbuf_detach(path, NULL);
74318423 508 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
f6039a94 509
af09ce24 510 hashmap_add(&pl->recursive_hashmap, &e->ent);
f6039a94 511
af09ce24
DS
512 while (e->patternlen) {
513 char *slash = strrchr(e->pattern, '/');
514 char *oldpattern = e->pattern;
515 size_t newlen;
516
391c3a10 517 if (!slash || slash == e->pattern)
af09ce24
DS
518 break;
519
520 newlen = slash - e->pattern;
521 e = xmalloc(sizeof(struct pattern_entry));
522 e->patternlen = newlen;
523 e->pattern = xstrndup(oldpattern, newlen);
74318423 524 hashmap_entry_init(&e->ent, fspathhash(e->pattern));
af09ce24
DS
525
526 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
527 hashmap_add(&pl->parent_hashmap, &e->ent);
528 }
529}
530
531static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
532{
533 strbuf_trim(line);
534
535 strbuf_trim_trailing_dir_sep(line);
536
ef076599
DS
537 if (strbuf_normalize_path(line))
538 die(_("could not normalize path %s"), line->buf);
539
af09ce24
DS
540 if (!line->len)
541 return;
542
543 if (line->buf[0] != '/')
a91cc7fa 544 strbuf_insertstr(line, 0, "/");
af09ce24
DS
545
546 insert_recursive_pattern(pl, line);
f6039a94
DS
547}
548
6fb705ab 549static void add_patterns_from_input(struct pattern_list *pl,
1530ff35 550 int argc, const char **argv,
00408ade 551 FILE *file)
f6039a94 552{
f6039a94 553 int i;
af09ce24 554 if (core_sparse_checkout_cone) {
7bffca95
DS
555 struct strbuf line = STRBUF_INIT;
556
6fb705ab
DS
557 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
558 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
559 pl->use_cone_patterns = 1;
af09ce24 560
00408ade 561 if (file) {
bd64de42 562 struct strbuf unquoted = STRBUF_INIT;
00408ade 563 while (!strbuf_getline(&line, file)) {
bd64de42
DS
564 if (line.buf[0] == '"') {
565 strbuf_reset(&unquoted);
566 if (unquote_c_style(&unquoted, line.buf, NULL))
567 die(_("unable to unquote C-style string '%s'"),
568 line.buf);
569
570 strbuf_swap(&unquoted, &line);
571 }
572
6fb705ab 573 strbuf_to_cone_pattern(&line, pl);
bd64de42
DS
574 }
575
576 strbuf_release(&unquoted);
af09ce24
DS
577 } else {
578 for (i = 0; i < argc; i++) {
579 strbuf_setlen(&line, 0);
580 strbuf_addstr(&line, argv[i]);
6fb705ab 581 strbuf_to_cone_pattern(&line, pl);
af09ce24 582 }
7bffca95
DS
583 }
584 } else {
00408ade 585 if (file) {
af09ce24
DS
586 struct strbuf line = STRBUF_INIT;
587
00408ade 588 while (!strbuf_getline(&line, file)) {
af09ce24
DS
589 size_t len;
590 char *buf = strbuf_detach(&line, &len);
6fb705ab 591 add_pattern(buf, empty_base, 0, pl, 0);
af09ce24
DS
592 }
593 } else {
594 for (i = 0; i < argc; i++)
6fb705ab 595 add_pattern(argv[i], empty_base, 0, pl, 0);
af09ce24 596 }
7bffca95 597 }
6fb705ab
DS
598}
599
4bf0c06c
DS
600enum modify_type {
601 REPLACE,
2631dc87 602 ADD,
4bf0c06c
DS
603};
604
2631dc87 605static void add_patterns_cone_mode(int argc, const char **argv,
1530ff35
EN
606 struct pattern_list *pl,
607 int use_stdin)
2631dc87
DS
608{
609 struct strbuf buffer = STRBUF_INIT;
610 struct pattern_entry *pe;
611 struct hashmap_iter iter;
612 struct pattern_list existing;
613 char *sparse_filename = get_sparse_checkout_filename();
614
00408ade
WS
615 add_patterns_from_input(pl, argc, argv,
616 use_stdin ? stdin : NULL);
2631dc87
DS
617
618 memset(&existing, 0, sizeof(existing));
619 existing.use_cone_patterns = core_sparse_checkout_cone;
620
621 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
1679d60b 622 &existing, NULL, 0))
2631dc87
DS
623 die(_("unable to load existing sparse-checkout patterns"));
624 free(sparse_filename);
625
a3eca584
DS
626 if (!existing.use_cone_patterns)
627 die(_("existing sparse-checkout patterns do not use cone mode"));
628
2631dc87
DS
629 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
630 if (!hashmap_contains_parent(&pl->recursive_hashmap,
631 pe->pattern, &buffer) ||
632 !hashmap_contains_parent(&pl->parent_hashmap,
633 pe->pattern, &buffer)) {
634 strbuf_reset(&buffer);
635 strbuf_addstr(&buffer, pe->pattern);
636 insert_recursive_pattern(pl, &buffer);
637 }
638 }
639
640 clear_pattern_list(&existing);
641 strbuf_release(&buffer);
642}
643
644static void add_patterns_literal(int argc, const char **argv,
1530ff35
EN
645 struct pattern_list *pl,
646 int use_stdin)
2631dc87
DS
647{
648 char *sparse_filename = get_sparse_checkout_filename();
649 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
1679d60b 650 pl, NULL, 0))
2631dc87
DS
651 die(_("unable to load existing sparse-checkout patterns"));
652 free(sparse_filename);
00408ade 653 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
2631dc87
DS
654}
655
1530ff35
EN
656static int modify_pattern_list(int argc, const char **argv, int use_stdin,
657 enum modify_type m)
6fb705ab 658{
6fb705ab
DS
659 int result;
660 int changed_config = 0;
836e25c5 661 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
6fb705ab 662
2631dc87
DS
663 switch (m) {
664 case ADD:
665 if (core_sparse_checkout_cone)
1530ff35 666 add_patterns_cone_mode(argc, argv, pl, use_stdin);
2631dc87 667 else
1530ff35 668 add_patterns_literal(argc, argv, pl, use_stdin);
2631dc87
DS
669 break;
670
671 case REPLACE:
00408ade
WS
672 add_patterns_from_input(pl, argc, argv,
673 use_stdin ? stdin : NULL);
2631dc87
DS
674 break;
675 }
f6039a94
DS
676
677 if (!core_apply_sparse_checkout) {
678 set_config(MODE_ALL_PATTERNS);
679 core_apply_sparse_checkout = 1;
680 changed_config = 1;
681 }
682
836e25c5 683 result = write_patterns_and_update(pl);
f6039a94
DS
684
685 if (result && changed_config)
686 set_config(MODE_NO_PATTERNS);
687
836e25c5
DS
688 clear_pattern_list(pl);
689 free(pl);
f6039a94
DS
690 return result;
691}
692
4ce50436
EN
693static void sanitize_paths(int argc, const char **argv,
694 const char *prefix, int skip_checks)
bb8b5e9a 695{
4ce50436
EN
696 int i;
697
bb8b5e9a
EN
698 if (!argc)
699 return;
700
701 if (prefix && *prefix && core_sparse_checkout_cone) {
702 /*
703 * The args are not pathspecs, so unfortunately we
704 * cannot imitate how cmd_add() uses parse_pathspec().
705 */
bb8b5e9a
EN
706 int prefix_len = strlen(prefix);
707
708 for (i = 0; i < argc; i++)
709 argv[i] = prefix_path(prefix, prefix_len, argv[i]);
710 }
711
4ce50436
EN
712 if (skip_checks)
713 return;
714
bb8b5e9a
EN
715 if (prefix && *prefix && !core_sparse_checkout_cone)
716 die(_("please run from the toplevel directory in non-cone mode"));
717
8dd7c473
EN
718 if (core_sparse_checkout_cone) {
719 for (i = 0; i < argc; i++) {
720 if (argv[i][0] == '/')
721 die(_("specify directories rather than patterns (no leading slash)"));
722 if (argv[i][0] == '!')
723 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks"));
724 if (strpbrk(argv[i], "*?[]"))
725 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
726 }
727 }
728
4ce50436
EN
729 for (i = 0; i < argc; i++) {
730 struct cache_entry *ce;
731 struct index_state *index = the_repository->index;
732 int pos = index_name_pos(index, argv[i], strlen(argv[i]));
733
734 if (pos < 0)
735 continue;
736 ce = index->cache[pos];
737 if (S_ISSPARSEDIR(ce->ce_mode))
738 continue;
739
740 if (core_sparse_checkout_cone)
741 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
742 else
743 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]);
744 }
bb8b5e9a
EN
745}
746
0b624e03 747static char const * const builtin_sparse_checkout_add_usage[] = {
4ce50436 748 N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
0b624e03
EN
749 NULL
750};
751
752static struct sparse_checkout_add_opts {
4ce50436 753 int skip_checks;
0b624e03
EN
754 int use_stdin;
755} add_opts;
756
757static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
758{
759 static struct option builtin_sparse_checkout_add_options[] = {
4ce50436
EN
760 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
761 N_("skip some sanity checks on the given paths that might give false positives"),
762 PARSE_OPT_NONEG),
0b624e03
EN
763 OPT_BOOL(0, "stdin", &add_opts.use_stdin,
764 N_("read patterns from standard in")),
765 OPT_END(),
766 };
767
24fc2cde 768 setup_work_tree();
45c5e470
EN
769 if (!core_apply_sparse_checkout)
770 die(_("no sparse-checkout to add to"));
771
0b624e03
EN
772 repo_read_index(the_repository);
773
774 argc = parse_options(argc, argv, prefix,
775 builtin_sparse_checkout_add_options,
f8ab66f9 776 builtin_sparse_checkout_add_usage, 0);
0b624e03 777
4ce50436 778 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
bb8b5e9a 779
0b624e03
EN
780 return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
781}
782
783static char const * const builtin_sparse_checkout_set_usage[] = {
4ce50436 784 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
0b624e03
EN
785 NULL
786};
787
788static struct sparse_checkout_set_opts {
f2e3a218
EN
789 int cone_mode;
790 int sparse_index;
4ce50436 791 int skip_checks;
0b624e03
EN
792 int use_stdin;
793} set_opts;
794
795static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
4bf0c06c 796{
f2e3a218
EN
797 int default_patterns_nr = 2;
798 const char *default_patterns[] = {"/*", "!/*/", NULL};
799
4bf0c06c 800 static struct option builtin_sparse_checkout_set_options[] = {
f2e3a218
EN
801 OPT_BOOL(0, "cone", &set_opts.cone_mode,
802 N_("initialize the sparse-checkout in cone mode")),
803 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
804 N_("toggle the use of a sparse index")),
4ce50436
EN
805 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
806 N_("skip some sanity checks on the given paths that might give false positives"),
807 PARSE_OPT_NONEG),
f85751a1
EN
808 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
809 N_("read patterns from standard in"),
810 PARSE_OPT_NONEG),
4bf0c06c
DS
811 OPT_END(),
812 };
813
24fc2cde 814 setup_work_tree();
4bf0c06c 815 repo_read_index(the_repository);
4bf0c06c 816
f2e3a218
EN
817 set_opts.cone_mode = -1;
818 set_opts.sparse_index = -1;
819
4bf0c06c
DS
820 argc = parse_options(argc, argv, prefix,
821 builtin_sparse_checkout_set_options,
f8ab66f9 822 builtin_sparse_checkout_set_usage, 0);
4bf0c06c 823
f2e3a218
EN
824 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
825 return 1;
826
827 /*
828 * Cone mode automatically specifies the toplevel directory. For
829 * non-cone mode, if nothing is specified, manually select just the
830 * top-level directory (much as 'init' would do).
831 */
53ded839 832 if (!core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
f2e3a218
EN
833 argv = default_patterns;
834 argc = default_patterns_nr;
bb8b5e9a 835 } else {
4ce50436 836 sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
f2e3a218
EN
837 }
838
0b624e03 839 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
4bf0c06c
DS
840}
841
75d3bee1 842static char const * const builtin_sparse_checkout_reapply_usage[] = {
959d670d 843 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
75d3bee1
JK
844 NULL
845};
846
4e256731
EN
847static struct sparse_checkout_reapply_opts {
848 int cone_mode;
849 int sparse_index;
850} reapply_opts;
851
1c3502b1
SG
852static int sparse_checkout_reapply(int argc, const char **argv,
853 const char *prefix)
5644ca28 854{
75d3bee1 855 static struct option builtin_sparse_checkout_reapply_options[] = {
4e256731
EN
856 OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
857 N_("initialize the sparse-checkout in cone mode")),
858 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
859 N_("toggle the use of a sparse index")),
75d3bee1
JK
860 OPT_END(),
861 };
862
24fc2cde 863 setup_work_tree();
45c5e470
EN
864 if (!core_apply_sparse_checkout)
865 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
866
f748012e
EN
867 reapply_opts.cone_mode = -1;
868 reapply_opts.sparse_index = -1;
869
ecd2d3ef 870 argc = parse_options(argc, argv, prefix,
75d3bee1
JK
871 builtin_sparse_checkout_reapply_options,
872 builtin_sparse_checkout_reapply_usage, 0);
873
5644ca28 874 repo_read_index(the_repository);
4e256731 875
4e256731
EN
876 if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
877 return 1;
878
5644ca28
EN
879 return update_working_directory(NULL);
880}
881
75d3bee1 882static char const * const builtin_sparse_checkout_disable_usage[] = {
959d670d 883 "git sparse-checkout disable",
75d3bee1
JK
884 NULL
885};
886
1c3502b1
SG
887static int sparse_checkout_disable(int argc, const char **argv,
888 const char *prefix)
72918c1a 889{
75d3bee1
JK
890 static struct option builtin_sparse_checkout_disable_options[] = {
891 OPT_END(),
892 };
99dfa6f9
DS
893 struct pattern_list pl;
894 struct strbuf match_all = STRBUF_INIT;
72918c1a 895
45c5e470
EN
896 /*
897 * We do not exit early if !core_apply_sparse_checkout; due to the
898 * ability for users to manually muck things up between
899 * direct editing of .git/info/sparse-checkout
900 * running read-tree -m u HEAD or update-index --skip-worktree
901 * direct toggling of config options
902 * users might end up with an index with SKIP_WORKTREE bit set on
903 * some files and not know how to undo it. So, here we just
904 * forcibly return to a dense checkout regardless of initial state.
905 */
906
24fc2cde 907 setup_work_tree();
ecd2d3ef 908 argc = parse_options(argc, argv, prefix,
75d3bee1
JK
909 builtin_sparse_checkout_disable_options,
910 builtin_sparse_checkout_disable_usage, 0);
911
cff4e913 912 repo_read_index(the_repository);
cff4e913 913
99dfa6f9
DS
914 memset(&pl, 0, sizeof(pl));
915 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
916 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
917 pl.use_cone_patterns = 0;
918 core_apply_sparse_checkout = 1;
72918c1a 919
99dfa6f9
DS
920 strbuf_addstr(&match_all, "/*");
921 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
72918c1a 922
dcc5fd5f
DS
923 prepare_repo_settings(the_repository);
924 the_repository->settings.sparse_index = 0;
925
99dfa6f9 926 if (update_working_directory(&pl))
72918c1a
DS
927 die(_("error while refreshing working directory"));
928
99dfa6f9 929 clear_pattern_list(&pl);
72918c1a
DS
930 return set_config(MODE_NO_PATTERNS);
931}
932
00408ade
WS
933static char const * const builtin_sparse_checkout_check_rules_usage[] = {
934 N_("git sparse-checkout check-rules [-z] [--skip-checks]"
935 "[--[no-]cone] [--rules-file <file>]"),
936 NULL
937};
938
939static struct sparse_checkout_check_rules_opts {
940 int cone_mode;
941 int null_termination;
942 char *rules_file;
943} check_rules_opts;
944
945static int check_rules(struct pattern_list *pl, int null_terminated) {
946 struct strbuf line = STRBUF_INIT;
947 struct strbuf unquoted = STRBUF_INIT;
948 char *path;
949 int line_terminator = null_terminated ? 0 : '\n';
950 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
951 : strbuf_getline;
952 the_repository->index->sparse_checkout_patterns = pl;
953 while (!getline_fn(&line, stdin)) {
954 path = line.buf;
955 if (!null_terminated && line.buf[0] == '"') {
956 strbuf_reset(&unquoted);
957 if (unquote_c_style(&unquoted, line.buf, NULL))
958 die(_("unable to unquote C-style string '%s'"),
959 line.buf);
960
961 path = unquoted.buf;
962 }
963
964 if (path_in_sparse_checkout(path, the_repository->index))
965 write_name_quoted(path, stdout, line_terminator);
966 }
967 strbuf_release(&line);
968 strbuf_release(&unquoted);
969
970 return 0;
971}
972
973static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
974{
975 static struct option builtin_sparse_checkout_check_rules_options[] = {
976 OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
977 N_("terminate input and output files by a NUL character")),
978 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
979 N_("when used with --rules-file interpret patterns as cone mode patterns")),
980 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
981 N_("use patterns in <file> instead of the current ones.")),
982 OPT_END(),
983 };
984
985 FILE *fp;
986 int ret;
987 struct pattern_list pl = {0};
988 char *sparse_filename;
989 check_rules_opts.cone_mode = -1;
990
991 argc = parse_options(argc, argv, prefix,
992 builtin_sparse_checkout_check_rules_options,
f8ab66f9 993 builtin_sparse_checkout_check_rules_usage, 0);
00408ade
WS
994
995 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
996 check_rules_opts.cone_mode = 1;
997
998 update_cone_mode(&check_rules_opts.cone_mode);
999 pl.use_cone_patterns = core_sparse_checkout_cone;
1000 if (check_rules_opts.rules_file) {
1001 fp = xfopen(check_rules_opts.rules_file, "r");
1002 add_patterns_from_input(&pl, argc, argv, fp);
1003 fclose(fp);
1004 } else {
1005 sparse_filename = get_sparse_checkout_filename();
1006 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1007 NULL, 0))
1008 die(_("unable to load existing sparse-checkout patterns"));
1009 free(sparse_filename);
1010 }
1011
1012 ret = check_rules(&pl, check_rules_opts.null_termination);
1013 clear_pattern_list(&pl);
1014 return ret;
1015}
1016
94c0956b
DS
1017int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
1018{
1c3502b1
SG
1019 parse_opt_subcommand_fn *fn = NULL;
1020 struct option builtin_sparse_checkout_options[] = {
1021 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1022 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1023 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1024 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1025 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1026 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
00408ade 1027 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
94c0956b
DS
1028 OPT_END(),
1029 };
1030
94c0956b
DS
1031 argc = parse_options(argc, argv, prefix,
1032 builtin_sparse_checkout_options,
1c3502b1 1033 builtin_sparse_checkout_usage, 0);
94c0956b
DS
1034
1035 git_config(git_default_config, NULL);
1036
598b1e7d
DS
1037 prepare_repo_settings(the_repository);
1038 the_repository->settings.command_requires_full_index = 0;
1039
1c3502b1 1040 return fn(argc, argv, prefix);
94c0956b 1041}