| 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) [<options>]"), |
| 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 | hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR); |
| 345 | |
| 346 | result = update_working_directory(repo, pl); |
| 347 | if (result) { |
| 348 | rollback_lock_file(&lk); |
| 349 | update_working_directory(repo, NULL); |
| 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | fp = fdopen_lock_file(&lk, "w"); |
| 354 | if (!fp) |
| 355 | die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk)); |
| 356 | |
| 357 | if (cfg->core_sparse_checkout_cone) |
| 358 | write_cone_to_file(fp, pl); |
| 359 | else |
| 360 | write_patterns_to_file(fp, pl); |
| 361 | |
| 362 | if (commit_lock_file(&lk)) |
| 363 | die_errno(_("unable to write %s"), sparse_filename); |
| 364 | |
| 365 | out: |
| 366 | clear_pattern_list(pl); |
| 367 | free(sparse_filename); |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | enum sparse_checkout_mode { |
| 372 | MODE_NO_PATTERNS = 0, |
| 373 | MODE_ALL_PATTERNS = 1, |
| 374 | MODE_CONE_PATTERNS = 2, |
| 375 | }; |
| 376 | |
| 377 | static int set_config(struct repository *repo, |
| 378 | enum sparse_checkout_mode mode) |
| 379 | { |
| 380 | /* Update to use worktree config, if not already. */ |
| 381 | if (init_worktree_config(repo)) { |
| 382 | error(_("failed to initialize worktree config")); |
| 383 | return 1; |
| 384 | } |
| 385 | |
| 386 | if (repo_config_set_worktree_gently(repo, |
| 387 | "core.sparseCheckout", |
| 388 | mode ? "true" : "false") || |
| 389 | repo_config_set_worktree_gently(repo, |
| 390 | "core.sparseCheckoutCone", |
| 391 | mode == MODE_CONE_PATTERNS ? |
| 392 | "true" : "false")) |
| 393 | return 1; |
| 394 | |
| 395 | if (mode == MODE_NO_PATTERNS) |
| 396 | return set_sparse_index_config(repo, 0); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static enum sparse_checkout_mode update_cone_mode(int *cone_mode) { |
| 402 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 403 | |
| 404 | /* If not specified, use previous definition of cone mode */ |
| 405 | if (*cone_mode == -1 && cfg->apply_sparse_checkout) |
| 406 | *cone_mode = cfg->core_sparse_checkout_cone; |
| 407 | |
| 408 | /* Set cone/non-cone mode appropriately */ |
| 409 | cfg->apply_sparse_checkout = 1; |
| 410 | if (*cone_mode == 1 || *cone_mode == -1) { |
| 411 | cfg->core_sparse_checkout_cone = 1; |
| 412 | return MODE_CONE_PATTERNS; |
| 413 | } |
| 414 | cfg->core_sparse_checkout_cone = 0; |
| 415 | return MODE_ALL_PATTERNS; |
| 416 | } |
| 417 | |
| 418 | static int update_modes(struct repository *repo, int *cone_mode, int *sparse_index) |
| 419 | { |
| 420 | int mode, record_mode; |
| 421 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 422 | |
| 423 | /* Determine if we need to record the mode; ensure sparse checkout on */ |
| 424 | record_mode = (*cone_mode != -1) || !cfg->apply_sparse_checkout; |
| 425 | |
| 426 | mode = update_cone_mode(cone_mode); |
| 427 | if (record_mode && set_config(repo, mode)) |
| 428 | return 1; |
| 429 | |
| 430 | /* Set sparse-index/non-sparse-index mode if specified */ |
| 431 | if (*sparse_index >= 0) { |
| 432 | if (set_sparse_index_config(repo, *sparse_index) < 0) |
| 433 | die(_("failed to modify sparse-index config")); |
| 434 | |
| 435 | /* force an index rewrite */ |
| 436 | repo_read_index(repo); |
| 437 | repo->index->updated_workdir = 1; |
| 438 | |
| 439 | if (!*sparse_index) |
| 440 | ensure_full_index(repo->index); |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static char const * const builtin_sparse_checkout_init_usage[] = { |
| 447 | "git sparse-checkout init [--cone] [--[no-]sparse-index]", |
| 448 | NULL |
| 449 | }; |
| 450 | |
| 451 | static struct sparse_checkout_init_opts { |
| 452 | int cone_mode; |
| 453 | int sparse_index; |
| 454 | } init_opts; |
| 455 | |
| 456 | static int sparse_checkout_init(int argc, const char **argv, const char *prefix, |
| 457 | struct repository *repo) |
| 458 | { |
| 459 | struct pattern_list pl; |
| 460 | char *sparse_filename; |
| 461 | int res; |
| 462 | struct object_id oid; |
| 463 | |
| 464 | static struct option builtin_sparse_checkout_init_options[] = { |
| 465 | OPT_BOOL(0, "cone", &init_opts.cone_mode, |
| 466 | N_("initialize the sparse-checkout in cone mode")), |
| 467 | OPT_BOOL(0, "sparse-index", &init_opts.sparse_index, |
| 468 | N_("toggle the use of a sparse index")), |
| 469 | OPT_END(), |
| 470 | }; |
| 471 | |
| 472 | setup_work_tree(the_repository); |
| 473 | repo_read_index(repo); |
| 474 | |
| 475 | init_opts.cone_mode = -1; |
| 476 | init_opts.sparse_index = -1; |
| 477 | |
| 478 | argc = parse_options(argc, argv, prefix, |
| 479 | builtin_sparse_checkout_init_options, |
| 480 | builtin_sparse_checkout_init_usage, 0); |
| 481 | |
| 482 | if (update_modes(repo, &init_opts.cone_mode, &init_opts.sparse_index)) |
| 483 | return 1; |
| 484 | |
| 485 | memset(&pl, 0, sizeof(pl)); |
| 486 | |
| 487 | sparse_filename = get_sparse_checkout_filename(); |
| 488 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); |
| 489 | |
| 490 | /* If we already have a sparse-checkout file, use it. */ |
| 491 | if (res >= 0) { |
| 492 | free(sparse_filename); |
| 493 | clear_pattern_list(&pl); |
| 494 | return update_working_directory(repo, NULL); |
| 495 | } |
| 496 | |
| 497 | if (repo_get_oid(repo, "HEAD", &oid)) { |
| 498 | FILE *fp; |
| 499 | |
| 500 | /* assume we are in a fresh repo, but update the sparse-checkout file */ |
| 501 | if (safe_create_leading_directories(repo, sparse_filename)) |
| 502 | die(_("unable to create leading directories of %s"), |
| 503 | sparse_filename); |
| 504 | fp = xfopen(sparse_filename, "w"); |
| 505 | if (!fp) |
| 506 | die(_("failed to open '%s'"), sparse_filename); |
| 507 | |
| 508 | free(sparse_filename); |
| 509 | fprintf(fp, "/*\n!/*/\n"); |
| 510 | fclose(fp); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | free(sparse_filename); |
| 515 | |
| 516 | add_pattern("/*", empty_base, 0, &pl, 0); |
| 517 | add_pattern("!/*/", empty_base, 0, &pl, 0); |
| 518 | pl.use_cone_patterns = init_opts.cone_mode; |
| 519 | |
| 520 | return write_patterns_and_update(repo, &pl); |
| 521 | } |
| 522 | |
| 523 | static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) |
| 524 | { |
| 525 | struct pattern_entry *e = xmalloc(sizeof(*e)); |
| 526 | e->patternlen = path->len; |
| 527 | e->pattern = strbuf_detach(path, NULL); |
| 528 | hashmap_entry_init(&e->ent, fspathhash(e->pattern)); |
| 529 | |
| 530 | hashmap_add(&pl->recursive_hashmap, &e->ent); |
| 531 | |
| 532 | while (e->patternlen) { |
| 533 | char *slash = strrchr(e->pattern, '/'); |
| 534 | char *oldpattern = e->pattern; |
| 535 | size_t newlen; |
| 536 | struct pattern_entry *dup; |
| 537 | |
| 538 | if (!slash || slash == e->pattern) |
| 539 | break; |
| 540 | |
| 541 | newlen = slash - e->pattern; |
| 542 | e = xmalloc(sizeof(struct pattern_entry)); |
| 543 | e->patternlen = newlen; |
| 544 | e->pattern = xstrndup(oldpattern, newlen); |
| 545 | hashmap_entry_init(&e->ent, fspathhash(e->pattern)); |
| 546 | |
| 547 | dup = hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL); |
| 548 | if (!dup) { |
| 549 | hashmap_add(&pl->parent_hashmap, &e->ent); |
| 550 | } else { |
| 551 | free(e->pattern); |
| 552 | free(e); |
| 553 | e = dup; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) |
| 559 | { |
| 560 | strbuf_trim(line); |
| 561 | |
| 562 | strbuf_trim_trailing_dir_sep(line); |
| 563 | |
| 564 | if (strbuf_normalize_path(line)) |
| 565 | die(_("could not normalize path %s"), line->buf); |
| 566 | |
| 567 | if (!line->len) |
| 568 | return; |
| 569 | |
| 570 | if (line->buf[0] != '/') |
| 571 | strbuf_insertstr(line, 0, "/"); |
| 572 | |
| 573 | insert_recursive_pattern(pl, line); |
| 574 | } |
| 575 | |
| 576 | static void add_patterns_from_input(struct pattern_list *pl, |
| 577 | int argc, const char **argv, |
| 578 | FILE *file) |
| 579 | { |
| 580 | int i; |
| 581 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 582 | |
| 583 | if (cfg->core_sparse_checkout_cone) { |
| 584 | struct strbuf line = STRBUF_INIT; |
| 585 | |
| 586 | hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
| 587 | hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); |
| 588 | pl->use_cone_patterns = 1; |
| 589 | |
| 590 | if (file) { |
| 591 | struct strbuf unquoted = STRBUF_INIT; |
| 592 | while (!strbuf_getline(&line, file)) { |
| 593 | if (line.buf[0] == '"') { |
| 594 | strbuf_reset(&unquoted); |
| 595 | if (unquote_c_style(&unquoted, line.buf, NULL)) |
| 596 | die(_("unable to unquote C-style string '%s'"), |
| 597 | line.buf); |
| 598 | |
| 599 | strbuf_swap(&unquoted, &line); |
| 600 | } |
| 601 | |
| 602 | strbuf_to_cone_pattern(&line, pl); |
| 603 | } |
| 604 | |
| 605 | strbuf_release(&unquoted); |
| 606 | } else { |
| 607 | for (i = 0; i < argc; i++) { |
| 608 | strbuf_setlen(&line, 0); |
| 609 | strbuf_addstr(&line, argv[i]); |
| 610 | strbuf_to_cone_pattern(&line, pl); |
| 611 | } |
| 612 | } |
| 613 | strbuf_release(&line); |
| 614 | } else { |
| 615 | if (file) { |
| 616 | struct strbuf line = STRBUF_INIT; |
| 617 | |
| 618 | while (!strbuf_getline(&line, file)) |
| 619 | add_pattern(line.buf, empty_base, 0, pl, 0); |
| 620 | |
| 621 | strbuf_release(&line); |
| 622 | } else { |
| 623 | for (i = 0; i < argc; i++) |
| 624 | add_pattern(argv[i], empty_base, 0, pl, 0); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | enum modify_type { |
| 630 | REPLACE, |
| 631 | ADD, |
| 632 | }; |
| 633 | |
| 634 | static void add_patterns_cone_mode(int argc, const char **argv, |
| 635 | struct pattern_list *pl, |
| 636 | int use_stdin) |
| 637 | { |
| 638 | struct strbuf buffer = STRBUF_INIT; |
| 639 | struct pattern_entry *pe; |
| 640 | struct hashmap_iter iter; |
| 641 | struct pattern_list existing; |
| 642 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 643 | char *sparse_filename = get_sparse_checkout_filename(); |
| 644 | |
| 645 | add_patterns_from_input(pl, argc, argv, |
| 646 | use_stdin ? stdin : NULL); |
| 647 | |
| 648 | memset(&existing, 0, sizeof(existing)); |
| 649 | existing.use_cone_patterns = cfg->core_sparse_checkout_cone; |
| 650 | |
| 651 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, |
| 652 | &existing, NULL, 0)) |
| 653 | die(_("unable to load existing sparse-checkout patterns")); |
| 654 | free(sparse_filename); |
| 655 | |
| 656 | if (!existing.use_cone_patterns) |
| 657 | die(_("existing sparse-checkout patterns do not use cone mode")); |
| 658 | |
| 659 | hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) { |
| 660 | if (!hashmap_contains_parent(&pl->recursive_hashmap, |
| 661 | pe->pattern, &buffer) || |
| 662 | !hashmap_contains_parent(&pl->parent_hashmap, |
| 663 | pe->pattern, &buffer)) { |
| 664 | strbuf_reset(&buffer); |
| 665 | strbuf_addstr(&buffer, pe->pattern); |
| 666 | insert_recursive_pattern(pl, &buffer); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | clear_pattern_list(&existing); |
| 671 | strbuf_release(&buffer); |
| 672 | } |
| 673 | |
| 674 | static void add_patterns_literal(int argc, const char **argv, |
| 675 | struct pattern_list *pl, |
| 676 | int use_stdin) |
| 677 | { |
| 678 | char *sparse_filename = get_sparse_checkout_filename(); |
| 679 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, |
| 680 | pl, NULL, 0)) |
| 681 | die(_("unable to load existing sparse-checkout patterns")); |
| 682 | free(sparse_filename); |
| 683 | add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL); |
| 684 | } |
| 685 | |
| 686 | static int modify_pattern_list(struct repository *repo, |
| 687 | struct strvec *args, int use_stdin, |
| 688 | enum modify_type m) |
| 689 | { |
| 690 | int result; |
| 691 | int changed_config = 0; |
| 692 | struct pattern_list *pl = xcalloc(1, sizeof(*pl)); |
| 693 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 694 | |
| 695 | switch (m) { |
| 696 | case ADD: |
| 697 | if (cfg->core_sparse_checkout_cone) |
| 698 | add_patterns_cone_mode(args->nr, args->v, pl, use_stdin); |
| 699 | else |
| 700 | add_patterns_literal(args->nr, args->v, pl, use_stdin); |
| 701 | break; |
| 702 | |
| 703 | case REPLACE: |
| 704 | add_patterns_from_input(pl, args->nr, args->v, |
| 705 | use_stdin ? stdin : NULL); |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | if (!cfg->apply_sparse_checkout) { |
| 710 | set_config(repo, MODE_ALL_PATTERNS); |
| 711 | cfg->apply_sparse_checkout = 1; |
| 712 | changed_config = 1; |
| 713 | } |
| 714 | |
| 715 | result = write_patterns_and_update(repo, pl); |
| 716 | |
| 717 | if (result && changed_config) |
| 718 | set_config(repo, MODE_NO_PATTERNS); |
| 719 | |
| 720 | clear_pattern_list(pl); |
| 721 | free(pl); |
| 722 | return result; |
| 723 | } |
| 724 | |
| 725 | static void sanitize_paths(struct repository *repo, |
| 726 | struct strvec *args, |
| 727 | const char *prefix, int skip_checks) |
| 728 | { |
| 729 | int i; |
| 730 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 731 | |
| 732 | if (!args->nr) |
| 733 | return; |
| 734 | |
| 735 | if (prefix && *prefix && cfg->core_sparse_checkout_cone) { |
| 736 | /* |
| 737 | * The args are not pathspecs, so unfortunately we |
| 738 | * cannot imitate how cmd_add() uses parse_pathspec(). |
| 739 | */ |
| 740 | int prefix_len = strlen(prefix); |
| 741 | |
| 742 | for (i = 0; i < args->nr; i++) { |
| 743 | char *prefixed_path = prefix_path(the_repository, prefix, |
| 744 | prefix_len, args->v[i]); |
| 745 | strvec_replace(args, i, prefixed_path); |
| 746 | free(prefixed_path); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (skip_checks) |
| 751 | return; |
| 752 | |
| 753 | if (prefix && *prefix && !cfg->core_sparse_checkout_cone) |
| 754 | die(_("please run from the toplevel directory in non-cone mode")); |
| 755 | |
| 756 | if (cfg->core_sparse_checkout_cone) { |
| 757 | for (i = 0; i < args->nr; i++) { |
| 758 | if (args->v[i][0] == '/') |
| 759 | die(_("specify directories rather than patterns (no leading slash)")); |
| 760 | if (args->v[i][0] == '!') |
| 761 | die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks")); |
| 762 | if (strpbrk(args->v[i], "*?[]")) |
| 763 | die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks")); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | for (i = 0; i < args->nr; i++) { |
| 768 | struct cache_entry *ce; |
| 769 | struct index_state *index = repo->index; |
| 770 | int pos = index_name_pos(index, args->v[i], strlen(args->v[i])); |
| 771 | |
| 772 | if (pos < 0) |
| 773 | continue; |
| 774 | ce = index->cache[pos]; |
| 775 | if (S_ISSPARSEDIR(ce->ce_mode)) |
| 776 | continue; |
| 777 | |
| 778 | if (cfg->core_sparse_checkout_cone) |
| 779 | die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), args->v[i]); |
| 780 | else |
| 781 | 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]); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static char const * const builtin_sparse_checkout_add_usage[] = { |
| 786 | N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"), |
| 787 | NULL |
| 788 | }; |
| 789 | |
| 790 | static struct sparse_checkout_add_opts { |
| 791 | int skip_checks; |
| 792 | int use_stdin; |
| 793 | } add_opts; |
| 794 | |
| 795 | static int sparse_checkout_add(int argc, const char **argv, const char *prefix, |
| 796 | struct repository *repo) |
| 797 | { |
| 798 | static struct option builtin_sparse_checkout_add_options[] = { |
| 799 | OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks, |
| 800 | N_("skip some sanity checks on the given paths that might give false positives"), |
| 801 | PARSE_OPT_NONEG), |
| 802 | OPT_BOOL(0, "stdin", &add_opts.use_stdin, |
| 803 | N_("read patterns from standard in")), |
| 804 | OPT_END(), |
| 805 | }; |
| 806 | struct strvec patterns = STRVEC_INIT; |
| 807 | int ret; |
| 808 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 809 | |
| 810 | setup_work_tree(the_repository); |
| 811 | if (!cfg->apply_sparse_checkout) |
| 812 | die(_("no sparse-checkout to add to")); |
| 813 | |
| 814 | repo_read_index(repo); |
| 815 | |
| 816 | argc = parse_options(argc, argv, prefix, |
| 817 | builtin_sparse_checkout_add_options, |
| 818 | builtin_sparse_checkout_add_usage, 0); |
| 819 | |
| 820 | for (int i = 0; i < argc; i++) |
| 821 | strvec_push(&patterns, argv[i]); |
| 822 | sanitize_paths(repo, &patterns, prefix, add_opts.skip_checks); |
| 823 | |
| 824 | ret = modify_pattern_list(repo, &patterns, add_opts.use_stdin, ADD); |
| 825 | |
| 826 | strvec_clear(&patterns); |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | static char const * const builtin_sparse_checkout_set_usage[] = { |
| 831 | N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"), |
| 832 | NULL |
| 833 | }; |
| 834 | |
| 835 | static struct sparse_checkout_set_opts { |
| 836 | int cone_mode; |
| 837 | int sparse_index; |
| 838 | int skip_checks; |
| 839 | int use_stdin; |
| 840 | } set_opts; |
| 841 | |
| 842 | static int sparse_checkout_set(int argc, const char **argv, const char *prefix, |
| 843 | struct repository *repo) |
| 844 | { |
| 845 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 846 | int default_patterns_nr = 2; |
| 847 | const char *default_patterns[] = {"/*", "!/*/", NULL}; |
| 848 | |
| 849 | static struct option builtin_sparse_checkout_set_options[] = { |
| 850 | OPT_BOOL(0, "cone", &set_opts.cone_mode, |
| 851 | N_("initialize the sparse-checkout in cone mode")), |
| 852 | OPT_BOOL(0, "sparse-index", &set_opts.sparse_index, |
| 853 | N_("toggle the use of a sparse index")), |
| 854 | OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks, |
| 855 | N_("skip some sanity checks on the given paths that might give false positives"), |
| 856 | PARSE_OPT_NONEG), |
| 857 | OPT_BOOL_F(0, "stdin", &set_opts.use_stdin, |
| 858 | N_("read patterns from standard in"), |
| 859 | PARSE_OPT_NONEG), |
| 860 | OPT_END(), |
| 861 | }; |
| 862 | struct strvec patterns = STRVEC_INIT; |
| 863 | int ret; |
| 864 | |
| 865 | setup_work_tree(the_repository); |
| 866 | repo_read_index(repo); |
| 867 | |
| 868 | set_opts.cone_mode = -1; |
| 869 | set_opts.sparse_index = -1; |
| 870 | |
| 871 | argc = parse_options(argc, argv, prefix, |
| 872 | builtin_sparse_checkout_set_options, |
| 873 | builtin_sparse_checkout_set_usage, 0); |
| 874 | |
| 875 | if (update_modes(repo, &set_opts.cone_mode, &set_opts.sparse_index)) |
| 876 | return 1; |
| 877 | |
| 878 | /* |
| 879 | * Cone mode automatically specifies the toplevel directory. For |
| 880 | * non-cone mode, if nothing is specified, manually select just the |
| 881 | * top-level directory (much as 'init' would do). |
| 882 | */ |
| 883 | if (!cfg->core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) { |
| 884 | for (int i = 0; i < default_patterns_nr; i++) |
| 885 | strvec_push(&patterns, default_patterns[i]); |
| 886 | } else { |
| 887 | for (int i = 0; i < argc; i++) |
| 888 | strvec_push(&patterns, argv[i]); |
| 889 | sanitize_paths(repo, &patterns, prefix, set_opts.skip_checks); |
| 890 | } |
| 891 | |
| 892 | ret = modify_pattern_list(repo, &patterns, set_opts.use_stdin, REPLACE); |
| 893 | |
| 894 | strvec_clear(&patterns); |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | static char const * const builtin_sparse_checkout_reapply_usage[] = { |
| 899 | "git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]", |
| 900 | NULL |
| 901 | }; |
| 902 | |
| 903 | static struct sparse_checkout_reapply_opts { |
| 904 | int cone_mode; |
| 905 | int sparse_index; |
| 906 | } reapply_opts; |
| 907 | |
| 908 | static int sparse_checkout_reapply(int argc, const char **argv, |
| 909 | const char *prefix, |
| 910 | struct repository *repo) |
| 911 | { |
| 912 | static struct option builtin_sparse_checkout_reapply_options[] = { |
| 913 | OPT_BOOL(0, "cone", &reapply_opts.cone_mode, |
| 914 | N_("initialize the sparse-checkout in cone mode")), |
| 915 | OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index, |
| 916 | N_("toggle the use of a sparse index")), |
| 917 | OPT_END(), |
| 918 | }; |
| 919 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 920 | |
| 921 | setup_work_tree(the_repository); |
| 922 | if (!cfg->apply_sparse_checkout) |
| 923 | die(_("must be in a sparse-checkout to reapply sparsity patterns")); |
| 924 | |
| 925 | reapply_opts.cone_mode = -1; |
| 926 | reapply_opts.sparse_index = -1; |
| 927 | |
| 928 | argc = parse_options(argc, argv, prefix, |
| 929 | builtin_sparse_checkout_reapply_options, |
| 930 | builtin_sparse_checkout_reapply_usage, 0); |
| 931 | |
| 932 | repo_read_index(repo); |
| 933 | |
| 934 | if (update_modes(repo, &reapply_opts.cone_mode, &reapply_opts.sparse_index)) |
| 935 | return 1; |
| 936 | |
| 937 | return update_working_directory(repo, NULL); |
| 938 | } |
| 939 | |
| 940 | static char const * const builtin_sparse_checkout_clean_usage[] = { |
| 941 | "git sparse-checkout clean [-n|--dry-run]", |
| 942 | NULL |
| 943 | }; |
| 944 | |
| 945 | static int list_file_iterator(const char *path, const void *data) |
| 946 | { |
| 947 | const char *msg = data; |
| 948 | |
| 949 | printf(msg, path); |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static void list_every_file_in_dir(const char *msg, |
| 954 | const char *directory) |
| 955 | { |
| 956 | struct strbuf path = STRBUF_INIT; |
| 957 | |
| 958 | strbuf_addstr(&path, directory); |
| 959 | for_each_file_in_dir(&path, list_file_iterator, msg); |
| 960 | strbuf_release(&path); |
| 961 | } |
| 962 | |
| 963 | static const char *msg_remove = N_("Removing %s\n"); |
| 964 | static const char *msg_would_remove = N_("Would remove %s\n"); |
| 965 | |
| 966 | static int sparse_checkout_clean(int argc, const char **argv, |
| 967 | const char *prefix, |
| 968 | struct repository *repo) |
| 969 | { |
| 970 | struct strbuf full_path = STRBUF_INIT; |
| 971 | const char *msg = msg_remove; |
| 972 | size_t worktree_len; |
| 973 | int force = 0, dry_run = 0, verbose = 0; |
| 974 | int require_force = 1; |
| 975 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 976 | |
| 977 | struct option builtin_sparse_checkout_clean_options[] = { |
| 978 | OPT__DRY_RUN(&dry_run, N_("dry run")), |
| 979 | OPT__FORCE(&force, N_("force"), PARSE_OPT_NOCOMPLETE), |
| 980 | OPT__VERBOSE(&verbose, N_("report each affected file, not just directories")), |
| 981 | OPT_END(), |
| 982 | }; |
| 983 | |
| 984 | setup_work_tree(the_repository); |
| 985 | if (!cfg->apply_sparse_checkout) |
| 986 | die(_("must be in a sparse-checkout to clean directories")); |
| 987 | if (!cfg->core_sparse_checkout_cone) |
| 988 | die(_("must be in a cone-mode sparse-checkout to clean directories")); |
| 989 | |
| 990 | argc = parse_options(argc, argv, prefix, |
| 991 | builtin_sparse_checkout_clean_options, |
| 992 | builtin_sparse_checkout_clean_usage, 0); |
| 993 | |
| 994 | repo_config_get_bool(repo, "clean.requireforce", &require_force); |
| 995 | if (require_force && !force && !dry_run) |
| 996 | die(_("for safety, refusing to clean without one of --force or --dry-run")); |
| 997 | |
| 998 | if (dry_run) |
| 999 | msg = msg_would_remove; |
| 1000 | |
| 1001 | if (repo_read_index(repo) < 0) |
| 1002 | die(_("failed to read index")); |
| 1003 | |
| 1004 | if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) || |
| 1005 | repo->index->sparse_index == INDEX_EXPANDED) |
| 1006 | die(_("failed to convert index to a sparse index; resolve merge conflicts and try again")); |
| 1007 | |
| 1008 | strbuf_addstr(&full_path, repo->worktree); |
| 1009 | strbuf_addch(&full_path, '/'); |
| 1010 | worktree_len = full_path.len; |
| 1011 | |
| 1012 | for (size_t i = 0; i < repo->index->cache_nr; i++) { |
| 1013 | struct cache_entry *ce = repo->index->cache[i]; |
| 1014 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
| 1015 | continue; |
| 1016 | strbuf_setlen(&full_path, worktree_len); |
| 1017 | strbuf_add(&full_path, ce->name, ce->ce_namelen); |
| 1018 | |
| 1019 | if (!is_directory(full_path.buf)) |
| 1020 | continue; |
| 1021 | |
| 1022 | if (verbose) |
| 1023 | list_every_file_in_dir(msg, ce->name); |
| 1024 | else |
| 1025 | printf(msg, ce->name); |
| 1026 | |
| 1027 | if (dry_run <= 0 && |
| 1028 | remove_dir_recursively(&full_path, 0)) |
| 1029 | warning_errno(_("failed to remove '%s'"), ce->name); |
| 1030 | } |
| 1031 | |
| 1032 | strbuf_release(&full_path); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | static char const * const builtin_sparse_checkout_disable_usage[] = { |
| 1037 | "git sparse-checkout disable", |
| 1038 | NULL |
| 1039 | }; |
| 1040 | |
| 1041 | static int sparse_checkout_disable(int argc, const char **argv, |
| 1042 | const char *prefix, |
| 1043 | struct repository *repo) |
| 1044 | { |
| 1045 | static struct option builtin_sparse_checkout_disable_options[] = { |
| 1046 | OPT_END(), |
| 1047 | }; |
| 1048 | struct pattern_list pl; |
| 1049 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 1050 | |
| 1051 | /* |
| 1052 | * We do not exit early if !repo->config_values.apply_sparse_checkout; due to the |
| 1053 | * ability for users to manually muck things up between |
| 1054 | * direct editing of .git/info/sparse-checkout |
| 1055 | * running read-tree -m u HEAD or update-index --skip-worktree |
| 1056 | * direct toggling of config options |
| 1057 | * users might end up with an index with SKIP_WORKTREE bit set on |
| 1058 | * some files and not know how to undo it. So, here we just |
| 1059 | * forcibly return to a dense checkout regardless of initial state. |
| 1060 | */ |
| 1061 | |
| 1062 | setup_work_tree(the_repository); |
| 1063 | argc = parse_options(argc, argv, prefix, |
| 1064 | builtin_sparse_checkout_disable_options, |
| 1065 | builtin_sparse_checkout_disable_usage, 0); |
| 1066 | |
| 1067 | /* |
| 1068 | * Disable the advice message for expanding a sparse index, as we |
| 1069 | * are expecting to do that when disabling sparse-checkout. |
| 1070 | */ |
| 1071 | give_advice_on_expansion = 0; |
| 1072 | repo_read_index(repo); |
| 1073 | |
| 1074 | memset(&pl, 0, sizeof(pl)); |
| 1075 | hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
| 1076 | hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); |
| 1077 | pl.use_cone_patterns = 0; |
| 1078 | cfg->apply_sparse_checkout = 1; |
| 1079 | |
| 1080 | add_pattern("/*", empty_base, 0, &pl, 0); |
| 1081 | |
| 1082 | prepare_repo_settings(the_repository); |
| 1083 | repo->settings.sparse_index = 0; |
| 1084 | |
| 1085 | if (update_working_directory(repo, &pl)) |
| 1086 | die(_("error while refreshing working directory")); |
| 1087 | |
| 1088 | clear_pattern_list(&pl); |
| 1089 | return set_config(repo, MODE_NO_PATTERNS); |
| 1090 | } |
| 1091 | |
| 1092 | static char const * const builtin_sparse_checkout_check_rules_usage[] = { |
| 1093 | N_("git sparse-checkout check-rules [-z] [--skip-checks]" |
| 1094 | "[--[no-]cone] [--rules-file <file>]"), |
| 1095 | NULL |
| 1096 | }; |
| 1097 | |
| 1098 | static struct sparse_checkout_check_rules_opts { |
| 1099 | int cone_mode; |
| 1100 | int null_termination; |
| 1101 | char *rules_file; |
| 1102 | } check_rules_opts; |
| 1103 | |
| 1104 | static int check_rules(struct repository *repo, |
| 1105 | struct pattern_list *pl, |
| 1106 | int null_terminated) |
| 1107 | { |
| 1108 | struct strbuf line = STRBUF_INIT; |
| 1109 | struct strbuf unquoted = STRBUF_INIT; |
| 1110 | char *path; |
| 1111 | int line_terminator = null_terminated ? 0 : '\n'; |
| 1112 | strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul |
| 1113 | : strbuf_getline; |
| 1114 | repo->index->sparse_checkout_patterns = pl; |
| 1115 | while (!getline_fn(&line, stdin)) { |
| 1116 | path = line.buf; |
| 1117 | if (!null_terminated && line.buf[0] == '"') { |
| 1118 | strbuf_reset(&unquoted); |
| 1119 | if (unquote_c_style(&unquoted, line.buf, NULL)) |
| 1120 | die(_("unable to unquote C-style string '%s'"), |
| 1121 | line.buf); |
| 1122 | |
| 1123 | path = unquoted.buf; |
| 1124 | } |
| 1125 | |
| 1126 | if (path_in_sparse_checkout(path, repo->index)) |
| 1127 | write_name_quoted(path, stdout, line_terminator); |
| 1128 | } |
| 1129 | strbuf_release(&line); |
| 1130 | strbuf_release(&unquoted); |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix, |
| 1136 | struct repository *repo) |
| 1137 | { |
| 1138 | static struct option builtin_sparse_checkout_check_rules_options[] = { |
| 1139 | OPT_BOOL('z', NULL, &check_rules_opts.null_termination, |
| 1140 | N_("terminate input and output files by a NUL character")), |
| 1141 | OPT_BOOL(0, "cone", &check_rules_opts.cone_mode, |
| 1142 | N_("when used with --rules-file interpret patterns as cone mode patterns")), |
| 1143 | OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file, |
| 1144 | N_("use patterns in <file> instead of the current ones.")), |
| 1145 | OPT_END(), |
| 1146 | }; |
| 1147 | |
| 1148 | FILE *fp; |
| 1149 | int ret; |
| 1150 | struct pattern_list pl = {0}; |
| 1151 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 1152 | char *sparse_filename; |
| 1153 | check_rules_opts.cone_mode = -1; |
| 1154 | |
| 1155 | argc = parse_options(argc, argv, prefix, |
| 1156 | builtin_sparse_checkout_check_rules_options, |
| 1157 | builtin_sparse_checkout_check_rules_usage, 0); |
| 1158 | |
| 1159 | if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0) |
| 1160 | check_rules_opts.cone_mode = 1; |
| 1161 | |
| 1162 | update_cone_mode(&check_rules_opts.cone_mode); |
| 1163 | pl.use_cone_patterns = cfg->core_sparse_checkout_cone; |
| 1164 | if (check_rules_opts.rules_file) { |
| 1165 | fp = xfopen(check_rules_opts.rules_file, "r"); |
| 1166 | add_patterns_from_input(&pl, argc, argv, fp); |
| 1167 | fclose(fp); |
| 1168 | } else { |
| 1169 | sparse_filename = get_sparse_checkout_filename(); |
| 1170 | if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, |
| 1171 | NULL, 0)) |
| 1172 | die(_("unable to load existing sparse-checkout patterns")); |
| 1173 | free(sparse_filename); |
| 1174 | } |
| 1175 | |
| 1176 | ret = check_rules(repo, &pl, check_rules_opts.null_termination); |
| 1177 | clear_pattern_list(&pl); |
| 1178 | free(check_rules_opts.rules_file); |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | int cmd_sparse_checkout(int argc, |
| 1183 | const char **argv, |
| 1184 | const char *prefix, |
| 1185 | struct repository *repo) |
| 1186 | { |
| 1187 | parse_opt_subcommand_fn *fn = NULL; |
| 1188 | struct option builtin_sparse_checkout_options[] = { |
| 1189 | OPT_SUBCOMMAND("list", &fn, sparse_checkout_list), |
| 1190 | OPT_SUBCOMMAND("init", &fn, sparse_checkout_init), |
| 1191 | OPT_SUBCOMMAND("set", &fn, sparse_checkout_set), |
| 1192 | OPT_SUBCOMMAND("add", &fn, sparse_checkout_add), |
| 1193 | OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply), |
| 1194 | OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean), |
| 1195 | OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable), |
| 1196 | OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules), |
| 1197 | OPT_END(), |
| 1198 | }; |
| 1199 | |
| 1200 | argc = parse_options(argc, argv, prefix, |
| 1201 | builtin_sparse_checkout_options, |
| 1202 | builtin_sparse_checkout_usage, 0); |
| 1203 | |
| 1204 | repo_config(the_repository, git_default_config, NULL); |
| 1205 | |
| 1206 | prepare_repo_settings(repo); |
| 1207 | repo->settings.command_requires_full_index = 0; |
| 1208 | |
| 1209 | return fn(argc, argv, prefix, repo); |
| 1210 | } |