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