]>
Commit | Line | Data |
---|---|---|
94c0956b DS |
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" | |
af09ce24 | 9 | #include "string-list.h" |
e091228e DS |
10 | #include "cache.h" |
11 | #include "cache-tree.h" | |
12 | #include "lockfile.h" | |
13 | #include "resolve-undo.h" | |
14 | #include "unpack-trees.h" | |
cff4e913 | 15 | #include "wt-status.h" |
94c0956b | 16 | |
416adc87 DS |
17 | static const char *empty_base = ""; |
18 | ||
94c0956b | 19 | static char const * const builtin_sparse_checkout_usage[] = { |
72918c1a | 20 | N_("git sparse-checkout (init|list|set|disable) <options>"), |
94c0956b DS |
21 | NULL |
22 | }; | |
23 | ||
24 | static char *get_sparse_checkout_filename(void) | |
25 | { | |
26 | return git_pathdup("info/sparse-checkout"); | |
27 | } | |
28 | ||
29 | static void write_patterns_to_file(FILE *fp, struct pattern_list *pl) | |
30 | { | |
31 | int i; | |
32 | ||
33 | for (i = 0; i < pl->nr; i++) { | |
34 | struct path_pattern *p = pl->patterns[i]; | |
35 | ||
36 | if (p->flags & PATTERN_FLAG_NEGATIVE) | |
37 | fprintf(fp, "!"); | |
38 | ||
39 | fprintf(fp, "%s", p->pattern); | |
40 | ||
41 | if (p->flags & PATTERN_FLAG_MUSTBEDIR) | |
42 | fprintf(fp, "/"); | |
43 | ||
44 | fprintf(fp, "\n"); | |
45 | } | |
46 | } | |
47 | ||
48 | static int sparse_checkout_list(int argc, const char **argv) | |
49 | { | |
50 | struct pattern_list pl; | |
51 | char *sparse_filename; | |
52 | int res; | |
53 | ||
54 | memset(&pl, 0, sizeof(pl)); | |
55 | ||
de11951b DS |
56 | pl.use_cone_patterns = core_sparse_checkout_cone; |
57 | ||
94c0956b DS |
58 | sparse_filename = get_sparse_checkout_filename(); |
59 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL); | |
60 | free(sparse_filename); | |
61 | ||
62 | if (res < 0) { | |
63 | warning(_("this worktree is not sparse (sparse-checkout file may not exist)")); | |
64 | return 0; | |
65 | } | |
66 | ||
de11951b DS |
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; | |
72 | ||
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); | |
76 | } | |
77 | ||
78 | string_list_sort(&sl); | |
79 | ||
80 | for (i = 0; i < sl.nr; i++) | |
81 | printf("%s\n", sl.items[i].string); | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
94c0956b DS |
86 | write_patterns_to_file(stdout, &pl); |
87 | clear_pattern_list(&pl); | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
e091228e | 92 | static int update_working_directory(struct pattern_list *pl) |
bab3c359 | 93 | { |
bab3c359 | 94 | int result = 0; |
e091228e DS |
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; | |
bab3c359 | 101 | |
e091228e DS |
102 | if (repo_read_index_unmerged(r)) |
103 | die(_("you need to resolve your current index first")); | |
104 | ||
105 | if (get_oid("HEAD", &oid)) | |
106 | return 0; | |
107 | ||
108 | tree = parse_tree_indirect(&oid); | |
109 | parse_tree(tree); | |
110 | init_tree_desc(&t, tree->buffer, tree->size); | |
111 | ||
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; | |
123 | ||
124 | resolve_undo_clear_index(r->index); | |
125 | setup_work_tree(); | |
126 | ||
127 | cache_tree_free(&r->index->cache_tree); | |
128 | ||
129 | repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR); | |
130 | ||
131 | core_apply_sparse_checkout = 1; | |
132 | result = unpack_trees(1, &t, &o); | |
133 | ||
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); | |
bab3c359 | 139 | |
bab3c359 DS |
140 | return result; |
141 | } | |
142 | ||
af09ce24 DS |
143 | static void write_cone_to_file(FILE *fp, struct pattern_list *pl) |
144 | { | |
145 | int i; | |
146 | struct pattern_entry *pe; | |
147 | struct hashmap_iter iter; | |
148 | struct string_list sl = STRING_LIST_INIT_DUP; | |
e9de487a | 149 | struct strbuf parent_pattern = STRBUF_INIT; |
af09ce24 | 150 | |
e9de487a DS |
151 | hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) { |
152 | if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL)) | |
153 | continue; | |
154 | ||
155 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
156 | pe->pattern, | |
157 | &parent_pattern)) | |
158 | string_list_insert(&sl, pe->pattern); | |
159 | } | |
af09ce24 DS |
160 | |
161 | string_list_sort(&sl); | |
162 | string_list_remove_duplicates(&sl, 0); | |
163 | ||
164 | fprintf(fp, "/*\n!/*/\n"); | |
165 | ||
166 | for (i = 0; i < sl.nr; i++) { | |
167 | char *pattern = sl.items[i].string; | |
168 | ||
169 | if (strlen(pattern)) | |
170 | fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern); | |
171 | } | |
172 | ||
173 | string_list_clear(&sl, 0); | |
174 | ||
e9de487a DS |
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); | |
180 | } | |
181 | ||
182 | strbuf_release(&parent_pattern); | |
af09ce24 DS |
183 | |
184 | string_list_sort(&sl); | |
185 | string_list_remove_duplicates(&sl, 0); | |
186 | ||
187 | for (i = 0; i < sl.nr; i++) { | |
188 | char *pattern = sl.items[i].string; | |
189 | fprintf(fp, "%s/\n", pattern); | |
190 | } | |
191 | } | |
192 | ||
193 | static int write_patterns_and_update(struct pattern_list *pl) | |
194 | { | |
195 | char *sparse_filename; | |
196 | FILE *fp; | |
fb10ca5b DS |
197 | int fd; |
198 | struct lock_file lk = LOCK_INIT; | |
e091228e DS |
199 | int result; |
200 | ||
fb10ca5b | 201 | sparse_filename = get_sparse_checkout_filename(); |
3c754067 DS |
202 | |
203 | if (safe_create_leading_directories(sparse_filename)) | |
204 | die(_("failed to create directory for sparse-checkout file")); | |
205 | ||
fb10ca5b DS |
206 | fd = hold_lock_file_for_update(&lk, sparse_filename, |
207 | LOCK_DIE_ON_ERROR); | |
e091228e | 208 | |
fb10ca5b | 209 | result = update_working_directory(pl); |
e091228e | 210 | if (result) { |
fb10ca5b DS |
211 | rollback_lock_file(&lk); |
212 | free(sparse_filename); | |
e091228e DS |
213 | clear_pattern_list(pl); |
214 | update_working_directory(NULL); | |
215 | return result; | |
216 | } | |
af09ce24 | 217 | |
fb10ca5b | 218 | fp = xfdopen(fd, "w"); |
af09ce24 DS |
219 | |
220 | if (core_sparse_checkout_cone) | |
221 | write_cone_to_file(fp, pl); | |
222 | else | |
223 | write_patterns_to_file(fp, pl); | |
224 | ||
fb10ca5b DS |
225 | fflush(fp); |
226 | commit_lock_file(&lk); | |
e091228e | 227 | |
af09ce24 | 228 | free(sparse_filename); |
e091228e | 229 | clear_pattern_list(pl); |
af09ce24 | 230 | |
e091228e | 231 | return 0; |
af09ce24 DS |
232 | } |
233 | ||
bab3c359 DS |
234 | enum sparse_checkout_mode { |
235 | MODE_NO_PATTERNS = 0, | |
236 | MODE_ALL_PATTERNS = 1, | |
af09ce24 | 237 | MODE_CONE_PATTERNS = 2, |
bab3c359 DS |
238 | }; |
239 | ||
240 | static int set_config(enum sparse_checkout_mode mode) | |
241 | { | |
242 | const char *config_path; | |
243 | ||
244 | if (git_config_set_gently("extensions.worktreeConfig", "true")) { | |
245 | error(_("failed to set extensions.worktreeConfig setting")); | |
246 | return 1; | |
247 | } | |
248 | ||
249 | config_path = git_path("config.worktree"); | |
250 | git_config_set_in_file_gently(config_path, | |
251 | "core.sparseCheckout", | |
252 | mode ? "true" : NULL); | |
253 | ||
af09ce24 DS |
254 | git_config_set_in_file_gently(config_path, |
255 | "core.sparseCheckoutCone", | |
256 | mode == MODE_CONE_PATTERNS ? "true" : NULL); | |
257 | ||
bab3c359 DS |
258 | return 0; |
259 | } | |
260 | ||
af09ce24 DS |
261 | static char const * const builtin_sparse_checkout_init_usage[] = { |
262 | N_("git sparse-checkout init [--cone]"), | |
263 | NULL | |
264 | }; | |
265 | ||
266 | static struct sparse_checkout_init_opts { | |
267 | int cone_mode; | |
268 | } init_opts; | |
269 | ||
bab3c359 DS |
270 | static int sparse_checkout_init(int argc, const char **argv) |
271 | { | |
272 | struct pattern_list pl; | |
273 | char *sparse_filename; | |
bab3c359 | 274 | int res; |
d89f09c8 | 275 | struct object_id oid; |
af09ce24 | 276 | int mode; |
416adc87 | 277 | struct strbuf pattern = STRBUF_INIT; |
bab3c359 | 278 | |
af09ce24 DS |
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")), | |
282 | OPT_END(), | |
283 | }; | |
284 | ||
cff4e913 DS |
285 | repo_read_index(the_repository); |
286 | require_clean_work_tree(the_repository, | |
287 | N_("initialize sparse-checkout"), NULL, 1, 0); | |
288 | ||
af09ce24 DS |
289 | argc = parse_options(argc, argv, NULL, |
290 | builtin_sparse_checkout_init_options, | |
291 | builtin_sparse_checkout_init_usage, 0); | |
292 | ||
e091228e DS |
293 | if (init_opts.cone_mode) { |
294 | mode = MODE_CONE_PATTERNS; | |
295 | core_sparse_checkout_cone = 1; | |
296 | } else | |
297 | mode = MODE_ALL_PATTERNS; | |
af09ce24 DS |
298 | |
299 | if (set_config(mode)) | |
bab3c359 DS |
300 | return 1; |
301 | ||
302 | memset(&pl, 0, sizeof(pl)); | |
303 | ||
304 | sparse_filename = get_sparse_checkout_filename(); | |
305 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL); | |
306 | ||
307 | /* If we already have a sparse-checkout file, use it. */ | |
308 | if (res >= 0) { | |
309 | free(sparse_filename); | |
416adc87 DS |
310 | core_apply_sparse_checkout = 1; |
311 | return update_working_directory(NULL); | |
bab3c359 DS |
312 | } |
313 | ||
416adc87 DS |
314 | if (get_oid("HEAD", &oid)) { |
315 | FILE *fp; | |
bab3c359 | 316 | |
416adc87 DS |
317 | /* assume we are in a fresh repo, but update the sparse-checkout file */ |
318 | fp = xfopen(sparse_filename, "w"); | |
319 | if (!fp) | |
320 | die(_("failed to open '%s'"), sparse_filename); | |
bab3c359 | 321 | |
416adc87 DS |
322 | free(sparse_filename); |
323 | fprintf(fp, "/*\n!/*/\n"); | |
324 | fclose(fp); | |
d89f09c8 DS |
325 | return 0; |
326 | } | |
327 | ||
416adc87 DS |
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); | |
332 | ||
333 | return write_patterns_and_update(&pl); | |
bab3c359 DS |
334 | } |
335 | ||
af09ce24 | 336 | static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) |
f6039a94 | 337 | { |
af09ce24 DS |
338 | struct pattern_entry *e = xmalloc(sizeof(*e)); |
339 | e->patternlen = path->len; | |
340 | e->pattern = strbuf_detach(path, NULL); | |
190a65f9 DS |
341 | hashmap_entry_init(&e->ent, |
342 | ignore_case ? | |
343 | strihash(e->pattern) : | |
344 | strhash(e->pattern)); | |
f6039a94 | 345 | |
af09ce24 | 346 | hashmap_add(&pl->recursive_hashmap, &e->ent); |
f6039a94 | 347 | |
af09ce24 DS |
348 | while (e->patternlen) { |
349 | char *slash = strrchr(e->pattern, '/'); | |
350 | char *oldpattern = e->pattern; | |
351 | size_t newlen; | |
352 | ||
353 | if (slash == e->pattern) | |
354 | break; | |
355 | ||
356 | newlen = slash - e->pattern; | |
357 | e = xmalloc(sizeof(struct pattern_entry)); | |
358 | e->patternlen = newlen; | |
359 | e->pattern = xstrndup(oldpattern, newlen); | |
190a65f9 DS |
360 | hashmap_entry_init(&e->ent, |
361 | ignore_case ? | |
362 | strihash(e->pattern) : | |
363 | strhash(e->pattern)); | |
af09ce24 DS |
364 | |
365 | if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL)) | |
366 | hashmap_add(&pl->parent_hashmap, &e->ent); | |
367 | } | |
368 | } | |
369 | ||
370 | static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) | |
371 | { | |
372 | strbuf_trim(line); | |
373 | ||
374 | strbuf_trim_trailing_dir_sep(line); | |
375 | ||
376 | if (!line->len) | |
377 | return; | |
378 | ||
379 | if (line->buf[0] != '/') | |
380 | strbuf_insert(line, 0, "/", 1); | |
381 | ||
382 | insert_recursive_pattern(pl, line); | |
f6039a94 DS |
383 | } |
384 | ||
7bffca95 DS |
385 | static char const * const builtin_sparse_checkout_set_usage[] = { |
386 | N_("git sparse-checkout set (--stdin | <patterns>)"), | |
387 | NULL | |
388 | }; | |
389 | ||
390 | static struct sparse_checkout_set_opts { | |
391 | int use_stdin; | |
392 | } set_opts; | |
393 | ||
f6039a94 DS |
394 | static int sparse_checkout_set(int argc, const char **argv, const char *prefix) |
395 | { | |
f6039a94 DS |
396 | int i; |
397 | struct pattern_list pl; | |
398 | int result; | |
399 | int changed_config = 0; | |
7bffca95 DS |
400 | |
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")), | |
404 | OPT_END(), | |
405 | }; | |
406 | ||
cff4e913 DS |
407 | repo_read_index(the_repository); |
408 | require_clean_work_tree(the_repository, | |
409 | N_("set sparse-checkout patterns"), NULL, 1, 0); | |
410 | ||
f6039a94 DS |
411 | memset(&pl, 0, sizeof(pl)); |
412 | ||
7bffca95 DS |
413 | argc = parse_options(argc, argv, prefix, |
414 | builtin_sparse_checkout_set_options, | |
415 | builtin_sparse_checkout_set_usage, | |
416 | PARSE_OPT_KEEP_UNKNOWN); | |
417 | ||
af09ce24 | 418 | if (core_sparse_checkout_cone) { |
7bffca95 DS |
419 | struct strbuf line = STRBUF_INIT; |
420 | ||
af09ce24 DS |
421 | hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
422 | hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
e091228e | 423 | pl.use_cone_patterns = 1; |
af09ce24 DS |
424 | |
425 | if (set_opts.use_stdin) { | |
426 | while (!strbuf_getline(&line, stdin)) | |
427 | strbuf_to_cone_pattern(&line, &pl); | |
428 | } else { | |
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); | |
433 | } | |
7bffca95 DS |
434 | } |
435 | } else { | |
af09ce24 DS |
436 | if (set_opts.use_stdin) { |
437 | struct strbuf line = STRBUF_INIT; | |
438 | ||
439 | while (!strbuf_getline(&line, stdin)) { | |
440 | size_t len; | |
441 | char *buf = strbuf_detach(&line, &len); | |
442 | add_pattern(buf, empty_base, 0, &pl, 0); | |
443 | } | |
444 | } else { | |
445 | for (i = 0; i < argc; i++) | |
446 | add_pattern(argv[i], empty_base, 0, &pl, 0); | |
447 | } | |
7bffca95 | 448 | } |
f6039a94 DS |
449 | |
450 | if (!core_apply_sparse_checkout) { | |
451 | set_config(MODE_ALL_PATTERNS); | |
452 | core_apply_sparse_checkout = 1; | |
453 | changed_config = 1; | |
454 | } | |
455 | ||
456 | result = write_patterns_and_update(&pl); | |
457 | ||
458 | if (result && changed_config) | |
459 | set_config(MODE_NO_PATTERNS); | |
460 | ||
461 | clear_pattern_list(&pl); | |
462 | return result; | |
463 | } | |
464 | ||
72918c1a DS |
465 | static int sparse_checkout_disable(int argc, const char **argv) |
466 | { | |
99dfa6f9 DS |
467 | struct pattern_list pl; |
468 | struct strbuf match_all = STRBUF_INIT; | |
72918c1a | 469 | |
cff4e913 DS |
470 | repo_read_index(the_repository); |
471 | require_clean_work_tree(the_repository, | |
472 | N_("disable sparse-checkout"), NULL, 1, 0); | |
473 | ||
99dfa6f9 DS |
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; | |
72918c1a | 479 | |
99dfa6f9 DS |
480 | strbuf_addstr(&match_all, "/*"); |
481 | add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0); | |
72918c1a | 482 | |
99dfa6f9 | 483 | if (update_working_directory(&pl)) |
72918c1a DS |
484 | die(_("error while refreshing working directory")); |
485 | ||
99dfa6f9 | 486 | clear_pattern_list(&pl); |
72918c1a DS |
487 | return set_config(MODE_NO_PATTERNS); |
488 | } | |
489 | ||
94c0956b DS |
490 | int cmd_sparse_checkout(int argc, const char **argv, const char *prefix) |
491 | { | |
492 | static struct option builtin_sparse_checkout_options[] = { | |
493 | OPT_END(), | |
494 | }; | |
495 | ||
496 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
497 | usage_with_options(builtin_sparse_checkout_usage, | |
498 | builtin_sparse_checkout_options); | |
499 | ||
500 | argc = parse_options(argc, argv, prefix, | |
501 | builtin_sparse_checkout_options, | |
502 | builtin_sparse_checkout_usage, | |
503 | PARSE_OPT_STOP_AT_NON_OPTION); | |
504 | ||
505 | git_config(git_default_config, NULL); | |
506 | ||
507 | if (argc > 0) { | |
508 | if (!strcmp(argv[0], "list")) | |
509 | return sparse_checkout_list(argc, argv); | |
bab3c359 DS |
510 | if (!strcmp(argv[0], "init")) |
511 | return sparse_checkout_init(argc, argv); | |
f6039a94 DS |
512 | if (!strcmp(argv[0], "set")) |
513 | return sparse_checkout_set(argc, argv, prefix); | |
72918c1a DS |
514 | if (!strcmp(argv[0], "disable")) |
515 | return sparse_checkout_disable(argc, argv); | |
94c0956b DS |
516 | } |
517 | ||
518 | usage_with_options(builtin_sparse_checkout_usage, | |
519 | builtin_sparse_checkout_options); | |
520 | } |