]>
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 | ||
75d3bee1 JK |
59 | argc = parse_options(argc, argv, NULL, |
60 | builtin_sparse_checkout_list_options, | |
61 | builtin_sparse_checkout_list_usage, 0); | |
62 | ||
94c0956b DS |
63 | memset(&pl, 0, sizeof(pl)); |
64 | ||
de11951b DS |
65 | pl.use_cone_patterns = core_sparse_checkout_cone; |
66 | ||
94c0956b DS |
67 | sparse_filename = get_sparse_checkout_filename(); |
68 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL); | |
69 | free(sparse_filename); | |
70 | ||
71 | if (res < 0) { | |
72 | warning(_("this worktree is not sparse (sparse-checkout file may not exist)")); | |
73 | return 0; | |
74 | } | |
75 | ||
de11951b DS |
76 | if (pl.use_cone_patterns) { |
77 | int i; | |
78 | struct pattern_entry *pe; | |
79 | struct hashmap_iter iter; | |
80 | struct string_list sl = STRING_LIST_INIT_DUP; | |
81 | ||
82 | hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) { | |
83 | /* pe->pattern starts with "/", skip it */ | |
84 | string_list_insert(&sl, pe->pattern + 1); | |
85 | } | |
86 | ||
87 | string_list_sort(&sl); | |
88 | ||
e55682ea DS |
89 | for (i = 0; i < sl.nr; i++) { |
90 | quote_c_style(sl.items[i].string, NULL, stdout, 0); | |
91 | printf("\n"); | |
92 | } | |
de11951b DS |
93 | |
94 | return 0; | |
95 | } | |
96 | ||
94c0956b DS |
97 | write_patterns_to_file(stdout, &pl); |
98 | clear_pattern_list(&pl); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
e091228e | 103 | static int update_working_directory(struct pattern_list *pl) |
bab3c359 | 104 | { |
f56f31af | 105 | enum update_sparsity_result result; |
e091228e DS |
106 | struct unpack_trees_options o; |
107 | struct lock_file lock_file = LOCK_INIT; | |
e091228e | 108 | struct repository *r = the_repository; |
bab3c359 | 109 | |
b5bfc08a EN |
110 | /* If no branch has been checked out, there are no updates to make. */ |
111 | if (is_index_unborn(r->index)) | |
112 | return UPDATE_SPARSITY_SUCCESS; | |
113 | ||
836e25c5 DS |
114 | r->index->sparse_checkout_patterns = pl; |
115 | ||
e091228e DS |
116 | memset(&o, 0, sizeof(o)); |
117 | o.verbose_update = isatty(2); | |
e091228e | 118 | o.update = 1; |
e091228e DS |
119 | o.head_idx = -1; |
120 | o.src_index = r->index; | |
121 | o.dst_index = r->index; | |
122 | o.skip_sparse_checkout = 0; | |
123 | o.pl = pl; | |
e091228e | 124 | |
e091228e DS |
125 | setup_work_tree(); |
126 | ||
e091228e DS |
127 | repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR); |
128 | ||
4ee5d50f | 129 | setup_unpack_trees_porcelain(&o, "sparse-checkout"); |
f56f31af | 130 | result = update_sparsity(&o); |
4ee5d50f | 131 | clear_unpack_trees_porcelain(&o); |
e091228e | 132 | |
f56f31af EN |
133 | if (result == UPDATE_SPARSITY_WARNINGS) |
134 | /* | |
135 | * We don't do any special handling of warnings from untracked | |
136 | * files in the way or dirty entries that can't be removed. | |
137 | */ | |
138 | result = UPDATE_SPARSITY_SUCCESS; | |
139 | if (result == UPDATE_SPARSITY_SUCCESS) | |
e091228e | 140 | write_locked_index(r->index, &lock_file, COMMIT_LOCK); |
f56f31af | 141 | else |
e091228e | 142 | rollback_lock_file(&lock_file); |
bab3c359 | 143 | |
836e25c5 | 144 | r->index->sparse_checkout_patterns = NULL; |
bab3c359 DS |
145 | return result; |
146 | } | |
147 | ||
d585f0e7 DS |
148 | static char *escaped_pattern(char *pattern) |
149 | { | |
150 | char *p = pattern; | |
151 | struct strbuf final = STRBUF_INIT; | |
152 | ||
153 | while (*p) { | |
e53ffe27 | 154 | if (is_glob_special(*p)) |
d585f0e7 DS |
155 | strbuf_addch(&final, '\\'); |
156 | ||
157 | strbuf_addch(&final, *p); | |
158 | p++; | |
159 | } | |
160 | ||
161 | return strbuf_detach(&final, NULL); | |
162 | } | |
163 | ||
af09ce24 DS |
164 | static void write_cone_to_file(FILE *fp, struct pattern_list *pl) |
165 | { | |
166 | int i; | |
167 | struct pattern_entry *pe; | |
168 | struct hashmap_iter iter; | |
169 | struct string_list sl = STRING_LIST_INIT_DUP; | |
e9de487a | 170 | struct strbuf parent_pattern = STRBUF_INIT; |
af09ce24 | 171 | |
e9de487a DS |
172 | hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) { |
173 | if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL)) | |
174 | continue; | |
175 | ||
176 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
177 | pe->pattern, | |
178 | &parent_pattern)) | |
179 | string_list_insert(&sl, pe->pattern); | |
180 | } | |
af09ce24 DS |
181 | |
182 | string_list_sort(&sl); | |
183 | string_list_remove_duplicates(&sl, 0); | |
184 | ||
185 | fprintf(fp, "/*\n!/*/\n"); | |
186 | ||
187 | for (i = 0; i < sl.nr; i++) { | |
d585f0e7 | 188 | char *pattern = escaped_pattern(sl.items[i].string); |
af09ce24 DS |
189 | |
190 | if (strlen(pattern)) | |
191 | fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern); | |
d585f0e7 | 192 | free(pattern); |
af09ce24 DS |
193 | } |
194 | ||
195 | string_list_clear(&sl, 0); | |
196 | ||
e9de487a DS |
197 | hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) { |
198 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
199 | pe->pattern, | |
200 | &parent_pattern)) | |
201 | string_list_insert(&sl, pe->pattern); | |
202 | } | |
203 | ||
204 | strbuf_release(&parent_pattern); | |
af09ce24 DS |
205 | |
206 | string_list_sort(&sl); | |
207 | string_list_remove_duplicates(&sl, 0); | |
208 | ||
209 | for (i = 0; i < sl.nr; i++) { | |
d585f0e7 | 210 | char *pattern = escaped_pattern(sl.items[i].string); |
af09ce24 | 211 | fprintf(fp, "%s/\n", pattern); |
d585f0e7 | 212 | free(pattern); |
af09ce24 DS |
213 | } |
214 | } | |
215 | ||
216 | static int write_patterns_and_update(struct pattern_list *pl) | |
217 | { | |
218 | char *sparse_filename; | |
219 | FILE *fp; | |
fb10ca5b DS |
220 | int fd; |
221 | struct lock_file lk = LOCK_INIT; | |
e091228e DS |
222 | int result; |
223 | ||
fb10ca5b | 224 | sparse_filename = get_sparse_checkout_filename(); |
3c754067 DS |
225 | |
226 | if (safe_create_leading_directories(sparse_filename)) | |
227 | die(_("failed to create directory for sparse-checkout file")); | |
228 | ||
fb10ca5b DS |
229 | fd = hold_lock_file_for_update(&lk, sparse_filename, |
230 | LOCK_DIE_ON_ERROR); | |
e091228e | 231 | |
fb10ca5b | 232 | result = update_working_directory(pl); |
e091228e | 233 | if (result) { |
fb10ca5b DS |
234 | rollback_lock_file(&lk); |
235 | free(sparse_filename); | |
e091228e DS |
236 | clear_pattern_list(pl); |
237 | update_working_directory(NULL); | |
238 | return result; | |
239 | } | |
af09ce24 | 240 | |
fb10ca5b | 241 | fp = xfdopen(fd, "w"); |
af09ce24 DS |
242 | |
243 | if (core_sparse_checkout_cone) | |
244 | write_cone_to_file(fp, pl); | |
245 | else | |
246 | write_patterns_to_file(fp, pl); | |
247 | ||
fb10ca5b DS |
248 | fflush(fp); |
249 | commit_lock_file(&lk); | |
e091228e | 250 | |
af09ce24 | 251 | free(sparse_filename); |
e091228e | 252 | clear_pattern_list(pl); |
af09ce24 | 253 | |
e091228e | 254 | return 0; |
af09ce24 DS |
255 | } |
256 | ||
bab3c359 DS |
257 | enum sparse_checkout_mode { |
258 | MODE_NO_PATTERNS = 0, | |
259 | MODE_ALL_PATTERNS = 1, | |
af09ce24 | 260 | MODE_CONE_PATTERNS = 2, |
bab3c359 DS |
261 | }; |
262 | ||
263 | static int set_config(enum sparse_checkout_mode mode) | |
264 | { | |
265 | const char *config_path; | |
266 | ||
98564d80 XL |
267 | if (upgrade_repository_format(1) < 0) |
268 | die(_("unable to upgrade repository format to enable worktreeConfig")); | |
bab3c359 DS |
269 | if (git_config_set_gently("extensions.worktreeConfig", "true")) { |
270 | error(_("failed to set extensions.worktreeConfig setting")); | |
271 | return 1; | |
272 | } | |
273 | ||
274 | config_path = git_path("config.worktree"); | |
275 | git_config_set_in_file_gently(config_path, | |
276 | "core.sparseCheckout", | |
277 | mode ? "true" : NULL); | |
278 | ||
af09ce24 DS |
279 | git_config_set_in_file_gently(config_path, |
280 | "core.sparseCheckoutCone", | |
281 | mode == MODE_CONE_PATTERNS ? "true" : NULL); | |
282 | ||
bab3c359 DS |
283 | return 0; |
284 | } | |
285 | ||
af09ce24 | 286 | static char const * const builtin_sparse_checkout_init_usage[] = { |
122ba1f7 | 287 | N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"), |
af09ce24 DS |
288 | NULL |
289 | }; | |
290 | ||
291 | static struct sparse_checkout_init_opts { | |
292 | int cone_mode; | |
122ba1f7 | 293 | int sparse_index; |
af09ce24 DS |
294 | } init_opts; |
295 | ||
bab3c359 DS |
296 | static int sparse_checkout_init(int argc, const char **argv) |
297 | { | |
298 | struct pattern_list pl; | |
299 | char *sparse_filename; | |
bab3c359 | 300 | int res; |
d89f09c8 | 301 | struct object_id oid; |
af09ce24 | 302 | int mode; |
416adc87 | 303 | struct strbuf pattern = STRBUF_INIT; |
bab3c359 | 304 | |
af09ce24 DS |
305 | static struct option builtin_sparse_checkout_init_options[] = { |
306 | OPT_BOOL(0, "cone", &init_opts.cone_mode, | |
307 | N_("initialize the sparse-checkout in cone mode")), | |
122ba1f7 DS |
308 | OPT_BOOL(0, "sparse-index", &init_opts.sparse_index, |
309 | N_("toggle the use of a sparse index")), | |
af09ce24 DS |
310 | OPT_END(), |
311 | }; | |
312 | ||
cff4e913 | 313 | repo_read_index(the_repository); |
cff4e913 | 314 | |
122ba1f7 DS |
315 | init_opts.sparse_index = -1; |
316 | ||
af09ce24 DS |
317 | argc = parse_options(argc, argv, NULL, |
318 | builtin_sparse_checkout_init_options, | |
319 | builtin_sparse_checkout_init_usage, 0); | |
320 | ||
e091228e DS |
321 | if (init_opts.cone_mode) { |
322 | mode = MODE_CONE_PATTERNS; | |
323 | core_sparse_checkout_cone = 1; | |
324 | } else | |
325 | mode = MODE_ALL_PATTERNS; | |
af09ce24 DS |
326 | |
327 | if (set_config(mode)) | |
bab3c359 DS |
328 | return 1; |
329 | ||
330 | memset(&pl, 0, sizeof(pl)); | |
331 | ||
332 | sparse_filename = get_sparse_checkout_filename(); | |
333 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL); | |
334 | ||
122ba1f7 DS |
335 | if (init_opts.sparse_index >= 0) { |
336 | if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0) | |
337 | die(_("failed to modify sparse-index config")); | |
338 | ||
339 | /* force an index rewrite */ | |
340 | repo_read_index(the_repository); | |
341 | the_repository->index->updated_workdir = 1; | |
342 | } | |
343 | ||
bab3c359 DS |
344 | /* If we already have a sparse-checkout file, use it. */ |
345 | if (res >= 0) { | |
346 | free(sparse_filename); | |
416adc87 DS |
347 | core_apply_sparse_checkout = 1; |
348 | return update_working_directory(NULL); | |
bab3c359 DS |
349 | } |
350 | ||
416adc87 DS |
351 | if (get_oid("HEAD", &oid)) { |
352 | FILE *fp; | |
bab3c359 | 353 | |
416adc87 DS |
354 | /* assume we are in a fresh repo, but update the sparse-checkout file */ |
355 | fp = xfopen(sparse_filename, "w"); | |
356 | if (!fp) | |
357 | die(_("failed to open '%s'"), sparse_filename); | |
bab3c359 | 358 | |
416adc87 DS |
359 | free(sparse_filename); |
360 | fprintf(fp, "/*\n!/*/\n"); | |
361 | fclose(fp); | |
d89f09c8 DS |
362 | return 0; |
363 | } | |
364 | ||
416adc87 DS |
365 | strbuf_addstr(&pattern, "/*"); |
366 | add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); | |
367 | strbuf_addstr(&pattern, "!/*/"); | |
368 | add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); | |
369 | ||
370 | return write_patterns_and_update(&pl); | |
bab3c359 DS |
371 | } |
372 | ||
af09ce24 | 373 | static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) |
f6039a94 | 374 | { |
af09ce24 DS |
375 | struct pattern_entry *e = xmalloc(sizeof(*e)); |
376 | e->patternlen = path->len; | |
377 | e->pattern = strbuf_detach(path, NULL); | |
190a65f9 DS |
378 | hashmap_entry_init(&e->ent, |
379 | ignore_case ? | |
380 | strihash(e->pattern) : | |
381 | strhash(e->pattern)); | |
f6039a94 | 382 | |
af09ce24 | 383 | hashmap_add(&pl->recursive_hashmap, &e->ent); |
f6039a94 | 384 | |
af09ce24 DS |
385 | while (e->patternlen) { |
386 | char *slash = strrchr(e->pattern, '/'); | |
387 | char *oldpattern = e->pattern; | |
388 | size_t newlen; | |
389 | ||
390 | if (slash == e->pattern) | |
391 | break; | |
392 | ||
393 | newlen = slash - e->pattern; | |
394 | e = xmalloc(sizeof(struct pattern_entry)); | |
395 | e->patternlen = newlen; | |
396 | e->pattern = xstrndup(oldpattern, newlen); | |
190a65f9 DS |
397 | hashmap_entry_init(&e->ent, |
398 | ignore_case ? | |
399 | strihash(e->pattern) : | |
400 | strhash(e->pattern)); | |
af09ce24 DS |
401 | |
402 | if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL)) | |
403 | hashmap_add(&pl->parent_hashmap, &e->ent); | |
404 | } | |
405 | } | |
406 | ||
407 | static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) | |
408 | { | |
409 | strbuf_trim(line); | |
410 | ||
411 | strbuf_trim_trailing_dir_sep(line); | |
412 | ||
ef076599 DS |
413 | if (strbuf_normalize_path(line)) |
414 | die(_("could not normalize path %s"), line->buf); | |
415 | ||
af09ce24 DS |
416 | if (!line->len) |
417 | return; | |
418 | ||
419 | if (line->buf[0] != '/') | |
a91cc7fa | 420 | strbuf_insertstr(line, 0, "/"); |
af09ce24 DS |
421 | |
422 | insert_recursive_pattern(pl, line); | |
f6039a94 DS |
423 | } |
424 | ||
7bffca95 | 425 | static char const * const builtin_sparse_checkout_set_usage[] = { |
2631dc87 | 426 | N_("git sparse-checkout (set|add) (--stdin | <patterns>)"), |
7bffca95 DS |
427 | NULL |
428 | }; | |
429 | ||
430 | static struct sparse_checkout_set_opts { | |
431 | int use_stdin; | |
432 | } set_opts; | |
433 | ||
6fb705ab DS |
434 | static void add_patterns_from_input(struct pattern_list *pl, |
435 | int argc, const char **argv) | |
f6039a94 | 436 | { |
f6039a94 | 437 | int i; |
af09ce24 | 438 | if (core_sparse_checkout_cone) { |
7bffca95 DS |
439 | struct strbuf line = STRBUF_INIT; |
440 | ||
6fb705ab DS |
441 | hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
442 | hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
443 | pl->use_cone_patterns = 1; | |
af09ce24 DS |
444 | |
445 | if (set_opts.use_stdin) { | |
bd64de42 DS |
446 | struct strbuf unquoted = STRBUF_INIT; |
447 | while (!strbuf_getline(&line, stdin)) { | |
448 | if (line.buf[0] == '"') { | |
449 | strbuf_reset(&unquoted); | |
450 | if (unquote_c_style(&unquoted, line.buf, NULL)) | |
451 | die(_("unable to unquote C-style string '%s'"), | |
452 | line.buf); | |
453 | ||
454 | strbuf_swap(&unquoted, &line); | |
455 | } | |
456 | ||
6fb705ab | 457 | strbuf_to_cone_pattern(&line, pl); |
bd64de42 DS |
458 | } |
459 | ||
460 | strbuf_release(&unquoted); | |
af09ce24 DS |
461 | } else { |
462 | for (i = 0; i < argc; i++) { | |
463 | strbuf_setlen(&line, 0); | |
464 | strbuf_addstr(&line, argv[i]); | |
6fb705ab | 465 | strbuf_to_cone_pattern(&line, pl); |
af09ce24 | 466 | } |
7bffca95 DS |
467 | } |
468 | } else { | |
af09ce24 DS |
469 | if (set_opts.use_stdin) { |
470 | struct strbuf line = STRBUF_INIT; | |
471 | ||
472 | while (!strbuf_getline(&line, stdin)) { | |
473 | size_t len; | |
474 | char *buf = strbuf_detach(&line, &len); | |
6fb705ab | 475 | add_pattern(buf, empty_base, 0, pl, 0); |
af09ce24 DS |
476 | } |
477 | } else { | |
478 | for (i = 0; i < argc; i++) | |
6fb705ab | 479 | add_pattern(argv[i], empty_base, 0, pl, 0); |
af09ce24 | 480 | } |
7bffca95 | 481 | } |
6fb705ab DS |
482 | } |
483 | ||
4bf0c06c DS |
484 | enum modify_type { |
485 | REPLACE, | |
2631dc87 | 486 | ADD, |
4bf0c06c DS |
487 | }; |
488 | ||
2631dc87 DS |
489 | static void add_patterns_cone_mode(int argc, const char **argv, |
490 | struct pattern_list *pl) | |
491 | { | |
492 | struct strbuf buffer = STRBUF_INIT; | |
493 | struct pattern_entry *pe; | |
494 | struct hashmap_iter iter; | |
495 | struct pattern_list existing; | |
496 | char *sparse_filename = get_sparse_checkout_filename(); | |
497 | ||
498 | add_patterns_from_input(pl, argc, argv); | |
499 | ||
500 | memset(&existing, 0, sizeof(existing)); | |
501 | existing.use_cone_patterns = core_sparse_checkout_cone; | |
502 | ||
503 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, | |
504 | &existing, NULL)) | |
505 | die(_("unable to load existing sparse-checkout patterns")); | |
506 | free(sparse_filename); | |
507 | ||
508 | hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) { | |
509 | if (!hashmap_contains_parent(&pl->recursive_hashmap, | |
510 | pe->pattern, &buffer) || | |
511 | !hashmap_contains_parent(&pl->parent_hashmap, | |
512 | pe->pattern, &buffer)) { | |
513 | strbuf_reset(&buffer); | |
514 | strbuf_addstr(&buffer, pe->pattern); | |
515 | insert_recursive_pattern(pl, &buffer); | |
516 | } | |
517 | } | |
518 | ||
519 | clear_pattern_list(&existing); | |
520 | strbuf_release(&buffer); | |
521 | } | |
522 | ||
523 | static void add_patterns_literal(int argc, const char **argv, | |
524 | struct pattern_list *pl) | |
525 | { | |
526 | char *sparse_filename = get_sparse_checkout_filename(); | |
527 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, | |
528 | pl, NULL)) | |
529 | die(_("unable to load existing sparse-checkout patterns")); | |
530 | free(sparse_filename); | |
531 | add_patterns_from_input(pl, argc, argv); | |
532 | } | |
533 | ||
4bf0c06c | 534 | static int modify_pattern_list(int argc, const char **argv, enum modify_type m) |
6fb705ab | 535 | { |
6fb705ab DS |
536 | int result; |
537 | int changed_config = 0; | |
836e25c5 | 538 | struct pattern_list *pl = xcalloc(1, sizeof(*pl)); |
6fb705ab | 539 | |
2631dc87 DS |
540 | switch (m) { |
541 | case ADD: | |
542 | if (core_sparse_checkout_cone) | |
836e25c5 | 543 | add_patterns_cone_mode(argc, argv, pl); |
2631dc87 | 544 | else |
836e25c5 | 545 | add_patterns_literal(argc, argv, pl); |
2631dc87 DS |
546 | break; |
547 | ||
548 | case REPLACE: | |
836e25c5 | 549 | add_patterns_from_input(pl, argc, argv); |
2631dc87 DS |
550 | break; |
551 | } | |
f6039a94 DS |
552 | |
553 | if (!core_apply_sparse_checkout) { | |
554 | set_config(MODE_ALL_PATTERNS); | |
555 | core_apply_sparse_checkout = 1; | |
556 | changed_config = 1; | |
557 | } | |
558 | ||
836e25c5 | 559 | result = write_patterns_and_update(pl); |
f6039a94 DS |
560 | |
561 | if (result && changed_config) | |
562 | set_config(MODE_NO_PATTERNS); | |
563 | ||
836e25c5 DS |
564 | clear_pattern_list(pl); |
565 | free(pl); | |
f6039a94 DS |
566 | return result; |
567 | } | |
568 | ||
2631dc87 DS |
569 | static int sparse_checkout_set(int argc, const char **argv, const char *prefix, |
570 | enum modify_type m) | |
4bf0c06c DS |
571 | { |
572 | static struct option builtin_sparse_checkout_set_options[] = { | |
573 | OPT_BOOL(0, "stdin", &set_opts.use_stdin, | |
574 | N_("read patterns from standard in")), | |
575 | OPT_END(), | |
576 | }; | |
577 | ||
578 | repo_read_index(the_repository); | |
4bf0c06c DS |
579 | |
580 | argc = parse_options(argc, argv, prefix, | |
581 | builtin_sparse_checkout_set_options, | |
582 | builtin_sparse_checkout_set_usage, | |
583 | PARSE_OPT_KEEP_UNKNOWN); | |
584 | ||
2631dc87 | 585 | return modify_pattern_list(argc, argv, m); |
4bf0c06c DS |
586 | } |
587 | ||
75d3bee1 JK |
588 | static char const * const builtin_sparse_checkout_reapply_usage[] = { |
589 | N_("git sparse-checkout reapply"), | |
590 | NULL | |
591 | }; | |
592 | ||
5644ca28 EN |
593 | static int sparse_checkout_reapply(int argc, const char **argv) |
594 | { | |
75d3bee1 JK |
595 | static struct option builtin_sparse_checkout_reapply_options[] = { |
596 | OPT_END(), | |
597 | }; | |
598 | ||
599 | argc = parse_options(argc, argv, NULL, | |
600 | builtin_sparse_checkout_reapply_options, | |
601 | builtin_sparse_checkout_reapply_usage, 0); | |
602 | ||
5644ca28 EN |
603 | repo_read_index(the_repository); |
604 | return update_working_directory(NULL); | |
605 | } | |
606 | ||
75d3bee1 JK |
607 | static char const * const builtin_sparse_checkout_disable_usage[] = { |
608 | N_("git sparse-checkout disable"), | |
609 | NULL | |
610 | }; | |
611 | ||
72918c1a DS |
612 | static int sparse_checkout_disable(int argc, const char **argv) |
613 | { | |
75d3bee1 JK |
614 | static struct option builtin_sparse_checkout_disable_options[] = { |
615 | OPT_END(), | |
616 | }; | |
99dfa6f9 DS |
617 | struct pattern_list pl; |
618 | struct strbuf match_all = STRBUF_INIT; | |
72918c1a | 619 | |
75d3bee1 JK |
620 | argc = parse_options(argc, argv, NULL, |
621 | builtin_sparse_checkout_disable_options, | |
622 | builtin_sparse_checkout_disable_usage, 0); | |
623 | ||
cff4e913 | 624 | repo_read_index(the_repository); |
cff4e913 | 625 | |
99dfa6f9 DS |
626 | memset(&pl, 0, sizeof(pl)); |
627 | hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); | |
628 | hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); | |
629 | pl.use_cone_patterns = 0; | |
630 | core_apply_sparse_checkout = 1; | |
72918c1a | 631 | |
99dfa6f9 DS |
632 | strbuf_addstr(&match_all, "/*"); |
633 | add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0); | |
72918c1a | 634 | |
99dfa6f9 | 635 | if (update_working_directory(&pl)) |
72918c1a DS |
636 | die(_("error while refreshing working directory")); |
637 | ||
99dfa6f9 | 638 | clear_pattern_list(&pl); |
72918c1a DS |
639 | return set_config(MODE_NO_PATTERNS); |
640 | } | |
641 | ||
94c0956b DS |
642 | int cmd_sparse_checkout(int argc, const char **argv, const char *prefix) |
643 | { | |
644 | static struct option builtin_sparse_checkout_options[] = { | |
645 | OPT_END(), | |
646 | }; | |
647 | ||
648 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
649 | usage_with_options(builtin_sparse_checkout_usage, | |
650 | builtin_sparse_checkout_options); | |
651 | ||
652 | argc = parse_options(argc, argv, prefix, | |
653 | builtin_sparse_checkout_options, | |
654 | builtin_sparse_checkout_usage, | |
655 | PARSE_OPT_STOP_AT_NON_OPTION); | |
656 | ||
657 | git_config(git_default_config, NULL); | |
658 | ||
659 | if (argc > 0) { | |
660 | if (!strcmp(argv[0], "list")) | |
661 | return sparse_checkout_list(argc, argv); | |
bab3c359 DS |
662 | if (!strcmp(argv[0], "init")) |
663 | return sparse_checkout_init(argc, argv); | |
f6039a94 | 664 | if (!strcmp(argv[0], "set")) |
2631dc87 DS |
665 | return sparse_checkout_set(argc, argv, prefix, REPLACE); |
666 | if (!strcmp(argv[0], "add")) | |
667 | return sparse_checkout_set(argc, argv, prefix, ADD); | |
5644ca28 EN |
668 | if (!strcmp(argv[0], "reapply")) |
669 | return sparse_checkout_reapply(argc, argv); | |
72918c1a DS |
670 | if (!strcmp(argv[0], "disable")) |
671 | return sparse_checkout_disable(argc, argv); | |
94c0956b DS |
672 | } |
673 | ||
674 | usage_with_options(builtin_sparse_checkout_usage, | |
675 | builtin_sparse_checkout_options); | |
676 | } |