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