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 quote_c_style(sl.items[i].string, NULL, stdout, 0);
89 write_patterns_to_file(stdout, &pl);
90 clear_pattern_list(&pl);
95 static int update_working_directory(struct pattern_list *pl)
98 struct unpack_trees_options o;
99 struct lock_file lock_file = LOCK_INIT;
100 struct object_id oid;
103 struct repository *r = the_repository;
105 if (repo_read_index_unmerged(r))
106 die(_("you need to resolve your current index first"));
108 if (get_oid("HEAD", &oid))
111 tree = parse_tree_indirect(&oid);
113 init_tree_desc(&t, tree->buffer, tree->size);
115 memset(&o, 0, sizeof(o));
116 o.verbose_update = isatty(2);
121 o.src_index = r->index;
122 o.dst_index = r->index;
123 o.skip_sparse_checkout = 0;
125 o.keep_pattern_list = !!pl;
127 resolve_undo_clear_index(r->index);
130 cache_tree_free(&r->index->cache_tree);
132 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
134 core_apply_sparse_checkout = 1;
135 result = unpack_trees(1, &t, &o);
138 prime_cache_tree(r, r->index, tree);
139 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
141 rollback_lock_file(&lock_file);
146 static char *escaped_pattern(char *pattern)
149 struct strbuf final = STRBUF_INIT;
152 if (is_glob_special(*p))
153 strbuf_addch(&final, '\\');
155 strbuf_addch(&final, *p);
159 return strbuf_detach(&final, NULL);
162 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
165 struct pattern_entry *pe;
166 struct hashmap_iter iter;
167 struct string_list sl = STRING_LIST_INIT_DUP;
168 struct strbuf parent_pattern = STRBUF_INIT;
170 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
171 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
174 if (!hashmap_contains_parent(&pl->recursive_hashmap,
177 string_list_insert(&sl, pe->pattern);
180 string_list_sort(&sl);
181 string_list_remove_duplicates(&sl, 0);
183 fprintf(fp, "/*\n!/*/\n");
185 for (i = 0; i < sl.nr; i++) {
186 char *pattern = escaped_pattern(sl.items[i].string);
189 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
193 string_list_clear(&sl, 0);
195 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
196 if (!hashmap_contains_parent(&pl->recursive_hashmap,
199 string_list_insert(&sl, pe->pattern);
202 strbuf_release(&parent_pattern);
204 string_list_sort(&sl);
205 string_list_remove_duplicates(&sl, 0);
207 for (i = 0; i < sl.nr; i++) {
208 char *pattern = escaped_pattern(sl.items[i].string);
209 fprintf(fp, "%s/\n", pattern);
214 static int write_patterns_and_update(struct pattern_list *pl)
216 char *sparse_filename;
219 struct lock_file lk = LOCK_INIT;
222 sparse_filename = get_sparse_checkout_filename();
224 if (safe_create_leading_directories(sparse_filename))
225 die(_("failed to create directory for sparse-checkout file"));
227 fd = hold_lock_file_for_update(&lk, sparse_filename,
230 result = update_working_directory(pl);
232 rollback_lock_file(&lk);
233 free(sparse_filename);
234 clear_pattern_list(pl);
235 update_working_directory(NULL);
239 fp = xfdopen(fd, "w");
241 if (core_sparse_checkout_cone)
242 write_cone_to_file(fp, pl);
244 write_patterns_to_file(fp, pl);
247 commit_lock_file(&lk);
249 free(sparse_filename);
250 clear_pattern_list(pl);
255 enum sparse_checkout_mode {
256 MODE_NO_PATTERNS = 0,
257 MODE_ALL_PATTERNS = 1,
258 MODE_CONE_PATTERNS = 2,
261 static int set_config(enum sparse_checkout_mode mode)
263 const char *config_path;
265 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
266 error(_("failed to set extensions.worktreeConfig setting"));
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);
282 static char const * const builtin_sparse_checkout_init_usage[] = {
283 N_("git sparse-checkout init [--cone]"),
287 static struct sparse_checkout_init_opts {
291 static int sparse_checkout_init(int argc, const char **argv)
293 struct pattern_list pl;
294 char *sparse_filename;
296 struct object_id oid;
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")),
306 repo_read_index(the_repository);
307 require_clean_work_tree(the_repository,
308 N_("initialize sparse-checkout"), NULL, 1, 0);
310 argc = parse_options(argc, argv, NULL,
311 builtin_sparse_checkout_init_options,
312 builtin_sparse_checkout_init_usage, 0);
314 if (init_opts.cone_mode) {
315 mode = MODE_CONE_PATTERNS;
316 core_sparse_checkout_cone = 1;
318 mode = MODE_ALL_PATTERNS;
320 if (set_config(mode))
323 memset(&pl, 0, sizeof(pl));
325 sparse_filename = get_sparse_checkout_filename();
326 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
328 /* If we already have a sparse-checkout file, use it. */
330 free(sparse_filename);
331 core_apply_sparse_checkout = 1;
332 return update_working_directory(NULL);
335 if (get_oid("HEAD", &oid)) {
338 /* assume we are in a fresh repo, but update the sparse-checkout file */
339 fp = xfopen(sparse_filename, "w");
341 die(_("failed to open '%s'"), sparse_filename);
343 free(sparse_filename);
344 fprintf(fp, "/*\n!/*/\n");
349 strbuf_addstr(&pattern, "/*");
350 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
351 strbuf_addstr(&pattern, "!/*/");
352 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
354 return write_patterns_and_update(&pl);
357 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
359 struct pattern_entry *e = xmalloc(sizeof(*e));
360 e->patternlen = path->len;
361 e->pattern = strbuf_detach(path, NULL);
362 hashmap_entry_init(&e->ent,
364 strihash(e->pattern) :
365 strhash(e->pattern));
367 hashmap_add(&pl->recursive_hashmap, &e->ent);
369 while (e->patternlen) {
370 char *slash = strrchr(e->pattern, '/');
371 char *oldpattern = e->pattern;
374 if (slash == e->pattern)
377 newlen = slash - e->pattern;
378 e = xmalloc(sizeof(struct pattern_entry));
379 e->patternlen = newlen;
380 e->pattern = xstrndup(oldpattern, newlen);
381 hashmap_entry_init(&e->ent,
383 strihash(e->pattern) :
384 strhash(e->pattern));
386 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
387 hashmap_add(&pl->parent_hashmap, &e->ent);
391 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
395 strbuf_trim_trailing_dir_sep(line);
400 if (line->buf[0] != '/')
401 strbuf_insertstr(line, 0, "/");
403 insert_recursive_pattern(pl, line);
406 static char const * const builtin_sparse_checkout_set_usage[] = {
407 N_("git sparse-checkout set (--stdin | <patterns>)"),
411 static struct sparse_checkout_set_opts {
415 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
418 struct pattern_list pl;
420 int changed_config = 0;
422 static struct option builtin_sparse_checkout_set_options[] = {
423 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
424 N_("read patterns from standard in")),
428 repo_read_index(the_repository);
429 require_clean_work_tree(the_repository,
430 N_("set sparse-checkout patterns"), NULL, 1, 0);
432 memset(&pl, 0, sizeof(pl));
434 argc = parse_options(argc, argv, prefix,
435 builtin_sparse_checkout_set_options,
436 builtin_sparse_checkout_set_usage,
437 PARSE_OPT_KEEP_UNKNOWN);
439 if (core_sparse_checkout_cone) {
440 struct strbuf line = STRBUF_INIT;
442 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
443 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
444 pl.use_cone_patterns = 1;
446 if (set_opts.use_stdin) {
447 struct strbuf unquoted = STRBUF_INIT;
448 while (!strbuf_getline(&line, stdin)) {
449 if (line.buf[0] == '"') {
450 strbuf_reset(&unquoted);
451 if (unquote_c_style(&unquoted, line.buf, NULL))
452 die(_("unable to unquote C-style string '%s'"),
455 strbuf_swap(&unquoted, &line);
458 strbuf_to_cone_pattern(&line, &pl);
461 strbuf_release(&unquoted);
463 for (i = 0; i < argc; i++) {
464 strbuf_setlen(&line, 0);
465 strbuf_addstr(&line, argv[i]);
466 strbuf_to_cone_pattern(&line, &pl);
470 if (set_opts.use_stdin) {
471 struct strbuf line = STRBUF_INIT;
473 while (!strbuf_getline(&line, stdin)) {
475 char *buf = strbuf_detach(&line, &len);
476 add_pattern(buf, empty_base, 0, &pl, 0);
479 for (i = 0; i < argc; i++)
480 add_pattern(argv[i], empty_base, 0, &pl, 0);
484 if (!core_apply_sparse_checkout) {
485 set_config(MODE_ALL_PATTERNS);
486 core_apply_sparse_checkout = 1;
490 result = write_patterns_and_update(&pl);
492 if (result && changed_config)
493 set_config(MODE_NO_PATTERNS);
495 clear_pattern_list(&pl);
499 static int sparse_checkout_disable(int argc, const char **argv)
501 struct pattern_list pl;
502 struct strbuf match_all = STRBUF_INIT;
504 repo_read_index(the_repository);
505 require_clean_work_tree(the_repository,
506 N_("disable sparse-checkout"), NULL, 1, 0);
508 memset(&pl, 0, sizeof(pl));
509 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
510 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
511 pl.use_cone_patterns = 0;
512 core_apply_sparse_checkout = 1;
514 strbuf_addstr(&match_all, "/*");
515 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
517 if (update_working_directory(&pl))
518 die(_("error while refreshing working directory"));
520 clear_pattern_list(&pl);
521 return set_config(MODE_NO_PATTERNS);
524 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
526 static struct option builtin_sparse_checkout_options[] = {
530 if (argc == 2 && !strcmp(argv[1], "-h"))
531 usage_with_options(builtin_sparse_checkout_usage,
532 builtin_sparse_checkout_options);
534 argc = parse_options(argc, argv, prefix,
535 builtin_sparse_checkout_options,
536 builtin_sparse_checkout_usage,
537 PARSE_OPT_STOP_AT_NON_OPTION);
539 git_config(git_default_config, NULL);
542 if (!strcmp(argv[0], "list"))
543 return sparse_checkout_list(argc, argv);
544 if (!strcmp(argv[0], "init"))
545 return sparse_checkout_init(argc, argv);
546 if (!strcmp(argv[0], "set"))
547 return sparse_checkout_set(argc, argv, prefix);
548 if (!strcmp(argv[0], "disable"))
549 return sparse_checkout_disable(argc, argv);
552 usage_with_options(builtin_sparse_checkout_usage,
553 builtin_sparse_checkout_options);