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