]>
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 | ||
188 | dir_clear(&dir); | |
189 | } | |
190 | ||
191 | string_list_clear(&sparse_dirs, 0); | |
192 | strbuf_release(&path); | |
193 | ||
194 | if (was_full) | |
195 | ensure_full_index(r->index); | |
196 | } | |
197 | ||
e091228e | 198 | static int update_working_directory(struct pattern_list *pl) |
bab3c359 | 199 | { |
f56f31af | 200 | enum update_sparsity_result result; |
e091228e DS |
201 | struct unpack_trees_options o; |
202 | struct lock_file lock_file = LOCK_INIT; | |
e091228e | 203 | struct repository *r = the_repository; |
bab3c359 | 204 | |
b5bfc08a EN |
205 | /* If no branch has been checked out, there are no updates to make. */ |
206 | if (is_index_unborn(r->index)) | |
207 | return UPDATE_SPARSITY_SUCCESS; | |
208 | ||
836e25c5 DS |
209 | r->index->sparse_checkout_patterns = pl; |
210 | ||
e091228e DS |
211 | memset(&o, 0, sizeof(o)); |
212 | o.verbose_update = isatty(2); | |
e091228e | 213 | o.update = 1; |
e091228e DS |
214 | o.head_idx = -1; |
215 | o.src_index = r->index; | |
216 | o.dst_index = r->index; | |
217 | o.skip_sparse_checkout = 0; | |
218 | o.pl = pl; | |
e091228e | 219 | |
e091228e DS |
220 | setup_work_tree(); |
221 | ||
e091228e DS |
222 | repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR); |
223 | ||
4ee5d50f | 224 | setup_unpack_trees_porcelain(&o, "sparse-checkout"); |
f56f31af | 225 | result = update_sparsity(&o); |
4ee5d50f | 226 | clear_unpack_trees_porcelain(&o); |
e091228e | 227 | |
f56f31af EN |
228 | if (result == UPDATE_SPARSITY_WARNINGS) |
229 | /* | |
230 | * We don't do any special handling of warnings from untracked | |
231 | * files in the way or dirty entries that can't be removed. | |
232 | */ | |
233 | result = UPDATE_SPARSITY_SUCCESS; | |
234 | if (result == UPDATE_SPARSITY_SUCCESS) | |
e091228e | 235 | write_locked_index(r->index, &lock_file, COMMIT_LOCK); |
f56f31af | 236 | else |
e091228e | 237 | rollback_lock_file(&lock_file); |
bab3c359 | 238 | |
55dfcf95 DS |
239 | clean_tracked_sparse_directories(r); |
240 | ||
836e25c5 | 241 | r->index->sparse_checkout_patterns = NULL; |
bab3c359 DS |
242 | return result; |
243 | } | |
244 | ||
d585f0e7 DS |
245 | static char *escaped_pattern(char *pattern) |
246 | { | |
247 | char *p = pattern; | |
248 | struct strbuf final = STRBUF_INIT; | |
249 | ||
250 | while (*p) { | |
e53ffe27 | 251 | if (is_glob_special(*p)) |
d585f0e7 DS |
252 | strbuf_addch(&final, '\\'); |
253 | ||
254 | strbuf_addch(&final, *p); | |
255 | p++; | |
256 | } | |
257 | ||
258 | return strbuf_detach(&final, NULL); | |
259 | } | |
260 | ||
af09ce24 DS |
261 | static void write_cone_to_file(FILE *fp, struct pattern_list *pl) |
262 | { | |
263 | int i; | |
264 | struct pattern_entry *pe; | |
265 | struct hashmap_iter iter; | |
266 | struct string_list sl = STRING_LIST_INIT_DUP; | |
e9de487a | 267 | struct strbuf parent_pattern = STRBUF_INIT; |
af09ce24 | 268 | |
e9de487a DS |
269 | hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) { |
270 | if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL)) | |
271 | continue; | |
272 | ||
273 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
274 | pe->pattern, | |
275 | &parent_pattern)) | |
276 | string_list_insert(&sl, pe->pattern); | |
277 | } | |
af09ce24 DS |
278 | |
279 | string_list_sort(&sl); | |
280 | string_list_remove_duplicates(&sl, 0); | |
281 | ||
282 | fprintf(fp, "/*\n!/*/\n"); | |
283 | ||
284 | for (i = 0; i < sl.nr; i++) { | |
d585f0e7 | 285 | char *pattern = escaped_pattern(sl.items[i].string); |
af09ce24 DS |
286 | |
287 | if (strlen(pattern)) | |
288 | fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern); | |
d585f0e7 | 289 | free(pattern); |
af09ce24 DS |
290 | } |
291 | ||
292 | string_list_clear(&sl, 0); | |
293 | ||
e9de487a DS |
294 | hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) { |
295 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
296 | pe->pattern, | |
297 | &parent_pattern)) | |
298 | string_list_insert(&sl, pe->pattern); | |
299 | } | |
300 | ||
301 | strbuf_release(&parent_pattern); | |
af09ce24 DS |
302 | |
303 | string_list_sort(&sl); | |
304 | string_list_remove_duplicates(&sl, 0); | |
305 | ||
306 | for (i = 0; i < sl.nr; i++) { | |
d585f0e7 | 307 | char *pattern = escaped_pattern(sl.items[i].string); |
af09ce24 | 308 | fprintf(fp, "%s/\n", pattern); |
d585f0e7 | 309 | free(pattern); |
af09ce24 DS |
310 | } |
311 | } | |
312 | ||
313 | static int write_patterns_and_update(struct pattern_list *pl) | |
314 | { | |
315 | char *sparse_filename; | |
316 | FILE *fp; | |
fb10ca5b DS |
317 | int fd; |
318 | struct lock_file lk = LOCK_INIT; | |
e091228e DS |
319 | int result; |
320 | ||
fb10ca5b | 321 | sparse_filename = get_sparse_checkout_filename(); |
3c754067 DS |
322 | |
323 | if (safe_create_leading_directories(sparse_filename)) | |
324 | die(_("failed to create directory for sparse-checkout file")); | |
325 | ||
fb10ca5b DS |
326 | fd = hold_lock_file_for_update(&lk, sparse_filename, |
327 | LOCK_DIE_ON_ERROR); | |
e091228e | 328 | |
fb10ca5b | 329 | result = update_working_directory(pl); |
e091228e | 330 | if (result) { |
fb10ca5b DS |
331 | rollback_lock_file(&lk); |
332 | free(sparse_filename); | |
e091228e DS |
333 | clear_pattern_list(pl); |
334 | update_working_directory(NULL); | |
335 | return result; | |
336 | } | |
af09ce24 | 337 | |
fb10ca5b | 338 | fp = xfdopen(fd, "w"); |
af09ce24 DS |
339 | |
340 | if (core_sparse_checkout_cone) | |
341 | write_cone_to_file(fp, pl); | |
342 | else | |
343 | write_patterns_to_file(fp, pl); | |
344 | ||
fb10ca5b DS |
345 | fflush(fp); |
346 | commit_lock_file(&lk); | |
e091228e | 347 | |
af09ce24 | 348 | free(sparse_filename); |
e091228e | 349 | clear_pattern_list(pl); |
af09ce24 | 350 | |
e091228e | 351 | return 0; |
af09ce24 DS |
352 | } |
353 | ||
bab3c359 DS |
354 | enum sparse_checkout_mode { |
355 | MODE_NO_PATTERNS = 0, | |
356 | MODE_ALL_PATTERNS = 1, | |
af09ce24 | 357 | MODE_CONE_PATTERNS = 2, |
bab3c359 DS |
358 | }; |
359 | ||
360 | static int set_config(enum sparse_checkout_mode mode) | |
361 | { | |
362 | const char *config_path; | |
363 | ||
98564d80 XL |
364 | if (upgrade_repository_format(1) < 0) |
365 | die(_("unable to upgrade repository format to enable worktreeConfig")); | |
bab3c359 DS |
366 | if (git_config_set_gently("extensions.worktreeConfig", "true")) { |
367 | error(_("failed to set extensions.worktreeConfig setting")); | |
368 | return 1; | |
369 | } | |
370 | ||
371 | config_path = git_path("config.worktree"); | |
372 | git_config_set_in_file_gently(config_path, | |
373 | "core.sparseCheckout", | |
374 | mode ? "true" : NULL); | |
375 | ||
af09ce24 DS |
376 | git_config_set_in_file_gently(config_path, |
377 | "core.sparseCheckoutCone", | |
378 | mode == MODE_CONE_PATTERNS ? "true" : NULL); | |
379 | ||
dcc5fd5f DS |
380 | if (mode == MODE_NO_PATTERNS) |
381 | set_sparse_index_config(the_repository, 0); | |
382 | ||
bab3c359 DS |
383 | return 0; |
384 | } | |
385 | ||
af09ce24 | 386 | static char const * const builtin_sparse_checkout_init_usage[] = { |
122ba1f7 | 387 | N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"), |
af09ce24 DS |
388 | NULL |
389 | }; | |
390 | ||
391 | static struct sparse_checkout_init_opts { | |
392 | int cone_mode; | |
122ba1f7 | 393 | int sparse_index; |
af09ce24 DS |
394 | } init_opts; |
395 | ||
bab3c359 DS |
396 | static int sparse_checkout_init(int argc, const char **argv) |
397 | { | |
398 | struct pattern_list pl; | |
399 | char *sparse_filename; | |
bab3c359 | 400 | int res; |
d89f09c8 | 401 | struct object_id oid; |
af09ce24 | 402 | int mode; |
416adc87 | 403 | struct strbuf pattern = STRBUF_INIT; |
bab3c359 | 404 | |
af09ce24 DS |
405 | static struct option builtin_sparse_checkout_init_options[] = { |
406 | OPT_BOOL(0, "cone", &init_opts.cone_mode, | |
407 | N_("initialize the sparse-checkout in cone mode")), | |
122ba1f7 DS |
408 | OPT_BOOL(0, "sparse-index", &init_opts.sparse_index, |
409 | N_("toggle the use of a sparse index")), | |
af09ce24 DS |
410 | OPT_END(), |
411 | }; | |
412 | ||
cff4e913 | 413 | repo_read_index(the_repository); |
cff4e913 | 414 | |
122ba1f7 DS |
415 | init_opts.sparse_index = -1; |
416 | ||
af09ce24 DS |
417 | argc = parse_options(argc, argv, NULL, |
418 | builtin_sparse_checkout_init_options, | |
419 | builtin_sparse_checkout_init_usage, 0); | |
420 | ||
e091228e DS |
421 | if (init_opts.cone_mode) { |
422 | mode = MODE_CONE_PATTERNS; | |
423 | core_sparse_checkout_cone = 1; | |
424 | } else | |
425 | mode = MODE_ALL_PATTERNS; | |
af09ce24 DS |
426 | |
427 | if (set_config(mode)) | |
bab3c359 DS |
428 | return 1; |
429 | ||
430 | memset(&pl, 0, sizeof(pl)); | |
431 | ||
432 | sparse_filename = get_sparse_checkout_filename(); | |
1679d60b | 433 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); |
bab3c359 | 434 | |
122ba1f7 DS |
435 | if (init_opts.sparse_index >= 0) { |
436 | if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0) | |
437 | die(_("failed to modify sparse-index config")); | |
438 | ||
439 | /* force an index rewrite */ | |
440 | repo_read_index(the_repository); | |
441 | the_repository->index->updated_workdir = 1; | |
442 | } | |
443 | ||
dcc5fd5f DS |
444 | core_apply_sparse_checkout = 1; |
445 | ||
bab3c359 DS |
446 | /* If we already have a sparse-checkout file, use it. */ |
447 | if (res >= 0) { | |
448 | free(sparse_filename); | |
416adc87 | 449 | return update_working_directory(NULL); |
bab3c359 DS |
450 | } |
451 | ||
416adc87 DS |
452 | if (get_oid("HEAD", &oid)) { |
453 | FILE *fp; | |
bab3c359 | 454 | |
416adc87 DS |
455 | /* assume we are in a fresh repo, but update the sparse-checkout file */ |
456 | fp = xfopen(sparse_filename, "w"); | |
457 | if (!fp) | |
458 | die(_("failed to open '%s'"), sparse_filename); | |
bab3c359 | 459 | |
416adc87 DS |
460 | free(sparse_filename); |
461 | fprintf(fp, "/*\n!/*/\n"); | |
462 | fclose(fp); | |
d89f09c8 DS |
463 | return 0; |
464 | } | |
465 | ||
416adc87 DS |
466 | strbuf_addstr(&pattern, "/*"); |
467 | add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); | |
468 | strbuf_addstr(&pattern, "!/*/"); | |
469 | add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); | |
dcc5fd5f | 470 | pl.use_cone_patterns = init_opts.cone_mode; |
416adc87 DS |
471 | |
472 | return write_patterns_and_update(&pl); | |
bab3c359 DS |
473 | } |
474 | ||
af09ce24 | 475 | static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) |
f6039a94 | 476 | { |
af09ce24 DS |
477 | struct pattern_entry *e = xmalloc(sizeof(*e)); |
478 | e->patternlen = path->len; | |
479 | e->pattern = strbuf_detach(path, NULL); | |
74318423 | 480 | hashmap_entry_init(&e->ent, fspathhash(e->pattern)); |
f6039a94 | 481 | |
af09ce24 | 482 | hashmap_add(&pl->recursive_hashmap, &e->ent); |
f6039a94 | 483 | |
af09ce24 DS |
484 | while (e->patternlen) { |
485 | char *slash = strrchr(e->pattern, '/'); | |
486 | char *oldpattern = e->pattern; | |
487 | size_t newlen; | |
488 | ||
489 | if (slash == e->pattern) | |
490 | break; | |
491 | ||
492 | newlen = slash - e->pattern; | |
493 | e = xmalloc(sizeof(struct pattern_entry)); | |
494 | e->patternlen = newlen; | |
495 | e->pattern = xstrndup(oldpattern, newlen); | |
74318423 | 496 | hashmap_entry_init(&e->ent, fspathhash(e->pattern)); |
af09ce24 DS |
497 | |
498 | if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL)) | |
499 | hashmap_add(&pl->parent_hashmap, &e->ent); | |
500 | } | |
501 | } | |
502 | ||
503 | static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) | |
504 | { | |
505 | strbuf_trim(line); | |
506 | ||
507 | strbuf_trim_trailing_dir_sep(line); | |
508 | ||
ef076599 DS |
509 | if (strbuf_normalize_path(line)) |
510 | die(_("could not normalize path %s"), line->buf); | |
511 | ||
af09ce24 DS |
512 | if (!line->len) |
513 | return; | |
514 | ||
515 | if (line->buf[0] != '/') | |
a91cc7fa | 516 | strbuf_insertstr(line, 0, "/"); |
af09ce24 DS |
517 | |
518 | insert_recursive_pattern(pl, line); | |
f6039a94 DS |
519 | } |
520 | ||
6fb705ab | 521 | static void add_patterns_from_input(struct pattern_list *pl, |
1530ff35 EN |
522 | int argc, const char **argv, |
523 | int use_stdin) | |
f6039a94 | 524 | { |
f6039a94 | 525 | int i; |
af09ce24 | 526 | if (core_sparse_checkout_cone) { |
7bffca95 DS |
527 | struct strbuf line = STRBUF_INIT; |
528 | ||
6fb705ab DS |
529 | hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
530 | hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
531 | pl->use_cone_patterns = 1; | |
af09ce24 | 532 | |
1530ff35 | 533 | if (use_stdin) { |
bd64de42 DS |
534 | struct strbuf unquoted = STRBUF_INIT; |
535 | while (!strbuf_getline(&line, stdin)) { | |
536 | if (line.buf[0] == '"') { | |
537 | strbuf_reset(&unquoted); | |
538 | if (unquote_c_style(&unquoted, line.buf, NULL)) | |
539 | die(_("unable to unquote C-style string '%s'"), | |
540 | line.buf); | |
541 | ||
542 | strbuf_swap(&unquoted, &line); | |
543 | } | |
544 | ||
6fb705ab | 545 | strbuf_to_cone_pattern(&line, pl); |
bd64de42 DS |
546 | } |
547 | ||
548 | strbuf_release(&unquoted); | |
af09ce24 DS |
549 | } else { |
550 | for (i = 0; i < argc; i++) { | |
551 | strbuf_setlen(&line, 0); | |
552 | strbuf_addstr(&line, argv[i]); | |
6fb705ab | 553 | strbuf_to_cone_pattern(&line, pl); |
af09ce24 | 554 | } |
7bffca95 DS |
555 | } |
556 | } else { | |
1530ff35 | 557 | if (use_stdin) { |
af09ce24 DS |
558 | struct strbuf line = STRBUF_INIT; |
559 | ||
560 | while (!strbuf_getline(&line, stdin)) { | |
561 | size_t len; | |
562 | char *buf = strbuf_detach(&line, &len); | |
6fb705ab | 563 | add_pattern(buf, empty_base, 0, pl, 0); |
af09ce24 DS |
564 | } |
565 | } else { | |
566 | for (i = 0; i < argc; i++) | |
6fb705ab | 567 | add_pattern(argv[i], empty_base, 0, pl, 0); |
af09ce24 | 568 | } |
7bffca95 | 569 | } |
6fb705ab DS |
570 | } |
571 | ||
4bf0c06c DS |
572 | enum modify_type { |
573 | REPLACE, | |
2631dc87 | 574 | ADD, |
4bf0c06c DS |
575 | }; |
576 | ||
2631dc87 | 577 | static void add_patterns_cone_mode(int argc, const char **argv, |
1530ff35 EN |
578 | struct pattern_list *pl, |
579 | int use_stdin) | |
2631dc87 DS |
580 | { |
581 | struct strbuf buffer = STRBUF_INIT; | |
582 | struct pattern_entry *pe; | |
583 | struct hashmap_iter iter; | |
584 | struct pattern_list existing; | |
585 | char *sparse_filename = get_sparse_checkout_filename(); | |
586 | ||
1530ff35 | 587 | add_patterns_from_input(pl, argc, argv, use_stdin); |
2631dc87 DS |
588 | |
589 | memset(&existing, 0, sizeof(existing)); | |
590 | existing.use_cone_patterns = core_sparse_checkout_cone; | |
591 | ||
592 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, | |
1679d60b | 593 | &existing, NULL, 0)) |
2631dc87 DS |
594 | die(_("unable to load existing sparse-checkout patterns")); |
595 | free(sparse_filename); | |
596 | ||
597 | hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) { | |
598 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
599 | pe->pattern, &buffer) || | |
600 | !hashmap_contains_parent(&pl->parent_hashmap, | |
601 | pe->pattern, &buffer)) { | |
602 | strbuf_reset(&buffer); | |
603 | strbuf_addstr(&buffer, pe->pattern); | |
604 | insert_recursive_pattern(pl, &buffer); | |
605 | } | |
606 | } | |
607 | ||
608 | clear_pattern_list(&existing); | |
609 | strbuf_release(&buffer); | |
610 | } | |
611 | ||
612 | static void add_patterns_literal(int argc, const char **argv, | |
1530ff35 EN |
613 | struct pattern_list *pl, |
614 | int use_stdin) | |
2631dc87 DS |
615 | { |
616 | char *sparse_filename = get_sparse_checkout_filename(); | |
617 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, | |
1679d60b | 618 | pl, NULL, 0)) |
2631dc87 DS |
619 | die(_("unable to load existing sparse-checkout patterns")); |
620 | free(sparse_filename); | |
1530ff35 | 621 | add_patterns_from_input(pl, argc, argv, use_stdin); |
2631dc87 DS |
622 | } |
623 | ||
1530ff35 EN |
624 | static int modify_pattern_list(int argc, const char **argv, int use_stdin, |
625 | enum modify_type m) | |
6fb705ab | 626 | { |
6fb705ab DS |
627 | int result; |
628 | int changed_config = 0; | |
836e25c5 | 629 | struct pattern_list *pl = xcalloc(1, sizeof(*pl)); |
6fb705ab | 630 | |
2631dc87 DS |
631 | switch (m) { |
632 | case ADD: | |
633 | if (core_sparse_checkout_cone) | |
1530ff35 | 634 | add_patterns_cone_mode(argc, argv, pl, use_stdin); |
2631dc87 | 635 | else |
1530ff35 | 636 | add_patterns_literal(argc, argv, pl, use_stdin); |
2631dc87 DS |
637 | break; |
638 | ||
639 | case REPLACE: | |
1530ff35 | 640 | add_patterns_from_input(pl, argc, argv, use_stdin); |
2631dc87 DS |
641 | break; |
642 | } | |
f6039a94 DS |
643 | |
644 | if (!core_apply_sparse_checkout) { | |
645 | set_config(MODE_ALL_PATTERNS); | |
646 | core_apply_sparse_checkout = 1; | |
647 | changed_config = 1; | |
648 | } | |
649 | ||
836e25c5 | 650 | result = write_patterns_and_update(pl); |
f6039a94 DS |
651 | |
652 | if (result && changed_config) | |
653 | set_config(MODE_NO_PATTERNS); | |
654 | ||
836e25c5 DS |
655 | clear_pattern_list(pl); |
656 | free(pl); | |
f6039a94 DS |
657 | return result; |
658 | } | |
659 | ||
0b624e03 EN |
660 | static char const * const builtin_sparse_checkout_add_usage[] = { |
661 | N_("git sparse-checkout add (--stdin | <patterns>)"), | |
662 | NULL | |
663 | }; | |
664 | ||
665 | static struct sparse_checkout_add_opts { | |
666 | int use_stdin; | |
667 | } add_opts; | |
668 | ||
669 | static int sparse_checkout_add(int argc, const char **argv, const char *prefix) | |
670 | { | |
671 | static struct option builtin_sparse_checkout_add_options[] = { | |
672 | OPT_BOOL(0, "stdin", &add_opts.use_stdin, | |
673 | N_("read patterns from standard in")), | |
674 | OPT_END(), | |
675 | }; | |
676 | ||
45c5e470 EN |
677 | if (!core_apply_sparse_checkout) |
678 | die(_("no sparse-checkout to add to")); | |
679 | ||
0b624e03 EN |
680 | repo_read_index(the_repository); |
681 | ||
682 | argc = parse_options(argc, argv, prefix, | |
683 | builtin_sparse_checkout_add_options, | |
684 | builtin_sparse_checkout_add_usage, | |
685 | PARSE_OPT_KEEP_UNKNOWN); | |
686 | ||
687 | return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD); | |
688 | } | |
689 | ||
690 | static char const * const builtin_sparse_checkout_set_usage[] = { | |
691 | N_("git sparse-checkout set (--stdin | <patterns>)"), | |
692 | NULL | |
693 | }; | |
694 | ||
695 | static struct sparse_checkout_set_opts { | |
696 | int use_stdin; | |
697 | } set_opts; | |
698 | ||
699 | static int sparse_checkout_set(int argc, const char **argv, const char *prefix) | |
4bf0c06c DS |
700 | { |
701 | static struct option builtin_sparse_checkout_set_options[] = { | |
702 | OPT_BOOL(0, "stdin", &set_opts.use_stdin, | |
703 | N_("read patterns from standard in")), | |
704 | OPT_END(), | |
705 | }; | |
706 | ||
707 | repo_read_index(the_repository); | |
4bf0c06c DS |
708 | |
709 | argc = parse_options(argc, argv, prefix, | |
710 | builtin_sparse_checkout_set_options, | |
711 | builtin_sparse_checkout_set_usage, | |
712 | PARSE_OPT_KEEP_UNKNOWN); | |
713 | ||
0b624e03 | 714 | return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE); |
4bf0c06c DS |
715 | } |
716 | ||
75d3bee1 JK |
717 | static char const * const builtin_sparse_checkout_reapply_usage[] = { |
718 | N_("git sparse-checkout reapply"), | |
719 | NULL | |
720 | }; | |
721 | ||
5644ca28 EN |
722 | static int sparse_checkout_reapply(int argc, const char **argv) |
723 | { | |
75d3bee1 JK |
724 | static struct option builtin_sparse_checkout_reapply_options[] = { |
725 | OPT_END(), | |
726 | }; | |
727 | ||
45c5e470 EN |
728 | if (!core_apply_sparse_checkout) |
729 | die(_("must be in a sparse-checkout to reapply sparsity patterns")); | |
730 | ||
75d3bee1 JK |
731 | argc = parse_options(argc, argv, NULL, |
732 | builtin_sparse_checkout_reapply_options, | |
733 | builtin_sparse_checkout_reapply_usage, 0); | |
734 | ||
5644ca28 EN |
735 | repo_read_index(the_repository); |
736 | return update_working_directory(NULL); | |
737 | } | |
738 | ||
75d3bee1 JK |
739 | static char const * const builtin_sparse_checkout_disable_usage[] = { |
740 | N_("git sparse-checkout disable"), | |
741 | NULL | |
742 | }; | |
743 | ||
72918c1a DS |
744 | static int sparse_checkout_disable(int argc, const char **argv) |
745 | { | |
75d3bee1 JK |
746 | static struct option builtin_sparse_checkout_disable_options[] = { |
747 | OPT_END(), | |
748 | }; | |
99dfa6f9 DS |
749 | struct pattern_list pl; |
750 | struct strbuf match_all = STRBUF_INIT; | |
72918c1a | 751 | |
45c5e470 EN |
752 | /* |
753 | * We do not exit early if !core_apply_sparse_checkout; due to the | |
754 | * ability for users to manually muck things up between | |
755 | * direct editing of .git/info/sparse-checkout | |
756 | * running read-tree -m u HEAD or update-index --skip-worktree | |
757 | * direct toggling of config options | |
758 | * users might end up with an index with SKIP_WORKTREE bit set on | |
759 | * some files and not know how to undo it. So, here we just | |
760 | * forcibly return to a dense checkout regardless of initial state. | |
761 | */ | |
762 | ||
75d3bee1 JK |
763 | argc = parse_options(argc, argv, NULL, |
764 | builtin_sparse_checkout_disable_options, | |
765 | builtin_sparse_checkout_disable_usage, 0); | |
766 | ||
cff4e913 | 767 | repo_read_index(the_repository); |
cff4e913 | 768 | |
99dfa6f9 DS |
769 | memset(&pl, 0, sizeof(pl)); |
770 | hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); | |
771 | hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
772 | pl.use_cone_patterns = 0; | |
773 | core_apply_sparse_checkout = 1; | |
72918c1a | 774 | |
99dfa6f9 DS |
775 | strbuf_addstr(&match_all, "/*"); |
776 | add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0); | |
72918c1a | 777 | |
dcc5fd5f DS |
778 | prepare_repo_settings(the_repository); |
779 | the_repository->settings.sparse_index = 0; | |
780 | ||
99dfa6f9 | 781 | if (update_working_directory(&pl)) |
72918c1a DS |
782 | die(_("error while refreshing working directory")); |
783 | ||
99dfa6f9 | 784 | clear_pattern_list(&pl); |
72918c1a DS |
785 | return set_config(MODE_NO_PATTERNS); |
786 | } | |
787 | ||
94c0956b DS |
788 | int cmd_sparse_checkout(int argc, const char **argv, const char *prefix) |
789 | { | |
790 | static struct option builtin_sparse_checkout_options[] = { | |
791 | OPT_END(), | |
792 | }; | |
793 | ||
794 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
795 | usage_with_options(builtin_sparse_checkout_usage, | |
796 | builtin_sparse_checkout_options); | |
797 | ||
798 | argc = parse_options(argc, argv, prefix, | |
799 | builtin_sparse_checkout_options, | |
800 | builtin_sparse_checkout_usage, | |
801 | PARSE_OPT_STOP_AT_NON_OPTION); | |
802 | ||
803 | git_config(git_default_config, NULL); | |
804 | ||
805 | if (argc > 0) { | |
806 | if (!strcmp(argv[0], "list")) | |
807 | return sparse_checkout_list(argc, argv); | |
bab3c359 DS |
808 | if (!strcmp(argv[0], "init")) |
809 | return sparse_checkout_init(argc, argv); | |
f6039a94 | 810 | if (!strcmp(argv[0], "set")) |
0b624e03 | 811 | return sparse_checkout_set(argc, argv, prefix); |
2631dc87 | 812 | if (!strcmp(argv[0], "add")) |
0b624e03 | 813 | return sparse_checkout_add(argc, argv, prefix); |
5644ca28 EN |
814 | if (!strcmp(argv[0], "reapply")) |
815 | return sparse_checkout_reapply(argc, argv); | |
72918c1a DS |
816 | if (!strcmp(argv[0], "disable")) |
817 | return sparse_checkout_disable(argc, argv); | |
94c0956b DS |
818 | } |
819 | ||
820 | usage_with_options(builtin_sparse_checkout_usage, | |
821 | builtin_sparse_checkout_options); | |
822 | } |