1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
7 #include "read-cache-ll.h"
8 #include "repository.h"
9 #include "sparse-index.h"
13 #include "cache-tree.h"
16 #include "fsmonitor-ll.h"
20 * This global is used by expand_index() to determine if we should give the
21 * advice for advice.sparseIndexExpanded when expanding a sparse index to a full
22 * one. However, this is sometimes done on purpose, such as in the sparse-checkout
23 * builtin, even when index.sparse=false. This may be disabled in
24 * convert_to_sparse() or by commands that know they will lead to a full
25 * expansion, but this message is not actionable.
27 int give_advice_on_expansion
= 1;
29 "The sparse index is expanding to a full index, a slow operation.\n" \
30 "Your working directory likely has contents that are outside of\n" \
31 "your sparse-checkout patterns. Use 'git sparse-checkout list' to\n" \
32 "see your sparse-checkout definition and compare it to your working\n" \
33 "directory contents. Running 'git clean' may assist in this cleanup."
35 struct modify_index_context
{
36 struct index_state
*write
;
37 struct pattern_list
*pl
;
40 static struct cache_entry
*construct_sparse_dir_entry(
41 struct index_state
*istate
,
42 const char *sparse_dir
,
43 struct cache_tree
*tree
)
45 struct cache_entry
*de
;
47 de
= make_cache_entry(istate
, S_IFDIR
, &tree
->oid
, sparse_dir
, 0, 0);
49 de
->ce_flags
|= CE_SKIP_WORKTREE
;
54 * Returns the number of entries "inserted" into the index.
56 static int convert_to_sparse_rec(struct index_state
*istate
,
59 const char *ct_path
, size_t ct_pathlen
,
60 struct cache_tree
*ct
)
62 int i
, can_convert
= 1;
63 int start_converted
= num_converted
;
64 struct strbuf child_path
= STRBUF_INIT
;
67 * Is the current path outside of the sparse cone?
68 * Then check if the region can be replaced by a sparse
69 * directory entry (everything is sparse and merged).
71 if (path_in_sparse_checkout(ct_path
, istate
))
74 for (i
= start
; can_convert
&& i
< end
; i
++) {
75 struct cache_entry
*ce
= istate
->cache
[i
];
78 S_ISGITLINK(ce
->ce_mode
) ||
79 !(ce
->ce_flags
& CE_SKIP_WORKTREE
))
84 struct cache_entry
*se
;
85 se
= construct_sparse_dir_entry(istate
, ct_path
, ct
);
87 istate
->cache
[num_converted
++] = se
;
91 for (i
= start
; i
< end
; ) {
92 int count
, span
, pos
= -1;
93 const char *base
, *slash
;
94 struct cache_entry
*ce
= istate
->cache
[i
];
97 * Detect if this is a normal entry outside of any subtree
100 base
= ce
->name
+ ct_pathlen
;
101 slash
= strchr(base
, '/');
104 pos
= cache_tree_subtree_pos(ct
, base
, slash
- base
);
107 istate
->cache
[num_converted
++] = ce
;
112 strbuf_setlen(&child_path
, 0);
113 strbuf_add(&child_path
, ce
->name
, slash
- ce
->name
+ 1);
115 span
= ct
->down
[pos
]->cache_tree
->entry_count
;
116 count
= convert_to_sparse_rec(istate
,
117 num_converted
, i
, i
+ span
,
118 child_path
.buf
, child_path
.len
,
119 ct
->down
[pos
]->cache_tree
);
120 num_converted
+= count
;
124 strbuf_release(&child_path
);
125 return num_converted
- start_converted
;
128 int set_sparse_index_config(struct repository
*repo
, int enable
)
130 int res
= repo_config_set_worktree_gently(repo
,
132 enable
? "true" : "false");
133 prepare_repo_settings(repo
);
134 repo
->settings
.sparse_index
= enable
;
138 static int index_has_unmerged_entries(struct index_state
*istate
)
141 for (i
= 0; i
< istate
->cache_nr
; i
++) {
142 if (ce_stage(istate
->cache
[i
]))
149 int is_sparse_index_allowed(struct index_state
*istate
, int flags
)
151 if (!core_apply_sparse_checkout
|| !core_sparse_checkout_cone
)
154 if (!(flags
& SPARSE_INDEX_MEMORY_ONLY
)) {
158 * The sparse index is not (yet) integrated with a split index.
160 if (istate
->split_index
|| git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
163 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
164 * index.sparse config variable to be on.
166 test_env
= git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
168 set_sparse_index_config(istate
->repo
, test_env
);
171 * Only convert to sparse if index.sparse is set.
173 prepare_repo_settings(istate
->repo
);
174 if (!istate
->repo
->settings
.sparse_index
)
178 if (init_sparse_checkout_patterns(istate
))
182 * We need cone-mode patterns to use sparse-index. If a user edits
183 * their sparse-checkout file manually, then we can detect during
184 * parsing that they are not actually using cone-mode patterns and
185 * hence we need to abort this conversion _without error_. Warnings
186 * already exist in the pattern parsing to inform the user of their
189 if (!istate
->sparse_checkout_patterns
->use_cone_patterns
)
195 int convert_to_sparse(struct index_state
*istate
, int flags
)
198 * If the index is already sparse, empty, or otherwise
199 * cannot be converted to sparse, do not convert.
201 if (istate
->sparse_index
== INDEX_COLLAPSED
|| !istate
->cache_nr
||
202 !is_sparse_index_allowed(istate
, flags
))
206 * If we are purposefully collapsing a full index, then don't give
207 * advice when it is expanded later.
209 give_advice_on_expansion
= 0;
212 * NEEDSWORK: If we have unmerged entries, then stay full.
213 * Unmerged entries prevent the cache-tree extension from working.
215 if (index_has_unmerged_entries(istate
))
218 if (!cache_tree_fully_valid(istate
->cache_tree
)) {
219 /* Clear and recompute the cache-tree */
220 cache_tree_free(&istate
->cache_tree
);
223 * Silently return if there is a problem with the cache tree update,
224 * which might just be due to a conflict state in some entry.
226 * This might create new tree objects, so be sure to use
227 * WRITE_TREE_MISSING_OK.
229 if (cache_tree_update(istate
, WRITE_TREE_MISSING_OK
))
233 remove_fsmonitor(istate
);
235 trace2_region_enter("index", "convert_to_sparse", istate
->repo
);
236 istate
->cache_nr
= convert_to_sparse_rec(istate
,
237 0, 0, istate
->cache_nr
,
238 "", 0, istate
->cache_tree
);
240 /* Clear and recompute the cache-tree */
241 cache_tree_free(&istate
->cache_tree
);
242 cache_tree_update(istate
, 0);
244 istate
->fsmonitor_has_run_once
= 0;
245 FREE_AND_NULL(istate
->fsmonitor_dirty
);
246 FREE_AND_NULL(istate
->fsmonitor_last_update
);
248 istate
->sparse_index
= INDEX_COLLAPSED
;
249 trace2_region_leave("index", "convert_to_sparse", istate
->repo
);
253 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
255 ALLOC_GROW(istate
->cache
, nr
+ 1, istate
->cache_alloc
);
257 istate
->cache
[nr
] = ce
;
258 add_name_hash(istate
, ce
);
261 static int add_path_to_index(const struct object_id
*oid
,
262 struct strbuf
*base
, const char *path
,
263 unsigned int mode
, void *context
)
265 struct modify_index_context
*ctx
= (struct modify_index_context
*)context
;
266 struct cache_entry
*ce
;
267 size_t len
= base
->len
;
271 size_t baselen
= base
->len
;
273 return READ_TREE_RECURSIVE
;
276 * Have we expanded to a point outside of the sparse-checkout?
278 * Artificially pad the path name with a slash "/" to
279 * indicate it as a directory, and add an arbitrary file
280 * name ("-") so we can consider base->buf as a file name
281 * to match against the cone-mode patterns.
283 * If we compared just "path", then we would expand more
284 * than we should. Since every file at root is always
285 * included, we would expand every directory at root at
286 * least one level deep instead of using sparse directory
289 strbuf_addstr(base
, path
);
290 strbuf_add(base
, "/-", 2);
292 if (path_matches_pattern_list(base
->buf
, base
->len
,
294 ctx
->pl
, ctx
->write
)) {
295 strbuf_setlen(base
, baselen
);
296 return READ_TREE_RECURSIVE
;
300 * The path "{base}{path}/" is a sparse directory. Create the correct
301 * name for inserting the entry into the index.
303 strbuf_setlen(base
, base
->len
- 1);
305 strbuf_addstr(base
, path
);
308 ce
= make_cache_entry(ctx
->write
, mode
, oid
, base
->buf
, 0, 0);
309 ce
->ce_flags
|= CE_SKIP_WORKTREE
| CE_EXTENDED
;
310 set_index_entry(ctx
->write
, ctx
->write
->cache_nr
++, ce
);
312 strbuf_setlen(base
, len
);
316 void expand_index(struct index_state
*istate
, struct pattern_list
*pl
)
319 struct index_state
*full
;
320 struct strbuf base
= STRBUF_INIT
;
321 const char *tr_region
;
322 struct modify_index_context ctx
;
325 * If the index is already full, then keep it full. We will convert
326 * it to a sparse index on write, if possible.
328 if (istate
->sparse_index
== INDEX_EXPANDED
)
332 * If our index is sparse, but our new pattern set does not use
333 * cone mode patterns, then we need to expand the index before we
334 * continue. A NULL pattern set indicates a full expansion to a
337 if (pl
&& !pl
->use_cone_patterns
) {
341 * We might contract file entries into sparse-directory
342 * entries, and for that we will need the cache tree to
345 cache_tree_free(&istate
->cache_tree
);
348 * If there is a problem creating the cache tree, then we
349 * need to expand to a full index since we cannot satisfy
350 * the current request as a sparse index.
352 if (cache_tree_update(istate
, 0))
356 if (!pl
&& give_advice_on_expansion
) {
357 give_advice_on_expansion
= 0;
358 advise_if_enabled(ADVICE_SPARSE_INDEX_EXPANDED
,
363 * A NULL pattern set indicates we are expanding a full index, so
364 * we use a special region name that indicates the full expansion.
365 * This is used by test cases, but also helps to differentiate the
368 tr_region
= pl
? "expand_index" : "ensure_full_index";
369 trace2_region_enter("index", tr_region
, istate
->repo
);
371 /* initialize basics of new index */
372 full
= xcalloc(1, sizeof(struct index_state
));
373 memcpy(full
, istate
, sizeof(struct index_state
));
376 * This slightly-misnamed 'full' index might still be sparse if we
377 * are only modifying the list of sparse directories. This hinges
378 * on whether we have a non-NULL pattern list.
380 full
->sparse_index
= pl
? INDEX_PARTIALLY_SPARSE
: INDEX_EXPANDED
;
382 /* then change the necessary things */
383 full
->cache_alloc
= (3 * istate
->cache_alloc
) / 2;
385 ALLOC_ARRAY(full
->cache
, full
->cache_alloc
);
390 for (i
= 0; i
< istate
->cache_nr
; i
++) {
391 struct cache_entry
*ce
= istate
->cache
[i
];
396 if (!S_ISSPARSEDIR(ce
->ce_mode
)) {
397 set_index_entry(full
, full
->cache_nr
++, ce
);
401 /* We now have a sparse directory entry. Should we expand? */
403 path_matches_pattern_list(ce
->name
, ce
->ce_namelen
,
405 pl
, istate
) == NOT_MATCHED
) {
406 set_index_entry(full
, full
->cache_nr
++, ce
);
410 if (!(ce
->ce_flags
& CE_SKIP_WORKTREE
))
411 warning(_("index entry is a directory, but not sparse (%08x)"),
414 /* recursively walk into cd->name */
415 tree
= lookup_tree(istate
->repo
, &ce
->oid
);
417 memset(&ps
, 0, sizeof(ps
));
422 strbuf_setlen(&base
, 0);
423 strbuf_add(&base
, ce
->name
, strlen(ce
->name
));
425 read_tree_at(istate
->repo
, tree
, &base
, 0, &ps
,
426 add_path_to_index
, &ctx
);
428 /* free directory entries. full entries are re-used */
429 discard_cache_entry(ce
);
432 /* Copy back into original index. */
433 memcpy(&istate
->name_hash
, &full
->name_hash
, sizeof(full
->name_hash
));
434 memcpy(&istate
->dir_hash
, &full
->dir_hash
, sizeof(full
->dir_hash
));
435 istate
->sparse_index
= pl
? INDEX_PARTIALLY_SPARSE
: INDEX_EXPANDED
;
437 istate
->cache
= full
->cache
;
438 istate
->cache_nr
= full
->cache_nr
;
439 istate
->cache_alloc
= full
->cache_alloc
;
440 istate
->fsmonitor_has_run_once
= 0;
441 FREE_AND_NULL(istate
->fsmonitor_dirty
);
442 FREE_AND_NULL(istate
->fsmonitor_last_update
);
444 strbuf_release(&base
);
447 /* Clear and recompute the cache-tree */
448 cache_tree_free(&istate
->cache_tree
);
449 cache_tree_update(istate
, 0);
451 trace2_region_leave("index", tr_region
, istate
->repo
);
454 void ensure_full_index(struct index_state
*istate
)
457 BUG("ensure_full_index() must get an index!");
458 expand_index(istate
, NULL
);
461 void ensure_correct_sparsity(struct index_state
*istate
)
464 * If the index can be sparse, make it sparse. Otherwise,
465 * ensure the index is full.
467 if (is_sparse_index_allowed(istate
, 0))
468 convert_to_sparse(istate
, 0);
470 ensure_full_index(istate
);
473 struct path_found_data
{
475 * The path stored in 'dir', if non-empty, corresponds to the most-
476 * recent path that we checked where:
478 * 1. The path should be a directory, according to the index.
479 * 2. The path does not exist.
480 * 3. The parent path _does_ exist. (This may be the root of the
481 * working directory.)
487 #define PATH_FOUND_DATA_INIT { \
491 static void clear_path_found_data(struct path_found_data
*data
)
493 strbuf_release(&data
->dir
);
497 * Return the length of the longest common substring that ends in a
498 * slash ('/') to indicate the longest common parent directory. Returns
499 * zero if no common directory exists.
501 static size_t max_common_dir_prefix(const char *path1
, const char *path2
)
503 size_t common_prefix
= 0;
504 for (size_t i
= 0; path1
[i
] && path2
[i
]; i
++) {
505 if (path1
[i
] != path2
[i
])
509 * If they agree at a directory separator, then add one
510 * to make sure it is included in the common prefix string.
513 common_prefix
= i
+ 1;
516 return common_prefix
;
519 static int path_found(const char *path
, struct path_found_data
*data
)
522 size_t common_prefix
;
525 * If data->dir is non-empty, then it contains a path that doesn't
526 * exist, including an ending slash ('/'). If it is a prefix of 'path',
527 * then we can return 0.
529 if (data
->dir
.len
&& !memcmp(path
, data
->dir
.buf
, data
->dir
.len
))
533 * Otherwise, we must check if the current path exists. If it does, then
534 * return 1. The cached directory will be skipped until we come across
535 * a missing path again.
538 if (!lstat(path
, &st
))
542 * At this point, we know that 'path' doesn't exist, and we know that
543 * the parent directory of 'data->dir' does exist. Let's set 'data->dir'
544 * to be the top-most non-existing directory of 'path'. If the first
545 * parent of 'path' exists, then we will act as though 'path'
546 * corresponds to a directory (by adding a slash).
548 common_prefix
= max_common_dir_prefix(path
, data
->dir
.buf
);
551 * At this point, 'path' and 'data->dir' have a common existing parent
552 * directory given by path[0..common_prefix] (which could have length 0).
553 * We "grow" the data->dir buffer by checking for existing directories
557 strbuf_setlen(&data
->dir
, common_prefix
);
559 /* Find the next directory in 'path'. */
560 const char *rest
= path
+ data
->dir
.len
;
561 const char *next_slash
= strchr(rest
, '/');
564 * If there are no more slashes, then 'path' doesn't contain a
565 * non-existent _parent_ directory. Set 'data->dir' to be equal
566 * to 'path' plus an additional slash, so it can be used for
567 * caching in the future. The filename of 'path' is considered
568 * a non-existent directory.
570 * Note: if "{path}/" exists as a directory, then it will never
571 * appear as a prefix of other callers to this method, assuming
572 * the context from the clear_skip_worktree... methods. If this
573 * method is reused, then this must be reconsidered.
576 strbuf_addstr(&data
->dir
, rest
);
577 strbuf_addch(&data
->dir
, '/');
582 * Now that we have a slash, let's grow 'data->dir' to include
583 * this slash, then test if we should stop.
585 strbuf_add(&data
->dir
, rest
, next_slash
- rest
+ 1);
587 /* If the parent dir doesn't exist, then stop here. */
589 if (lstat(data
->dir
.buf
, &st
))
594 * At this point, 'data->dir' is equal to 'path' plus a slash character,
595 * and the parent directory of 'path' definitely exists. Moreover, we
596 * know that 'path' doesn't exist, or we would have returned 1 earlier.
601 static int clear_skip_worktree_from_present_files_sparse(struct index_state
*istate
)
603 struct path_found_data data
= PATH_FOUND_DATA_INIT
;
608 trace2_region_enter("index", "clear_skip_worktree_from_present_files_sparse",
610 for (int i
= 0; i
< istate
->cache_nr
; i
++) {
611 struct cache_entry
*ce
= istate
->cache
[i
];
613 if (ce_skip_worktree(ce
)) {
615 if (path_found(ce
->name
, &data
)) {
616 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
620 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
625 trace2_data_intmax("index", istate
->repo
,
626 "sparse_path_count", path_count
);
627 trace2_data_intmax("index", istate
->repo
,
628 "sparse_lstat_count", data
.lstat_count
);
629 trace2_region_leave("index", "clear_skip_worktree_from_present_files_sparse",
631 clear_path_found_data(&data
);
635 static void clear_skip_worktree_from_present_files_full(struct index_state
*istate
)
637 struct path_found_data data
= PATH_FOUND_DATA_INIT
;
641 trace2_region_enter("index", "clear_skip_worktree_from_present_files_full",
643 for (int i
= 0; i
< istate
->cache_nr
; i
++) {
644 struct cache_entry
*ce
= istate
->cache
[i
];
646 if (S_ISSPARSEDIR(ce
->ce_mode
))
647 BUG("ensure-full-index did not fully flatten?");
649 if (ce_skip_worktree(ce
)) {
651 if (path_found(ce
->name
, &data
))
652 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
656 trace2_data_intmax("index", istate
->repo
,
657 "full_path_count", path_count
);
658 trace2_data_intmax("index", istate
->repo
,
659 "full_lstat_count", data
.lstat_count
);
660 trace2_region_leave("index", "clear_skip_worktree_from_present_files_full",
662 clear_path_found_data(&data
);
665 void clear_skip_worktree_from_present_files(struct index_state
*istate
)
667 if (!core_apply_sparse_checkout
||
668 sparse_expect_files_outside_of_patterns
)
671 if (clear_skip_worktree_from_present_files_sparse(istate
)) {
672 ensure_full_index(istate
);
673 clear_skip_worktree_from_present_files_full(istate
);
678 * This static global helps avoid infinite recursion between
679 * expand_to_path() and index_file_exists().
681 static int in_expand_to_path
= 0;
683 void expand_to_path(struct index_state
*istate
,
684 const char *path
, size_t pathlen
, int icase
)
686 struct strbuf path_mutable
= STRBUF_INIT
;
689 /* prevent extra recursion */
690 if (in_expand_to_path
)
693 if (!istate
->sparse_index
)
696 in_expand_to_path
= 1;
699 * We only need to actually expand a region if the
700 * following are both true:
702 * 1. 'path' is not already in the index.
703 * 2. Some parent directory of 'path' is a sparse directory.
706 if (index_file_exists(istate
, path
, pathlen
, icase
))
709 strbuf_add(&path_mutable
, path
, pathlen
);
710 strbuf_addch(&path_mutable
, '/');
712 /* Check the name hash for all parent directories */
714 while (substr_len
< pathlen
) {
716 char *replace
= strchr(path_mutable
.buf
+ substr_len
, '/');
721 /* replace the character _after_ the slash */
725 substr_len
= replace
- path_mutable
.buf
;
726 if (index_file_exists(istate
, path_mutable
.buf
,
727 substr_len
, icase
)) {
729 * We found a parent directory in the name-hash
730 * hashtable, because only sparse directory entries
731 * have a trailing '/' character. Since "path" wasn't
732 * in the index, perhaps it exists within this
733 * sparse-directory. Expand accordingly.
735 ensure_full_index(istate
);
743 strbuf_release(&path_mutable
);
744 in_expand_to_path
= 0;