l10n: bg.po: Updated Bulgarian translation (5204t)
[git.git] / builtin / sparse-checkout.c
bloba4bdd7c4940260d8d8fa19a6d4de2ee37d82e7ac
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 #include "sparse-index.h"
19 static const char *empty_base = "";
21 static char const * const builtin_sparse_checkout_usage[] = {
22 N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
23 NULL
26 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
28 int i;
30 for (i = 0; i < pl->nr; i++) {
31 struct path_pattern *p = pl->patterns[i];
33 if (p->flags & PATTERN_FLAG_NEGATIVE)
34 fprintf(fp, "!");
36 fprintf(fp, "%s", p->pattern);
38 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
39 fprintf(fp, "/");
41 fprintf(fp, "\n");
45 static char const * const builtin_sparse_checkout_list_usage[] = {
46 N_("git sparse-checkout list"),
47 NULL
50 static int sparse_checkout_list(int argc, const char **argv)
52 static struct option builtin_sparse_checkout_list_options[] = {
53 OPT_END(),
55 struct pattern_list pl;
56 char *sparse_filename;
57 int res;
59 argc = parse_options(argc, argv, NULL,
60 builtin_sparse_checkout_list_options,
61 builtin_sparse_checkout_list_usage, 0);
63 memset(&pl, 0, sizeof(pl));
65 pl.use_cone_patterns = core_sparse_checkout_cone;
67 sparse_filename = get_sparse_checkout_filename();
68 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
69 free(sparse_filename);
71 if (res < 0) {
72 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
73 return 0;
76 if (pl.use_cone_patterns) {
77 int i;
78 struct pattern_entry *pe;
79 struct hashmap_iter iter;
80 struct string_list sl = STRING_LIST_INIT_DUP;
82 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
83 /* pe->pattern starts with "/", skip it */
84 string_list_insert(&sl, pe->pattern + 1);
87 string_list_sort(&sl);
89 for (i = 0; i < sl.nr; i++) {
90 quote_c_style(sl.items[i].string, NULL, stdout, 0);
91 printf("\n");
94 return 0;
97 write_patterns_to_file(stdout, &pl);
98 clear_pattern_list(&pl);
100 return 0;
103 static int update_working_directory(struct pattern_list *pl)
105 enum update_sparsity_result result;
106 struct unpack_trees_options o;
107 struct lock_file lock_file = LOCK_INIT;
108 struct repository *r = the_repository;
110 /* If no branch has been checked out, there are no updates to make. */
111 if (is_index_unborn(r->index))
112 return UPDATE_SPARSITY_SUCCESS;
114 r->index->sparse_checkout_patterns = pl;
116 memset(&o, 0, sizeof(o));
117 o.verbose_update = isatty(2);
118 o.update = 1;
119 o.head_idx = -1;
120 o.src_index = r->index;
121 o.dst_index = r->index;
122 o.skip_sparse_checkout = 0;
123 o.pl = pl;
125 setup_work_tree();
127 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
129 setup_unpack_trees_porcelain(&o, "sparse-checkout");
130 result = update_sparsity(&o);
131 clear_unpack_trees_porcelain(&o);
133 if (result == UPDATE_SPARSITY_WARNINGS)
135 * We don't do any special handling of warnings from untracked
136 * files in the way or dirty entries that can't be removed.
138 result = UPDATE_SPARSITY_SUCCESS;
139 if (result == UPDATE_SPARSITY_SUCCESS)
140 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
141 else
142 rollback_lock_file(&lock_file);
144 r->index->sparse_checkout_patterns = NULL;
145 return result;
148 static char *escaped_pattern(char *pattern)
150 char *p = pattern;
151 struct strbuf final = STRBUF_INIT;
153 while (*p) {
154 if (is_glob_special(*p))
155 strbuf_addch(&final, '\\');
157 strbuf_addch(&final, *p);
158 p++;
161 return strbuf_detach(&final, NULL);
164 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
166 int i;
167 struct pattern_entry *pe;
168 struct hashmap_iter iter;
169 struct string_list sl = STRING_LIST_INIT_DUP;
170 struct strbuf parent_pattern = STRBUF_INIT;
172 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
173 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
174 continue;
176 if (!hashmap_contains_parent(&pl->recursive_hashmap,
177 pe->pattern,
178 &parent_pattern))
179 string_list_insert(&sl, pe->pattern);
182 string_list_sort(&sl);
183 string_list_remove_duplicates(&sl, 0);
185 fprintf(fp, "/*\n!/*/\n");
187 for (i = 0; i < sl.nr; i++) {
188 char *pattern = escaped_pattern(sl.items[i].string);
190 if (strlen(pattern))
191 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
192 free(pattern);
195 string_list_clear(&sl, 0);
197 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
198 if (!hashmap_contains_parent(&pl->recursive_hashmap,
199 pe->pattern,
200 &parent_pattern))
201 string_list_insert(&sl, pe->pattern);
204 strbuf_release(&parent_pattern);
206 string_list_sort(&sl);
207 string_list_remove_duplicates(&sl, 0);
209 for (i = 0; i < sl.nr; i++) {
210 char *pattern = escaped_pattern(sl.items[i].string);
211 fprintf(fp, "%s/\n", pattern);
212 free(pattern);
216 static int write_patterns_and_update(struct pattern_list *pl)
218 char *sparse_filename;
219 FILE *fp;
220 int fd;
221 struct lock_file lk = LOCK_INIT;
222 int result;
224 sparse_filename = get_sparse_checkout_filename();
226 if (safe_create_leading_directories(sparse_filename))
227 die(_("failed to create directory for sparse-checkout file"));
229 fd = hold_lock_file_for_update(&lk, sparse_filename,
230 LOCK_DIE_ON_ERROR);
232 result = update_working_directory(pl);
233 if (result) {
234 rollback_lock_file(&lk);
235 free(sparse_filename);
236 clear_pattern_list(pl);
237 update_working_directory(NULL);
238 return result;
241 fp = xfdopen(fd, "w");
243 if (core_sparse_checkout_cone)
244 write_cone_to_file(fp, pl);
245 else
246 write_patterns_to_file(fp, pl);
248 fflush(fp);
249 commit_lock_file(&lk);
251 free(sparse_filename);
252 clear_pattern_list(pl);
254 return 0;
257 enum sparse_checkout_mode {
258 MODE_NO_PATTERNS = 0,
259 MODE_ALL_PATTERNS = 1,
260 MODE_CONE_PATTERNS = 2,
263 static int set_config(enum sparse_checkout_mode mode)
265 const char *config_path;
267 if (upgrade_repository_format(1) < 0)
268 die(_("unable to upgrade repository format to enable worktreeConfig"));
269 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
270 error(_("failed to set extensions.worktreeConfig setting"));
271 return 1;
274 config_path = git_path("config.worktree");
275 git_config_set_in_file_gently(config_path,
276 "core.sparseCheckout",
277 mode ? "true" : NULL);
279 git_config_set_in_file_gently(config_path,
280 "core.sparseCheckoutCone",
281 mode == MODE_CONE_PATTERNS ? "true" : NULL);
283 if (mode == MODE_NO_PATTERNS)
284 set_sparse_index_config(the_repository, 0);
286 return 0;
289 static char const * const builtin_sparse_checkout_init_usage[] = {
290 N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
291 NULL
294 static struct sparse_checkout_init_opts {
295 int cone_mode;
296 int sparse_index;
297 } init_opts;
299 static int sparse_checkout_init(int argc, const char **argv)
301 struct pattern_list pl;
302 char *sparse_filename;
303 int res;
304 struct object_id oid;
305 int mode;
306 struct strbuf pattern = STRBUF_INIT;
308 static struct option builtin_sparse_checkout_init_options[] = {
309 OPT_BOOL(0, "cone", &init_opts.cone_mode,
310 N_("initialize the sparse-checkout in cone mode")),
311 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
312 N_("toggle the use of a sparse index")),
313 OPT_END(),
316 repo_read_index(the_repository);
318 init_opts.sparse_index = -1;
320 argc = parse_options(argc, argv, NULL,
321 builtin_sparse_checkout_init_options,
322 builtin_sparse_checkout_init_usage, 0);
324 if (init_opts.cone_mode) {
325 mode = MODE_CONE_PATTERNS;
326 core_sparse_checkout_cone = 1;
327 } else
328 mode = MODE_ALL_PATTERNS;
330 if (set_config(mode))
331 return 1;
333 memset(&pl, 0, sizeof(pl));
335 sparse_filename = get_sparse_checkout_filename();
336 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
338 if (init_opts.sparse_index >= 0) {
339 if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0)
340 die(_("failed to modify sparse-index config"));
342 /* force an index rewrite */
343 repo_read_index(the_repository);
344 the_repository->index->updated_workdir = 1;
347 core_apply_sparse_checkout = 1;
349 /* If we already have a sparse-checkout file, use it. */
350 if (res >= 0) {
351 free(sparse_filename);
352 return update_working_directory(NULL);
355 if (get_oid("HEAD", &oid)) {
356 FILE *fp;
358 /* assume we are in a fresh repo, but update the sparse-checkout file */
359 fp = xfopen(sparse_filename, "w");
360 if (!fp)
361 die(_("failed to open '%s'"), sparse_filename);
363 free(sparse_filename);
364 fprintf(fp, "/*\n!/*/\n");
365 fclose(fp);
366 return 0;
369 strbuf_addstr(&pattern, "/*");
370 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
371 strbuf_addstr(&pattern, "!/*/");
372 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
373 pl.use_cone_patterns = init_opts.cone_mode;
375 return write_patterns_and_update(&pl);
378 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
380 struct pattern_entry *e = xmalloc(sizeof(*e));
381 e->patternlen = path->len;
382 e->pattern = strbuf_detach(path, NULL);
383 hashmap_entry_init(&e->ent,
384 ignore_case ?
385 strihash(e->pattern) :
386 strhash(e->pattern));
388 hashmap_add(&pl->recursive_hashmap, &e->ent);
390 while (e->patternlen) {
391 char *slash = strrchr(e->pattern, '/');
392 char *oldpattern = e->pattern;
393 size_t newlen;
395 if (slash == e->pattern)
396 break;
398 newlen = slash - e->pattern;
399 e = xmalloc(sizeof(struct pattern_entry));
400 e->patternlen = newlen;
401 e->pattern = xstrndup(oldpattern, newlen);
402 hashmap_entry_init(&e->ent,
403 ignore_case ?
404 strihash(e->pattern) :
405 strhash(e->pattern));
407 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
408 hashmap_add(&pl->parent_hashmap, &e->ent);
412 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
414 strbuf_trim(line);
416 strbuf_trim_trailing_dir_sep(line);
418 if (strbuf_normalize_path(line))
419 die(_("could not normalize path %s"), line->buf);
421 if (!line->len)
422 return;
424 if (line->buf[0] != '/')
425 strbuf_insertstr(line, 0, "/");
427 insert_recursive_pattern(pl, line);
430 static char const * const builtin_sparse_checkout_set_usage[] = {
431 N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
432 NULL
435 static struct sparse_checkout_set_opts {
436 int use_stdin;
437 } set_opts;
439 static void add_patterns_from_input(struct pattern_list *pl,
440 int argc, const char **argv)
442 int i;
443 if (core_sparse_checkout_cone) {
444 struct strbuf line = STRBUF_INIT;
446 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
447 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
448 pl->use_cone_patterns = 1;
450 if (set_opts.use_stdin) {
451 struct strbuf unquoted = STRBUF_INIT;
452 while (!strbuf_getline(&line, stdin)) {
453 if (line.buf[0] == '"') {
454 strbuf_reset(&unquoted);
455 if (unquote_c_style(&unquoted, line.buf, NULL))
456 die(_("unable to unquote C-style string '%s'"),
457 line.buf);
459 strbuf_swap(&unquoted, &line);
462 strbuf_to_cone_pattern(&line, pl);
465 strbuf_release(&unquoted);
466 } else {
467 for (i = 0; i < argc; i++) {
468 strbuf_setlen(&line, 0);
469 strbuf_addstr(&line, argv[i]);
470 strbuf_to_cone_pattern(&line, pl);
473 } else {
474 if (set_opts.use_stdin) {
475 struct strbuf line = STRBUF_INIT;
477 while (!strbuf_getline(&line, stdin)) {
478 size_t len;
479 char *buf = strbuf_detach(&line, &len);
480 add_pattern(buf, empty_base, 0, pl, 0);
482 } else {
483 for (i = 0; i < argc; i++)
484 add_pattern(argv[i], empty_base, 0, pl, 0);
489 enum modify_type {
490 REPLACE,
491 ADD,
494 static void add_patterns_cone_mode(int argc, const char **argv,
495 struct pattern_list *pl)
497 struct strbuf buffer = STRBUF_INIT;
498 struct pattern_entry *pe;
499 struct hashmap_iter iter;
500 struct pattern_list existing;
501 char *sparse_filename = get_sparse_checkout_filename();
503 add_patterns_from_input(pl, argc, argv);
505 memset(&existing, 0, sizeof(existing));
506 existing.use_cone_patterns = core_sparse_checkout_cone;
508 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
509 &existing, NULL, 0))
510 die(_("unable to load existing sparse-checkout patterns"));
511 free(sparse_filename);
513 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
514 if (!hashmap_contains_parent(&pl->recursive_hashmap,
515 pe->pattern, &buffer) ||
516 !hashmap_contains_parent(&pl->parent_hashmap,
517 pe->pattern, &buffer)) {
518 strbuf_reset(&buffer);
519 strbuf_addstr(&buffer, pe->pattern);
520 insert_recursive_pattern(pl, &buffer);
524 clear_pattern_list(&existing);
525 strbuf_release(&buffer);
528 static void add_patterns_literal(int argc, const char **argv,
529 struct pattern_list *pl)
531 char *sparse_filename = get_sparse_checkout_filename();
532 if (add_patterns_from_file_to_list(sparse_filename, "", 0,
533 pl, NULL, 0))
534 die(_("unable to load existing sparse-checkout patterns"));
535 free(sparse_filename);
536 add_patterns_from_input(pl, argc, argv);
539 static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
541 int result;
542 int changed_config = 0;
543 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
545 switch (m) {
546 case ADD:
547 if (core_sparse_checkout_cone)
548 add_patterns_cone_mode(argc, argv, pl);
549 else
550 add_patterns_literal(argc, argv, pl);
551 break;
553 case REPLACE:
554 add_patterns_from_input(pl, argc, argv);
555 break;
558 if (!core_apply_sparse_checkout) {
559 set_config(MODE_ALL_PATTERNS);
560 core_apply_sparse_checkout = 1;
561 changed_config = 1;
564 result = write_patterns_and_update(pl);
566 if (result && changed_config)
567 set_config(MODE_NO_PATTERNS);
569 clear_pattern_list(pl);
570 free(pl);
571 return result;
574 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
575 enum modify_type m)
577 static struct option builtin_sparse_checkout_set_options[] = {
578 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
579 N_("read patterns from standard in")),
580 OPT_END(),
583 repo_read_index(the_repository);
585 argc = parse_options(argc, argv, prefix,
586 builtin_sparse_checkout_set_options,
587 builtin_sparse_checkout_set_usage,
588 PARSE_OPT_KEEP_UNKNOWN);
590 return modify_pattern_list(argc, argv, m);
593 static char const * const builtin_sparse_checkout_reapply_usage[] = {
594 N_("git sparse-checkout reapply"),
595 NULL
598 static int sparse_checkout_reapply(int argc, const char **argv)
600 static struct option builtin_sparse_checkout_reapply_options[] = {
601 OPT_END(),
604 argc = parse_options(argc, argv, NULL,
605 builtin_sparse_checkout_reapply_options,
606 builtin_sparse_checkout_reapply_usage, 0);
608 repo_read_index(the_repository);
609 return update_working_directory(NULL);
612 static char const * const builtin_sparse_checkout_disable_usage[] = {
613 N_("git sparse-checkout disable"),
614 NULL
617 static int sparse_checkout_disable(int argc, const char **argv)
619 static struct option builtin_sparse_checkout_disable_options[] = {
620 OPT_END(),
622 struct pattern_list pl;
623 struct strbuf match_all = STRBUF_INIT;
625 argc = parse_options(argc, argv, NULL,
626 builtin_sparse_checkout_disable_options,
627 builtin_sparse_checkout_disable_usage, 0);
629 repo_read_index(the_repository);
631 memset(&pl, 0, sizeof(pl));
632 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
633 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
634 pl.use_cone_patterns = 0;
635 core_apply_sparse_checkout = 1;
637 strbuf_addstr(&match_all, "/*");
638 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
640 prepare_repo_settings(the_repository);
641 the_repository->settings.sparse_index = 0;
643 if (update_working_directory(&pl))
644 die(_("error while refreshing working directory"));
646 clear_pattern_list(&pl);
647 return set_config(MODE_NO_PATTERNS);
650 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
652 static struct option builtin_sparse_checkout_options[] = {
653 OPT_END(),
656 if (argc == 2 && !strcmp(argv[1], "-h"))
657 usage_with_options(builtin_sparse_checkout_usage,
658 builtin_sparse_checkout_options);
660 argc = parse_options(argc, argv, prefix,
661 builtin_sparse_checkout_options,
662 builtin_sparse_checkout_usage,
663 PARSE_OPT_STOP_AT_NON_OPTION);
665 git_config(git_default_config, NULL);
667 if (argc > 0) {
668 if (!strcmp(argv[0], "list"))
669 return sparse_checkout_list(argc, argv);
670 if (!strcmp(argv[0], "init"))
671 return sparse_checkout_init(argc, argv);
672 if (!strcmp(argv[0], "set"))
673 return sparse_checkout_set(argc, argv, prefix, REPLACE);
674 if (!strcmp(argv[0], "add"))
675 return sparse_checkout_set(argc, argv, prefix, ADD);
676 if (!strcmp(argv[0], "reapply"))
677 return sparse_checkout_reapply(argc, argv);
678 if (!strcmp(argv[0], "disable"))
679 return sparse_checkout_disable(argc, argv);
682 usage_with_options(builtin_sparse_checkout_usage,
683 builtin_sparse_checkout_options);