4 #include "parse-options.h"
6 #include "repository.h"
7 #include "run-command.h"
9 #include "string-list.h"
11 #include "cache-tree.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 #include "wt-status.h"
18 static const char *empty_base = "";
20 static char const * const builtin_sparse_checkout_usage[] = {
21 N_("git sparse-checkout (init|list|set|disable) <options>"),
25 static char *get_sparse_checkout_filename(void)
27 return git_pathdup("info/sparse-checkout");
30 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
34 for (i = 0; i < pl->nr; i++) {
35 struct path_pattern *p = pl->patterns[i];
37 if (p->flags & PATTERN_FLAG_NEGATIVE)
40 fprintf(fp, "%s", p->pattern);
42 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
49 static int sparse_checkout_list(int argc, const char **argv)
51 struct pattern_list pl;
52 char *sparse_filename;
55 memset(&pl, 0, sizeof(pl));
57 pl.use_cone_patterns = core_sparse_checkout_cone;
59 sparse_filename = get_sparse_checkout_filename();
60 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
61 free(sparse_filename);
64 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
68 if (pl.use_cone_patterns) {
70 struct pattern_entry *pe;
71 struct hashmap_iter iter;
72 struct string_list sl = STRING_LIST_INIT_DUP;
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);
79 string_list_sort(&sl);
81 for (i = 0; i < sl.nr; i++)
82 printf("%s\n", sl.items[i].string);
87 write_patterns_to_file(stdout, &pl);
88 clear_pattern_list(&pl);
93 static int update_working_directory(struct pattern_list *pl)
96 struct unpack_trees_options o;
97 struct lock_file lock_file = LOCK_INIT;
101 struct repository *r = the_repository;
103 if (repo_read_index_unmerged(r))
104 die(_("you need to resolve your current index first"));
106 if (get_oid("HEAD", &oid))
109 tree = parse_tree_indirect(&oid);
111 init_tree_desc(&t, tree->buffer, tree->size);
113 memset(&o, 0, sizeof(o));
114 o.verbose_update = isatty(2);
119 o.src_index = r->index;
120 o.dst_index = r->index;
121 o.skip_sparse_checkout = 0;
123 o.keep_pattern_list = !!pl;
125 resolve_undo_clear_index(r->index);
128 cache_tree_free(&r->index->cache_tree);
130 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
132 core_apply_sparse_checkout = 1;
133 result = unpack_trees(1, &t, &o);
136 prime_cache_tree(r, r->index, tree);
137 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
139 rollback_lock_file(&lock_file);
144 static char *escaped_pattern(char *pattern)
147 struct strbuf final = STRBUF_INIT;
150 if (*p == '*' || *p == '\\')
151 strbuf_addch(&final, '\\');
153 strbuf_addch(&final, *p);
157 return strbuf_detach(&final, NULL);
160 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
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))
172 if (!hashmap_contains_parent(&pl->recursive_hashmap,
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);
187 fprintf(fp, "%s/\n!%s/*/\n", pattern, 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,
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);
212 static int write_patterns_and_update(struct pattern_list *pl)
214 char *sparse_filename;
217 struct lock_file lk = LOCK_INIT;
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,
228 result = update_working_directory(pl);
230 rollback_lock_file(&lk);
231 free(sparse_filename);
232 clear_pattern_list(pl);
233 update_working_directory(NULL);
237 fp = xfdopen(fd, "w");
239 if (core_sparse_checkout_cone)
240 write_cone_to_file(fp, pl);
242 write_patterns_to_file(fp, pl);
245 commit_lock_file(&lk);
247 free(sparse_filename);
248 clear_pattern_list(pl);
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 (git_config_set_gently("extensions.worktreeConfig", "true")) {
264 error(_("failed to set extensions.worktreeConfig setting"));
268 config_path = git_path("config.worktree");
269 git_config_set_in_file_gently(config_path,
270 "core.sparseCheckout",
271 mode ? "true" : NULL);
273 git_config_set_in_file_gently(config_path,
274 "core.sparseCheckoutCone",
275 mode == MODE_CONE_PATTERNS ? "true" : NULL);
280 static char const * const builtin_sparse_checkout_init_usage[] = {
281 N_("git sparse-checkout init [--cone]"),
285 static struct sparse_checkout_init_opts {
289 static int sparse_checkout_init(int argc, const char **argv)
291 struct pattern_list pl;
292 char *sparse_filename;
294 struct object_id oid;
296 struct strbuf pattern = STRBUF_INIT;
298 static struct option builtin_sparse_checkout_init_options[] = {
299 OPT_BOOL(0, "cone", &init_opts.cone_mode,
300 N_("initialize the sparse-checkout in cone mode")),
304 repo_read_index(the_repository);
305 require_clean_work_tree(the_repository,
306 N_("initialize sparse-checkout"), NULL, 1, 0);
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;
316 mode = MODE_ALL_PATTERNS;
318 if (set_config(mode))
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. */
328 free(sparse_filename);
329 core_apply_sparse_checkout = 1;
330 return update_working_directory(NULL);
333 if (get_oid("HEAD", &oid)) {
336 /* assume we are in a fresh repo, but update the sparse-checkout file */
337 fp = xfopen(sparse_filename, "w");
339 die(_("failed to open '%s'"), sparse_filename);
341 free(sparse_filename);
342 fprintf(fp, "/*\n!/*/\n");
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,
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;
372 if (slash == e->pattern)
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,
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)
393 strbuf_trim_trailing_dir_sep(line);
398 if (line->buf[0] != '/')
399 strbuf_insert(line, 0, "/", 1);
401 insert_recursive_pattern(pl, line);
404 static char const * const builtin_sparse_checkout_set_usage[] = {
405 N_("git sparse-checkout set (--stdin | <patterns>)"),
409 static struct sparse_checkout_set_opts {
413 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
416 struct pattern_list pl;
418 int changed_config = 0;
420 static struct option builtin_sparse_checkout_set_options[] = {
421 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
422 N_("read patterns from standard in")),
426 repo_read_index(the_repository);
427 require_clean_work_tree(the_repository,
428 N_("set sparse-checkout patterns"), NULL, 1, 0);
430 memset(&pl, 0, sizeof(pl));
432 argc = parse_options(argc, argv, prefix,
433 builtin_sparse_checkout_set_options,
434 builtin_sparse_checkout_set_usage,
435 PARSE_OPT_KEEP_UNKNOWN);
437 if (core_sparse_checkout_cone) {
438 struct strbuf line = STRBUF_INIT;
440 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
441 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
442 pl.use_cone_patterns = 1;
444 if (set_opts.use_stdin) {
445 while (!strbuf_getline(&line, stdin))
446 strbuf_to_cone_pattern(&line, &pl);
448 for (i = 0; i < argc; i++) {
449 strbuf_setlen(&line, 0);
450 strbuf_addstr(&line, argv[i]);
451 strbuf_to_cone_pattern(&line, &pl);
455 if (set_opts.use_stdin) {
456 struct strbuf line = STRBUF_INIT;
458 while (!strbuf_getline(&line, stdin)) {
460 char *buf = strbuf_detach(&line, &len);
461 add_pattern(buf, empty_base, 0, &pl, 0);
464 for (i = 0; i < argc; i++)
465 add_pattern(argv[i], empty_base, 0, &pl, 0);
469 if (!core_apply_sparse_checkout) {
470 set_config(MODE_ALL_PATTERNS);
471 core_apply_sparse_checkout = 1;
475 result = write_patterns_and_update(&pl);
477 if (result && changed_config)
478 set_config(MODE_NO_PATTERNS);
480 clear_pattern_list(&pl);
484 static int sparse_checkout_disable(int argc, const char **argv)
486 struct pattern_list pl;
487 struct strbuf match_all = STRBUF_INIT;
489 repo_read_index(the_repository);
490 require_clean_work_tree(the_repository,
491 N_("disable sparse-checkout"), NULL, 1, 0);
493 memset(&pl, 0, sizeof(pl));
494 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
495 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
496 pl.use_cone_patterns = 0;
497 core_apply_sparse_checkout = 1;
499 strbuf_addstr(&match_all, "/*");
500 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
502 if (update_working_directory(&pl))
503 die(_("error while refreshing working directory"));
505 clear_pattern_list(&pl);
506 return set_config(MODE_NO_PATTERNS);
509 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
511 static struct option builtin_sparse_checkout_options[] = {
515 if (argc == 2 && !strcmp(argv[1], "-h"))
516 usage_with_options(builtin_sparse_checkout_usage,
517 builtin_sparse_checkout_options);
519 argc = parse_options(argc, argv, prefix,
520 builtin_sparse_checkout_options,
521 builtin_sparse_checkout_usage,
522 PARSE_OPT_STOP_AT_NON_OPTION);
524 git_config(git_default_config, NULL);
527 if (!strcmp(argv[0], "list"))
528 return sparse_checkout_list(argc, argv);
529 if (!strcmp(argv[0], "init"))
530 return sparse_checkout_init(argc, argv);
531 if (!strcmp(argv[0], "set"))
532 return sparse_checkout_set(argc, argv, prefix);
533 if (!strcmp(argv[0], "disable"))
534 return sparse_checkout_disable(argc, argv);
537 usage_with_options(builtin_sparse_checkout_usage,
538 builtin_sparse_checkout_options);