1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "environment.h"
9 #include "read-cache-ll.h"
10 #include "repository.h"
11 #include "sparse-index.h"
15 #include "cache-tree.h"
18 #include "fsmonitor-ll.h"
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.
29 int give_advice_on_expansion
= 1;
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. Running 'git clean' may assist in this cleanup."
37 struct modify_index_context
{
38 struct index_state
*write
;
39 struct pattern_list
*pl
;
42 static struct cache_entry
*construct_sparse_dir_entry(
43 struct index_state
*istate
,
44 const char *sparse_dir
,
45 struct cache_tree
*tree
)
47 struct cache_entry
*de
;
49 de
= make_cache_entry(istate
, S_IFDIR
, &tree
->oid
, sparse_dir
, 0, 0);
51 de
->ce_flags
|= CE_SKIP_WORKTREE
;
56 * Returns the number of entries "inserted" into the index.
58 static int convert_to_sparse_rec(struct index_state
*istate
,
61 const char *ct_path
, size_t ct_pathlen
,
62 struct cache_tree
*ct
)
64 int i
, can_convert
= 1;
65 int start_converted
= num_converted
;
66 struct strbuf child_path
= STRBUF_INIT
;
69 * Is the current path outside of the sparse cone?
70 * Then check if the region can be replaced by a sparse
71 * directory entry (everything is sparse and merged).
73 if (path_in_sparse_checkout(ct_path
, istate
))
76 for (i
= start
; can_convert
&& i
< end
; i
++) {
77 struct cache_entry
*ce
= istate
->cache
[i
];
80 S_ISGITLINK(ce
->ce_mode
) ||
81 !(ce
->ce_flags
& CE_SKIP_WORKTREE
))
86 struct cache_entry
*se
;
87 se
= construct_sparse_dir_entry(istate
, ct_path
, ct
);
89 istate
->cache
[num_converted
++] = se
;
93 for (i
= start
; i
< end
; ) {
94 int count
, span
, pos
= -1;
95 const char *base
, *slash
;
96 struct cache_entry
*ce
= istate
->cache
[i
];
99 * Detect if this is a normal entry outside of any subtree
102 base
= ce
->name
+ ct_pathlen
;
103 slash
= strchr(base
, '/');
106 pos
= cache_tree_subtree_pos(ct
, base
, slash
- base
);
109 istate
->cache
[num_converted
++] = ce
;
114 strbuf_setlen(&child_path
, 0);
115 strbuf_add(&child_path
, ce
->name
, slash
- ce
->name
+ 1);
117 span
= ct
->down
[pos
]->cache_tree
->entry_count
;
118 count
= convert_to_sparse_rec(istate
,
119 num_converted
, i
, i
+ span
,
120 child_path
.buf
, child_path
.len
,
121 ct
->down
[pos
]->cache_tree
);
122 num_converted
+= count
;
126 strbuf_release(&child_path
);
127 return num_converted
- start_converted
;
130 int set_sparse_index_config(struct repository
*repo
, int enable
)
132 int res
= repo_config_set_worktree_gently(repo
,
134 enable
? "true" : "false");
135 prepare_repo_settings(repo
);
136 repo
->settings
.sparse_index
= enable
;
140 static int index_has_unmerged_entries(struct index_state
*istate
)
143 for (i
= 0; i
< istate
->cache_nr
; i
++) {
144 if (ce_stage(istate
->cache
[i
]))
151 int is_sparse_index_allowed(struct index_state
*istate
, int flags
)
153 if (!core_apply_sparse_checkout
|| !core_sparse_checkout_cone
)
156 if (!(flags
& SPARSE_INDEX_MEMORY_ONLY
)) {
160 * The sparse index is not (yet) integrated with a split index.
162 if (istate
->split_index
|| git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
165 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
166 * index.sparse config variable to be on.
168 test_env
= git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
170 set_sparse_index_config(istate
->repo
, test_env
);
173 * Only convert to sparse if index.sparse is set.
175 prepare_repo_settings(istate
->repo
);
176 if (!istate
->repo
->settings
.sparse_index
)
180 if (init_sparse_checkout_patterns(istate
))
184 * We need cone-mode patterns to use sparse-index. If a user edits
185 * their sparse-checkout file manually, then we can detect during
186 * parsing that they are not actually using cone-mode patterns and
187 * hence we need to abort this conversion _without error_. Warnings
188 * already exist in the pattern parsing to inform the user of their
191 if (!istate
->sparse_checkout_patterns
->use_cone_patterns
)
197 int convert_to_sparse(struct index_state
*istate
, int flags
)
200 * If the index is already sparse, empty, or otherwise
201 * cannot be converted to sparse, do not convert.
203 if (istate
->sparse_index
== INDEX_COLLAPSED
|| !istate
->cache_nr
||
204 !is_sparse_index_allowed(istate
, flags
))
208 * If we are purposefully collapsing a full index, then don't give
209 * advice when it is expanded later.
211 give_advice_on_expansion
= 0;
214 * NEEDSWORK: If we have unmerged entries, then stay full.
215 * Unmerged entries prevent the cache-tree extension from working.
217 if (index_has_unmerged_entries(istate
))
220 if (!cache_tree_fully_valid(istate
->cache_tree
)) {
221 /* Clear and recompute the cache-tree */
222 cache_tree_free(&istate
->cache_tree
);
225 * Silently return if there is a problem with the cache tree update,
226 * which might just be due to a conflict state in some entry.
228 * This might create new tree objects, so be sure to use
229 * WRITE_TREE_MISSING_OK.
231 if (cache_tree_update(istate
, WRITE_TREE_MISSING_OK
))
235 remove_fsmonitor(istate
);
237 trace2_region_enter("index", "convert_to_sparse", istate
->repo
);
238 istate
->cache_nr
= convert_to_sparse_rec(istate
,
239 0, 0, istate
->cache_nr
,
240 "", 0, istate
->cache_tree
);
242 /* Clear and recompute the cache-tree */
243 cache_tree_free(&istate
->cache_tree
);
244 cache_tree_update(istate
, 0);
246 istate
->fsmonitor_has_run_once
= 0;
247 ewah_free(istate
->fsmonitor_dirty
);
248 istate
->fsmonitor_dirty
= NULL
;
249 FREE_AND_NULL(istate
->fsmonitor_last_update
);
251 istate
->sparse_index
= INDEX_COLLAPSED
;
252 trace2_region_leave("index", "convert_to_sparse", istate
->repo
);
256 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
258 ALLOC_GROW(istate
->cache
, nr
+ 1, istate
->cache_alloc
);
260 istate
->cache
[nr
] = ce
;
261 add_name_hash(istate
, ce
);
264 static int add_path_to_index(const struct object_id
*oid
,
265 struct strbuf
*base
, const char *path
,
266 unsigned int mode
, void *context
)
268 struct modify_index_context
*ctx
= (struct modify_index_context
*)context
;
269 struct cache_entry
*ce
;
270 size_t len
= base
->len
;
274 size_t baselen
= base
->len
;
276 return READ_TREE_RECURSIVE
;
279 * Have we expanded to a point outside of the sparse-checkout?
281 * Artificially pad the path name with a slash "/" to
282 * indicate it as a directory, and add an arbitrary file
283 * name ("-") so we can consider base->buf as a file name
284 * to match against the cone-mode patterns.
286 * If we compared just "path", then we would expand more
287 * than we should. Since every file at root is always
288 * included, we would expand every directory at root at
289 * least one level deep instead of using sparse directory
292 strbuf_addstr(base
, path
);
293 strbuf_add(base
, "/-", 2);
295 if (path_matches_pattern_list(base
->buf
, base
->len
,
297 ctx
->pl
, ctx
->write
)) {
298 strbuf_setlen(base
, baselen
);
299 return READ_TREE_RECURSIVE
;
303 * The path "{base}{path}/" is a sparse directory. Create the correct
304 * name for inserting the entry into the index.
306 strbuf_setlen(base
, base
->len
- 1);
308 strbuf_addstr(base
, path
);
311 ce
= make_cache_entry(ctx
->write
, mode
, oid
, base
->buf
, 0, 0);
312 ce
->ce_flags
|= CE_SKIP_WORKTREE
| CE_EXTENDED
;
313 set_index_entry(ctx
->write
, ctx
->write
->cache_nr
++, ce
);
315 strbuf_setlen(base
, len
);
319 void expand_index(struct index_state
*istate
, struct pattern_list
*pl
)
322 struct index_state
*full
;
323 struct strbuf base
= STRBUF_INIT
;
324 const char *tr_region
;
325 struct modify_index_context ctx
;
328 * If the index is already full, then keep it full. We will convert
329 * it to a sparse index on write, if possible.
331 if (istate
->sparse_index
== INDEX_EXPANDED
)
335 * If our index is sparse, but our new pattern set does not use
336 * cone mode patterns, then we need to expand the index before we
337 * continue. A NULL pattern set indicates a full expansion to a
340 if (pl
&& !pl
->use_cone_patterns
) {
344 * We might contract file entries into sparse-directory
345 * entries, and for that we will need the cache tree to
348 cache_tree_free(&istate
->cache_tree
);
351 * If there is a problem creating the cache tree, then we
352 * need to expand to a full index since we cannot satisfy
353 * the current request as a sparse index.
355 if (cache_tree_update(istate
, 0))
359 if (!pl
&& give_advice_on_expansion
) {
360 give_advice_on_expansion
= 0;
361 advise_if_enabled(ADVICE_SPARSE_INDEX_EXPANDED
,
366 * A NULL pattern set indicates we are expanding a full index, so
367 * we use a special region name that indicates the full expansion.
368 * This is used by test cases, but also helps to differentiate the
371 tr_region
= pl
? "expand_index" : "ensure_full_index";
372 trace2_region_enter("index", tr_region
, istate
->repo
);
374 /* initialize basics of new index */
375 full
= xcalloc(1, sizeof(struct index_state
));
376 memcpy(full
, istate
, sizeof(struct index_state
));
379 * This slightly-misnamed 'full' index might still be sparse if we
380 * are only modifying the list of sparse directories. This hinges
381 * on whether we have a non-NULL pattern list.
383 full
->sparse_index
= pl
? INDEX_PARTIALLY_SPARSE
: INDEX_EXPANDED
;
385 /* then change the necessary things */
386 full
->cache_alloc
= (3 * istate
->cache_alloc
) / 2;
388 ALLOC_ARRAY(full
->cache
, full
->cache_alloc
);
393 for (i
= 0; i
< istate
->cache_nr
; i
++) {
394 struct cache_entry
*ce
= istate
->cache
[i
];
399 if (!S_ISSPARSEDIR(ce
->ce_mode
)) {
400 set_index_entry(full
, full
->cache_nr
++, ce
);
404 /* We now have a sparse directory entry. Should we expand? */
406 path_matches_pattern_list(ce
->name
, ce
->ce_namelen
,
408 pl
, istate
) == NOT_MATCHED
) {
409 set_index_entry(full
, full
->cache_nr
++, ce
);
413 if (!(ce
->ce_flags
& CE_SKIP_WORKTREE
))
414 warning(_("index entry is a directory, but not sparse (%08x)"),
417 /* recursively walk into cd->name */
418 tree
= lookup_tree(istate
->repo
, &ce
->oid
);
420 memset(&ps
, 0, sizeof(ps
));
425 strbuf_setlen(&base
, 0);
426 strbuf_add(&base
, ce
->name
, strlen(ce
->name
));
428 read_tree_at(istate
->repo
, tree
, &base
, 0, &ps
,
429 add_path_to_index
, &ctx
);
431 /* free directory entries. full entries are re-used */
432 discard_cache_entry(ce
);
435 /* Copy back into original index. */
436 memcpy(&istate
->name_hash
, &full
->name_hash
, sizeof(full
->name_hash
));
437 memcpy(&istate
->dir_hash
, &full
->dir_hash
, sizeof(full
->dir_hash
));
438 istate
->sparse_index
= pl
? INDEX_PARTIALLY_SPARSE
: INDEX_EXPANDED
;
440 istate
->cache
= full
->cache
;
441 istate
->cache_nr
= full
->cache_nr
;
442 istate
->cache_alloc
= full
->cache_alloc
;
443 istate
->fsmonitor_has_run_once
= 0;
444 ewah_free(istate
->fsmonitor_dirty
);
445 istate
->fsmonitor_dirty
= NULL
;
446 FREE_AND_NULL(istate
->fsmonitor_last_update
);
448 strbuf_release(&base
);
451 /* Clear and recompute the cache-tree */
452 cache_tree_free(&istate
->cache_tree
);
453 cache_tree_update(istate
, 0);
455 trace2_region_leave("index", tr_region
, istate
->repo
);
458 void ensure_full_index(struct index_state
*istate
)
461 BUG("ensure_full_index() must get an index!");
462 expand_index(istate
, NULL
);
465 void ensure_correct_sparsity(struct index_state
*istate
)
468 * If the index can be sparse, make it sparse. Otherwise,
469 * ensure the index is full.
471 if (is_sparse_index_allowed(istate
, 0))
472 convert_to_sparse(istate
, 0);
474 ensure_full_index(istate
);
477 struct path_found_data
{
479 * The path stored in 'dir', if non-empty, corresponds to the most-
480 * recent path that we checked where:
482 * 1. The path should be a directory, according to the index.
483 * 2. The path does not exist.
484 * 3. The parent path _does_ exist. (This may be the root of the
485 * working directory.)
491 #define PATH_FOUND_DATA_INIT { \
495 static void clear_path_found_data(struct path_found_data
*data
)
497 strbuf_release(&data
->dir
);
501 * Return the length of the longest common substring that ends in a
502 * slash ('/') to indicate the longest common parent directory. Returns
503 * zero if no common directory exists.
505 static size_t max_common_dir_prefix(const char *path1
, const char *path2
)
507 size_t common_prefix
= 0;
508 for (size_t i
= 0; path1
[i
] && path2
[i
]; i
++) {
509 if (path1
[i
] != path2
[i
])
513 * If they agree at a directory separator, then add one
514 * to make sure it is included in the common prefix string.
517 common_prefix
= i
+ 1;
520 return common_prefix
;
523 static int path_found(const char *path
, struct path_found_data
*data
)
526 size_t common_prefix
;
529 * If data->dir is non-empty, then it contains a path that doesn't
530 * exist, including an ending slash ('/'). If it is a prefix of 'path',
531 * then we can return 0.
533 if (data
->dir
.len
&& !memcmp(path
, data
->dir
.buf
, data
->dir
.len
))
537 * Otherwise, we must check if the current path exists. If it does, then
538 * return 1. The cached directory will be skipped until we come across
539 * a missing path again.
542 if (!lstat(path
, &st
))
546 * At this point, we know that 'path' doesn't exist, and we know that
547 * the parent directory of 'data->dir' does exist. Let's set 'data->dir'
548 * to be the top-most non-existing directory of 'path'. If the first
549 * parent of 'path' exists, then we will act as though 'path'
550 * corresponds to a directory (by adding a slash).
552 common_prefix
= max_common_dir_prefix(path
, data
->dir
.buf
);
555 * At this point, 'path' and 'data->dir' have a common existing parent
556 * directory given by path[0..common_prefix] (which could have length 0).
557 * We "grow" the data->dir buffer by checking for existing directories
561 strbuf_setlen(&data
->dir
, common_prefix
);
563 /* Find the next directory in 'path'. */
564 const char *rest
= path
+ data
->dir
.len
;
565 const char *next_slash
= strchr(rest
, '/');
568 * If there are no more slashes, then 'path' doesn't contain a
569 * non-existent _parent_ directory. Set 'data->dir' to be equal
570 * to 'path' plus an additional slash, so it can be used for
571 * caching in the future. The filename of 'path' is considered
572 * a non-existent directory.
574 * Note: if "{path}/" exists as a directory, then it will never
575 * appear as a prefix of other callers to this method, assuming
576 * the context from the clear_skip_worktree... methods. If this
577 * method is reused, then this must be reconsidered.
580 strbuf_addstr(&data
->dir
, rest
);
581 strbuf_addch(&data
->dir
, '/');
586 * Now that we have a slash, let's grow 'data->dir' to include
587 * this slash, then test if we should stop.
589 strbuf_add(&data
->dir
, rest
, next_slash
- rest
+ 1);
591 /* If the parent dir doesn't exist, then stop here. */
593 if (lstat(data
->dir
.buf
, &st
))
598 * At this point, 'data->dir' is equal to 'path' plus a slash character,
599 * and the parent directory of 'path' definitely exists. Moreover, we
600 * know that 'path' doesn't exist, or we would have returned 1 earlier.
605 static int clear_skip_worktree_from_present_files_sparse(struct index_state
*istate
)
607 struct path_found_data data
= PATH_FOUND_DATA_INIT
;
612 trace2_region_enter("index", "clear_skip_worktree_from_present_files_sparse",
614 for (int i
= 0; i
< istate
->cache_nr
; i
++) {
615 struct cache_entry
*ce
= istate
->cache
[i
];
617 if (ce_skip_worktree(ce
)) {
619 if (path_found(ce
->name
, &data
)) {
620 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
624 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
629 trace2_data_intmax("index", istate
->repo
,
630 "sparse_path_count", path_count
);
631 trace2_data_intmax("index", istate
->repo
,
632 "sparse_lstat_count", data
.lstat_count
);
633 trace2_region_leave("index", "clear_skip_worktree_from_present_files_sparse",
635 clear_path_found_data(&data
);
639 static void clear_skip_worktree_from_present_files_full(struct index_state
*istate
)
641 struct path_found_data data
= PATH_FOUND_DATA_INIT
;
645 trace2_region_enter("index", "clear_skip_worktree_from_present_files_full",
647 for (int i
= 0; i
< istate
->cache_nr
; i
++) {
648 struct cache_entry
*ce
= istate
->cache
[i
];
650 if (S_ISSPARSEDIR(ce
->ce_mode
))
651 BUG("ensure-full-index did not fully flatten?");
653 if (ce_skip_worktree(ce
)) {
655 if (path_found(ce
->name
, &data
))
656 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
660 trace2_data_intmax("index", istate
->repo
,
661 "full_path_count", path_count
);
662 trace2_data_intmax("index", istate
->repo
,
663 "full_lstat_count", data
.lstat_count
);
664 trace2_region_leave("index", "clear_skip_worktree_from_present_files_full",
666 clear_path_found_data(&data
);
669 void clear_skip_worktree_from_present_files(struct index_state
*istate
)
671 if (!core_apply_sparse_checkout
||
672 sparse_expect_files_outside_of_patterns
)
675 if (clear_skip_worktree_from_present_files_sparse(istate
)) {
676 ensure_full_index(istate
);
677 clear_skip_worktree_from_present_files_full(istate
);
682 * This static global helps avoid infinite recursion between
683 * expand_to_path() and index_file_exists().
685 static int in_expand_to_path
= 0;
687 void expand_to_path(struct index_state
*istate
,
688 const char *path
, size_t pathlen
, int icase
)
690 struct strbuf path_mutable
= STRBUF_INIT
;
693 /* prevent extra recursion */
694 if (in_expand_to_path
)
697 if (!istate
->sparse_index
)
700 in_expand_to_path
= 1;
703 * We only need to actually expand a region if the
704 * following are both true:
706 * 1. 'path' is not already in the index.
707 * 2. Some parent directory of 'path' is a sparse directory.
710 if (index_file_exists(istate
, path
, pathlen
, icase
))
713 strbuf_add(&path_mutable
, path
, pathlen
);
714 strbuf_addch(&path_mutable
, '/');
716 /* Check the name hash for all parent directories */
718 while (substr_len
< pathlen
) {
720 char *replace
= strchr(path_mutable
.buf
+ substr_len
, '/');
725 /* replace the character _after_ the slash */
729 substr_len
= replace
- path_mutable
.buf
;
730 if (index_file_exists(istate
, path_mutable
.buf
,
731 substr_len
, icase
)) {
733 * We found a parent directory in the name-hash
734 * hashtable, because only sparse directory entries
735 * have a trailing '/' character. Since "path" wasn't
736 * in the index, perhaps it exists within this
737 * sparse-directory. Expand accordingly.
739 ensure_full_index(istate
);
747 strbuf_release(&path_mutable
);
748 in_expand_to_path
= 0;