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