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