add: include magic part of pathspec on --refresh error
[git/debian.git] / builtin / sparse-checkout.c
blob2306a9ad98e08a0c9d5240cdd21d1caf9669eb27
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"
18 static const char *empty_base = "";
20 static char const * const builtin_sparse_checkout_usage[] = {
21 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
22 NULL
25 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
27 int i;
29 for (i = 0; i < pl->nr; i++) {
30 struct path_pattern *p = pl->patterns[i];
32 if (p->flags & PATTERN_FLAG_NEGATIVE)
33 fprintf(fp, "!");
35 fprintf(fp, "%s", p->pattern);
37 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
38 fprintf(fp, "/");
40 fprintf(fp, "\n");
44 static char const * const builtin_sparse_checkout_list_usage[] = {
45 N_("git sparse-checkout list"),
46 NULL
49 static int sparse_checkout_list(int argc, const char **argv)
51 static struct option builtin_sparse_checkout_list_options[] = {
52 OPT_END(),
54 struct pattern_list pl;
55 char *sparse_filename;
56 int res;
58 argc = parse_options(argc, argv, NULL,
59 builtin_sparse_checkout_list_options,
60 builtin_sparse_checkout_list_usage, 0);
62 memset(&pl, 0, sizeof(pl));
64 pl.use_cone_patterns = core_sparse_checkout_cone;
66 sparse_filename = get_sparse_checkout_filename();
67 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
68 free(sparse_filename);
70 if (res < 0) {
71 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
72 return 0;
75 if (pl.use_cone_patterns) {
76 int i;
77 struct pattern_entry *pe;
78 struct hashmap_iter iter;
79 struct string_list sl = STRING_LIST_INIT_DUP;
81 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
82 /* pe->pattern starts with "/", skip it */
83 string_list_insert(&sl, pe->pattern + 1);
86 string_list_sort(&sl);
88 for (i = 0; i < sl.nr; i++) {
89 quote_c_style(sl.items[i].string, NULL, stdout, 0);
90 printf("\n");
93 return 0;
96 write_patterns_to_file(stdout, &pl);
97 clear_pattern_list(&pl);
99 return 0;
102 static int update_working_directory(struct pattern_list *pl)
104 enum update_sparsity_result result;
105 struct unpack_trees_options o;
106 struct lock_file lock_file = LOCK_INIT;
107 struct repository *r = the_repository;
109 /* If no branch has been checked out, there are no updates to make. */
110 if (is_index_unborn(r->index))
111 return UPDATE_SPARSITY_SUCCESS;
113 memset(&o, 0, sizeof(o));
114 o.verbose_update = isatty(2);
115 o.update = 1;
116 o.head_idx = -1;
117 o.src_index = r->index;
118 o.dst_index = r->index;
119 o.skip_sparse_checkout = 0;
120 o.pl = pl;
122 setup_work_tree();
124 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
126 setup_unpack_trees_porcelain(&o, "sparse-checkout");
127 result = update_sparsity(&o);
128 clear_unpack_trees_porcelain(&o);
130 if (result == UPDATE_SPARSITY_WARNINGS)
132 * We don't do any special handling of warnings from untracked
133 * files in the way or dirty entries that can't be removed.
135 result = UPDATE_SPARSITY_SUCCESS;
136 if (result == UPDATE_SPARSITY_SUCCESS)
137 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
138 else
139 rollback_lock_file(&lock_file);
141 return result;
144 static char *escaped_pattern(char *pattern)
146 char *p = pattern;
147 struct strbuf final = STRBUF_INIT;
149 while (*p) {
150 if (is_glob_special(*p))
151 strbuf_addch(&final, '\\');
153 strbuf_addch(&final, *p);
154 p++;
157 return strbuf_detach(&final, NULL);
160 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
162 int i;
163 struct pattern_entry *pe;
164 struct hashmap_iter iter;
165 struct string_list sl = STRING_LIST_INIT_DUP;
166 struct strbuf parent_pattern = STRBUF_INIT;
168 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
169 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
170 continue;
172 if (!hashmap_contains_parent(&pl->recursive_hashmap,
173 pe->pattern,
174 &parent_pattern))
175 string_list_insert(&sl, pe->pattern);
178 string_list_sort(&sl);
179 string_list_remove_duplicates(&sl, 0);
181 fprintf(fp, "/*\n!/*/\n");
183 for (i = 0; i < sl.nr; i++) {
184 char *pattern = escaped_pattern(sl.items[i].string);
186 if (strlen(pattern))
187 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
188 free(pattern);
191 string_list_clear(&sl, 0);
193 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
194 if (!hashmap_contains_parent(&pl->recursive_hashmap,
195 pe->pattern,
196 &parent_pattern))
197 string_list_insert(&sl, pe->pattern);
200 strbuf_release(&parent_pattern);
202 string_list_sort(&sl);
203 string_list_remove_duplicates(&sl, 0);
205 for (i = 0; i < sl.nr; i++) {
206 char *pattern = escaped_pattern(sl.items[i].string);
207 fprintf(fp, "%s/\n", pattern);
208 free(pattern);
212 static int write_patterns_and_update(struct pattern_list *pl)
214 char *sparse_filename;
215 FILE *fp;
216 int fd;
217 struct lock_file lk = LOCK_INIT;
218 int result;
220 sparse_filename = get_sparse_checkout_filename();
222 if (safe_create_leading_directories(sparse_filename))
223 die(_("failed to create directory for sparse-checkout file"));
225 fd = hold_lock_file_for_update(&lk, sparse_filename,
226 LOCK_DIE_ON_ERROR);
228 result = update_working_directory(pl);
229 if (result) {
230 rollback_lock_file(&lk);
231 free(sparse_filename);
232 clear_pattern_list(pl);
233 update_working_directory(NULL);
234 return result;
237 fp = xfdopen(fd, "w");
239 if (core_sparse_checkout_cone)
240 write_cone_to_file(fp, pl);
241 else
242 write_patterns_to_file(fp, pl);
244 fflush(fp);
245 commit_lock_file(&lk);
247 free(sparse_filename);
248 clear_pattern_list(pl);
250 return 0;
253 enum sparse_checkout_mode {
254 MODE_NO_PATTERNS = 0,
255 MODE_ALL_PATTERNS = 1,
256 MODE_CONE_PATTERNS = 2,
259 static int set_config(enum sparse_checkout_mode mode)
261 const char *config_path;
263 if (upgrade_repository_format(1) < 0)
264 die(_("unable to upgrade repository format to enable worktreeConfig"));
265 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
266 error(_("failed to set extensions.worktreeConfig setting"));
267 return 1;
270 config_path = git_path("config.worktree");
271 git_config_set_in_file_gently(config_path,
272 "core.sparseCheckout",
273 mode ? "true" : NULL);
275 git_config_set_in_file_gently(config_path,
276 "core.sparseCheckoutCone",
277 mode == MODE_CONE_PATTERNS ? "true" : NULL);
279 return 0;
282 static char const * const builtin_sparse_checkout_init_usage[] = {
283 N_("git sparse-checkout init [--cone]"),
284 NULL
287 static struct sparse_checkout_init_opts {
288 int cone_mode;
289 } init_opts;
291 static int sparse_checkout_init(int argc, const char **argv)
293 struct pattern_list pl;
294 char *sparse_filename;
295 int res;
296 struct object_id oid;
297 int mode;
298 struct strbuf pattern = STRBUF_INIT;
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(),
306 repo_read_index(the_repository);
308 argc = parse_options(argc, argv, NULL,
309 builtin_sparse_checkout_init_options,
310 builtin_sparse_checkout_init_usage, 0);
312 if (init_opts.cone_mode) {
313 mode = MODE_CONE_PATTERNS;
314 core_sparse_checkout_cone = 1;
315 } else
316 mode = MODE_ALL_PATTERNS;
318 if (set_config(mode))
319 return 1;
321 memset(&pl, 0, sizeof(pl));
323 sparse_filename = get_sparse_checkout_filename();
324 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
326 /* If we already have a sparse-checkout file, use it. */
327 if (res >= 0) {
328 free(sparse_filename);
329 core_apply_sparse_checkout = 1;
330 return update_working_directory(NULL);
333 if (get_oid("HEAD", &oid)) {
334 FILE *fp;
336 /* assume we are in a fresh repo, but update the sparse-checkout file */
337 fp = xfopen(sparse_filename, "w");
338 if (!fp)
339 die(_("failed to open '%s'"), sparse_filename);
341 free(sparse_filename);
342 fprintf(fp, "/*\n!/*/\n");
343 fclose(fp);
344 return 0;
347 strbuf_addstr(&pattern, "/*");
348 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
349 strbuf_addstr(&pattern, "!/*/");
350 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
352 return write_patterns_and_update(&pl);
355 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
357 struct pattern_entry *e = xmalloc(sizeof(*e));
358 e->patternlen = path->len;
359 e->pattern = strbuf_detach(path, NULL);
360 hashmap_entry_init(&e->ent,
361 ignore_case ?
362 strihash(e->pattern) :
363 strhash(e->pattern));
365 hashmap_add(&pl->recursive_hashmap, &e->ent);
367 while (e->patternlen) {
368 char *slash = strrchr(e->pattern, '/');
369 char *oldpattern = e->pattern;
370 size_t newlen;
372 if (slash == e->pattern)
373 break;
375 newlen = slash - e->pattern;
376 e = xmalloc(sizeof(struct pattern_entry));
377 e->patternlen = newlen;
378 e->pattern = xstrndup(oldpattern, newlen);
379 hashmap_entry_init(&e->ent,
380 ignore_case ?
381 strihash(e->pattern) :
382 strhash(e->pattern));
384 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
385 hashmap_add(&pl->parent_hashmap, &e->ent);
389 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
391 strbuf_trim(line);
393 strbuf_trim_trailing_dir_sep(line);
395 if (strbuf_normalize_path(line))
396 die(_("could not normalize path %s"), line->buf);
398 if (!line->len)
399 return;
401 if (line->buf[0] != '/')
402 strbuf_insertstr(line, 0, "/");
404 insert_recursive_pattern(pl, line);
407 static char const * const builtin_sparse_checkout_set_usage[] = {
408 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
409 NULL
412 static struct sparse_checkout_set_opts {
413 int use_stdin;
414 } set_opts;
416 static void add_patterns_from_input(struct pattern_list *pl,
417 int argc, const char **argv)
419 int i;
420 if (core_sparse_checkout_cone) {
421 struct strbuf line = STRBUF_INIT;
423 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
424 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
425 pl->use_cone_patterns = 1;
427 if (set_opts.use_stdin) {
428 struct strbuf unquoted = STRBUF_INIT;
429 while (!strbuf_getline(&line, stdin)) {
430 if (line.buf[0] == '"') {
431 strbuf_reset(&unquoted);
432 if (unquote_c_style(&unquoted, line.buf, NULL))
433 die(_("unable to unquote C-style string '%s'"),
434 line.buf);
436 strbuf_swap(&unquoted, &line);
439 strbuf_to_cone_pattern(&line, pl);
442 strbuf_release(&unquoted);
443 } else {
444 for (i = 0; i < argc; i++) {
445 strbuf_setlen(&line, 0);
446 strbuf_addstr(&line, argv[i]);
447 strbuf_to_cone_pattern(&line, pl);
450 } else {
451 if (set_opts.use_stdin) {
452 struct strbuf line = STRBUF_INIT;
454 while (!strbuf_getline(&line, stdin)) {
455 size_t len;
456 char *buf = strbuf_detach(&line, &len);
457 add_pattern(buf, empty_base, 0, pl, 0);
459 } else {
460 for (i = 0; i < argc; i++)
461 add_pattern(argv[i], empty_base, 0, pl, 0);
466 enum modify_type {
467 REPLACE,
468 ADD,
471 static void add_patterns_cone_mode(int argc, const char **argv,
472 struct pattern_list *pl)
474 struct strbuf buffer = STRBUF_INIT;
475 struct pattern_entry *pe;
476 struct hashmap_iter iter;
477 struct pattern_list existing;
478 char *sparse_filename = get_sparse_checkout_filename();
480 add_patterns_from_input(pl, argc, argv);
482 memset(&existing, 0, sizeof(existing));
483 existing.use_cone_patterns = core_sparse_checkout_cone;
485 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
486 &existing, NULL))
487 die(_("unable to load existing sparse-checkout patterns"));
488 free(sparse_filename);
490 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
491 if (!hashmap_contains_parent(&pl->recursive_hashmap,
492 pe->pattern, &buffer) ||
493 !hashmap_contains_parent(&pl->parent_hashmap,
494 pe->pattern, &buffer)) {
495 strbuf_reset(&buffer);
496 strbuf_addstr(&buffer, pe->pattern);
497 insert_recursive_pattern(pl, &buffer);
501 clear_pattern_list(&existing);
502 strbuf_release(&buffer);
505 static void add_patterns_literal(int argc, const char **argv,
506 struct pattern_list *pl)
508 char *sparse_filename = get_sparse_checkout_filename();
509 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
510 pl, NULL))
511 die(_("unable to load existing sparse-checkout patterns"));
512 free(sparse_filename);
513 add_patterns_from_input(pl, argc, argv);
516 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
518 int result;
519 int changed_config = 0;
520 struct pattern_list pl;
521 memset(&pl, 0, sizeof(pl));
523 switch (m) {
524 case ADD:
525 if (core_sparse_checkout_cone)
526 add_patterns_cone_mode(argc, argv, &pl);
527 else
528 add_patterns_literal(argc, argv, &pl);
529 break;
531 case REPLACE:
532 add_patterns_from_input(&pl, argc, argv);
533 break;
536 if (!core_apply_sparse_checkout) {
537 set_config(MODE_ALL_PATTERNS);
538 core_apply_sparse_checkout = 1;
539 changed_config = 1;
542 result = write_patterns_and_update(&pl);
544 if (result && changed_config)
545 set_config(MODE_NO_PATTERNS);
547 clear_pattern_list(&pl);
548 return result;
551 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
552 enum modify_type m)
554 static struct option builtin_sparse_checkout_set_options[] = {
555 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
556 N_("read patterns from standard in")),
557 OPT_END(),
560 repo_read_index(the_repository);
562 argc = parse_options(argc, argv, prefix,
563 builtin_sparse_checkout_set_options,
564 builtin_sparse_checkout_set_usage,
565 PARSE_OPT_KEEP_UNKNOWN);
567 return modify_pattern_list(argc, argv, m);
570 static char const * const builtin_sparse_checkout_reapply_usage[] = {
571 N_("git sparse-checkout reapply"),
572 NULL
575 static int sparse_checkout_reapply(int argc, const char **argv)
577 static struct option builtin_sparse_checkout_reapply_options[] = {
578 OPT_END(),
581 argc = parse_options(argc, argv, NULL,
582 builtin_sparse_checkout_reapply_options,
583 builtin_sparse_checkout_reapply_usage, 0);
585 repo_read_index(the_repository);
586 return update_working_directory(NULL);
589 static char const * const builtin_sparse_checkout_disable_usage[] = {
590 N_("git sparse-checkout disable"),
591 NULL
594 static int sparse_checkout_disable(int argc, const char **argv)
596 static struct option builtin_sparse_checkout_disable_options[] = {
597 OPT_END(),
599 struct pattern_list pl;
600 struct strbuf match_all = STRBUF_INIT;
602 argc = parse_options(argc, argv, NULL,
603 builtin_sparse_checkout_disable_options,
604 builtin_sparse_checkout_disable_usage, 0);
606 repo_read_index(the_repository);
608 memset(&pl, 0, sizeof(pl));
609 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
610 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
611 pl.use_cone_patterns = 0;
612 core_apply_sparse_checkout = 1;
614 strbuf_addstr(&match_all, "/*");
615 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
617 if (update_working_directory(&pl))
618 die(_("error while refreshing working directory"));
620 clear_pattern_list(&pl);
621 return set_config(MODE_NO_PATTERNS);
624 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
626 static struct option builtin_sparse_checkout_options[] = {
627 OPT_END(),
630 if (argc == 2 && !strcmp(argv[1], "-h"))
631 usage_with_options(builtin_sparse_checkout_usage,
632 builtin_sparse_checkout_options);
634 argc = parse_options(argc, argv, prefix,
635 builtin_sparse_checkout_options,
636 builtin_sparse_checkout_usage,
637 PARSE_OPT_STOP_AT_NON_OPTION);
639 git_config(git_default_config, NULL);
641 if (argc > 0) {
642 if (!strcmp(argv[0], "list"))
643 return sparse_checkout_list(argc, argv);
644 if (!strcmp(argv[0], "init"))
645 return sparse_checkout_init(argc, argv);
646 if (!strcmp(argv[0], "set"))
647 return sparse_checkout_set(argc, argv, prefix, REPLACE);
648 if (!strcmp(argv[0], "add"))
649 return sparse_checkout_set(argc, argv, prefix, ADD);
650 if (!strcmp(argv[0], "reapply"))
651 return sparse_checkout_reapply(argc, argv);
652 if (!strcmp(argv[0], "disable"))
653 return sparse_checkout_disable(argc, argv);
656 usage_with_options(builtin_sparse_checkout_usage,
657 builtin_sparse_checkout_options);