Merge tag 'v2.25.1' into debian-sid
[git/debian.git] / builtin / sparse-checkout.c
blobb3bed891cb15e9a97a587cf2a6dab49f309578d2
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 #include "pathspec.h"
6 #include "repository.h"
7 #include "run-command.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10 #include "cache.h"
11 #include "cache-tree.h"
12 #include "lockfile.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>"),
21 NULL
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)
31 int i;
33 for (i = 0; i < pl->nr; i++) {
34 struct path_pattern *p = pl->patterns[i];
36 if (p->flags & PATTERN_FLAG_NEGATIVE)
37 fprintf(fp, "!");
39 fprintf(fp, "%s", p->pattern);
41 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
42 fprintf(fp, "/");
44 fprintf(fp, "\n");
48 static int sparse_checkout_list(int argc, const char **argv)
50 struct pattern_list pl;
51 char *sparse_filename;
52 int res;
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);
62 if (res < 0) {
63 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
64 return 0;
67 if (pl.use_cone_patterns) {
68 int i;
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);
83 return 0;
86 write_patterns_to_file(stdout, &pl);
87 clear_pattern_list(&pl);
89 return 0;
92 static int update_working_directory(struct pattern_list *pl)
94 int result = 0;
95 struct unpack_trees_options o;
96 struct lock_file lock_file = LOCK_INIT;
97 struct object_id oid;
98 struct tree *tree;
99 struct tree_desc t;
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))
106 return 0;
108 tree = parse_tree_indirect(&oid);
109 parse_tree(tree);
110 init_tree_desc(&t, tree->buffer, tree->size);
112 memset(&o, 0, sizeof(o));
113 o.verbose_update = isatty(2);
114 o.merge = 1;
115 o.update = 1;
116 o.fn = oneway_merge;
117 o.head_idx = -1;
118 o.src_index = r->index;
119 o.dst_index = r->index;
120 o.skip_sparse_checkout = 0;
121 o.pl = pl;
122 o.keep_pattern_list = !!pl;
124 resolve_undo_clear_index(r->index);
125 setup_work_tree();
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);
134 if (!result) {
135 prime_cache_tree(r, r->index, tree);
136 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
137 } else
138 rollback_lock_file(&lock_file);
140 return result;
143 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
145 int i;
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))
153 continue;
155 if (!hashmap_contains_parent(&pl->recursive_hashmap,
156 pe->pattern,
157 &parent_pattern))
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;
169 if (strlen(pattern))
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,
177 pe->pattern,
178 &parent_pattern))
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;
196 FILE *fp;
197 int fd;
198 struct lock_file lk = LOCK_INIT;
199 int result;
201 sparse_filename = get_sparse_checkout_filename();
202 fd = hold_lock_file_for_update(&lk, sparse_filename,
203 LOCK_DIE_ON_ERROR);
205 result = update_working_directory(pl);
206 if (result) {
207 rollback_lock_file(&lk);
208 free(sparse_filename);
209 clear_pattern_list(pl);
210 update_working_directory(NULL);
211 return result;
214 fp = xfdopen(fd, "w");
216 if (core_sparse_checkout_cone)
217 write_cone_to_file(fp, pl);
218 else
219 write_patterns_to_file(fp, pl);
221 fflush(fp);
222 commit_lock_file(&lk);
224 free(sparse_filename);
225 clear_pattern_list(pl);
227 return 0;
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"));
242 return 1;
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);
254 return 0;
257 static char const * const builtin_sparse_checkout_init_usage[] = {
258 N_("git sparse-checkout init [--cone]"),
259 NULL
262 static struct sparse_checkout_init_opts {
263 int cone_mode;
264 } init_opts;
266 static int sparse_checkout_init(int argc, const char **argv)
268 struct pattern_list pl;
269 char *sparse_filename;
270 int res;
271 struct object_id oid;
272 int mode;
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")),
278 OPT_END(),
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;
292 } else
293 mode = MODE_ALL_PATTERNS;
295 if (set_config(mode))
296 return 1;
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. */
304 if (res >= 0) {
305 free(sparse_filename);
306 core_apply_sparse_checkout = 1;
307 return update_working_directory(NULL);
310 if (get_oid("HEAD", &oid)) {
311 FILE *fp;
313 /* assume we are in a fresh repo, but update the sparse-checkout file */
314 fp = xfopen(sparse_filename, "w");
315 if (!fp)
316 die(_("failed to open '%s'"), sparse_filename);
318 free(sparse_filename);
319 fprintf(fp, "/*\n!/*/\n");
320 fclose(fp);
321 return 0;
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,
338 ignore_case ?
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;
347 size_t newlen;
349 if (slash == e->pattern)
350 break;
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,
357 ignore_case ?
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)
368 strbuf_trim(line);
370 strbuf_trim_trailing_dir_sep(line);
372 if (!line->len)
373 return;
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>)"),
383 NULL
386 static struct sparse_checkout_set_opts {
387 int use_stdin;
388 } set_opts;
390 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
392 int i;
393 struct pattern_list pl;
394 int result;
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")),
400 OPT_END(),
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);
424 } else {
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);
431 } else {
432 if (set_opts.use_stdin) {
433 struct strbuf line = STRBUF_INIT;
435 while (!strbuf_getline(&line, stdin)) {
436 size_t len;
437 char *buf = strbuf_detach(&line, &len);
438 add_pattern(buf, empty_base, 0, &pl, 0);
440 } else {
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;
449 changed_config = 1;
452 result = write_patterns_and_update(&pl);
454 if (result && changed_config)
455 set_config(MODE_NO_PATTERNS);
457 clear_pattern_list(&pl);
458 return result;
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[] = {
489 OPT_END(),
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);
503 if (argc > 0) {
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);