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