| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "git-compat-util.h" |
| 5 | #include "environment.h" |
| 6 | #include "ewah/ewok.h" |
| 7 | #include "gettext.h" |
| 8 | #include "name-hash.h" |
| 9 | #include "read-cache-ll.h" |
| 10 | #include "repository.h" |
| 11 | #include "sparse-index.h" |
| 12 | #include "tree.h" |
| 13 | #include "pathspec.h" |
| 14 | #include "trace2.h" |
| 15 | #include "cache-tree.h" |
| 16 | #include "config.h" |
| 17 | #include "dir.h" |
| 18 | #include "fsmonitor-ll.h" |
| 19 | #include "advice.h" |
| 20 | |
| 21 | /** |
| 22 | * This global is used by expand_index() to determine if we should give the |
| 23 | * advice for advice.sparseIndexExpanded when expanding a sparse index to a full |
| 24 | * one. However, this is sometimes done on purpose, such as in the sparse-checkout |
| 25 | * builtin, even when index.sparse=false. This may be disabled in |
| 26 | * convert_to_sparse() or by commands that know they will lead to a full |
| 27 | * expansion, but this message is not actionable. |
| 28 | */ |
| 29 | int give_advice_on_expansion = 1; |
| 30 | #define ADVICE_MSG \ |
| 31 | "The sparse index is expanding to a full index, a slow operation.\n" \ |
| 32 | "Your working directory likely has contents that are outside of\n" \ |
| 33 | "your sparse-checkout patterns. Use 'git sparse-checkout list' to\n" \ |
| 34 | "see your sparse-checkout definition and compare it to your working\n" \ |
| 35 | "directory contents. Cleaning up any merge conflicts or staged\n" \ |
| 36 | "changes before running 'git sparse-checkout clean' or 'git\n" \ |
| 37 | "sparse-checkout reapply' may assist in this cleanup." |
| 38 | |
| 39 | struct modify_index_context { |
| 40 | struct index_state *write; |
| 41 | struct pattern_list *pl; |
| 42 | }; |
| 43 | |
| 44 | static struct cache_entry *construct_sparse_dir_entry( |
| 45 | struct index_state *istate, |
| 46 | const char *sparse_dir, |
| 47 | struct cache_tree *tree) |
| 48 | { |
| 49 | struct cache_entry *de; |
| 50 | |
| 51 | de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0); |
| 52 | |
| 53 | de->ce_flags |= CE_SKIP_WORKTREE; |
| 54 | return de; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Returns the number of entries "inserted" into the index. |
| 59 | */ |
| 60 | static int convert_to_sparse_rec(struct index_state *istate, |
| 61 | int num_converted, |
| 62 | int start, int end, |
| 63 | const char *ct_path, size_t ct_pathlen, |
| 64 | struct cache_tree *ct) |
| 65 | { |
| 66 | int i, can_convert = 1; |
| 67 | int start_converted = num_converted; |
| 68 | struct strbuf child_path = STRBUF_INIT; |
| 69 | |
| 70 | /* |
| 71 | * Is the current path outside of the sparse cone? |
| 72 | * Then check if the region can be replaced by a sparse |
| 73 | * directory entry (everything is sparse and merged). |
| 74 | */ |
| 75 | if (path_in_sparse_checkout(ct_path, istate)) |
| 76 | can_convert = 0; |
| 77 | |
| 78 | for (i = start; can_convert && i < end; i++) { |
| 79 | struct cache_entry *ce = istate->cache[i]; |
| 80 | |
| 81 | if (ce_stage(ce) || |
| 82 | S_ISGITLINK(ce->ce_mode) || |
| 83 | !(ce->ce_flags & CE_SKIP_WORKTREE)) |
| 84 | can_convert = 0; |
| 85 | } |
| 86 | |
| 87 | if (can_convert) { |
| 88 | struct cache_entry *se; |
| 89 | se = construct_sparse_dir_entry(istate, ct_path, ct); |
| 90 | |
| 91 | istate->cache[num_converted++] = se; |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | for (i = start; i < end; ) { |
| 96 | int count, span, pos = -1; |
| 97 | const char *base, *slash; |
| 98 | struct cache_entry *ce = istate->cache[i]; |
| 99 | |
| 100 | /* |
| 101 | * Detect if this is a normal entry outside of any subtree |
| 102 | * entry. |
| 103 | */ |
| 104 | base = ce->name + ct_pathlen; |
| 105 | slash = strchr(base, '/'); |
| 106 | |
| 107 | if (slash) |
| 108 | pos = cache_tree_subtree_pos(ct, base, slash - base); |
| 109 | |
| 110 | if (pos < 0) { |
| 111 | istate->cache[num_converted++] = ce; |
| 112 | i++; |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | span = ct->down[pos]->cache_tree->entry_count; |
| 117 | if (span < 0) { |
| 118 | /* cache-tree entry is invalidated, cannot collapse. */ |
| 119 | istate->cache[num_converted++] = ce; |
| 120 | i++; |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | strbuf_setlen(&child_path, 0); |
| 125 | strbuf_add(&child_path, ce->name, slash - ce->name + 1); |
| 126 | |
| 127 | count = convert_to_sparse_rec(istate, |
| 128 | num_converted, i, i + span, |
| 129 | child_path.buf, child_path.len, |
| 130 | ct->down[pos]->cache_tree); |
| 131 | num_converted += count; |
| 132 | i += span; |
| 133 | } |
| 134 | |
| 135 | strbuf_release(&child_path); |
| 136 | return num_converted - start_converted; |
| 137 | } |
| 138 | |
| 139 | int set_sparse_index_config(struct repository *repo, int enable) |
| 140 | { |
| 141 | int res = repo_config_set_worktree_gently(repo, |
| 142 | "index.sparse", |
| 143 | enable ? "true" : "false"); |
| 144 | prepare_repo_settings(repo); |
| 145 | repo->settings.sparse_index = enable; |
| 146 | return res; |
| 147 | } |
| 148 | |
| 149 | static int index_has_unmerged_entries(struct index_state *istate) |
| 150 | { |
| 151 | int i; |
| 152 | for (i = 0; i < istate->cache_nr; i++) { |
| 153 | if (ce_stage(istate->cache[i])) |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int is_sparse_index_allowed(struct index_state *istate, int flags) |
| 161 | { |
| 162 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 163 | |
| 164 | if (!cfg->apply_sparse_checkout || !cfg->core_sparse_checkout_cone) |
| 165 | return 0; |
| 166 | |
| 167 | if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) { |
| 168 | int test_env; |
| 169 | |
| 170 | /* |
| 171 | * The sparse index is not (yet) integrated with a split index. |
| 172 | */ |
| 173 | if (istate->split_index || git_env_bool("GIT_TEST_SPLIT_INDEX", 0)) |
| 174 | return 0; |
| 175 | /* |
| 176 | * The GIT_TEST_SPARSE_INDEX environment variable triggers the |
| 177 | * index.sparse config variable to be on. |
| 178 | */ |
| 179 | test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1); |
| 180 | if (test_env >= 0) |
| 181 | set_sparse_index_config(istate->repo, test_env); |
| 182 | |
| 183 | /* |
| 184 | * Only convert to sparse if index.sparse is set. |
| 185 | */ |
| 186 | prepare_repo_settings(istate->repo); |
| 187 | if (!istate->repo->settings.sparse_index) |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | if (init_sparse_checkout_patterns(istate)) |
| 192 | return 0; |
| 193 | |
| 194 | /* |
| 195 | * We need cone-mode patterns to use sparse-index. If a user edits |
| 196 | * their sparse-checkout file manually, then we can detect during |
| 197 | * parsing that they are not actually using cone-mode patterns and |
| 198 | * hence we need to abort this conversion _without error_. Warnings |
| 199 | * already exist in the pattern parsing to inform the user of their |
| 200 | * bad patterns. |
| 201 | */ |
| 202 | if (!istate->sparse_checkout_patterns->use_cone_patterns) |
| 203 | return 0; |
| 204 | |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | int convert_to_sparse(struct index_state *istate, int flags) |
| 209 | { |
| 210 | /* |
| 211 | * If the index is already sparse, empty, or otherwise |
| 212 | * cannot be converted to sparse, do not convert. |
| 213 | */ |
| 214 | if (istate->sparse_index == INDEX_COLLAPSED || !istate->cache_nr || |
| 215 | !is_sparse_index_allowed(istate, flags)) |
| 216 | return 0; |
| 217 | |
| 218 | /* |
| 219 | * If we are purposefully collapsing a full index, then don't give |
| 220 | * advice when it is expanded later. |
| 221 | */ |
| 222 | give_advice_on_expansion = 0; |
| 223 | |
| 224 | /* |
| 225 | * NEEDSWORK: If we have unmerged entries, then stay full. |
| 226 | * Unmerged entries prevent the cache-tree extension from working. |
| 227 | */ |
| 228 | if (index_has_unmerged_entries(istate)) |
| 229 | return 0; |
| 230 | |
| 231 | if (!cache_tree_fully_valid(istate->cache_tree)) { |
| 232 | /* Clear and recompute the cache-tree */ |
| 233 | cache_tree_free(&istate->cache_tree); |
| 234 | |
| 235 | /* |
| 236 | * Silently return if there is a problem with the cache tree update, |
| 237 | * which might just be due to a conflict state in some entry. |
| 238 | * |
| 239 | * This might create new tree objects, so be sure to use |
| 240 | * WRITE_TREE_MISSING_OK. |
| 241 | */ |
| 242 | if (cache_tree_update(istate, WRITE_TREE_MISSING_OK)) |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | remove_fsmonitor(istate); |
| 247 | |
| 248 | trace2_region_enter("index", "convert_to_sparse", istate->repo); |
| 249 | istate->cache_nr = convert_to_sparse_rec(istate, |
| 250 | 0, 0, istate->cache_nr, |
| 251 | "", 0, istate->cache_tree); |
| 252 | |
| 253 | /* Clear and recompute the cache-tree */ |
| 254 | cache_tree_free(&istate->cache_tree); |
| 255 | cache_tree_update(istate, 0); |
| 256 | |
| 257 | istate->fsmonitor_has_run_once = 0; |
| 258 | ewah_free(istate->fsmonitor_dirty); |
| 259 | istate->fsmonitor_dirty = NULL; |
| 260 | FREE_AND_NULL(istate->fsmonitor_last_update); |
| 261 | |
| 262 | istate->sparse_index = INDEX_COLLAPSED; |
| 263 | trace2_region_leave("index", "convert_to_sparse", istate->repo); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
| 268 | { |
| 269 | ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc); |
| 270 | |
| 271 | istate->cache[nr] = ce; |
| 272 | add_name_hash(istate, ce); |
| 273 | } |
| 274 | |
| 275 | static int add_path_to_index(const struct object_id *oid, |
| 276 | struct strbuf *base, const char *path, |
| 277 | unsigned int mode, void *context) |
| 278 | { |
| 279 | struct modify_index_context *ctx = (struct modify_index_context *)context; |
| 280 | struct cache_entry *ce; |
| 281 | size_t len = base->len; |
| 282 | |
| 283 | if (S_ISDIR(mode)) { |
| 284 | int dtype; |
| 285 | size_t baselen = base->len; |
| 286 | if (!ctx->pl) |
| 287 | return READ_TREE_RECURSIVE; |
| 288 | |
| 289 | /* |
| 290 | * Have we expanded to a point outside of the sparse-checkout? |
| 291 | * |
| 292 | * Artificially pad the path name with a slash "/" to |
| 293 | * indicate it as a directory, and add an arbitrary file |
| 294 | * name ("-") so we can consider base->buf as a file name |
| 295 | * to match against the cone-mode patterns. |
| 296 | * |
| 297 | * If we compared just "path", then we would expand more |
| 298 | * than we should. Since every file at root is always |
| 299 | * included, we would expand every directory at root at |
| 300 | * least one level deep instead of using sparse directory |
| 301 | * entries. |
| 302 | */ |
| 303 | strbuf_addstr(base, path); |
| 304 | strbuf_add(base, "/-", 2); |
| 305 | |
| 306 | if (path_matches_pattern_list(base->buf, base->len, |
| 307 | NULL, &dtype, |
| 308 | ctx->pl, ctx->write)) { |
| 309 | strbuf_setlen(base, baselen); |
| 310 | return READ_TREE_RECURSIVE; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * The path "{base}{path}/" is a sparse directory. Create the correct |
| 315 | * name for inserting the entry into the index. |
| 316 | */ |
| 317 | strbuf_setlen(base, base->len - 1); |
| 318 | } else { |
| 319 | strbuf_addstr(base, path); |
| 320 | } |
| 321 | |
| 322 | ce = make_cache_entry(ctx->write, mode, oid, base->buf, 0, 0); |
| 323 | ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED; |
| 324 | set_index_entry(ctx->write, ctx->write->cache_nr++, ce); |
| 325 | |
| 326 | strbuf_setlen(base, len); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | void expand_index(struct index_state *istate, struct pattern_list *pl) |
| 331 | { |
| 332 | int i; |
| 333 | struct index_state *full; |
| 334 | struct strbuf base = STRBUF_INIT; |
| 335 | const char *tr_region; |
| 336 | struct modify_index_context ctx; |
| 337 | |
| 338 | /* |
| 339 | * If the index is already full, then keep it full. We will convert |
| 340 | * it to a sparse index on write, if possible. |
| 341 | */ |
| 342 | if (istate->sparse_index == INDEX_EXPANDED) |
| 343 | return; |
| 344 | |
| 345 | /* |
| 346 | * If our index is sparse, but our new pattern set does not use |
| 347 | * cone mode patterns, then we need to expand the index before we |
| 348 | * continue. A NULL pattern set indicates a full expansion to a |
| 349 | * full index. |
| 350 | */ |
| 351 | if (pl && !pl->use_cone_patterns) { |
| 352 | pl = NULL; |
| 353 | } else { |
| 354 | /* |
| 355 | * We might contract file entries into sparse-directory |
| 356 | * entries, and for that we will need the cache tree to |
| 357 | * be recomputed. |
| 358 | */ |
| 359 | cache_tree_free(&istate->cache_tree); |
| 360 | |
| 361 | /* |
| 362 | * If there is a problem creating the cache tree, then we |
| 363 | * need to expand to a full index since we cannot satisfy |
| 364 | * the current request as a sparse index. |
| 365 | */ |
| 366 | if (cache_tree_update(istate, 0)) |
| 367 | pl = NULL; |
| 368 | } |
| 369 | |
| 370 | if (!pl && give_advice_on_expansion) { |
| 371 | give_advice_on_expansion = 0; |
| 372 | advise_if_enabled(ADVICE_SPARSE_INDEX_EXPANDED, |
| 373 | _(ADVICE_MSG)); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * A NULL pattern set indicates we are expanding a full index, so |
| 378 | * we use a special region name that indicates the full expansion. |
| 379 | * This is used by test cases, but also helps to differentiate the |
| 380 | * two cases. |
| 381 | */ |
| 382 | tr_region = pl ? "expand_index" : "ensure_full_index"; |
| 383 | trace2_region_enter("index", tr_region, istate->repo); |
| 384 | |
| 385 | /* initialize basics of new index */ |
| 386 | full = xcalloc(1, sizeof(struct index_state)); |
| 387 | memcpy(full, istate, sizeof(struct index_state)); |
| 388 | |
| 389 | /* |
| 390 | * This slightly-misnamed 'full' index might still be sparse if we |
| 391 | * are only modifying the list of sparse directories. This hinges |
| 392 | * on whether we have a non-NULL pattern list. |
| 393 | */ |
| 394 | full->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED; |
| 395 | |
| 396 | /* then change the necessary things */ |
| 397 | full->cache_alloc = (3 * istate->cache_alloc) / 2; |
| 398 | full->cache_nr = 0; |
| 399 | ALLOC_ARRAY(full->cache, full->cache_alloc); |
| 400 | |
| 401 | ctx.write = full; |
| 402 | ctx.pl = pl; |
| 403 | |
| 404 | for (i = 0; i < istate->cache_nr; i++) { |
| 405 | struct cache_entry *ce = istate->cache[i]; |
| 406 | struct tree *tree; |
| 407 | struct pathspec ps; |
| 408 | int dtype; |
| 409 | |
| 410 | if (!S_ISSPARSEDIR(ce->ce_mode)) { |
| 411 | set_index_entry(full, full->cache_nr++, ce); |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | /* We now have a sparse directory entry. Should we expand? */ |
| 416 | if (pl && |
| 417 | path_matches_pattern_list(ce->name, ce->ce_namelen, |
| 418 | NULL, &dtype, |
| 419 | pl, istate) == NOT_MATCHED) { |
| 420 | set_index_entry(full, full->cache_nr++, ce); |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | if (!(ce->ce_flags & CE_SKIP_WORKTREE)) |
| 425 | warning(_("index entry is a directory, but not sparse (%08x)"), |
| 426 | ce->ce_flags); |
| 427 | |
| 428 | /* recursively walk into cd->name */ |
| 429 | tree = lookup_tree(istate->repo, &ce->oid); |
| 430 | |
| 431 | memset(&ps, 0, sizeof(ps)); |
| 432 | ps.recursive = 1; |
| 433 | ps.has_wildcard = 1; |
| 434 | ps.max_depth = -1; |
| 435 | |
| 436 | strbuf_setlen(&base, 0); |
| 437 | strbuf_add(&base, ce->name, strlen(ce->name)); |
| 438 | |
| 439 | read_tree_at(istate->repo, tree, &base, 0, &ps, |
| 440 | add_path_to_index, &ctx); |
| 441 | |
| 442 | /* free directory entries. full entries are re-used */ |
| 443 | discard_cache_entry(ce); |
| 444 | } |
| 445 | |
| 446 | /* Copy back into original index. */ |
| 447 | memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash)); |
| 448 | memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash)); |
| 449 | istate->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED; |
| 450 | free(istate->cache); |
| 451 | istate->cache = full->cache; |
| 452 | istate->cache_nr = full->cache_nr; |
| 453 | istate->cache_alloc = full->cache_alloc; |
| 454 | istate->fsmonitor_has_run_once = 0; |
| 455 | ewah_free(istate->fsmonitor_dirty); |
| 456 | istate->fsmonitor_dirty = NULL; |
| 457 | FREE_AND_NULL(istate->fsmonitor_last_update); |
| 458 | |
| 459 | strbuf_release(&base); |
| 460 | free(full); |
| 461 | |
| 462 | /* Clear and recompute the cache-tree */ |
| 463 | cache_tree_free(&istate->cache_tree); |
| 464 | cache_tree_update(istate, 0); |
| 465 | |
| 466 | trace2_region_leave("index", tr_region, istate->repo); |
| 467 | } |
| 468 | |
| 469 | void ensure_full_index(struct index_state *istate) |
| 470 | { |
| 471 | if (!istate) |
| 472 | BUG("ensure_full_index() must get an index!"); |
| 473 | expand_index(istate, NULL); |
| 474 | } |
| 475 | |
| 476 | void ensure_correct_sparsity(struct index_state *istate) |
| 477 | { |
| 478 | /* |
| 479 | * If the index can be sparse, make it sparse. Otherwise, |
| 480 | * ensure the index is full. |
| 481 | */ |
| 482 | if (is_sparse_index_allowed(istate, 0)) |
| 483 | convert_to_sparse(istate, 0); |
| 484 | else |
| 485 | ensure_full_index(istate); |
| 486 | } |
| 487 | |
| 488 | struct path_found_data { |
| 489 | /** |
| 490 | * The path stored in 'dir', if non-empty, corresponds to the most- |
| 491 | * recent path that we checked where: |
| 492 | * |
| 493 | * 1. The path should be a directory, according to the index. |
| 494 | * 2. The path does not exist. |
| 495 | * 3. The parent path _does_ exist. (This may be the root of the |
| 496 | * working directory.) |
| 497 | */ |
| 498 | struct strbuf dir; |
| 499 | size_t lstat_count; |
| 500 | }; |
| 501 | |
| 502 | #define PATH_FOUND_DATA_INIT { \ |
| 503 | .dir = STRBUF_INIT \ |
| 504 | } |
| 505 | |
| 506 | static void clear_path_found_data(struct path_found_data *data) |
| 507 | { |
| 508 | strbuf_release(&data->dir); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Return the length of the longest common substring that ends in a |
| 513 | * slash ('/') to indicate the longest common parent directory. Returns |
| 514 | * zero if no common directory exists. |
| 515 | */ |
| 516 | static size_t max_common_dir_prefix(const char *path1, const char *path2) |
| 517 | { |
| 518 | size_t common_prefix = 0; |
| 519 | for (size_t i = 0; path1[i] && path2[i]; i++) { |
| 520 | if (path1[i] != path2[i]) |
| 521 | break; |
| 522 | |
| 523 | /* |
| 524 | * If they agree at a directory separator, then add one |
| 525 | * to make sure it is included in the common prefix string. |
| 526 | */ |
| 527 | if (path1[i] == '/') |
| 528 | common_prefix = i + 1; |
| 529 | } |
| 530 | |
| 531 | return common_prefix; |
| 532 | } |
| 533 | |
| 534 | static int path_found(const char *path, struct path_found_data *data) |
| 535 | { |
| 536 | struct stat st; |
| 537 | size_t common_prefix; |
| 538 | |
| 539 | /* |
| 540 | * If data->dir is non-empty, then it contains a path that doesn't |
| 541 | * exist, including an ending slash ('/'). If it is a prefix of 'path', |
| 542 | * then we can return 0. |
| 543 | */ |
| 544 | if (data->dir.len && !memcmp(path, data->dir.buf, data->dir.len)) |
| 545 | return 0; |
| 546 | |
| 547 | /* |
| 548 | * Otherwise, we must check if the current path exists. If it does, then |
| 549 | * return 1. The cached directory will be skipped until we come across |
| 550 | * a missing path again. |
| 551 | */ |
| 552 | data->lstat_count++; |
| 553 | if (!lstat(path, &st)) |
| 554 | return 1; |
| 555 | |
| 556 | /* |
| 557 | * At this point, we know that 'path' doesn't exist, and we know that |
| 558 | * the parent directory of 'data->dir' does exist. Let's set 'data->dir' |
| 559 | * to be the top-most non-existing directory of 'path'. If the first |
| 560 | * parent of 'path' exists, then we will act as though 'path' |
| 561 | * corresponds to a directory (by adding a slash). |
| 562 | */ |
| 563 | common_prefix = max_common_dir_prefix(path, data->dir.buf); |
| 564 | |
| 565 | /* |
| 566 | * At this point, 'path' and 'data->dir' have a common existing parent |
| 567 | * directory given by path[0..common_prefix] (which could have length 0). |
| 568 | * We "grow" the data->dir buffer by checking for existing directories |
| 569 | * along 'path'. |
| 570 | */ |
| 571 | |
| 572 | strbuf_setlen(&data->dir, common_prefix); |
| 573 | while (1) { |
| 574 | /* Find the next directory in 'path'. */ |
| 575 | const char *rest = path + data->dir.len; |
| 576 | const char *next_slash = strchr(rest, '/'); |
| 577 | |
| 578 | /* |
| 579 | * If there are no more slashes, then 'path' doesn't contain a |
| 580 | * non-existent _parent_ directory. Set 'data->dir' to be equal |
| 581 | * to 'path' plus an additional slash, so it can be used for |
| 582 | * caching in the future. The filename of 'path' is considered |
| 583 | * a non-existent directory. |
| 584 | * |
| 585 | * Note: if "{path}/" exists as a directory, then it will never |
| 586 | * appear as a prefix of other callers to this method, assuming |
| 587 | * the context from the clear_skip_worktree... methods. If this |
| 588 | * method is reused, then this must be reconsidered. |
| 589 | */ |
| 590 | if (!next_slash) { |
| 591 | strbuf_addstr(&data->dir, rest); |
| 592 | strbuf_addch(&data->dir, '/'); |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Now that we have a slash, let's grow 'data->dir' to include |
| 598 | * this slash, then test if we should stop. |
| 599 | */ |
| 600 | strbuf_add(&data->dir, rest, next_slash - rest + 1); |
| 601 | |
| 602 | /* If the parent dir doesn't exist, then stop here. */ |
| 603 | data->lstat_count++; |
| 604 | if (lstat(data->dir.buf, &st)) |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * At this point, 'data->dir' is equal to 'path' plus a slash character, |
| 610 | * and the parent directory of 'path' definitely exists. Moreover, we |
| 611 | * know that 'path' doesn't exist, or we would have returned 1 earlier. |
| 612 | */ |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static int clear_skip_worktree_from_present_files_sparse(struct index_state *istate) |
| 617 | { |
| 618 | struct path_found_data data = PATH_FOUND_DATA_INIT; |
| 619 | |
| 620 | int path_count = 0; |
| 621 | int to_restart = 0; |
| 622 | |
| 623 | trace2_region_enter("index", "clear_skip_worktree_from_present_files_sparse", |
| 624 | istate->repo); |
| 625 | for (int i = 0; i < istate->cache_nr; i++) { |
| 626 | struct cache_entry *ce = istate->cache[i]; |
| 627 | |
| 628 | if (ce_skip_worktree(ce)) { |
| 629 | path_count++; |
| 630 | if (path_found(ce->name, &data)) { |
| 631 | if (S_ISSPARSEDIR(ce->ce_mode)) { |
| 632 | to_restart = 1; |
| 633 | break; |
| 634 | } |
| 635 | ce->ce_flags &= ~CE_SKIP_WORKTREE; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | trace2_data_intmax("index", istate->repo, |
| 641 | "sparse_path_count", path_count); |
| 642 | trace2_data_intmax("index", istate->repo, |
| 643 | "sparse_lstat_count", data.lstat_count); |
| 644 | trace2_region_leave("index", "clear_skip_worktree_from_present_files_sparse", |
| 645 | istate->repo); |
| 646 | clear_path_found_data(&data); |
| 647 | return to_restart; |
| 648 | } |
| 649 | |
| 650 | static void clear_skip_worktree_from_present_files_full(struct index_state *istate) |
| 651 | { |
| 652 | struct path_found_data data = PATH_FOUND_DATA_INIT; |
| 653 | |
| 654 | int path_count = 0; |
| 655 | |
| 656 | trace2_region_enter("index", "clear_skip_worktree_from_present_files_full", |
| 657 | istate->repo); |
| 658 | for (int i = 0; i < istate->cache_nr; i++) { |
| 659 | struct cache_entry *ce = istate->cache[i]; |
| 660 | |
| 661 | if (S_ISSPARSEDIR(ce->ce_mode)) |
| 662 | BUG("ensure-full-index did not fully flatten?"); |
| 663 | |
| 664 | if (ce_skip_worktree(ce)) { |
| 665 | path_count++; |
| 666 | if (path_found(ce->name, &data)) |
| 667 | ce->ce_flags &= ~CE_SKIP_WORKTREE; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | trace2_data_intmax("index", istate->repo, |
| 672 | "full_path_count", path_count); |
| 673 | trace2_data_intmax("index", istate->repo, |
| 674 | "full_lstat_count", data.lstat_count); |
| 675 | trace2_region_leave("index", "clear_skip_worktree_from_present_files_full", |
| 676 | istate->repo); |
| 677 | clear_path_found_data(&data); |
| 678 | } |
| 679 | |
| 680 | void clear_skip_worktree_from_present_files(struct index_state *istate) |
| 681 | { |
| 682 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 683 | |
| 684 | if (!cfg->apply_sparse_checkout || |
| 685 | cfg->sparse_expect_files_outside_of_patterns) |
| 686 | return; |
| 687 | |
| 688 | if (clear_skip_worktree_from_present_files_sparse(istate)) { |
| 689 | ensure_full_index(istate); |
| 690 | clear_skip_worktree_from_present_files_full(istate); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * This static global helps avoid infinite recursion between |
| 696 | * expand_to_path() and index_file_exists(). |
| 697 | */ |
| 698 | static int in_expand_to_path = 0; |
| 699 | |
| 700 | void expand_to_path(struct index_state *istate, |
| 701 | const char *path, size_t pathlen, int icase) |
| 702 | { |
| 703 | struct strbuf path_mutable = STRBUF_INIT; |
| 704 | size_t substr_len; |
| 705 | |
| 706 | /* prevent extra recursion */ |
| 707 | if (in_expand_to_path) |
| 708 | return; |
| 709 | |
| 710 | if (!istate->sparse_index) |
| 711 | return; |
| 712 | |
| 713 | in_expand_to_path = 1; |
| 714 | |
| 715 | /* |
| 716 | * We only need to actually expand a region if the |
| 717 | * following are both true: |
| 718 | * |
| 719 | * 1. 'path' is not already in the index. |
| 720 | * 2. Some parent directory of 'path' is a sparse directory. |
| 721 | */ |
| 722 | |
| 723 | if (index_file_exists(istate, path, pathlen, icase)) |
| 724 | goto cleanup; |
| 725 | |
| 726 | strbuf_add(&path_mutable, path, pathlen); |
| 727 | strbuf_addch(&path_mutable, '/'); |
| 728 | |
| 729 | /* Check the name hash for all parent directories */ |
| 730 | substr_len = 0; |
| 731 | while (substr_len < pathlen) { |
| 732 | char temp; |
| 733 | char *replace = strchr(path_mutable.buf + substr_len, '/'); |
| 734 | |
| 735 | if (!replace) |
| 736 | break; |
| 737 | |
| 738 | /* replace the character _after_ the slash */ |
| 739 | replace++; |
| 740 | temp = *replace; |
| 741 | *replace = '\0'; |
| 742 | substr_len = replace - path_mutable.buf; |
| 743 | if (index_file_exists(istate, path_mutable.buf, |
| 744 | substr_len, icase)) { |
| 745 | /* |
| 746 | * We found a parent directory in the name-hash |
| 747 | * hashtable, because only sparse directory entries |
| 748 | * have a trailing '/' character. Since "path" wasn't |
| 749 | * in the index, perhaps it exists within this |
| 750 | * sparse-directory. Expand accordingly. |
| 751 | */ |
| 752 | ensure_full_index(istate); |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | *replace = temp; |
| 757 | } |
| 758 | |
| 759 | cleanup: |
| 760 | strbuf_release(&path_mutable); |
| 761 | in_expand_to_path = 0; |
| 762 | } |