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();
202 fd = hold_lock_file_for_update(&lk, sparse_filename,
205 result = update_working_directory(pl);
207 rollback_lock_file(&lk);
208 free(sparse_filename);
209 clear_pattern_list(pl);
210 update_working_directory(NULL);
214 fp = xfdopen(fd, "w");
216 if (core_sparse_checkout_cone)
217 write_cone_to_file(fp, pl);
219 write_patterns_to_file(fp, pl);
222 commit_lock_file(&lk);
224 free(sparse_filename);
225 clear_pattern_list(pl);
230 enum sparse_checkout_mode {
231 MODE_NO_PATTERNS = 0,
232 MODE_ALL_PATTERNS = 1,
233 MODE_CONE_PATTERNS = 2,
236 static int set_config(enum sparse_checkout_mode mode)
238 const char *config_path;
240 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
241 error(_("failed to set extensions.worktreeConfig setting"));
245 config_path = git_path("config.worktree");
246 git_config_set_in_file_gently(config_path,
247 "core.sparseCheckout",
248 mode ? "true" : NULL);
250 git_config_set_in_file_gently(config_path,
251 "core.sparseCheckoutCone",
252 mode == MODE_CONE_PATTERNS ? "true" : NULL);
257 static char const * const builtin_sparse_checkout_init_usage[] = {
258 N_("git sparse-checkout init [--cone]"),
262 static struct sparse_checkout_init_opts {
266 static int sparse_checkout_init(int argc, const char **argv)
268 struct pattern_list pl;
269 char *sparse_filename;
271 struct object_id oid;
273 struct strbuf pattern = STRBUF_INIT;
275 static struct option builtin_sparse_checkout_init_options[] = {
276 OPT_BOOL(0, "cone", &init_opts.cone_mode,
277 N_("initialize the sparse-checkout in cone mode")),
281 repo_read_index(the_repository);
282 require_clean_work_tree(the_repository,
283 N_("initialize sparse-checkout"), NULL, 1, 0);
285 argc = parse_options(argc, argv, NULL,
286 builtin_sparse_checkout_init_options,
287 builtin_sparse_checkout_init_usage, 0);
289 if (init_opts.cone_mode) {
290 mode = MODE_CONE_PATTERNS;
291 core_sparse_checkout_cone = 1;
293 mode = MODE_ALL_PATTERNS;
295 if (set_config(mode))
298 memset(&pl, 0, sizeof(pl));
300 sparse_filename = get_sparse_checkout_filename();
301 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
303 /* If we already have a sparse-checkout file, use it. */
305 free(sparse_filename);
306 core_apply_sparse_checkout = 1;
307 return update_working_directory(NULL);
310 if (get_oid("HEAD", &oid)) {
313 /* assume we are in a fresh repo, but update the sparse-checkout file */
314 fp = xfopen(sparse_filename, "w");
316 die(_("failed to open '%s'"), sparse_filename);
318 free(sparse_filename);
319 fprintf(fp, "/*\n!/*/\n");
324 strbuf_addstr(&pattern, "/*");
325 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
326 strbuf_addstr(&pattern, "!/*/");
327 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
329 return write_patterns_and_update(&pl);
332 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
334 struct pattern_entry *e = xmalloc(sizeof(*e));
335 e->patternlen = path->len;
336 e->pattern = strbuf_detach(path, NULL);
337 hashmap_entry_init(&e->ent,
339 strihash(e->pattern) :
340 strhash(e->pattern));
342 hashmap_add(&pl->recursive_hashmap, &e->ent);
344 while (e->patternlen) {
345 char *slash = strrchr(e->pattern, '/');
346 char *oldpattern = e->pattern;
349 if (slash == e->pattern)
352 newlen = slash - e->pattern;
353 e = xmalloc(sizeof(struct pattern_entry));
354 e->patternlen = newlen;
355 e->pattern = xstrndup(oldpattern, newlen);
356 hashmap_entry_init(&e->ent,
358 strihash(e->pattern) :
359 strhash(e->pattern));
361 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
362 hashmap_add(&pl->parent_hashmap, &e->ent);
366 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
370 strbuf_trim_trailing_dir_sep(line);
375 if (line->buf[0] != '/')
376 strbuf_insert(line, 0, "/", 1);
378 insert_recursive_pattern(pl, line);
381 static char const * const builtin_sparse_checkout_set_usage[] = {
382 N_("git sparse-checkout set (--stdin | <patterns>)"),
386 static struct sparse_checkout_set_opts {
390 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
393 struct pattern_list pl;
395 int changed_config = 0;
397 static struct option builtin_sparse_checkout_set_options[] = {
398 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
399 N_("read patterns from standard in")),
403 repo_read_index(the_repository);
404 require_clean_work_tree(the_repository,
405 N_("set sparse-checkout patterns"), NULL, 1, 0);
407 memset(&pl, 0, sizeof(pl));
409 argc = parse_options(argc, argv, prefix,
410 builtin_sparse_checkout_set_options,
411 builtin_sparse_checkout_set_usage,
412 PARSE_OPT_KEEP_UNKNOWN);
414 if (core_sparse_checkout_cone) {
415 struct strbuf line = STRBUF_INIT;
417 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
418 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
419 pl.use_cone_patterns = 1;
421 if (set_opts.use_stdin) {
422 while (!strbuf_getline(&line, stdin))
423 strbuf_to_cone_pattern(&line, &pl);
425 for (i = 0; i < argc; i++) {
426 strbuf_setlen(&line, 0);
427 strbuf_addstr(&line, argv[i]);
428 strbuf_to_cone_pattern(&line, &pl);
432 if (set_opts.use_stdin) {
433 struct strbuf line = STRBUF_INIT;
435 while (!strbuf_getline(&line, stdin)) {
437 char *buf = strbuf_detach(&line, &len);
438 add_pattern(buf, empty_base, 0, &pl, 0);
441 for (i = 0; i < argc; i++)
442 add_pattern(argv[i], empty_base, 0, &pl, 0);
446 if (!core_apply_sparse_checkout) {
447 set_config(MODE_ALL_PATTERNS);
448 core_apply_sparse_checkout = 1;
452 result = write_patterns_and_update(&pl);
454 if (result && changed_config)
455 set_config(MODE_NO_PATTERNS);
457 clear_pattern_list(&pl);
461 static int sparse_checkout_disable(int argc, const char **argv)
463 struct pattern_list pl;
464 struct strbuf match_all = STRBUF_INIT;
466 repo_read_index(the_repository);
467 require_clean_work_tree(the_repository,
468 N_("disable sparse-checkout"), NULL, 1, 0);
470 memset(&pl, 0, sizeof(pl));
471 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
472 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
473 pl.use_cone_patterns = 0;
474 core_apply_sparse_checkout = 1;
476 strbuf_addstr(&match_all, "/*");
477 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
479 if (update_working_directory(&pl))
480 die(_("error while refreshing working directory"));
482 clear_pattern_list(&pl);
483 return set_config(MODE_NO_PATTERNS);
486 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
488 static struct option builtin_sparse_checkout_options[] = {
492 if (argc == 2 && !strcmp(argv[1], "-h"))
493 usage_with_options(builtin_sparse_checkout_usage,
494 builtin_sparse_checkout_options);
496 argc = parse_options(argc, argv, prefix,
497 builtin_sparse_checkout_options,
498 builtin_sparse_checkout_usage,
499 PARSE_OPT_STOP_AT_NON_OPTION);
501 git_config(git_default_config, NULL);
504 if (!strcmp(argv[0], "list"))
505 return sparse_checkout_list(argc, argv);
506 if (!strcmp(argv[0], "init"))
507 return sparse_checkout_init(argc, argv);
508 if (!strcmp(argv[0], "set"))
509 return sparse_checkout_set(argc, argv, prefix);
510 if (!strcmp(argv[0], "disable"))
511 return sparse_checkout_disable(argc, argv);
514 usage_with_options(builtin_sparse_checkout_usage,
515 builtin_sparse_checkout_options);