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"
17 static const char *empty_base = "";
19 static char const * const builtin_sparse_checkout_usage[] = {
20 N_("git sparse-checkout (init|list|set|disable) <options>"),
24 static char *get_sparse_checkout_filename(void)
26 return git_pathdup("info/sparse-checkout");
29 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
33 for (i = 0; i < pl->nr; i++) {
34 struct path_pattern *p = pl->patterns[i];
36 if (p->flags & PATTERN_FLAG_NEGATIVE)
39 fprintf(fp, "%s", p->pattern);
41 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
48 static int sparse_checkout_list(int argc, const char **argv)
50 struct pattern_list pl;
51 char *sparse_filename;
54 memset(&pl, 0, sizeof(pl));
56 pl.use_cone_patterns = core_sparse_checkout_cone;
58 sparse_filename = get_sparse_checkout_filename();
59 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
60 free(sparse_filename);
63 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
67 if (pl.use_cone_patterns) {
69 struct pattern_entry *pe;
70 struct hashmap_iter iter;
71 struct string_list sl = STRING_LIST_INIT_DUP;
73 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
74 /* pe->pattern starts with "/", skip it */
75 string_list_insert(&sl, pe->pattern + 1);
78 string_list_sort(&sl);
80 for (i = 0; i < sl.nr; i++)
81 printf("%s\n", sl.items[i].string);
86 write_patterns_to_file(stdout, &pl);
87 clear_pattern_list(&pl);
92 static int update_working_directory(struct pattern_list *pl)
95 struct unpack_trees_options o;
96 struct lock_file lock_file = LOCK_INIT;
100 struct repository *r = the_repository;
102 if (repo_read_index_unmerged(r))
103 die(_("you need to resolve your current index first"));
105 if (get_oid("HEAD", &oid))
108 tree = parse_tree_indirect(&oid);
110 init_tree_desc(&t, tree->buffer, tree->size);
112 memset(&o, 0, sizeof(o));
113 o.verbose_update = isatty(2);
118 o.src_index = r->index;
119 o.dst_index = r->index;
120 o.skip_sparse_checkout = 0;
122 o.keep_pattern_list = !!pl;
124 resolve_undo_clear_index(r->index);
127 cache_tree_free(&r->index->cache_tree);
129 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
131 core_apply_sparse_checkout = 1;
132 result = unpack_trees(1, &t, &o);
135 prime_cache_tree(r, r->index, tree);
136 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
138 rollback_lock_file(&lock_file);
143 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
146 struct pattern_entry *pe;
147 struct hashmap_iter iter;
148 struct string_list sl = STRING_LIST_INIT_DUP;
149 struct strbuf parent_pattern = STRBUF_INIT;
151 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
152 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
155 if (!hashmap_contains_parent(&pl->recursive_hashmap,
158 string_list_insert(&sl, pe->pattern);
161 string_list_sort(&sl);
162 string_list_remove_duplicates(&sl, 0);
164 fprintf(fp, "/*\n!/*/\n");
166 for (i = 0; i < sl.nr; i++) {
167 char *pattern = sl.items[i].string;
170 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
173 string_list_clear(&sl, 0);
175 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
176 if (!hashmap_contains_parent(&pl->recursive_hashmap,
179 string_list_insert(&sl, pe->pattern);
182 strbuf_release(&parent_pattern);
184 string_list_sort(&sl);
185 string_list_remove_duplicates(&sl, 0);
187 for (i = 0; i < sl.nr; i++) {
188 char *pattern = sl.items[i].string;
189 fprintf(fp, "%s/\n", pattern);
193 static int write_patterns_and_update(struct pattern_list *pl)
195 char *sparse_filename;
198 struct lock_file lk = LOCK_INIT;
201 sparse_filename = get_sparse_checkout_filename();
203 if (safe_create_leading_directories(sparse_filename))
204 die(_("failed to create directory for sparse-checkout file"));
206 fd = hold_lock_file_for_update(&lk, sparse_filename,
209 result = update_working_directory(pl);
211 rollback_lock_file(&lk);
212 free(sparse_filename);
213 clear_pattern_list(pl);
214 update_working_directory(NULL);
218 fp = xfdopen(fd, "w");
220 if (core_sparse_checkout_cone)
221 write_cone_to_file(fp, pl);
223 write_patterns_to_file(fp, pl);
226 commit_lock_file(&lk);
228 free(sparse_filename);
229 clear_pattern_list(pl);
234 enum sparse_checkout_mode {
235 MODE_NO_PATTERNS = 0,
236 MODE_ALL_PATTERNS = 1,
237 MODE_CONE_PATTERNS = 2,
240 static int set_config(enum sparse_checkout_mode mode)
242 const char *config_path;
244 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
245 error(_("failed to set extensions.worktreeConfig setting"));
249 config_path = git_path("config.worktree");
250 git_config_set_in_file_gently(config_path,
251 "core.sparseCheckout",
252 mode ? "true" : NULL);
254 git_config_set_in_file_gently(config_path,
255 "core.sparseCheckoutCone",
256 mode == MODE_CONE_PATTERNS ? "true" : NULL);
261 static char const * const builtin_sparse_checkout_init_usage[] = {
262 N_("git sparse-checkout init [--cone]"),
266 static struct sparse_checkout_init_opts {
270 static int sparse_checkout_init(int argc, const char **argv)
272 struct pattern_list pl;
273 char *sparse_filename;
275 struct object_id oid;
277 struct strbuf pattern = STRBUF_INIT;
279 static struct option builtin_sparse_checkout_init_options[] = {
280 OPT_BOOL(0, "cone", &init_opts.cone_mode,
281 N_("initialize the sparse-checkout in cone mode")),
285 repo_read_index(the_repository);
286 require_clean_work_tree(the_repository,
287 N_("initialize sparse-checkout"), NULL, 1, 0);
289 argc = parse_options(argc, argv, NULL,
290 builtin_sparse_checkout_init_options,
291 builtin_sparse_checkout_init_usage, 0);
293 if (init_opts.cone_mode) {
294 mode = MODE_CONE_PATTERNS;
295 core_sparse_checkout_cone = 1;
297 mode = MODE_ALL_PATTERNS;
299 if (set_config(mode))
302 memset(&pl, 0, sizeof(pl));
304 sparse_filename = get_sparse_checkout_filename();
305 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
307 /* If we already have a sparse-checkout file, use it. */
309 free(sparse_filename);
310 core_apply_sparse_checkout = 1;
311 return update_working_directory(NULL);
314 if (get_oid("HEAD", &oid)) {
317 /* assume we are in a fresh repo, but update the sparse-checkout file */
318 fp = xfopen(sparse_filename, "w");
320 die(_("failed to open '%s'"), sparse_filename);
322 free(sparse_filename);
323 fprintf(fp, "/*\n!/*/\n");
328 strbuf_addstr(&pattern, "/*");
329 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
330 strbuf_addstr(&pattern, "!/*/");
331 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
333 return write_patterns_and_update(&pl);
336 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
338 struct pattern_entry *e = xmalloc(sizeof(*e));
339 e->patternlen = path->len;
340 e->pattern = strbuf_detach(path, NULL);
341 hashmap_entry_init(&e->ent,
343 strihash(e->pattern) :
344 strhash(e->pattern));
346 hashmap_add(&pl->recursive_hashmap, &e->ent);
348 while (e->patternlen) {
349 char *slash = strrchr(e->pattern, '/');
350 char *oldpattern = e->pattern;
353 if (slash == e->pattern)
356 newlen = slash - e->pattern;
357 e = xmalloc(sizeof(struct pattern_entry));
358 e->patternlen = newlen;
359 e->pattern = xstrndup(oldpattern, newlen);
360 hashmap_entry_init(&e->ent,
362 strihash(e->pattern) :
363 strhash(e->pattern));
365 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
366 hashmap_add(&pl->parent_hashmap, &e->ent);
370 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
374 strbuf_trim_trailing_dir_sep(line);
379 if (line->buf[0] != '/')
380 strbuf_insert(line, 0, "/", 1);
382 insert_recursive_pattern(pl, line);
385 static char const * const builtin_sparse_checkout_set_usage[] = {
386 N_("git sparse-checkout set (--stdin | <patterns>)"),
390 static struct sparse_checkout_set_opts {
394 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
397 struct pattern_list pl;
399 int changed_config = 0;
401 static struct option builtin_sparse_checkout_set_options[] = {
402 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
403 N_("read patterns from standard in")),
407 repo_read_index(the_repository);
408 require_clean_work_tree(the_repository,
409 N_("set sparse-checkout patterns"), NULL, 1, 0);
411 memset(&pl, 0, sizeof(pl));
413 argc = parse_options(argc, argv, prefix,
414 builtin_sparse_checkout_set_options,
415 builtin_sparse_checkout_set_usage,
416 PARSE_OPT_KEEP_UNKNOWN);
418 if (core_sparse_checkout_cone) {
419 struct strbuf line = STRBUF_INIT;
421 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
422 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
423 pl.use_cone_patterns = 1;
425 if (set_opts.use_stdin) {
426 while (!strbuf_getline(&line, stdin))
427 strbuf_to_cone_pattern(&line, &pl);
429 for (i = 0; i < argc; i++) {
430 strbuf_setlen(&line, 0);
431 strbuf_addstr(&line, argv[i]);
432 strbuf_to_cone_pattern(&line, &pl);
436 if (set_opts.use_stdin) {
437 struct strbuf line = STRBUF_INIT;
439 while (!strbuf_getline(&line, stdin)) {
441 char *buf = strbuf_detach(&line, &len);
442 add_pattern(buf, empty_base, 0, &pl, 0);
445 for (i = 0; i < argc; i++)
446 add_pattern(argv[i], empty_base, 0, &pl, 0);
450 if (!core_apply_sparse_checkout) {
451 set_config(MODE_ALL_PATTERNS);
452 core_apply_sparse_checkout = 1;
456 result = write_patterns_and_update(&pl);
458 if (result && changed_config)
459 set_config(MODE_NO_PATTERNS);
461 clear_pattern_list(&pl);
465 static int sparse_checkout_disable(int argc, const char **argv)
467 struct pattern_list pl;
468 struct strbuf match_all = STRBUF_INIT;
470 repo_read_index(the_repository);
471 require_clean_work_tree(the_repository,
472 N_("disable sparse-checkout"), NULL, 1, 0);
474 memset(&pl, 0, sizeof(pl));
475 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
476 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
477 pl.use_cone_patterns = 0;
478 core_apply_sparse_checkout = 1;
480 strbuf_addstr(&match_all, "/*");
481 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
483 if (update_working_directory(&pl))
484 die(_("error while refreshing working directory"));
486 clear_pattern_list(&pl);
487 return set_config(MODE_NO_PATTERNS);
490 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
492 static struct option builtin_sparse_checkout_options[] = {
496 if (argc == 2 && !strcmp(argv[1], "-h"))
497 usage_with_options(builtin_sparse_checkout_usage,
498 builtin_sparse_checkout_options);
500 argc = parse_options(argc, argv, prefix,
501 builtin_sparse_checkout_options,
502 builtin_sparse_checkout_usage,
503 PARSE_OPT_STOP_AT_NON_OPTION);
505 git_config(git_default_config, NULL);
508 if (!strcmp(argv[0], "list"))
509 return sparse_checkout_list(argc, argv);
510 if (!strcmp(argv[0], "init"))
511 return sparse_checkout_init(argc, argv);
512 if (!strcmp(argv[0], "set"))
513 return sparse_checkout_set(argc, argv, prefix);
514 if (!strcmp(argv[0], "disable"))
515 return sparse_checkout_disable(argc, argv);
518 usage_with_options(builtin_sparse_checkout_usage,
519 builtin_sparse_checkout_options);