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"
16 static const char *empty_base = "";
18 static char const * const builtin_sparse_checkout_usage[] = {
19 N_("git sparse-checkout (init|list|set|disable) <options>"),
23 static char *get_sparse_checkout_filename(void)
25 return git_pathdup("info/sparse-checkout");
28 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
32 for (i = 0; i < pl->nr; i++) {
33 struct path_pattern *p = pl->patterns[i];
35 if (p->flags & PATTERN_FLAG_NEGATIVE)
38 fprintf(fp, "%s", p->pattern);
40 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
47 static int sparse_checkout_list(int argc, const char **argv)
49 struct pattern_list pl;
50 char *sparse_filename;
53 memset(&pl, 0, sizeof(pl));
55 sparse_filename = get_sparse_checkout_filename();
56 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
57 free(sparse_filename);
60 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
64 write_patterns_to_file(stdout, &pl);
65 clear_pattern_list(&pl);
70 static int update_working_directory(struct pattern_list *pl)
73 struct unpack_trees_options o;
74 struct lock_file lock_file = LOCK_INIT;
78 struct repository *r = the_repository;
80 if (repo_read_index_unmerged(r))
81 die(_("you need to resolve your current index first"));
83 if (get_oid("HEAD", &oid))
86 tree = parse_tree_indirect(&oid);
88 init_tree_desc(&t, tree->buffer, tree->size);
90 memset(&o, 0, sizeof(o));
91 o.verbose_update = isatty(2);
96 o.src_index = r->index;
97 o.dst_index = r->index;
98 o.skip_sparse_checkout = 0;
100 o.keep_pattern_list = !!pl;
102 resolve_undo_clear_index(r->index);
105 cache_tree_free(&r->index->cache_tree);
107 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
109 core_apply_sparse_checkout = 1;
110 result = unpack_trees(1, &t, &o);
113 prime_cache_tree(r, r->index, tree);
114 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
116 rollback_lock_file(&lock_file);
121 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
124 struct pattern_entry *pe;
125 struct hashmap_iter iter;
126 struct string_list sl = STRING_LIST_INIT_DUP;
127 struct strbuf parent_pattern = STRBUF_INIT;
129 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
130 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
133 if (!hashmap_contains_parent(&pl->recursive_hashmap,
136 string_list_insert(&sl, pe->pattern);
139 string_list_sort(&sl);
140 string_list_remove_duplicates(&sl, 0);
142 fprintf(fp, "/*\n!/*/\n");
144 for (i = 0; i < sl.nr; i++) {
145 char *pattern = sl.items[i].string;
148 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
151 string_list_clear(&sl, 0);
153 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
154 if (!hashmap_contains_parent(&pl->recursive_hashmap,
157 string_list_insert(&sl, pe->pattern);
160 strbuf_release(&parent_pattern);
162 string_list_sort(&sl);
163 string_list_remove_duplicates(&sl, 0);
165 for (i = 0; i < sl.nr; i++) {
166 char *pattern = sl.items[i].string;
167 fprintf(fp, "%s/\n", pattern);
171 static int write_patterns_and_update(struct pattern_list *pl)
173 char *sparse_filename;
176 struct lock_file lk = LOCK_INIT;
179 sparse_filename = get_sparse_checkout_filename();
180 fd = hold_lock_file_for_update(&lk, sparse_filename,
183 result = update_working_directory(pl);
185 rollback_lock_file(&lk);
186 free(sparse_filename);
187 clear_pattern_list(pl);
188 update_working_directory(NULL);
192 fp = xfdopen(fd, "w");
194 if (core_sparse_checkout_cone)
195 write_cone_to_file(fp, pl);
197 write_patterns_to_file(fp, pl);
200 commit_lock_file(&lk);
202 free(sparse_filename);
203 clear_pattern_list(pl);
208 enum sparse_checkout_mode {
209 MODE_NO_PATTERNS = 0,
210 MODE_ALL_PATTERNS = 1,
211 MODE_CONE_PATTERNS = 2,
214 static int set_config(enum sparse_checkout_mode mode)
216 const char *config_path;
218 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
219 error(_("failed to set extensions.worktreeConfig setting"));
223 config_path = git_path("config.worktree");
224 git_config_set_in_file_gently(config_path,
225 "core.sparseCheckout",
226 mode ? "true" : NULL);
228 git_config_set_in_file_gently(config_path,
229 "core.sparseCheckoutCone",
230 mode == MODE_CONE_PATTERNS ? "true" : NULL);
235 static char const * const builtin_sparse_checkout_init_usage[] = {
236 N_("git sparse-checkout init [--cone]"),
240 static struct sparse_checkout_init_opts {
244 static int sparse_checkout_init(int argc, const char **argv)
246 struct pattern_list pl;
247 char *sparse_filename;
249 struct object_id oid;
251 struct strbuf pattern = STRBUF_INIT;
253 static struct option builtin_sparse_checkout_init_options[] = {
254 OPT_BOOL(0, "cone", &init_opts.cone_mode,
255 N_("initialize the sparse-checkout in cone mode")),
259 argc = parse_options(argc, argv, NULL,
260 builtin_sparse_checkout_init_options,
261 builtin_sparse_checkout_init_usage, 0);
263 if (init_opts.cone_mode) {
264 mode = MODE_CONE_PATTERNS;
265 core_sparse_checkout_cone = 1;
267 mode = MODE_ALL_PATTERNS;
269 if (set_config(mode))
272 memset(&pl, 0, sizeof(pl));
274 sparse_filename = get_sparse_checkout_filename();
275 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
277 /* If we already have a sparse-checkout file, use it. */
279 free(sparse_filename);
280 core_apply_sparse_checkout = 1;
281 return update_working_directory(NULL);
284 if (get_oid("HEAD", &oid)) {
287 /* assume we are in a fresh repo, but update the sparse-checkout file */
288 fp = xfopen(sparse_filename, "w");
290 die(_("failed to open '%s'"), sparse_filename);
292 free(sparse_filename);
293 fprintf(fp, "/*\n!/*/\n");
298 strbuf_addstr(&pattern, "/*");
299 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
300 strbuf_addstr(&pattern, "!/*/");
301 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
303 return write_patterns_and_update(&pl);
306 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
308 struct pattern_entry *e = xmalloc(sizeof(*e));
309 e->patternlen = path->len;
310 e->pattern = strbuf_detach(path, NULL);
311 hashmap_entry_init(&e->ent, memhash(e->pattern, e->patternlen));
313 hashmap_add(&pl->recursive_hashmap, &e->ent);
315 while (e->patternlen) {
316 char *slash = strrchr(e->pattern, '/');
317 char *oldpattern = e->pattern;
320 if (slash == e->pattern)
323 newlen = slash - e->pattern;
324 e = xmalloc(sizeof(struct pattern_entry));
325 e->patternlen = newlen;
326 e->pattern = xstrndup(oldpattern, newlen);
327 hashmap_entry_init(&e->ent, memhash(e->pattern, e->patternlen));
329 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
330 hashmap_add(&pl->parent_hashmap, &e->ent);
334 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
338 strbuf_trim_trailing_dir_sep(line);
343 if (line->buf[0] != '/')
344 strbuf_insert(line, 0, "/", 1);
346 insert_recursive_pattern(pl, line);
349 static char const * const builtin_sparse_checkout_set_usage[] = {
350 N_("git sparse-checkout set (--stdin | <patterns>)"),
354 static struct sparse_checkout_set_opts {
358 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
361 struct pattern_list pl;
363 int changed_config = 0;
365 static struct option builtin_sparse_checkout_set_options[] = {
366 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
367 N_("read patterns from standard in")),
371 memset(&pl, 0, sizeof(pl));
373 argc = parse_options(argc, argv, prefix,
374 builtin_sparse_checkout_set_options,
375 builtin_sparse_checkout_set_usage,
376 PARSE_OPT_KEEP_UNKNOWN);
378 if (core_sparse_checkout_cone) {
379 struct strbuf line = STRBUF_INIT;
381 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
382 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
383 pl.use_cone_patterns = 1;
385 if (set_opts.use_stdin) {
386 while (!strbuf_getline(&line, stdin))
387 strbuf_to_cone_pattern(&line, &pl);
389 for (i = 0; i < argc; i++) {
390 strbuf_setlen(&line, 0);
391 strbuf_addstr(&line, argv[i]);
392 strbuf_to_cone_pattern(&line, &pl);
396 if (set_opts.use_stdin) {
397 struct strbuf line = STRBUF_INIT;
399 while (!strbuf_getline(&line, stdin)) {
401 char *buf = strbuf_detach(&line, &len);
402 add_pattern(buf, empty_base, 0, &pl, 0);
405 for (i = 0; i < argc; i++)
406 add_pattern(argv[i], empty_base, 0, &pl, 0);
410 if (!core_apply_sparse_checkout) {
411 set_config(MODE_ALL_PATTERNS);
412 core_apply_sparse_checkout = 1;
416 result = write_patterns_and_update(&pl);
418 if (result && changed_config)
419 set_config(MODE_NO_PATTERNS);
421 clear_pattern_list(&pl);
425 static int sparse_checkout_disable(int argc, const char **argv)
427 struct pattern_list pl;
428 struct strbuf match_all = STRBUF_INIT;
430 memset(&pl, 0, sizeof(pl));
431 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
432 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
433 pl.use_cone_patterns = 0;
434 core_apply_sparse_checkout = 1;
436 strbuf_addstr(&match_all, "/*");
437 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
439 if (update_working_directory(&pl))
440 die(_("error while refreshing working directory"));
442 clear_pattern_list(&pl);
443 return set_config(MODE_NO_PATTERNS);
446 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
448 static struct option builtin_sparse_checkout_options[] = {
452 if (argc == 2 && !strcmp(argv[1], "-h"))
453 usage_with_options(builtin_sparse_checkout_usage,
454 builtin_sparse_checkout_options);
456 argc = parse_options(argc, argv, prefix,
457 builtin_sparse_checkout_options,
458 builtin_sparse_checkout_usage,
459 PARSE_OPT_STOP_AT_NON_OPTION);
461 git_config(git_default_config, NULL);
464 if (!strcmp(argv[0], "list"))
465 return sparse_checkout_list(argc, argv);
466 if (!strcmp(argv[0], "init"))
467 return sparse_checkout_init(argc, argv);
468 if (!strcmp(argv[0], "set"))
469 return sparse_checkout_set(argc, argv, prefix);
470 if (!strcmp(argv[0], "disable"))
471 return sparse_checkout_disable(argc, argv);
474 usage_with_options(builtin_sparse_checkout_usage,
475 builtin_sparse_checkout_options);