Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/sparse-checkout.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / sparse-checkout.c` 1211 lines ยท 33432 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "builtin.h" 5 #include "abspath.h" 6 #include "config.h" 7 #include "dir.h" 8 #include "environment.h" 9 #include "gettext.h" 10 #include "object-file.h" 11 #include "object-name.h" 12 #include "parse-options.h" 13 #include "path.h" 14 #include "pathspec.h" 15 #include "strbuf.h" 16 #include "string-list.h" 17 #include "lockfile.h" 18 #include "unpack-trees.h" 19 #include "quote.h" 20 #include "setup.h" 21 #include "sparse-index.h" 22 #include "worktree.h" 23 24 static const char *empty_base = ""; 25 26 static char const * const builtin_sparse_checkout_usage[] = { 27 N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules | clean) []"), 28 NULL 29 }; 30 31 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl) 32 { 33 int i; 34 35 for (i = 0; i < pl->nr; i++) { 36 struct path_pattern *p = pl->patterns[i]; 37 38 if (p->flags & PATTERN_FLAG_NEGATIVE) 39 fprintf(fp, "!"); 40 41 fprintf(fp, "%s", p->pattern); 42 43 if (p->flags & PATTERN_FLAG_MUSTBEDIR) 44 fprintf(fp, "/"); 45 46 fprintf(fp, "\n"); 47 } 48 } 49 50 static char const * const builtin_sparse_checkout_list_usage[] = { 51 "git sparse-checkout list", 52 NULL 53 }; 54 55 static int sparse_checkout_list(int argc, const char **argv, const char *prefix, 56 struct repository *repo UNUSED) 57 { 58 static struct option builtin_sparse_checkout_list_options[] = { 59 OPT_END(), 60 }; 61 struct pattern_list pl; 62 char *sparse_filename; 63 int res; 64 struct repo_config_values *cfg = repo_config_values(the_repository); 65 66 setup_work_tree(the_repository); 67 if (!cfg->apply_sparse_checkout) 68 die(_("this worktree is not sparse")); 69 70 argc = parse_options(argc, argv, prefix, 71 builtin_sparse_checkout_list_options, 72 builtin_sparse_checkout_list_usage, 0); 73 74 memset(&pl, 0, sizeof(pl)); 75 76 pl.use_cone_patterns = cfg->core_sparse_checkout_cone; 77 78 sparse_filename = get_sparse_checkout_filename(); 79 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); 80 free(sparse_filename); 81 82 if (res < 0) { 83 warning(_("this worktree is not sparse (sparse-checkout file may not exist)")); 84 return 0; 85 } 86 87 if (pl.use_cone_patterns) { 88 int i; 89 struct pattern_entry *pe; 90 struct hashmap_iter iter; 91 struct string_list sl = STRING_LIST_INIT_DUP; 92 93 hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) { 94 /* pe->pattern starts with "/", skip it */ 95 string_list_append(&sl, pe->pattern + 1); 96 } 97 98 string_list_sort_u(&sl, 0); 99 100 for (i = 0; i < sl.nr; i++) { 101 quote_c_style(sl.items[i].string, NULL, stdout, 0); 102 printf("\n"); 103 } 104 105 string_list_clear(&sl, 0); 106 } else { 107 write_patterns_to_file(stdout, &pl); 108 } 109 110 clear_pattern_list(&pl); 111 112 return 0; 113 } 114 115 static void clean_tracked_sparse_directories(struct repository *r) 116 { 117 int i, was_full = 0; 118 struct strbuf path = STRBUF_INIT; 119 size_t pathlen; 120 struct string_list_item *item; 121 struct string_list sparse_dirs = STRING_LIST_INIT_DUP; 122 123 /* 124 * If we are not using cone mode patterns, then we cannot 125 * delete directories outside of the sparse cone. 126 */ 127 if (!r || !r->index || !r->worktree) 128 return; 129 if (init_sparse_checkout_patterns(r->index) || 130 !r->index->sparse_checkout_patterns->use_cone_patterns) 131 return; 132 133 /* 134 * Use the sparse index as a data structure to assist finding 135 * directories that are safe to delete. This conversion to a 136 * sparse index will not delete directories that contain 137 * conflicted entries or submodules. 138 */ 139 if (r->index->sparse_index == INDEX_EXPANDED) { 140 /* 141 * If something, such as a merge conflict or other concern, 142 * prevents us from converting to a sparse index, then do 143 * not try deleting files. 144 */ 145 if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY)) 146 return; 147 was_full = 1; 148 } 149 150 strbuf_addstr(&path, r->worktree); 151 strbuf_complete(&path, '/'); 152 pathlen = path.len; 153 154 /* 155 * Collect directories that have gone out of scope but also 156 * exist on disk, so there is some work to be done. We need to 157 * store the entries in a list before exploring, since that might 158 * expand the sparse-index again. 159 */ 160 for (i = 0; i < r->index->cache_nr; i++) { 161 struct cache_entry *ce = r->index->cache[i]; 162 163 if (S_ISSPARSEDIR(ce->ce_mode) && 164 repo_file_exists(r, ce->name)) 165 string_list_append(&sparse_dirs, ce->name); 166 } 167 168 for_each_string_list_item(item, &sparse_dirs) { 169 struct dir_struct dir = DIR_INIT; 170 struct pathspec p = { 0 }; 171 struct strvec s = STRVEC_INIT; 172 173 strbuf_setlen(&path, pathlen); 174 strbuf_addstr(&path, item->string); 175 176 dir.flags |= DIR_SHOW_IGNORED_TOO; 177 178 setup_standard_excludes(&dir); 179 strvec_push(&s, path.buf); 180 181 parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v); 182 fill_directory(&dir, r->index, &p); 183 184 if (dir.nr) { 185 warning(_("directory '%s' contains untracked files," 186 " but is not in the sparse-checkout cone"), 187 item->string); 188 } else if (remove_dir_recursively(&path, 0)) { 189 /* 190 * Removal is "best effort". If something blocks 191 * the deletion, then continue with a warning. 192 */ 193 warning(_("failed to remove directory '%s'"), 194 item->string); 195 } 196 197 strvec_clear(&s); 198 clear_pathspec(&p); 199 dir_clear(&dir); 200 } 201 202 string_list_clear(&sparse_dirs, 0); 203 strbuf_release(&path); 204 205 if (was_full) 206 ensure_full_index(r->index); 207 } 208 209 static int update_working_directory(struct repository *r, 210 struct pattern_list *pl) 211 { 212 enum update_sparsity_result result; 213 struct unpack_trees_options o; 214 struct lock_file lock_file = LOCK_INIT; 215 struct pattern_list *old_pl; 216 217 /* If no branch has been checked out, there are no updates to make. */ 218 if (is_index_unborn(r->index)) 219 return UPDATE_SPARSITY_SUCCESS; 220 221 old_pl = r->index->sparse_checkout_patterns; 222 r->index->sparse_checkout_patterns = pl; 223 224 memset(&o, 0, sizeof(o)); 225 o.verbose_update = isatty(2); 226 o.update = 1; 227 o.head_idx = -1; 228 o.src_index = r->index; 229 o.dst_index = r->index; 230 o.skip_sparse_checkout = 0; 231 232 setup_work_tree(the_repository); 233 234 repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR); 235 236 setup_unpack_trees_porcelain(&o, "sparse-checkout"); 237 result = update_sparsity(&o, pl); 238 clear_unpack_trees_porcelain(&o); 239 240 if (result == UPDATE_SPARSITY_WARNINGS) 241 /* 242 * We don't do any special handling of warnings from untracked 243 * files in the way or dirty entries that can't be removed. 244 */ 245 result = UPDATE_SPARSITY_SUCCESS; 246 if (result == UPDATE_SPARSITY_SUCCESS) 247 write_locked_index(r->index, &lock_file, COMMIT_LOCK); 248 else 249 rollback_lock_file(&lock_file); 250 251 clean_tracked_sparse_directories(r); 252 253 if (r->index->sparse_checkout_patterns != pl) { 254 clear_pattern_list(r->index->sparse_checkout_patterns); 255 FREE_AND_NULL(r->index->sparse_checkout_patterns); 256 } 257 r->index->sparse_checkout_patterns = old_pl; 258 259 return result; 260 } 261 262 static char *escaped_pattern(char *pattern) 263 { 264 char *p = pattern; 265 struct strbuf final = STRBUF_INIT; 266 267 while (*p) { 268 if (is_glob_special(*p)) 269 strbuf_addch(&final, '\\'); 270 271 strbuf_addch(&final, *p); 272 p++; 273 } 274 275 return strbuf_detach(&final, NULL); 276 } 277 278 static void write_cone_to_file(FILE *fp, struct pattern_list *pl) 279 { 280 int i; 281 struct pattern_entry *pe; 282 struct hashmap_iter iter; 283 struct string_list sl = STRING_LIST_INIT_DUP; 284 struct strbuf parent_pattern = STRBUF_INIT; 285 286 hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) { 287 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL)) 288 continue; 289 290 if (!hashmap_contains_parent(&pl->recursive_hashmap, 291 pe->pattern, 292 &parent_pattern)) 293 string_list_append(&sl, pe->pattern); 294 } 295 296 string_list_sort_u(&sl, 0); 297 298 fprintf(fp, "/*\n!/*/\n"); 299 300 for (i = 0; i < sl.nr; i++) { 301 char *pattern = escaped_pattern(sl.items[i].string); 302 303 if (strlen(pattern)) 304 fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern); 305 free(pattern); 306 } 307 308 string_list_clear(&sl, 0); 309 310 hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) { 311 if (!hashmap_contains_parent(&pl->recursive_hashmap, 312 pe->pattern, 313 &parent_pattern)) 314 string_list_append(&sl, pe->pattern); 315 } 316 317 strbuf_release(&parent_pattern); 318 319 string_list_sort_u(&sl, 0); 320 321 for (i = 0; i < sl.nr; i++) { 322 char *pattern = escaped_pattern(sl.items[i].string); 323 fprintf(fp, "%s/\n", pattern); 324 free(pattern); 325 } 326 327 string_list_clear(&sl, 0); 328 } 329 330 static int write_patterns_and_update(struct repository *repo, 331 struct pattern_list *pl) 332 { 333 char *sparse_filename; 334 FILE *fp; 335 struct lock_file lk = LOCK_INIT; 336 int result; 337 struct repo_config_values *cfg = repo_config_values(the_repository); 338 339 sparse_filename = get_sparse_checkout_filename(); 340 341 if (safe_create_leading_directories(repo, sparse_filename)) 342 die(_("failed to create directory for sparse-checkout file")); 343 344 repo_hold_lock_file_for_update(repo, &lk, sparse_filename, 345 LOCK_DIE_ON_ERROR); 346 347 result = update_working_directory(repo, pl); 348 if (result) { 349 rollback_lock_file(&lk); 350 update_working_directory(repo, NULL); 351 goto out; 352 } 353 354 fp = fdopen_lock_file(&lk, "w"); 355 if (!fp) 356 die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk)); 357 358 if (cfg->core_sparse_checkout_cone) 359 write_cone_to_file(fp, pl); 360 else 361 write_patterns_to_file(fp, pl); 362 363 if (commit_lock_file(&lk)) 364 die_errno(_("unable to write %s"), sparse_filename); 365 366 out: 367 clear_pattern_list(pl); 368 free(sparse_filename); 369 return result; 370 } 371 372 enum sparse_checkout_mode { 373 MODE_NO_PATTERNS = 0, 374 MODE_ALL_PATTERNS = 1, 375 MODE_CONE_PATTERNS = 2, 376 }; 377 378 static int set_config(struct repository *repo, 379 enum sparse_checkout_mode mode) 380 { 381 /* Update to use worktree config, if not already. */ 382 if (init_worktree_config(repo)) { 383 error(_("failed to initialize worktree config")); 384 return 1; 385 } 386 387 if (repo_config_set_worktree_gently(repo, 388 "core.sparseCheckout", 389 mode ? "true" : "false") || 390 repo_config_set_worktree_gently(repo, 391 "core.sparseCheckoutCone", 392 mode == MODE_CONE_PATTERNS ? 393 "true" : "false")) 394 return 1; 395 396 if (mode == MODE_NO_PATTERNS) 397 return set_sparse_index_config(repo, 0); 398 399 return 0; 400 } 401 402 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) { 403 struct repo_config_values *cfg = repo_config_values(the_repository); 404 405 /* If not specified, use previous definition of cone mode */ 406 if (*cone_mode == -1 && cfg->apply_sparse_checkout) 407 *cone_mode = cfg->core_sparse_checkout_cone; 408 409 /* Set cone/non-cone mode appropriately */ 410 cfg->apply_sparse_checkout = 1; 411 if (*cone_mode == 1 || *cone_mode == -1) { 412 cfg->core_sparse_checkout_cone = 1; 413 return MODE_CONE_PATTERNS; 414 } 415 cfg->core_sparse_checkout_cone = 0; 416 return MODE_ALL_PATTERNS; 417 } 418 419 static int update_modes(struct repository *repo, int *cone_mode, int *sparse_index) 420 { 421 int mode, record_mode; 422 struct repo_config_values *cfg = repo_config_values(the_repository); 423 424 /* Determine if we need to record the mode; ensure sparse checkout on */ 425 record_mode = (*cone_mode != -1) || !cfg->apply_sparse_checkout; 426 427 mode = update_cone_mode(cone_mode); 428 if (record_mode && set_config(repo, mode)) 429 return 1; 430 431 /* Set sparse-index/non-sparse-index mode if specified */ 432 if (*sparse_index >= 0) { 433 if (set_sparse_index_config(repo, *sparse_index) < 0) 434 die(_("failed to modify sparse-index config")); 435 436 /* force an index rewrite */ 437 repo_read_index(repo); 438 repo->index->updated_workdir = 1; 439 440 if (!*sparse_index) 441 ensure_full_index(repo->index); 442 } 443 444 return 0; 445 } 446 447 static char const * const builtin_sparse_checkout_init_usage[] = { 448 "git sparse-checkout init [--cone] [--[no-]sparse-index]", 449 NULL 450 }; 451 452 static struct sparse_checkout_init_opts { 453 int cone_mode; 454 int sparse_index; 455 } init_opts; 456 457 static int sparse_checkout_init(int argc, const char **argv, const char *prefix, 458 struct repository *repo) 459 { 460 struct pattern_list pl; 461 char *sparse_filename; 462 int res; 463 struct object_id oid; 464 465 static struct option builtin_sparse_checkout_init_options[] = { 466 OPT_BOOL(0, "cone", &init_opts.cone_mode, 467 N_("initialize the sparse-checkout in cone mode")), 468 OPT_BOOL(0, "sparse-index", &init_opts.sparse_index, 469 N_("toggle the use of a sparse index")), 470 OPT_END(), 471 }; 472 473 setup_work_tree(the_repository); 474 repo_read_index(repo); 475 476 init_opts.cone_mode = -1; 477 init_opts.sparse_index = -1; 478 479 argc = parse_options(argc, argv, prefix, 480 builtin_sparse_checkout_init_options, 481 builtin_sparse_checkout_init_usage, 0); 482 483 if (update_modes(repo, &init_opts.cone_mode, &init_opts.sparse_index)) 484 return 1; 485 486 memset(&pl, 0, sizeof(pl)); 487 488 sparse_filename = get_sparse_checkout_filename(); 489 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); 490 491 /* If we already have a sparse-checkout file, use it. */ 492 if (res >= 0) { 493 free(sparse_filename); 494 clear_pattern_list(&pl); 495 return update_working_directory(repo, NULL); 496 } 497 498 if (repo_get_oid(repo, "HEAD", &oid)) { 499 FILE *fp; 500 501 /* assume we are in a fresh repo, but update the sparse-checkout file */ 502 if (safe_create_leading_directories(repo, sparse_filename)) 503 die(_("unable to create leading directories of %s"), 504 sparse_filename); 505 fp = xfopen(sparse_filename, "w"); 506 if (!fp) 507 die(_("failed to open '%s'"), sparse_filename); 508 509 free(sparse_filename); 510 fprintf(fp, "/*\n!/*/\n"); 511 fclose(fp); 512 return 0; 513 } 514 515 free(sparse_filename); 516 517 add_pattern("/*", empty_base, 0, &pl, 0); 518 add_pattern("!/*/", empty_base, 0, &pl, 0); 519 pl.use_cone_patterns = init_opts.cone_mode; 520 521 return write_patterns_and_update(repo, &pl); 522 } 523 524 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) 525 { 526 struct pattern_entry *e = xmalloc(sizeof(*e)); 527 e->patternlen = path->len; 528 e->pattern = strbuf_detach(path, NULL); 529 hashmap_entry_init(&e->ent, fspathhash(e->pattern)); 530 531 hashmap_add(&pl->recursive_hashmap, &e->ent); 532 533 while (e->patternlen) { 534 char *slash = strrchr(e->pattern, '/'); 535 char *oldpattern = e->pattern; 536 size_t newlen; 537 struct pattern_entry *dup; 538 539 if (!slash || slash == e->pattern) 540 break; 541 542 newlen = slash - e->pattern; 543 e = xmalloc(sizeof(struct pattern_entry)); 544 e->patternlen = newlen; 545 e->pattern = xstrndup(oldpattern, newlen); 546 hashmap_entry_init(&e->ent, fspathhash(e->pattern)); 547 548 dup = hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL); 549 if (!dup) { 550 hashmap_add(&pl->parent_hashmap, &e->ent); 551 } else { 552 free(e->pattern); 553 free(e); 554 e = dup; 555 } 556 } 557 } 558 559 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) 560 { 561 strbuf_trim(line); 562 563 strbuf_trim_trailing_dir_sep(line); 564 565 if (strbuf_normalize_path(line)) 566 die(_("could not normalize path %s"), line->buf); 567 568 if (!line->len) 569 return; 570 571 if (line->buf[0] != '/') 572 strbuf_insertstr(line, 0, "/"); 573 574 insert_recursive_pattern(pl, line); 575 } 576 577 static void add_patterns_from_input(struct pattern_list *pl, 578 int argc, const char **argv, 579 FILE *file) 580 { 581 int i; 582 struct repo_config_values *cfg = repo_config_values(the_repository); 583 584 if (cfg->core_sparse_checkout_cone) { 585 struct strbuf line = STRBUF_INIT; 586 587 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); 588 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); 589 pl->use_cone_patterns = 1; 590 591 if (file) { 592 struct strbuf unquoted = STRBUF_INIT; 593 while (!strbuf_getline(&line, file)) { 594 if (line.buf[0] == '"') { 595 strbuf_reset(&unquoted); 596 if (unquote_c_style(&unquoted, line.buf, NULL)) 597 die(_("unable to unquote C-style string '%s'"), 598 line.buf); 599 600 strbuf_swap(&unquoted, &line); 601 } 602 603 strbuf_to_cone_pattern(&line, pl); 604 } 605 606 strbuf_release(&unquoted); 607 } else { 608 for (i = 0; i < argc; i++) { 609 strbuf_setlen(&line, 0); 610 strbuf_addstr(&line, argv[i]); 611 strbuf_to_cone_pattern(&line, pl); 612 } 613 } 614 strbuf_release(&line); 615 } else { 616 if (file) { 617 struct strbuf line = STRBUF_INIT; 618 619 while (!strbuf_getline(&line, file)) 620 add_pattern(line.buf, empty_base, 0, pl, 0); 621 622 strbuf_release(&line); 623 } else { 624 for (i = 0; i < argc; i++) 625 add_pattern(argv[i], empty_base, 0, pl, 0); 626 } 627 } 628 } 629 630 enum modify_type { 631 REPLACE, 632 ADD, 633 }; 634 635 static void add_patterns_cone_mode(int argc, const char **argv, 636 struct pattern_list *pl, 637 int use_stdin) 638 { 639 struct strbuf buffer = STRBUF_INIT; 640 struct pattern_entry *pe; 641 struct hashmap_iter iter; 642 struct pattern_list existing; 643 struct repo_config_values *cfg = repo_config_values(the_repository); 644 char *sparse_filename = get_sparse_checkout_filename(); 645 646 add_patterns_from_input(pl, argc, argv, 647 use_stdin ? stdin : NULL); 648 649 memset(&existing, 0, sizeof(existing)); 650 existing.use_cone_patterns = cfg->core_sparse_checkout_cone; 651 652 if (add_patterns_from_file_to_list(sparse_filename, "", 0, 653 &existing, NULL, 0)) 654 die(_("unable to load existing sparse-checkout patterns")); 655 free(sparse_filename); 656 657 if (!existing.use_cone_patterns) 658 die(_("existing sparse-checkout patterns do not use cone mode")); 659 660 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) { 661 if (!hashmap_contains_parent(&pl->recursive_hashmap, 662 pe->pattern, &buffer) || 663 !hashmap_contains_parent(&pl->parent_hashmap, 664 pe->pattern, &buffer)) { 665 strbuf_reset(&buffer); 666 strbuf_addstr(&buffer, pe->pattern); 667 insert_recursive_pattern(pl, &buffer); 668 } 669 } 670 671 clear_pattern_list(&existing); 672 strbuf_release(&buffer); 673 } 674 675 static void add_patterns_literal(int argc, const char **argv, 676 struct pattern_list *pl, 677 int use_stdin) 678 { 679 char *sparse_filename = get_sparse_checkout_filename(); 680 if (add_patterns_from_file_to_list(sparse_filename, "", 0, 681 pl, NULL, 0)) 682 die(_("unable to load existing sparse-checkout patterns")); 683 free(sparse_filename); 684 add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL); 685 } 686 687 static int modify_pattern_list(struct repository *repo, 688 struct strvec *args, int use_stdin, 689 enum modify_type m) 690 { 691 int result; 692 int changed_config = 0; 693 struct pattern_list *pl = xcalloc(1, sizeof(*pl)); 694 struct repo_config_values *cfg = repo_config_values(the_repository); 695 696 switch (m) { 697 case ADD: 698 if (cfg->core_sparse_checkout_cone) 699 add_patterns_cone_mode(args->nr, args->v, pl, use_stdin); 700 else 701 add_patterns_literal(args->nr, args->v, pl, use_stdin); 702 break; 703 704 case REPLACE: 705 add_patterns_from_input(pl, args->nr, args->v, 706 use_stdin ? stdin : NULL); 707 break; 708 } 709 710 if (!cfg->apply_sparse_checkout) { 711 set_config(repo, MODE_ALL_PATTERNS); 712 cfg->apply_sparse_checkout = 1; 713 changed_config = 1; 714 } 715 716 result = write_patterns_and_update(repo, pl); 717 718 if (result && changed_config) 719 set_config(repo, MODE_NO_PATTERNS); 720 721 clear_pattern_list(pl); 722 free(pl); 723 return result; 724 } 725 726 static void sanitize_paths(struct repository *repo, 727 struct strvec *args, 728 const char *prefix, int skip_checks) 729 { 730 int i; 731 struct repo_config_values *cfg = repo_config_values(the_repository); 732 733 if (!args->nr) 734 return; 735 736 if (prefix && *prefix && cfg->core_sparse_checkout_cone) { 737 /* 738 * The args are not pathspecs, so unfortunately we 739 * cannot imitate how cmd_add() uses parse_pathspec(). 740 */ 741 int prefix_len = strlen(prefix); 742 743 for (i = 0; i < args->nr; i++) { 744 char *prefixed_path = prefix_path(the_repository, prefix, 745 prefix_len, args->v[i]); 746 strvec_replace(args, i, prefixed_path); 747 free(prefixed_path); 748 } 749 } 750 751 if (skip_checks) 752 return; 753 754 if (prefix && *prefix && !cfg->core_sparse_checkout_cone) 755 die(_("please run from the toplevel directory in non-cone mode")); 756 757 if (cfg->core_sparse_checkout_cone) { 758 for (i = 0; i < args->nr; i++) { 759 if (args->v[i][0] == '/') 760 die(_("specify directories rather than patterns (no leading slash)")); 761 if (args->v[i][0] == '!') 762 die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks")); 763 if (strpbrk(args->v[i], "*?[]")) 764 die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks")); 765 } 766 } 767 768 for (i = 0; i < args->nr; i++) { 769 struct cache_entry *ce; 770 struct index_state *index = repo->index; 771 int pos = index_name_pos(index, args->v[i], strlen(args->v[i])); 772 773 if (pos < 0) 774 continue; 775 ce = index->cache[pos]; 776 if (S_ISSPARSEDIR(ce->ce_mode)) 777 continue; 778 779 if (cfg->core_sparse_checkout_cone) 780 die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), args->v[i]); 781 else 782 warning(_("pass a leading slash before paths such as '%s' if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)."), args->v[i]); 783 } 784 } 785 786 static char const * const builtin_sparse_checkout_add_usage[] = { 787 N_("git sparse-checkout add [--skip-checks] (--stdin | )"), 788 NULL 789 }; 790 791 static struct sparse_checkout_add_opts { 792 int skip_checks; 793 int use_stdin; 794 } add_opts; 795 796 static int sparse_checkout_add(int argc, const char **argv, const char *prefix, 797 struct repository *repo) 798 { 799 static struct option builtin_sparse_checkout_add_options[] = { 800 OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks, 801 N_("skip some sanity checks on the given paths that might give false positives"), 802 PARSE_OPT_NONEG), 803 OPT_BOOL(0, "stdin", &add_opts.use_stdin, 804 N_("read patterns from standard in")), 805 OPT_END(), 806 }; 807 struct strvec patterns = STRVEC_INIT; 808 int ret; 809 struct repo_config_values *cfg = repo_config_values(the_repository); 810 811 setup_work_tree(the_repository); 812 if (!cfg->apply_sparse_checkout) 813 die(_("no sparse-checkout to add to")); 814 815 repo_read_index(repo); 816 817 argc = parse_options(argc, argv, prefix, 818 builtin_sparse_checkout_add_options, 819 builtin_sparse_checkout_add_usage, 0); 820 821 for (int i = 0; i < argc; i++) 822 strvec_push(&patterns, argv[i]); 823 sanitize_paths(repo, &patterns, prefix, add_opts.skip_checks); 824 825 ret = modify_pattern_list(repo, &patterns, add_opts.use_stdin, ADD); 826 827 strvec_clear(&patterns); 828 return ret; 829 } 830 831 static char const * const builtin_sparse_checkout_set_usage[] = { 832 N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | )"), 833 NULL 834 }; 835 836 static struct sparse_checkout_set_opts { 837 int cone_mode; 838 int sparse_index; 839 int skip_checks; 840 int use_stdin; 841 } set_opts; 842 843 static int sparse_checkout_set(int argc, const char **argv, const char *prefix, 844 struct repository *repo) 845 { 846 struct repo_config_values *cfg = repo_config_values(the_repository); 847 int default_patterns_nr = 2; 848 const char *default_patterns[] = {"/*", "!/*/", NULL}; 849 850 static struct option builtin_sparse_checkout_set_options[] = { 851 OPT_BOOL(0, "cone", &set_opts.cone_mode, 852 N_("initialize the sparse-checkout in cone mode")), 853 OPT_BOOL(0, "sparse-index", &set_opts.sparse_index, 854 N_("toggle the use of a sparse index")), 855 OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks, 856 N_("skip some sanity checks on the given paths that might give false positives"), 857 PARSE_OPT_NONEG), 858 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin, 859 N_("read patterns from standard in"), 860 PARSE_OPT_NONEG), 861 OPT_END(), 862 }; 863 struct strvec patterns = STRVEC_INIT; 864 int ret; 865 866 setup_work_tree(the_repository); 867 repo_read_index(repo); 868 869 set_opts.cone_mode = -1; 870 set_opts.sparse_index = -1; 871 872 argc = parse_options(argc, argv, prefix, 873 builtin_sparse_checkout_set_options, 874 builtin_sparse_checkout_set_usage, 0); 875 876 if (update_modes(repo, &set_opts.cone_mode, &set_opts.sparse_index)) 877 return 1; 878 879 /* 880 * Cone mode automatically specifies the toplevel directory. For 881 * non-cone mode, if nothing is specified, manually select just the 882 * top-level directory (much as 'init' would do). 883 */ 884 if (!cfg->core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) { 885 for (int i = 0; i < default_patterns_nr; i++) 886 strvec_push(&patterns, default_patterns[i]); 887 } else { 888 for (int i = 0; i < argc; i++) 889 strvec_push(&patterns, argv[i]); 890 sanitize_paths(repo, &patterns, prefix, set_opts.skip_checks); 891 } 892 893 ret = modify_pattern_list(repo, &patterns, set_opts.use_stdin, REPLACE); 894 895 strvec_clear(&patterns); 896 return ret; 897 } 898 899 static char const * const builtin_sparse_checkout_reapply_usage[] = { 900 "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]", 901 NULL 902 }; 903 904 static struct sparse_checkout_reapply_opts { 905 int cone_mode; 906 int sparse_index; 907 } reapply_opts; 908 909 static int sparse_checkout_reapply(int argc, const char **argv, 910 const char *prefix, 911 struct repository *repo) 912 { 913 static struct option builtin_sparse_checkout_reapply_options[] = { 914 OPT_BOOL(0, "cone", &reapply_opts.cone_mode, 915 N_("initialize the sparse-checkout in cone mode")), 916 OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index, 917 N_("toggle the use of a sparse index")), 918 OPT_END(), 919 }; 920 struct repo_config_values *cfg = repo_config_values(the_repository); 921 922 setup_work_tree(the_repository); 923 if (!cfg->apply_sparse_checkout) 924 die(_("must be in a sparse-checkout to reapply sparsity patterns")); 925 926 reapply_opts.cone_mode = -1; 927 reapply_opts.sparse_index = -1; 928 929 argc = parse_options(argc, argv, prefix, 930 builtin_sparse_checkout_reapply_options, 931 builtin_sparse_checkout_reapply_usage, 0); 932 933 repo_read_index(repo); 934 935 if (update_modes(repo, &reapply_opts.cone_mode, &reapply_opts.sparse_index)) 936 return 1; 937 938 return update_working_directory(repo, NULL); 939 } 940 941 static char const * const builtin_sparse_checkout_clean_usage[] = { 942 "git sparse-checkout clean [-n|--dry-run]", 943 NULL 944 }; 945 946 static int list_file_iterator(const char *path, const void *data) 947 { 948 const char *msg = data; 949 950 printf(msg, path); 951 return 0; 952 } 953 954 static void list_every_file_in_dir(const char *msg, 955 const char *directory) 956 { 957 struct strbuf path = STRBUF_INIT; 958 959 strbuf_addstr(&path, directory); 960 for_each_file_in_dir(&path, list_file_iterator, msg); 961 strbuf_release(&path); 962 } 963 964 static const char *msg_remove = N_("Removing %s\n"); 965 static const char *msg_would_remove = N_("Would remove %s\n"); 966 967 static int sparse_checkout_clean(int argc, const char **argv, 968 const char *prefix, 969 struct repository *repo) 970 { 971 struct strbuf full_path = STRBUF_INIT; 972 const char *msg = msg_remove; 973 size_t worktree_len; 974 int force = 0, dry_run = 0, verbose = 0; 975 int require_force = 1; 976 struct repo_config_values *cfg = repo_config_values(the_repository); 977 978 struct option builtin_sparse_checkout_clean_options[] = { 979 OPT__DRY_RUN(&dry_run, N_("dry run")), 980 OPT__FORCE(&force, N_("force"), PARSE_OPT_NOCOMPLETE), 981 OPT__VERBOSE(&verbose, N_("report each affected file, not just directories")), 982 OPT_END(), 983 }; 984 985 setup_work_tree(the_repository); 986 if (!cfg->apply_sparse_checkout) 987 die(_("must be in a sparse-checkout to clean directories")); 988 if (!cfg->core_sparse_checkout_cone) 989 die(_("must be in a cone-mode sparse-checkout to clean directories")); 990 991 argc = parse_options(argc, argv, prefix, 992 builtin_sparse_checkout_clean_options, 993 builtin_sparse_checkout_clean_usage, 0); 994 995 repo_config_get_bool(repo, "clean.requireforce", &require_force); 996 if (require_force && !force && !dry_run) 997 die(_("for safety, refusing to clean without one of --force or --dry-run")); 998 999 if (dry_run) 1000 msg = msg_would_remove; 1001 1002 if (repo_read_index(repo) < 0) 1003 die(_("failed to read index")); 1004 1005 if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) || 1006 repo->index->sparse_index == INDEX_EXPANDED) 1007 die(_("failed to convert index to a sparse index; resolve merge conflicts and try again")); 1008 1009 strbuf_addstr(&full_path, repo->worktree); 1010 strbuf_addch(&full_path, '/'); 1011 worktree_len = full_path.len; 1012 1013 for (size_t i = 0; i < repo->index->cache_nr; i++) { 1014 struct cache_entry *ce = repo->index->cache[i]; 1015 if (!S_ISSPARSEDIR(ce->ce_mode)) 1016 continue; 1017 strbuf_setlen(&full_path, worktree_len); 1018 strbuf_add(&full_path, ce->name, ce->ce_namelen); 1019 1020 if (!is_directory(full_path.buf)) 1021 continue; 1022 1023 if (verbose) 1024 list_every_file_in_dir(msg, ce->name); 1025 else 1026 printf(msg, ce->name); 1027 1028 if (dry_run <= 0 && 1029 remove_dir_recursively(&full_path, 0)) 1030 warning_errno(_("failed to remove '%s'"), ce->name); 1031 } 1032 1033 strbuf_release(&full_path); 1034 return 0; 1035 } 1036 1037 static char const * const builtin_sparse_checkout_disable_usage[] = { 1038 "git sparse-checkout disable", 1039 NULL 1040 }; 1041 1042 static int sparse_checkout_disable(int argc, const char **argv, 1043 const char *prefix, 1044 struct repository *repo) 1045 { 1046 static struct option builtin_sparse_checkout_disable_options[] = { 1047 OPT_END(), 1048 }; 1049 struct pattern_list pl; 1050 struct repo_config_values *cfg = repo_config_values(the_repository); 1051 1052 /* 1053 * We do not exit early if !repo->config_values.apply_sparse_checkout; due to the 1054 * ability for users to manually muck things up between 1055 * direct editing of .git/info/sparse-checkout 1056 * running read-tree -m u HEAD or update-index --skip-worktree 1057 * direct toggling of config options 1058 * users might end up with an index with SKIP_WORKTREE bit set on 1059 * some files and not know how to undo it. So, here we just 1060 * forcibly return to a dense checkout regardless of initial state. 1061 */ 1062 1063 setup_work_tree(the_repository); 1064 argc = parse_options(argc, argv, prefix, 1065 builtin_sparse_checkout_disable_options, 1066 builtin_sparse_checkout_disable_usage, 0); 1067 1068 /* 1069 * Disable the advice message for expanding a sparse index, as we 1070 * are expecting to do that when disabling sparse-checkout. 1071 */ 1072 give_advice_on_expansion = 0; 1073 repo_read_index(repo); 1074 1075 memset(&pl, 0, sizeof(pl)); 1076 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); 1077 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); 1078 pl.use_cone_patterns = 0; 1079 cfg->apply_sparse_checkout = 1; 1080 1081 add_pattern("/*", empty_base, 0, &pl, 0); 1082 1083 prepare_repo_settings(the_repository); 1084 repo->settings.sparse_index = 0; 1085 1086 if (update_working_directory(repo, &pl)) 1087 die(_("error while refreshing working directory")); 1088 1089 clear_pattern_list(&pl); 1090 return set_config(repo, MODE_NO_PATTERNS); 1091 } 1092 1093 static char const * const builtin_sparse_checkout_check_rules_usage[] = { 1094 N_("git sparse-checkout check-rules [-z] [--skip-checks]" 1095 "[--[no-]cone] [--rules-file ]"), 1096 NULL 1097 }; 1098 1099 static struct sparse_checkout_check_rules_opts { 1100 int cone_mode; 1101 int null_termination; 1102 char *rules_file; 1103 } check_rules_opts; 1104 1105 static int check_rules(struct repository *repo, 1106 struct pattern_list *pl, 1107 int null_terminated) 1108 { 1109 struct strbuf line = STRBUF_INIT; 1110 struct strbuf unquoted = STRBUF_INIT; 1111 char *path; 1112 int line_terminator = null_terminated ? 0 : '\n'; 1113 strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul 1114 : strbuf_getline; 1115 repo->index->sparse_checkout_patterns = pl; 1116 while (!getline_fn(&line, stdin)) { 1117 path = line.buf; 1118 if (!null_terminated && line.buf[0] == '"') { 1119 strbuf_reset(&unquoted); 1120 if (unquote_c_style(&unquoted, line.buf, NULL)) 1121 die(_("unable to unquote C-style string '%s'"), 1122 line.buf); 1123 1124 path = unquoted.buf; 1125 } 1126 1127 if (path_in_sparse_checkout(path, repo->index)) 1128 write_name_quoted(path, stdout, line_terminator); 1129 } 1130 strbuf_release(&line); 1131 strbuf_release(&unquoted); 1132 1133 return 0; 1134 } 1135 1136 static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix, 1137 struct repository *repo) 1138 { 1139 static struct option builtin_sparse_checkout_check_rules_options[] = { 1140 OPT_BOOL('z', NULL, &check_rules_opts.null_termination, 1141 N_("terminate input and output files by a NUL character")), 1142 OPT_BOOL(0, "cone", &check_rules_opts.cone_mode, 1143 N_("when used with --rules-file interpret patterns as cone mode patterns")), 1144 OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file, 1145 N_("use patterns in instead of the current ones.")), 1146 OPT_END(), 1147 }; 1148 1149 FILE *fp; 1150 int ret; 1151 struct pattern_list pl = {0}; 1152 struct repo_config_values *cfg = repo_config_values(the_repository); 1153 char *sparse_filename; 1154 check_rules_opts.cone_mode = -1; 1155 1156 argc = parse_options(argc, argv, prefix, 1157 builtin_sparse_checkout_check_rules_options, 1158 builtin_sparse_checkout_check_rules_usage, 0); 1159 1160 if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0) 1161 check_rules_opts.cone_mode = 1; 1162 1163 update_cone_mode(&check_rules_opts.cone_mode); 1164 pl.use_cone_patterns = cfg->core_sparse_checkout_cone; 1165 if (check_rules_opts.rules_file) { 1166 fp = xfopen(check_rules_opts.rules_file, "r"); 1167 add_patterns_from_input(&pl, argc, argv, fp); 1168 fclose(fp); 1169 } else { 1170 sparse_filename = get_sparse_checkout_filename(); 1171 if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, 1172 NULL, 0)) 1173 die(_("unable to load existing sparse-checkout patterns")); 1174 free(sparse_filename); 1175 } 1176 1177 ret = check_rules(repo, &pl, check_rules_opts.null_termination); 1178 clear_pattern_list(&pl); 1179 free(check_rules_opts.rules_file); 1180 return ret; 1181 } 1182 1183 int cmd_sparse_checkout(int argc, 1184 const char **argv, 1185 const char *prefix, 1186 struct repository *repo) 1187 { 1188 parse_opt_subcommand_fn *fn = NULL; 1189 struct option builtin_sparse_checkout_options[] = { 1190 OPT_SUBCOMMAND("list", &fn, sparse_checkout_list), 1191 OPT_SUBCOMMAND("init", &fn, sparse_checkout_init), 1192 OPT_SUBCOMMAND("set", &fn, sparse_checkout_set), 1193 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add), 1194 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply), 1195 OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean), 1196 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable), 1197 OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules), 1198 OPT_END(), 1199 }; 1200 1201 argc = parse_options(argc, argv, prefix, 1202 builtin_sparse_checkout_options, 1203 builtin_sparse_checkout_usage, 0); 1204 1205 repo_config(the_repository, git_default_config, NULL); 1206 1207 prepare_repo_settings(repo); 1208 repo->settings.command_requires_full_index = 0; 1209 1210 return fn(argc, argv, prefix, repo); 1211 }