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