]>
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" |
d585f0e7 | 16 | #include "quote.h" |
122ba1f7 | 17 | #include "sparse-index.h" |
94c0956b | 18 | |
416adc87 DS |
19 | static const char *empty_base = ""; |
20 | ||
94c0956b | 21 | static char const * const builtin_sparse_checkout_usage[] = { |
5644ca28 | 22 | N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"), |
94c0956b DS |
23 | NULL |
24 | }; | |
25 | ||
94c0956b DS |
26 | static void write_patterns_to_file(FILE *fp, struct pattern_list *pl) |
27 | { | |
28 | int i; | |
29 | ||
30 | for (i = 0; i < pl->nr; i++) { | |
31 | struct path_pattern *p = pl->patterns[i]; | |
32 | ||
33 | if (p->flags & PATTERN_FLAG_NEGATIVE) | |
34 | fprintf(fp, "!"); | |
35 | ||
36 | fprintf(fp, "%s", p->pattern); | |
37 | ||
38 | if (p->flags & PATTERN_FLAG_MUSTBEDIR) | |
39 | fprintf(fp, "/"); | |
40 | ||
41 | fprintf(fp, "\n"); | |
42 | } | |
43 | } | |
44 | ||
75d3bee1 JK |
45 | static char const * const builtin_sparse_checkout_list_usage[] = { |
46 | N_("git sparse-checkout list"), | |
47 | NULL | |
48 | }; | |
49 | ||
94c0956b DS |
50 | static int sparse_checkout_list(int argc, const char **argv) |
51 | { | |
75d3bee1 JK |
52 | static struct option builtin_sparse_checkout_list_options[] = { |
53 | OPT_END(), | |
54 | }; | |
94c0956b DS |
55 | struct pattern_list pl; |
56 | char *sparse_filename; | |
57 | int res; | |
58 | ||
45c5e470 EN |
59 | if (!core_apply_sparse_checkout) |
60 | die(_("this worktree is not sparse")); | |
61 | ||
75d3bee1 JK |
62 | argc = parse_options(argc, argv, NULL, |
63 | builtin_sparse_checkout_list_options, | |
64 | builtin_sparse_checkout_list_usage, 0); | |
65 | ||
94c0956b DS |
66 | memset(&pl, 0, sizeof(pl)); |
67 | ||
de11951b DS |
68 | pl.use_cone_patterns = core_sparse_checkout_cone; |
69 | ||
94c0956b | 70 | sparse_filename = get_sparse_checkout_filename(); |
1679d60b | 71 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); |
94c0956b DS |
72 | free(sparse_filename); |
73 | ||
74 | if (res < 0) { | |
75 | warning(_("this worktree is not sparse (sparse-checkout file may not exist)")); | |
76 | return 0; | |
77 | } | |
78 | ||
de11951b DS |
79 | if (pl.use_cone_patterns) { |
80 | int i; | |
81 | struct pattern_entry *pe; | |
82 | struct hashmap_iter iter; | |
83 | struct string_list sl = STRING_LIST_INIT_DUP; | |
84 | ||
85 | hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) { | |
86 | /* pe->pattern starts with "/", skip it */ | |
87 | string_list_insert(&sl, pe->pattern + 1); | |
88 | } | |
89 | ||
90 | string_list_sort(&sl); | |
91 | ||
e55682ea DS |
92 | for (i = 0; i < sl.nr; i++) { |
93 | quote_c_style(sl.items[i].string, NULL, stdout, 0); | |
94 | printf("\n"); | |
95 | } | |
de11951b DS |
96 | |
97 | return 0; | |
98 | } | |
99 | ||
94c0956b DS |
100 | write_patterns_to_file(stdout, &pl); |
101 | clear_pattern_list(&pl); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
55dfcf95 DS |
106 | static void clean_tracked_sparse_directories(struct repository *r) |
107 | { | |
108 | int i, was_full = 0; | |
109 | struct strbuf path = STRBUF_INIT; | |
110 | size_t pathlen; | |
111 | struct string_list_item *item; | |
112 | struct string_list sparse_dirs = STRING_LIST_INIT_DUP; | |
113 | ||
114 | /* | |
115 | * If we are not using cone mode patterns, then we cannot | |
116 | * delete directories outside of the sparse cone. | |
117 | */ | |
118 | if (!r || !r->index || !r->worktree) | |
119 | return; | |
120 | if (init_sparse_checkout_patterns(r->index) || | |
121 | !r->index->sparse_checkout_patterns->use_cone_patterns) | |
122 | return; | |
123 | ||
124 | /* | |
125 | * Use the sparse index as a data structure to assist finding | |
126 | * directories that are safe to delete. This conversion to a | |
127 | * sparse index will not delete directories that contain | |
128 | * conflicted entries or submodules. | |
129 | */ | |
130 | if (!r->index->sparse_index) { | |
131 | /* | |
132 | * If something, such as a merge conflict or other concern, | |
133 | * prevents us from converting to a sparse index, then do | |
134 | * not try deleting files. | |
135 | */ | |
136 | if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY)) | |
137 | return; | |
138 | was_full = 1; | |
139 | } | |
140 | ||
141 | strbuf_addstr(&path, r->worktree); | |
142 | strbuf_complete(&path, '/'); | |
143 | pathlen = path.len; | |
144 | ||
145 | /* | |
146 | * Collect directories that have gone out of scope but also | |
147 | * exist on disk, so there is some work to be done. We need to | |
148 | * store the entries in a list before exploring, since that might | |
149 | * expand the sparse-index again. | |
150 | */ | |
151 | for (i = 0; i < r->index->cache_nr; i++) { | |
152 | struct cache_entry *ce = r->index->cache[i]; | |
153 | ||
154 | if (S_ISSPARSEDIR(ce->ce_mode) && | |
155 | repo_file_exists(r, ce->name)) | |
156 | string_list_append(&sparse_dirs, ce->name); | |
157 | } | |
158 | ||
159 | for_each_string_list_item(item, &sparse_dirs) { | |
160 | struct dir_struct dir = DIR_INIT; | |
161 | struct pathspec p = { 0 }; | |
162 | struct strvec s = STRVEC_INIT; | |
163 | ||
164 | strbuf_setlen(&path, pathlen); | |
165 | strbuf_addstr(&path, item->string); | |
166 | ||
167 | dir.flags |= DIR_SHOW_IGNORED_TOO; | |
168 | ||
169 | setup_standard_excludes(&dir); | |
170 | strvec_push(&s, path.buf); | |
171 | ||
172 | parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v); | |
173 | fill_directory(&dir, r->index, &p); | |
174 | ||
175 | if (dir.nr) { | |
176 | warning(_("directory '%s' contains untracked files," | |
177 | " but is not in the sparse-checkout cone"), | |
178 | item->string); | |
179 | } else if (remove_dir_recursively(&path, 0)) { | |
180 | /* | |
181 | * Removal is "best effort". If something blocks | |
182 | * the deletion, then continue with a warning. | |
183 | */ | |
184 | warning(_("failed to remove directory '%s'"), | |
185 | item->string); | |
186 | } | |
187 | ||
0f03f04c EN |
188 | strvec_clear(&s); |
189 | clear_pathspec(&p); | |
55dfcf95 DS |
190 | dir_clear(&dir); |
191 | } | |
192 | ||
193 | string_list_clear(&sparse_dirs, 0); | |
194 | strbuf_release(&path); | |
195 | ||
196 | if (was_full) | |
197 | ensure_full_index(r->index); | |
198 | } | |
199 | ||
e091228e | 200 | static int update_working_directory(struct pattern_list *pl) |
bab3c359 | 201 | { |
f56f31af | 202 | enum update_sparsity_result result; |
e091228e DS |
203 | struct unpack_trees_options o; |
204 | struct lock_file lock_file = LOCK_INIT; | |
e091228e | 205 | struct repository *r = the_repository; |
bab3c359 | 206 | |
b5bfc08a EN |
207 | /* If no branch has been checked out, there are no updates to make. */ |
208 | if (is_index_unborn(r->index)) | |
209 | return UPDATE_SPARSITY_SUCCESS; | |
210 | ||
836e25c5 DS |
211 | r->index->sparse_checkout_patterns = pl; |
212 | ||
e091228e DS |
213 | memset(&o, 0, sizeof(o)); |
214 | o.verbose_update = isatty(2); | |
e091228e | 215 | o.update = 1; |
e091228e DS |
216 | o.head_idx = -1; |
217 | o.src_index = r->index; | |
218 | o.dst_index = r->index; | |
219 | o.skip_sparse_checkout = 0; | |
220 | o.pl = pl; | |
e091228e | 221 | |
e091228e DS |
222 | setup_work_tree(); |
223 | ||
e091228e DS |
224 | repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR); |
225 | ||
4ee5d50f | 226 | setup_unpack_trees_porcelain(&o, "sparse-checkout"); |
f56f31af | 227 | result = update_sparsity(&o); |
4ee5d50f | 228 | clear_unpack_trees_porcelain(&o); |
e091228e | 229 | |
f56f31af EN |
230 | if (result == UPDATE_SPARSITY_WARNINGS) |
231 | /* | |
232 | * We don't do any special handling of warnings from untracked | |
233 | * files in the way or dirty entries that can't be removed. | |
234 | */ | |
235 | result = UPDATE_SPARSITY_SUCCESS; | |
236 | if (result == UPDATE_SPARSITY_SUCCESS) | |
e091228e | 237 | write_locked_index(r->index, &lock_file, COMMIT_LOCK); |
f56f31af | 238 | else |
e091228e | 239 | rollback_lock_file(&lock_file); |
bab3c359 | 240 | |
55dfcf95 DS |
241 | clean_tracked_sparse_directories(r); |
242 | ||
836e25c5 | 243 | r->index->sparse_checkout_patterns = NULL; |
bab3c359 DS |
244 | return result; |
245 | } | |
246 | ||
d585f0e7 DS |
247 | static char *escaped_pattern(char *pattern) |
248 | { | |
249 | char *p = pattern; | |
250 | struct strbuf final = STRBUF_INIT; | |
251 | ||
252 | while (*p) { | |
e53ffe27 | 253 | if (is_glob_special(*p)) |
d585f0e7 DS |
254 | strbuf_addch(&final, '\\'); |
255 | ||
256 | strbuf_addch(&final, *p); | |
257 | p++; | |
258 | } | |
259 | ||
260 | return strbuf_detach(&final, NULL); | |
261 | } | |
262 | ||
af09ce24 DS |
263 | static void write_cone_to_file(FILE *fp, struct pattern_list *pl) |
264 | { | |
265 | int i; | |
266 | struct pattern_entry *pe; | |
267 | struct hashmap_iter iter; | |
268 | struct string_list sl = STRING_LIST_INIT_DUP; | |
e9de487a | 269 | struct strbuf parent_pattern = STRBUF_INIT; |
af09ce24 | 270 | |
e9de487a DS |
271 | hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) { |
272 | if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL)) | |
273 | continue; | |
274 | ||
275 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
276 | pe->pattern, | |
277 | &parent_pattern)) | |
278 | string_list_insert(&sl, pe->pattern); | |
279 | } | |
af09ce24 DS |
280 | |
281 | string_list_sort(&sl); | |
282 | string_list_remove_duplicates(&sl, 0); | |
283 | ||
284 | fprintf(fp, "/*\n!/*/\n"); | |
285 | ||
286 | for (i = 0; i < sl.nr; i++) { | |
d585f0e7 | 287 | char *pattern = escaped_pattern(sl.items[i].string); |
af09ce24 DS |
288 | |
289 | if (strlen(pattern)) | |
290 | fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern); | |
d585f0e7 | 291 | free(pattern); |
af09ce24 DS |
292 | } |
293 | ||
294 | string_list_clear(&sl, 0); | |
295 | ||
e9de487a DS |
296 | hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) { |
297 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
298 | pe->pattern, | |
299 | &parent_pattern)) | |
300 | string_list_insert(&sl, pe->pattern); | |
301 | } | |
302 | ||
303 | strbuf_release(&parent_pattern); | |
af09ce24 DS |
304 | |
305 | string_list_sort(&sl); | |
306 | string_list_remove_duplicates(&sl, 0); | |
307 | ||
308 | for (i = 0; i < sl.nr; i++) { | |
d585f0e7 | 309 | char *pattern = escaped_pattern(sl.items[i].string); |
af09ce24 | 310 | fprintf(fp, "%s/\n", pattern); |
d585f0e7 | 311 | free(pattern); |
af09ce24 DS |
312 | } |
313 | } | |
314 | ||
315 | static int write_patterns_and_update(struct pattern_list *pl) | |
316 | { | |
317 | char *sparse_filename; | |
318 | FILE *fp; | |
fb10ca5b DS |
319 | int fd; |
320 | struct lock_file lk = LOCK_INIT; | |
e091228e DS |
321 | int result; |
322 | ||
fb10ca5b | 323 | sparse_filename = get_sparse_checkout_filename(); |
3c754067 DS |
324 | |
325 | if (safe_create_leading_directories(sparse_filename)) | |
326 | die(_("failed to create directory for sparse-checkout file")); | |
327 | ||
fb10ca5b DS |
328 | fd = hold_lock_file_for_update(&lk, sparse_filename, |
329 | LOCK_DIE_ON_ERROR); | |
e091228e | 330 | |
fb10ca5b | 331 | result = update_working_directory(pl); |
e091228e | 332 | if (result) { |
fb10ca5b DS |
333 | rollback_lock_file(&lk); |
334 | free(sparse_filename); | |
e091228e DS |
335 | clear_pattern_list(pl); |
336 | update_working_directory(NULL); | |
337 | return result; | |
338 | } | |
af09ce24 | 339 | |
fb10ca5b | 340 | fp = xfdopen(fd, "w"); |
af09ce24 DS |
341 | |
342 | if (core_sparse_checkout_cone) | |
343 | write_cone_to_file(fp, pl); | |
344 | else | |
345 | write_patterns_to_file(fp, pl); | |
346 | ||
fb10ca5b DS |
347 | fflush(fp); |
348 | commit_lock_file(&lk); | |
e091228e | 349 | |
af09ce24 | 350 | free(sparse_filename); |
e091228e | 351 | clear_pattern_list(pl); |
af09ce24 | 352 | |
e091228e | 353 | return 0; |
af09ce24 DS |
354 | } |
355 | ||
bab3c359 DS |
356 | enum sparse_checkout_mode { |
357 | MODE_NO_PATTERNS = 0, | |
358 | MODE_ALL_PATTERNS = 1, | |
af09ce24 | 359 | MODE_CONE_PATTERNS = 2, |
bab3c359 DS |
360 | }; |
361 | ||
362 | static int set_config(enum sparse_checkout_mode mode) | |
363 | { | |
364 | const char *config_path; | |
365 | ||
98564d80 XL |
366 | if (upgrade_repository_format(1) < 0) |
367 | die(_("unable to upgrade repository format to enable worktreeConfig")); | |
bab3c359 DS |
368 | if (git_config_set_gently("extensions.worktreeConfig", "true")) { |
369 | error(_("failed to set extensions.worktreeConfig setting")); | |
370 | return 1; | |
371 | } | |
372 | ||
373 | config_path = git_path("config.worktree"); | |
374 | git_config_set_in_file_gently(config_path, | |
375 | "core.sparseCheckout", | |
376 | mode ? "true" : NULL); | |
377 | ||
af09ce24 DS |
378 | git_config_set_in_file_gently(config_path, |
379 | "core.sparseCheckoutCone", | |
380 | mode == MODE_CONE_PATTERNS ? "true" : NULL); | |
381 | ||
dcc5fd5f DS |
382 | if (mode == MODE_NO_PATTERNS) |
383 | set_sparse_index_config(the_repository, 0); | |
384 | ||
bab3c359 DS |
385 | return 0; |
386 | } | |
387 | ||
be61fd11 EN |
388 | static int update_modes(int *cone_mode, int *sparse_index) |
389 | { | |
390 | int mode, record_mode; | |
391 | ||
392 | /* Determine if we need to record the mode; ensure sparse checkout on */ | |
393 | record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout; | |
394 | ||
395 | /* If not specified, use previous definition of cone mode */ | |
396 | if (*cone_mode == -1 && core_apply_sparse_checkout) | |
397 | *cone_mode = core_sparse_checkout_cone; | |
398 | ||
399 | /* Set cone/non-cone mode appropriately */ | |
400 | core_apply_sparse_checkout = 1; | |
401 | if (*cone_mode == 1) { | |
402 | mode = MODE_CONE_PATTERNS; | |
403 | core_sparse_checkout_cone = 1; | |
404 | } else { | |
405 | mode = MODE_ALL_PATTERNS; | |
406 | } | |
407 | if (record_mode && set_config(mode)) | |
408 | return 1; | |
409 | ||
410 | /* Set sparse-index/non-sparse-index mode if specified */ | |
411 | if (*sparse_index >= 0) { | |
412 | if (set_sparse_index_config(the_repository, *sparse_index) < 0) | |
413 | die(_("failed to modify sparse-index config")); | |
414 | ||
415 | /* force an index rewrite */ | |
416 | repo_read_index(the_repository); | |
417 | the_repository->index->updated_workdir = 1; | |
418 | } | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
af09ce24 | 423 | static char const * const builtin_sparse_checkout_init_usage[] = { |
122ba1f7 | 424 | N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"), |
af09ce24 DS |
425 | NULL |
426 | }; | |
427 | ||
428 | static struct sparse_checkout_init_opts { | |
429 | int cone_mode; | |
122ba1f7 | 430 | int sparse_index; |
af09ce24 DS |
431 | } init_opts; |
432 | ||
bab3c359 DS |
433 | static int sparse_checkout_init(int argc, const char **argv) |
434 | { | |
435 | struct pattern_list pl; | |
436 | char *sparse_filename; | |
bab3c359 | 437 | int res; |
d89f09c8 | 438 | struct object_id oid; |
416adc87 | 439 | struct strbuf pattern = STRBUF_INIT; |
bab3c359 | 440 | |
af09ce24 DS |
441 | static struct option builtin_sparse_checkout_init_options[] = { |
442 | OPT_BOOL(0, "cone", &init_opts.cone_mode, | |
443 | N_("initialize the sparse-checkout in cone mode")), | |
122ba1f7 DS |
444 | OPT_BOOL(0, "sparse-index", &init_opts.sparse_index, |
445 | N_("toggle the use of a sparse index")), | |
af09ce24 DS |
446 | OPT_END(), |
447 | }; | |
448 | ||
cff4e913 | 449 | repo_read_index(the_repository); |
cff4e913 | 450 | |
be61fd11 | 451 | init_opts.cone_mode = -1; |
122ba1f7 DS |
452 | init_opts.sparse_index = -1; |
453 | ||
af09ce24 DS |
454 | argc = parse_options(argc, argv, NULL, |
455 | builtin_sparse_checkout_init_options, | |
456 | builtin_sparse_checkout_init_usage, 0); | |
457 | ||
be61fd11 | 458 | if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index)) |
bab3c359 DS |
459 | return 1; |
460 | ||
461 | memset(&pl, 0, sizeof(pl)); | |
462 | ||
463 | sparse_filename = get_sparse_checkout_filename(); | |
1679d60b | 464 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); |
bab3c359 DS |
465 | |
466 | /* If we already have a sparse-checkout file, use it. */ | |
467 | if (res >= 0) { | |
468 | free(sparse_filename); | |
416adc87 | 469 | return update_working_directory(NULL); |
bab3c359 DS |
470 | } |
471 | ||
416adc87 DS |
472 | if (get_oid("HEAD", &oid)) { |
473 | FILE *fp; | |
bab3c359 | 474 | |
416adc87 | 475 | /* assume we are in a fresh repo, but update the sparse-checkout file */ |
7f44842a JT |
476 | if (safe_create_leading_directories(sparse_filename)) |
477 | die(_("unable to create leading directories of %s"), | |
478 | sparse_filename); | |
416adc87 DS |
479 | fp = xfopen(sparse_filename, "w"); |
480 | if (!fp) | |
481 | die(_("failed to open '%s'"), sparse_filename); | |
bab3c359 | 482 | |
416adc87 DS |
483 | free(sparse_filename); |
484 | fprintf(fp, "/*\n!/*/\n"); | |
485 | fclose(fp); | |
d89f09c8 DS |
486 | return 0; |
487 | } | |
488 | ||
416adc87 DS |
489 | strbuf_addstr(&pattern, "/*"); |
490 | add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); | |
491 | strbuf_addstr(&pattern, "!/*/"); | |
492 | add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); | |
dcc5fd5f | 493 | pl.use_cone_patterns = init_opts.cone_mode; |
416adc87 DS |
494 | |
495 | return write_patterns_and_update(&pl); | |
bab3c359 DS |
496 | } |
497 | ||
af09ce24 | 498 | static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) |
f6039a94 | 499 | { |
af09ce24 DS |
500 | struct pattern_entry *e = xmalloc(sizeof(*e)); |
501 | e->patternlen = path->len; | |
502 | e->pattern = strbuf_detach(path, NULL); | |
74318423 | 503 | hashmap_entry_init(&e->ent, fspathhash(e->pattern)); |
f6039a94 | 504 | |
af09ce24 | 505 | hashmap_add(&pl->recursive_hashmap, &e->ent); |
f6039a94 | 506 | |
af09ce24 DS |
507 | while (e->patternlen) { |
508 | char *slash = strrchr(e->pattern, '/'); | |
509 | char *oldpattern = e->pattern; | |
510 | size_t newlen; | |
511 | ||
391c3a10 | 512 | if (!slash || slash == e->pattern) |
af09ce24 DS |
513 | break; |
514 | ||
515 | newlen = slash - e->pattern; | |
516 | e = xmalloc(sizeof(struct pattern_entry)); | |
517 | e->patternlen = newlen; | |
518 | e->pattern = xstrndup(oldpattern, newlen); | |
74318423 | 519 | hashmap_entry_init(&e->ent, fspathhash(e->pattern)); |
af09ce24 DS |
520 | |
521 | if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL)) | |
522 | hashmap_add(&pl->parent_hashmap, &e->ent); | |
523 | } | |
524 | } | |
525 | ||
526 | static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) | |
527 | { | |
528 | strbuf_trim(line); | |
529 | ||
530 | strbuf_trim_trailing_dir_sep(line); | |
531 | ||
ef076599 DS |
532 | if (strbuf_normalize_path(line)) |
533 | die(_("could not normalize path %s"), line->buf); | |
534 | ||
af09ce24 DS |
535 | if (!line->len) |
536 | return; | |
537 | ||
538 | if (line->buf[0] != '/') | |
a91cc7fa | 539 | strbuf_insertstr(line, 0, "/"); |
af09ce24 DS |
540 | |
541 | insert_recursive_pattern(pl, line); | |
f6039a94 DS |
542 | } |
543 | ||
6fb705ab | 544 | static void add_patterns_from_input(struct pattern_list *pl, |
1530ff35 EN |
545 | int argc, const char **argv, |
546 | int use_stdin) | |
f6039a94 | 547 | { |
f6039a94 | 548 | int i; |
af09ce24 | 549 | if (core_sparse_checkout_cone) { |
7bffca95 DS |
550 | struct strbuf line = STRBUF_INIT; |
551 | ||
6fb705ab DS |
552 | hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
553 | hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
554 | pl->use_cone_patterns = 1; | |
af09ce24 | 555 | |
1530ff35 | 556 | if (use_stdin) { |
bd64de42 DS |
557 | struct strbuf unquoted = STRBUF_INIT; |
558 | while (!strbuf_getline(&line, stdin)) { | |
559 | if (line.buf[0] == '"') { | |
560 | strbuf_reset(&unquoted); | |
561 | if (unquote_c_style(&unquoted, line.buf, NULL)) | |
562 | die(_("unable to unquote C-style string '%s'"), | |
563 | line.buf); | |
564 | ||
565 | strbuf_swap(&unquoted, &line); | |
566 | } | |
567 | ||
6fb705ab | 568 | strbuf_to_cone_pattern(&line, pl); |
bd64de42 DS |
569 | } |
570 | ||
571 | strbuf_release(&unquoted); | |
af09ce24 DS |
572 | } else { |
573 | for (i = 0; i < argc; i++) { | |
574 | strbuf_setlen(&line, 0); | |
575 | strbuf_addstr(&line, argv[i]); | |
6fb705ab | 576 | strbuf_to_cone_pattern(&line, pl); |
af09ce24 | 577 | } |
7bffca95 DS |
578 | } |
579 | } else { | |
1530ff35 | 580 | if (use_stdin) { |
af09ce24 DS |
581 | struct strbuf line = STRBUF_INIT; |
582 | ||
583 | while (!strbuf_getline(&line, stdin)) { | |
584 | size_t len; | |
585 | char *buf = strbuf_detach(&line, &len); | |
6fb705ab | 586 | add_pattern(buf, empty_base, 0, pl, 0); |
af09ce24 DS |
587 | } |
588 | } else { | |
589 | for (i = 0; i < argc; i++) | |
6fb705ab | 590 | add_pattern(argv[i], empty_base, 0, pl, 0); |
af09ce24 | 591 | } |
7bffca95 | 592 | } |
6fb705ab DS |
593 | } |
594 | ||
4bf0c06c DS |
595 | enum modify_type { |
596 | REPLACE, | |
2631dc87 | 597 | ADD, |
4bf0c06c DS |
598 | }; |
599 | ||
2631dc87 | 600 | static void add_patterns_cone_mode(int argc, const char **argv, |
1530ff35 EN |
601 | struct pattern_list *pl, |
602 | int use_stdin) | |
2631dc87 DS |
603 | { |
604 | struct strbuf buffer = STRBUF_INIT; | |
605 | struct pattern_entry *pe; | |
606 | struct hashmap_iter iter; | |
607 | struct pattern_list existing; | |
608 | char *sparse_filename = get_sparse_checkout_filename(); | |
609 | ||
1530ff35 | 610 | add_patterns_from_input(pl, argc, argv, use_stdin); |
2631dc87 DS |
611 | |
612 | memset(&existing, 0, sizeof(existing)); | |
613 | existing.use_cone_patterns = core_sparse_checkout_cone; | |
614 | ||
615 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, | |
1679d60b | 616 | &existing, NULL, 0)) |
2631dc87 DS |
617 | die(_("unable to load existing sparse-checkout patterns")); |
618 | free(sparse_filename); | |
619 | ||
a3eca584 DS |
620 | if (!existing.use_cone_patterns) |
621 | die(_("existing sparse-checkout patterns do not use cone mode")); | |
622 | ||
2631dc87 DS |
623 | hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) { |
624 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
625 | pe->pattern, &buffer) || | |
626 | !hashmap_contains_parent(&pl->parent_hashmap, | |
627 | pe->pattern, &buffer)) { | |
628 | strbuf_reset(&buffer); | |
629 | strbuf_addstr(&buffer, pe->pattern); | |
630 | insert_recursive_pattern(pl, &buffer); | |
631 | } | |
632 | } | |
633 | ||
634 | clear_pattern_list(&existing); | |
635 | strbuf_release(&buffer); | |
636 | } | |
637 | ||
638 | static void add_patterns_literal(int argc, const char **argv, | |
1530ff35 EN |
639 | struct pattern_list *pl, |
640 | int use_stdin) | |
2631dc87 DS |
641 | { |
642 | char *sparse_filename = get_sparse_checkout_filename(); | |
643 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, | |
1679d60b | 644 | pl, NULL, 0)) |
2631dc87 DS |
645 | die(_("unable to load existing sparse-checkout patterns")); |
646 | free(sparse_filename); | |
1530ff35 | 647 | add_patterns_from_input(pl, argc, argv, use_stdin); |
2631dc87 DS |
648 | } |
649 | ||
1530ff35 EN |
650 | static int modify_pattern_list(int argc, const char **argv, int use_stdin, |
651 | enum modify_type m) | |
6fb705ab | 652 | { |
6fb705ab DS |
653 | int result; |
654 | int changed_config = 0; | |
836e25c5 | 655 | struct pattern_list *pl = xcalloc(1, sizeof(*pl)); |
6fb705ab | 656 | |
2631dc87 DS |
657 | switch (m) { |
658 | case ADD: | |
659 | if (core_sparse_checkout_cone) | |
1530ff35 | 660 | add_patterns_cone_mode(argc, argv, pl, use_stdin); |
2631dc87 | 661 | else |
1530ff35 | 662 | add_patterns_literal(argc, argv, pl, use_stdin); |
2631dc87 DS |
663 | break; |
664 | ||
665 | case REPLACE: | |
1530ff35 | 666 | add_patterns_from_input(pl, argc, argv, use_stdin); |
2631dc87 DS |
667 | break; |
668 | } | |
f6039a94 DS |
669 | |
670 | if (!core_apply_sparse_checkout) { | |
671 | set_config(MODE_ALL_PATTERNS); | |
672 | core_apply_sparse_checkout = 1; | |
673 | changed_config = 1; | |
674 | } | |
675 | ||
836e25c5 | 676 | result = write_patterns_and_update(pl); |
f6039a94 DS |
677 | |
678 | if (result && changed_config) | |
679 | set_config(MODE_NO_PATTERNS); | |
680 | ||
836e25c5 DS |
681 | clear_pattern_list(pl); |
682 | free(pl); | |
f6039a94 DS |
683 | return result; |
684 | } | |
685 | ||
0b624e03 EN |
686 | static char const * const builtin_sparse_checkout_add_usage[] = { |
687 | N_("git sparse-checkout add (--stdin | <patterns>)"), | |
688 | NULL | |
689 | }; | |
690 | ||
691 | static struct sparse_checkout_add_opts { | |
692 | int use_stdin; | |
693 | } add_opts; | |
694 | ||
695 | static int sparse_checkout_add(int argc, const char **argv, const char *prefix) | |
696 | { | |
697 | static struct option builtin_sparse_checkout_add_options[] = { | |
698 | OPT_BOOL(0, "stdin", &add_opts.use_stdin, | |
699 | N_("read patterns from standard in")), | |
700 | OPT_END(), | |
701 | }; | |
702 | ||
45c5e470 EN |
703 | if (!core_apply_sparse_checkout) |
704 | die(_("no sparse-checkout to add to")); | |
705 | ||
0b624e03 EN |
706 | repo_read_index(the_repository); |
707 | ||
708 | argc = parse_options(argc, argv, prefix, | |
709 | builtin_sparse_checkout_add_options, | |
710 | builtin_sparse_checkout_add_usage, | |
711 | PARSE_OPT_KEEP_UNKNOWN); | |
712 | ||
713 | return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD); | |
714 | } | |
715 | ||
716 | static char const * const builtin_sparse_checkout_set_usage[] = { | |
f2e3a218 | 717 | N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | <patterns>)"), |
0b624e03 EN |
718 | NULL |
719 | }; | |
720 | ||
721 | static struct sparse_checkout_set_opts { | |
f2e3a218 EN |
722 | int cone_mode; |
723 | int sparse_index; | |
0b624e03 EN |
724 | int use_stdin; |
725 | } set_opts; | |
726 | ||
727 | static int sparse_checkout_set(int argc, const char **argv, const char *prefix) | |
4bf0c06c | 728 | { |
f2e3a218 EN |
729 | int default_patterns_nr = 2; |
730 | const char *default_patterns[] = {"/*", "!/*/", NULL}; | |
731 | ||
4bf0c06c | 732 | static struct option builtin_sparse_checkout_set_options[] = { |
f2e3a218 EN |
733 | OPT_BOOL(0, "cone", &set_opts.cone_mode, |
734 | N_("initialize the sparse-checkout in cone mode")), | |
735 | OPT_BOOL(0, "sparse-index", &set_opts.sparse_index, | |
736 | N_("toggle the use of a sparse index")), | |
f85751a1 EN |
737 | OPT_BOOL_F(0, "stdin", &set_opts.use_stdin, |
738 | N_("read patterns from standard in"), | |
739 | PARSE_OPT_NONEG), | |
4bf0c06c DS |
740 | OPT_END(), |
741 | }; | |
742 | ||
743 | repo_read_index(the_repository); | |
4bf0c06c | 744 | |
f2e3a218 EN |
745 | set_opts.cone_mode = -1; |
746 | set_opts.sparse_index = -1; | |
747 | ||
4bf0c06c DS |
748 | argc = parse_options(argc, argv, prefix, |
749 | builtin_sparse_checkout_set_options, | |
750 | builtin_sparse_checkout_set_usage, | |
751 | PARSE_OPT_KEEP_UNKNOWN); | |
752 | ||
f2e3a218 EN |
753 | if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index)) |
754 | return 1; | |
755 | ||
756 | /* | |
757 | * Cone mode automatically specifies the toplevel directory. For | |
758 | * non-cone mode, if nothing is specified, manually select just the | |
759 | * top-level directory (much as 'init' would do). | |
760 | */ | |
761 | if (!core_sparse_checkout_cone && argc == 0) { | |
762 | argv = default_patterns; | |
763 | argc = default_patterns_nr; | |
764 | } | |
765 | ||
0b624e03 | 766 | return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE); |
4bf0c06c DS |
767 | } |
768 | ||
75d3bee1 | 769 | static char const * const builtin_sparse_checkout_reapply_usage[] = { |
dfac9b60 | 770 | N_("git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]"), |
75d3bee1 JK |
771 | NULL |
772 | }; | |
773 | ||
4e256731 EN |
774 | static struct sparse_checkout_reapply_opts { |
775 | int cone_mode; | |
776 | int sparse_index; | |
777 | } reapply_opts; | |
778 | ||
5644ca28 EN |
779 | static int sparse_checkout_reapply(int argc, const char **argv) |
780 | { | |
75d3bee1 | 781 | static struct option builtin_sparse_checkout_reapply_options[] = { |
4e256731 EN |
782 | OPT_BOOL(0, "cone", &reapply_opts.cone_mode, |
783 | N_("initialize the sparse-checkout in cone mode")), | |
784 | OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index, | |
785 | N_("toggle the use of a sparse index")), | |
75d3bee1 JK |
786 | OPT_END(), |
787 | }; | |
788 | ||
45c5e470 EN |
789 | if (!core_apply_sparse_checkout) |
790 | die(_("must be in a sparse-checkout to reapply sparsity patterns")); | |
791 | ||
75d3bee1 JK |
792 | argc = parse_options(argc, argv, NULL, |
793 | builtin_sparse_checkout_reapply_options, | |
794 | builtin_sparse_checkout_reapply_usage, 0); | |
795 | ||
5644ca28 | 796 | repo_read_index(the_repository); |
4e256731 EN |
797 | |
798 | reapply_opts.cone_mode = -1; | |
799 | reapply_opts.sparse_index = -1; | |
800 | ||
801 | if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index)) | |
802 | return 1; | |
803 | ||
5644ca28 EN |
804 | return update_working_directory(NULL); |
805 | } | |
806 | ||
75d3bee1 JK |
807 | static char const * const builtin_sparse_checkout_disable_usage[] = { |
808 | N_("git sparse-checkout disable"), | |
809 | NULL | |
810 | }; | |
811 | ||
72918c1a DS |
812 | static int sparse_checkout_disable(int argc, const char **argv) |
813 | { | |
75d3bee1 JK |
814 | static struct option builtin_sparse_checkout_disable_options[] = { |
815 | OPT_END(), | |
816 | }; | |
99dfa6f9 DS |
817 | struct pattern_list pl; |
818 | struct strbuf match_all = STRBUF_INIT; | |
72918c1a | 819 | |
45c5e470 EN |
820 | /* |
821 | * We do not exit early if !core_apply_sparse_checkout; due to the | |
822 | * ability for users to manually muck things up between | |
823 | * direct editing of .git/info/sparse-checkout | |
824 | * running read-tree -m u HEAD or update-index --skip-worktree | |
825 | * direct toggling of config options | |
826 | * users might end up with an index with SKIP_WORKTREE bit set on | |
827 | * some files and not know how to undo it. So, here we just | |
828 | * forcibly return to a dense checkout regardless of initial state. | |
829 | */ | |
830 | ||
75d3bee1 JK |
831 | argc = parse_options(argc, argv, NULL, |
832 | builtin_sparse_checkout_disable_options, | |
833 | builtin_sparse_checkout_disable_usage, 0); | |
834 | ||
cff4e913 | 835 | repo_read_index(the_repository); |
cff4e913 | 836 | |
99dfa6f9 DS |
837 | memset(&pl, 0, sizeof(pl)); |
838 | hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); | |
839 | hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
840 | pl.use_cone_patterns = 0; | |
841 | core_apply_sparse_checkout = 1; | |
72918c1a | 842 | |
99dfa6f9 DS |
843 | strbuf_addstr(&match_all, "/*"); |
844 | add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0); | |
72918c1a | 845 | |
dcc5fd5f DS |
846 | prepare_repo_settings(the_repository); |
847 | the_repository->settings.sparse_index = 0; | |
848 | ||
99dfa6f9 | 849 | if (update_working_directory(&pl)) |
72918c1a DS |
850 | die(_("error while refreshing working directory")); |
851 | ||
99dfa6f9 | 852 | clear_pattern_list(&pl); |
72918c1a DS |
853 | return set_config(MODE_NO_PATTERNS); |
854 | } | |
855 | ||
94c0956b DS |
856 | int cmd_sparse_checkout(int argc, const char **argv, const char *prefix) |
857 | { | |
858 | static struct option builtin_sparse_checkout_options[] = { | |
859 | OPT_END(), | |
860 | }; | |
861 | ||
862 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
863 | usage_with_options(builtin_sparse_checkout_usage, | |
864 | builtin_sparse_checkout_options); | |
865 | ||
866 | argc = parse_options(argc, argv, prefix, | |
867 | builtin_sparse_checkout_options, | |
868 | builtin_sparse_checkout_usage, | |
869 | PARSE_OPT_STOP_AT_NON_OPTION); | |
870 | ||
871 | git_config(git_default_config, NULL); | |
872 | ||
873 | if (argc > 0) { | |
874 | if (!strcmp(argv[0], "list")) | |
875 | return sparse_checkout_list(argc, argv); | |
bab3c359 DS |
876 | if (!strcmp(argv[0], "init")) |
877 | return sparse_checkout_init(argc, argv); | |
f6039a94 | 878 | if (!strcmp(argv[0], "set")) |
0b624e03 | 879 | return sparse_checkout_set(argc, argv, prefix); |
2631dc87 | 880 | if (!strcmp(argv[0], "add")) |
0b624e03 | 881 | return sparse_checkout_add(argc, argv, prefix); |
5644ca28 EN |
882 | if (!strcmp(argv[0], "reapply")) |
883 | return sparse_checkout_reapply(argc, argv); | |
72918c1a DS |
884 | if (!strcmp(argv[0], "disable")) |
885 | return sparse_checkout_disable(argc, argv); | |
94c0956b DS |
886 | } |
887 | ||
888 | usage_with_options(builtin_sparse_checkout_usage, | |
889 | builtin_sparse_checkout_options); | |
890 | } |