]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/sparse-checkout.c
Sync with 2.25.5
[thirdparty/git.git] / builtin / sparse-checkout.c
CommitLineData
94c0956b
DS
1#include "builtin.h"
2#include "config.h"
3#include "dir.h"
4#include "parse-options.h"
5#include "pathspec.h"
6#include "repository.h"
7#include "run-command.h"
8#include "strbuf.h"
af09ce24 9#include "string-list.h"
e091228e
DS
10#include "cache.h"
11#include "cache-tree.h"
12#include "lockfile.h"
13#include "resolve-undo.h"
14#include "unpack-trees.h"
cff4e913 15#include "wt-status.h"
d585f0e7 16#include "quote.h"
94c0956b 17
416adc87
DS
18static const char *empty_base = "";
19
94c0956b 20static char const * const builtin_sparse_checkout_usage[] = {
2631dc87 21 N_("git sparse-checkout (init|list|set|add|disable) <options>"),
94c0956b
DS
22 NULL
23};
24
25static char *get_sparse_checkout_filename(void)
26{
27 return git_pathdup("info/sparse-checkout");
28}
29
30static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
31{
32 int i;
33
34 for (i = 0; i < pl->nr; i++) {
35 struct path_pattern *p = pl->patterns[i];
36
37 if (p->flags & PATTERN_FLAG_NEGATIVE)
38 fprintf(fp, "!");
39
40 fprintf(fp, "%s", p->pattern);
41
42 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
43 fprintf(fp, "/");
44
45 fprintf(fp, "\n");
46 }
47}
48
49static int sparse_checkout_list(int argc, const char **argv)
50{
51 struct pattern_list pl;
52 char *sparse_filename;
53 int res;
54
55 memset(&pl, 0, sizeof(pl));
56
de11951b
DS
57 pl.use_cone_patterns = core_sparse_checkout_cone;
58
94c0956b
DS
59 sparse_filename = get_sparse_checkout_filename();
60 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
61 free(sparse_filename);
62
63 if (res < 0) {
64 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
65 return 0;
66 }
67
de11951b
DS
68 if (pl.use_cone_patterns) {
69 int i;
70 struct pattern_entry *pe;
71 struct hashmap_iter iter;
72 struct string_list sl = STRING_LIST_INIT_DUP;
73
74 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
75 /* pe->pattern starts with "/", skip it */
76 string_list_insert(&sl, pe->pattern + 1);
77 }
78
79 string_list_sort(&sl);
80
e55682ea
DS
81 for (i = 0; i < sl.nr; i++) {
82 quote_c_style(sl.items[i].string, NULL, stdout, 0);
83 printf("\n");
84 }
de11951b
DS
85
86 return 0;
87 }
88
94c0956b
DS
89 write_patterns_to_file(stdout, &pl);
90 clear_pattern_list(&pl);
91
92 return 0;
93}
94
e091228e 95static int update_working_directory(struct pattern_list *pl)
bab3c359 96{
bab3c359 97 int result = 0;
e091228e
DS
98 struct unpack_trees_options o;
99 struct lock_file lock_file = LOCK_INIT;
100 struct object_id oid;
101 struct tree *tree;
102 struct tree_desc t;
103 struct repository *r = the_repository;
bab3c359 104
e091228e
DS
105 if (repo_read_index_unmerged(r))
106 die(_("you need to resolve your current index first"));
107
108 if (get_oid("HEAD", &oid))
109 return 0;
110
111 tree = parse_tree_indirect(&oid);
112 parse_tree(tree);
113 init_tree_desc(&t, tree->buffer, tree->size);
114
115 memset(&o, 0, sizeof(o));
116 o.verbose_update = isatty(2);
117 o.merge = 1;
118 o.update = 1;
119 o.fn = oneway_merge;
120 o.head_idx = -1;
121 o.src_index = r->index;
122 o.dst_index = r->index;
123 o.skip_sparse_checkout = 0;
124 o.pl = pl;
125 o.keep_pattern_list = !!pl;
126
127 resolve_undo_clear_index(r->index);
128 setup_work_tree();
129
130 cache_tree_free(&r->index->cache_tree);
131
132 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
133
134 core_apply_sparse_checkout = 1;
135 result = unpack_trees(1, &t, &o);
136
137 if (!result) {
138 prime_cache_tree(r, r->index, tree);
139 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
140 } else
141 rollback_lock_file(&lock_file);
bab3c359 142
bab3c359
DS
143 return result;
144}
145
d585f0e7
DS
146static char *escaped_pattern(char *pattern)
147{
148 char *p = pattern;
149 struct strbuf final = STRBUF_INIT;
150
151 while (*p) {
e53ffe27 152 if (is_glob_special(*p))
d585f0e7
DS
153 strbuf_addch(&final, '\\');
154
155 strbuf_addch(&final, *p);
156 p++;
157 }
158
159 return strbuf_detach(&final, NULL);
160}
161
af09ce24
DS
162static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
163{
164 int i;
165 struct pattern_entry *pe;
166 struct hashmap_iter iter;
167 struct string_list sl = STRING_LIST_INIT_DUP;
e9de487a 168 struct strbuf parent_pattern = STRBUF_INIT;
af09ce24 169
e9de487a
DS
170 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
171 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
172 continue;
173
174 if (!hashmap_contains_parent(&pl->recursive_hashmap,
175 pe->pattern,
176 &parent_pattern))
177 string_list_insert(&sl, pe->pattern);
178 }
af09ce24
DS
179
180 string_list_sort(&sl);
181 string_list_remove_duplicates(&sl, 0);
182
183 fprintf(fp, "/*\n!/*/\n");
184
185 for (i = 0; i < sl.nr; i++) {
d585f0e7 186 char *pattern = escaped_pattern(sl.items[i].string);
af09ce24
DS
187
188 if (strlen(pattern))
189 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
d585f0e7 190 free(pattern);
af09ce24
DS
191 }
192
193 string_list_clear(&sl, 0);
194
e9de487a
DS
195 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
196 if (!hashmap_contains_parent(&pl->recursive_hashmap,
197 pe->pattern,
198 &parent_pattern))
199 string_list_insert(&sl, pe->pattern);
200 }
201
202 strbuf_release(&parent_pattern);
af09ce24
DS
203
204 string_list_sort(&sl);
205 string_list_remove_duplicates(&sl, 0);
206
207 for (i = 0; i < sl.nr; i++) {
d585f0e7 208 char *pattern = escaped_pattern(sl.items[i].string);
af09ce24 209 fprintf(fp, "%s/\n", pattern);
d585f0e7 210 free(pattern);
af09ce24
DS
211 }
212}
213
214static int write_patterns_and_update(struct pattern_list *pl)
215{
216 char *sparse_filename;
217 FILE *fp;
fb10ca5b
DS
218 int fd;
219 struct lock_file lk = LOCK_INIT;
e091228e
DS
220 int result;
221
fb10ca5b 222 sparse_filename = get_sparse_checkout_filename();
3c754067
DS
223
224 if (safe_create_leading_directories(sparse_filename))
225 die(_("failed to create directory for sparse-checkout file"));
226
fb10ca5b
DS
227 fd = hold_lock_file_for_update(&lk, sparse_filename,
228 LOCK_DIE_ON_ERROR);
e091228e 229
fb10ca5b 230 result = update_working_directory(pl);
e091228e 231 if (result) {
fb10ca5b
DS
232 rollback_lock_file(&lk);
233 free(sparse_filename);
e091228e
DS
234 clear_pattern_list(pl);
235 update_working_directory(NULL);
236 return result;
237 }
af09ce24 238
fb10ca5b 239 fp = xfdopen(fd, "w");
af09ce24
DS
240
241 if (core_sparse_checkout_cone)
242 write_cone_to_file(fp, pl);
243 else
244 write_patterns_to_file(fp, pl);
245
fb10ca5b
DS
246 fflush(fp);
247 commit_lock_file(&lk);
e091228e 248
af09ce24 249 free(sparse_filename);
e091228e 250 clear_pattern_list(pl);
af09ce24 251
e091228e 252 return 0;
af09ce24
DS
253}
254
bab3c359
DS
255enum sparse_checkout_mode {
256 MODE_NO_PATTERNS = 0,
257 MODE_ALL_PATTERNS = 1,
af09ce24 258 MODE_CONE_PATTERNS = 2,
bab3c359
DS
259};
260
261static int set_config(enum sparse_checkout_mode mode)
262{
263 const char *config_path;
264
265 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
266 error(_("failed to set extensions.worktreeConfig setting"));
267 return 1;
268 }
269
270 config_path = git_path("config.worktree");
271 git_config_set_in_file_gently(config_path,
272 "core.sparseCheckout",
273 mode ? "true" : NULL);
274
af09ce24
DS
275 git_config_set_in_file_gently(config_path,
276 "core.sparseCheckoutCone",
277 mode == MODE_CONE_PATTERNS ? "true" : NULL);
278
bab3c359
DS
279 return 0;
280}
281
af09ce24
DS
282static char const * const builtin_sparse_checkout_init_usage[] = {
283 N_("git sparse-checkout init [--cone]"),
284 NULL
285};
286
287static struct sparse_checkout_init_opts {
288 int cone_mode;
289} init_opts;
290
bab3c359
DS
291static int sparse_checkout_init(int argc, const char **argv)
292{
293 struct pattern_list pl;
294 char *sparse_filename;
bab3c359 295 int res;
d89f09c8 296 struct object_id oid;
af09ce24 297 int mode;
416adc87 298 struct strbuf pattern = STRBUF_INIT;
bab3c359 299
af09ce24
DS
300 static struct option builtin_sparse_checkout_init_options[] = {
301 OPT_BOOL(0, "cone", &init_opts.cone_mode,
302 N_("initialize the sparse-checkout in cone mode")),
303 OPT_END(),
304 };
305
cff4e913
DS
306 repo_read_index(the_repository);
307 require_clean_work_tree(the_repository,
308 N_("initialize sparse-checkout"), NULL, 1, 0);
309
af09ce24
DS
310 argc = parse_options(argc, argv, NULL,
311 builtin_sparse_checkout_init_options,
312 builtin_sparse_checkout_init_usage, 0);
313
e091228e
DS
314 if (init_opts.cone_mode) {
315 mode = MODE_CONE_PATTERNS;
316 core_sparse_checkout_cone = 1;
317 } else
318 mode = MODE_ALL_PATTERNS;
af09ce24
DS
319
320 if (set_config(mode))
bab3c359
DS
321 return 1;
322
323 memset(&pl, 0, sizeof(pl));
324
325 sparse_filename = get_sparse_checkout_filename();
326 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
327
328 /* If we already have a sparse-checkout file, use it. */
329 if (res >= 0) {
330 free(sparse_filename);
416adc87
DS
331 core_apply_sparse_checkout = 1;
332 return update_working_directory(NULL);
bab3c359
DS
333 }
334
416adc87
DS
335 if (get_oid("HEAD", &oid)) {
336 FILE *fp;
bab3c359 337
416adc87
DS
338 /* assume we are in a fresh repo, but update the sparse-checkout file */
339 fp = xfopen(sparse_filename, "w");
340 if (!fp)
341 die(_("failed to open '%s'"), sparse_filename);
bab3c359 342
416adc87
DS
343 free(sparse_filename);
344 fprintf(fp, "/*\n!/*/\n");
345 fclose(fp);
d89f09c8
DS
346 return 0;
347 }
348
416adc87
DS
349 strbuf_addstr(&pattern, "/*");
350 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
351 strbuf_addstr(&pattern, "!/*/");
352 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
353
354 return write_patterns_and_update(&pl);
bab3c359
DS
355}
356
af09ce24 357static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
f6039a94 358{
af09ce24
DS
359 struct pattern_entry *e = xmalloc(sizeof(*e));
360 e->patternlen = path->len;
361 e->pattern = strbuf_detach(path, NULL);
190a65f9
DS
362 hashmap_entry_init(&e->ent,
363 ignore_case ?
364 strihash(e->pattern) :
365 strhash(e->pattern));
f6039a94 366
af09ce24 367 hashmap_add(&pl->recursive_hashmap, &e->ent);
f6039a94 368
af09ce24
DS
369 while (e->patternlen) {
370 char *slash = strrchr(e->pattern, '/');
371 char *oldpattern = e->pattern;
372 size_t newlen;
373
374 if (slash == e->pattern)
375 break;
376
377 newlen = slash - e->pattern;
378 e = xmalloc(sizeof(struct pattern_entry));
379 e->patternlen = newlen;
380 e->pattern = xstrndup(oldpattern, newlen);
190a65f9
DS
381 hashmap_entry_init(&e->ent,
382 ignore_case ?
383 strihash(e->pattern) :
384 strhash(e->pattern));
af09ce24
DS
385
386 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
387 hashmap_add(&pl->parent_hashmap, &e->ent);
388 }
389}
390
391static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
392{
393 strbuf_trim(line);
394
395 strbuf_trim_trailing_dir_sep(line);
396
ef076599
DS
397 if (strbuf_normalize_path(line))
398 die(_("could not normalize path %s"), line->buf);
399
af09ce24
DS
400 if (!line->len)
401 return;
402
403 if (line->buf[0] != '/')
a91cc7fa 404 strbuf_insertstr(line, 0, "/");
af09ce24
DS
405
406 insert_recursive_pattern(pl, line);
f6039a94
DS
407}
408
7bffca95 409static char const * const builtin_sparse_checkout_set_usage[] = {
2631dc87 410 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
7bffca95
DS
411 NULL
412};
413
414static struct sparse_checkout_set_opts {
415 int use_stdin;
416} set_opts;
417
6fb705ab
DS
418static void add_patterns_from_input(struct pattern_list *pl,
419 int argc, const char **argv)
f6039a94 420{
f6039a94 421 int i;
af09ce24 422 if (core_sparse_checkout_cone) {
7bffca95
DS
423 struct strbuf line = STRBUF_INIT;
424
6fb705ab
DS
425 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
426 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
427 pl->use_cone_patterns = 1;
af09ce24
DS
428
429 if (set_opts.use_stdin) {
bd64de42
DS
430 struct strbuf unquoted = STRBUF_INIT;
431 while (!strbuf_getline(&line, stdin)) {
432 if (line.buf[0] == '"') {
433 strbuf_reset(&unquoted);
434 if (unquote_c_style(&unquoted, line.buf, NULL))
435 die(_("unable to unquote C-style string '%s'"),
436 line.buf);
437
438 strbuf_swap(&unquoted, &line);
439 }
440
6fb705ab 441 strbuf_to_cone_pattern(&line, pl);
bd64de42
DS
442 }
443
444 strbuf_release(&unquoted);
af09ce24
DS
445 } else {
446 for (i = 0; i < argc; i++) {
447 strbuf_setlen(&line, 0);
448 strbuf_addstr(&line, argv[i]);
6fb705ab 449 strbuf_to_cone_pattern(&line, pl);
af09ce24 450 }
7bffca95
DS
451 }
452 } else {
af09ce24
DS
453 if (set_opts.use_stdin) {
454 struct strbuf line = STRBUF_INIT;
455
456 while (!strbuf_getline(&line, stdin)) {
457 size_t len;
458 char *buf = strbuf_detach(&line, &len);
6fb705ab 459 add_pattern(buf, empty_base, 0, pl, 0);
af09ce24
DS
460 }
461 } else {
462 for (i = 0; i < argc; i++)
6fb705ab 463 add_pattern(argv[i], empty_base, 0, pl, 0);
af09ce24 464 }
7bffca95 465 }
6fb705ab
DS
466}
467
4bf0c06c
DS
468enum modify_type {
469 REPLACE,
2631dc87 470 ADD,
4bf0c06c
DS
471};
472
2631dc87
DS
473static void add_patterns_cone_mode(int argc, const char **argv,
474 struct pattern_list *pl)
475{
476 struct strbuf buffer = STRBUF_INIT;
477 struct pattern_entry *pe;
478 struct hashmap_iter iter;
479 struct pattern_list existing;
480 char *sparse_filename = get_sparse_checkout_filename();
481
482 add_patterns_from_input(pl, argc, argv);
483
484 memset(&existing, 0, sizeof(existing));
485 existing.use_cone_patterns = core_sparse_checkout_cone;
486
487 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
488 &existing, NULL))
489 die(_("unable to load existing sparse-checkout patterns"));
490 free(sparse_filename);
491
492 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
493 if (!hashmap_contains_parent(&pl->recursive_hashmap,
494 pe->pattern, &buffer) ||
495 !hashmap_contains_parent(&pl->parent_hashmap,
496 pe->pattern, &buffer)) {
497 strbuf_reset(&buffer);
498 strbuf_addstr(&buffer, pe->pattern);
499 insert_recursive_pattern(pl, &buffer);
500 }
501 }
502
503 clear_pattern_list(&existing);
504 strbuf_release(&buffer);
505}
506
507static void add_patterns_literal(int argc, const char **argv,
508 struct pattern_list *pl)
509{
510 char *sparse_filename = get_sparse_checkout_filename();
511 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
512 pl, NULL))
513 die(_("unable to load existing sparse-checkout patterns"));
514 free(sparse_filename);
515 add_patterns_from_input(pl, argc, argv);
516}
517
4bf0c06c 518static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
6fb705ab 519{
6fb705ab
DS
520 int result;
521 int changed_config = 0;
4bf0c06c 522 struct pattern_list pl;
6fb705ab
DS
523 memset(&pl, 0, sizeof(pl));
524
2631dc87
DS
525 switch (m) {
526 case ADD:
527 if (core_sparse_checkout_cone)
528 add_patterns_cone_mode(argc, argv, &pl);
529 else
530 add_patterns_literal(argc, argv, &pl);
531 break;
532
533 case REPLACE:
534 add_patterns_from_input(&pl, argc, argv);
535 break;
536 }
f6039a94
DS
537
538 if (!core_apply_sparse_checkout) {
539 set_config(MODE_ALL_PATTERNS);
540 core_apply_sparse_checkout = 1;
541 changed_config = 1;
542 }
543
544 result = write_patterns_and_update(&pl);
545
546 if (result && changed_config)
547 set_config(MODE_NO_PATTERNS);
548
549 clear_pattern_list(&pl);
550 return result;
551}
552
2631dc87
DS
553static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
554 enum modify_type m)
4bf0c06c
DS
555{
556 static struct option builtin_sparse_checkout_set_options[] = {
557 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
558 N_("read patterns from standard in")),
559 OPT_END(),
560 };
561
562 repo_read_index(the_repository);
563 require_clean_work_tree(the_repository,
564 N_("set sparse-checkout patterns"), NULL, 1, 0);
565
566 argc = parse_options(argc, argv, prefix,
567 builtin_sparse_checkout_set_options,
568 builtin_sparse_checkout_set_usage,
569 PARSE_OPT_KEEP_UNKNOWN);
570
2631dc87 571 return modify_pattern_list(argc, argv, m);
4bf0c06c
DS
572}
573
72918c1a
DS
574static int sparse_checkout_disable(int argc, const char **argv)
575{
99dfa6f9
DS
576 struct pattern_list pl;
577 struct strbuf match_all = STRBUF_INIT;
72918c1a 578
cff4e913
DS
579 repo_read_index(the_repository);
580 require_clean_work_tree(the_repository,
581 N_("disable sparse-checkout"), NULL, 1, 0);
582
99dfa6f9
DS
583 memset(&pl, 0, sizeof(pl));
584 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
585 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
586 pl.use_cone_patterns = 0;
587 core_apply_sparse_checkout = 1;
72918c1a 588
99dfa6f9
DS
589 strbuf_addstr(&match_all, "/*");
590 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
72918c1a 591
99dfa6f9 592 if (update_working_directory(&pl))
72918c1a
DS
593 die(_("error while refreshing working directory"));
594
99dfa6f9 595 clear_pattern_list(&pl);
72918c1a
DS
596 return set_config(MODE_NO_PATTERNS);
597}
598
94c0956b
DS
599int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
600{
601 static struct option builtin_sparse_checkout_options[] = {
602 OPT_END(),
603 };
604
605 if (argc == 2 && !strcmp(argv[1], "-h"))
606 usage_with_options(builtin_sparse_checkout_usage,
607 builtin_sparse_checkout_options);
608
609 argc = parse_options(argc, argv, prefix,
610 builtin_sparse_checkout_options,
611 builtin_sparse_checkout_usage,
612 PARSE_OPT_STOP_AT_NON_OPTION);
613
614 git_config(git_default_config, NULL);
615
616 if (argc > 0) {
617 if (!strcmp(argv[0], "list"))
618 return sparse_checkout_list(argc, argv);
bab3c359
DS
619 if (!strcmp(argv[0], "init"))
620 return sparse_checkout_init(argc, argv);
f6039a94 621 if (!strcmp(argv[0], "set"))
2631dc87
DS
622 return sparse_checkout_set(argc, argv, prefix, REPLACE);
623 if (!strcmp(argv[0], "add"))
624 return sparse_checkout_set(argc, argv, prefix, ADD);
72918c1a
DS
625 if (!strcmp(argv[0], "disable"))
626 return sparse_checkout_disable(argc, argv);
94c0956b
DS
627 }
628
629 usage_with_options(builtin_sparse_checkout_usage,
630 builtin_sparse_checkout_options);
631}