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().
26 static int give_advice_on_expansion
= 1;
28 "The sparse index is expanding to a full index, a slow operation.\n" \
29 "Your working directory likely has contents that are outside of\n" \
30 "your sparse-checkout patterns. Use 'git sparse-checkout list' to\n" \
31 "see your sparse-checkout definition and compare it to your working\n" \
32 "directory contents. Running 'git clean' may assist in this cleanup."
34 struct modify_index_context
{
35 struct index_state
*write
;
36 struct pattern_list
*pl
;
39 static struct cache_entry
*construct_sparse_dir_entry(
40 struct index_state
*istate
,
41 const char *sparse_dir
,
42 struct cache_tree
*tree
)
44 struct cache_entry
*de
;
46 de
= make_cache_entry(istate
, S_IFDIR
, &tree
->oid
, sparse_dir
, 0, 0);
48 de
->ce_flags
|= CE_SKIP_WORKTREE
;
53 * Returns the number of entries "inserted" into the index.
55 static int convert_to_sparse_rec(struct index_state
*istate
,
58 const char *ct_path
, size_t ct_pathlen
,
59 struct cache_tree
*ct
)
61 int i
, can_convert
= 1;
62 int start_converted
= num_converted
;
63 struct strbuf child_path
= STRBUF_INIT
;
66 * Is the current path outside of the sparse cone?
67 * Then check if the region can be replaced by a sparse
68 * directory entry (everything is sparse and merged).
70 if (path_in_sparse_checkout(ct_path
, istate
))
73 for (i
= start
; can_convert
&& i
< end
; i
++) {
74 struct cache_entry
*ce
= istate
->cache
[i
];
77 S_ISGITLINK(ce
->ce_mode
) ||
78 !(ce
->ce_flags
& CE_SKIP_WORKTREE
))
83 struct cache_entry
*se
;
84 se
= construct_sparse_dir_entry(istate
, ct_path
, ct
);
86 istate
->cache
[num_converted
++] = se
;
90 for (i
= start
; i
< end
; ) {
91 int count
, span
, pos
= -1;
92 const char *base
, *slash
;
93 struct cache_entry
*ce
= istate
->cache
[i
];
96 * Detect if this is a normal entry outside of any subtree
99 base
= ce
->name
+ ct_pathlen
;
100 slash
= strchr(base
, '/');
103 pos
= cache_tree_subtree_pos(ct
, base
, slash
- base
);
106 istate
->cache
[num_converted
++] = ce
;
111 strbuf_setlen(&child_path
, 0);
112 strbuf_add(&child_path
, ce
->name
, slash
- ce
->name
+ 1);
114 span
= ct
->down
[pos
]->cache_tree
->entry_count
;
115 count
= convert_to_sparse_rec(istate
,
116 num_converted
, i
, i
+ span
,
117 child_path
.buf
, child_path
.len
,
118 ct
->down
[pos
]->cache_tree
);
119 num_converted
+= count
;
123 strbuf_release(&child_path
);
124 return num_converted
- start_converted
;
127 int set_sparse_index_config(struct repository
*repo
, int enable
)
129 int res
= repo_config_set_worktree_gently(repo
,
131 enable
? "true" : "false");
132 prepare_repo_settings(repo
);
133 repo
->settings
.sparse_index
= enable
;
137 static int index_has_unmerged_entries(struct index_state
*istate
)
140 for (i
= 0; i
< istate
->cache_nr
; i
++) {
141 if (ce_stage(istate
->cache
[i
]))
148 int is_sparse_index_allowed(struct index_state
*istate
, int flags
)
150 if (!core_apply_sparse_checkout
|| !core_sparse_checkout_cone
)
153 if (!(flags
& SPARSE_INDEX_MEMORY_ONLY
)) {
157 * The sparse index is not (yet) integrated with a split index.
159 if (istate
->split_index
|| git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
162 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
163 * index.sparse config variable to be on.
165 test_env
= git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
167 set_sparse_index_config(istate
->repo
, test_env
);
170 * Only convert to sparse if index.sparse is set.
172 prepare_repo_settings(istate
->repo
);
173 if (!istate
->repo
->settings
.sparse_index
)
177 if (init_sparse_checkout_patterns(istate
))
181 * We need cone-mode patterns to use sparse-index. If a user edits
182 * their sparse-checkout file manually, then we can detect during
183 * parsing that they are not actually using cone-mode patterns and
184 * hence we need to abort this conversion _without error_. Warnings
185 * already exist in the pattern parsing to inform the user of their
188 if (!istate
->sparse_checkout_patterns
->use_cone_patterns
)
194 int convert_to_sparse(struct index_state
*istate
, int flags
)
197 * If the index is already sparse, empty, or otherwise
198 * cannot be converted to sparse, do not convert.
200 if (istate
->sparse_index
== INDEX_COLLAPSED
|| !istate
->cache_nr
||
201 !is_sparse_index_allowed(istate
, flags
))
205 * If we are purposefully collapsing a full index, then don't give
206 * advice when it is expanded later.
208 give_advice_on_expansion
= 0;
211 * NEEDSWORK: If we have unmerged entries, then stay full.
212 * Unmerged entries prevent the cache-tree extension from working.
214 if (index_has_unmerged_entries(istate
))
217 if (!cache_tree_fully_valid(istate
->cache_tree
)) {
218 /* Clear and recompute the cache-tree */
219 cache_tree_free(&istate
->cache_tree
);
222 * Silently return if there is a problem with the cache tree update,
223 * which might just be due to a conflict state in some entry.
225 * This might create new tree objects, so be sure to use
226 * WRITE_TREE_MISSING_OK.
228 if (cache_tree_update(istate
, WRITE_TREE_MISSING_OK
))
232 remove_fsmonitor(istate
);
234 trace2_region_enter("index", "convert_to_sparse", istate
->repo
);
235 istate
->cache_nr
= convert_to_sparse_rec(istate
,
236 0, 0, istate
->cache_nr
,
237 "", 0, istate
->cache_tree
);
239 /* Clear and recompute the cache-tree */
240 cache_tree_free(&istate
->cache_tree
);
241 cache_tree_update(istate
, 0);
243 istate
->fsmonitor_has_run_once
= 0;
244 FREE_AND_NULL(istate
->fsmonitor_dirty
);
245 FREE_AND_NULL(istate
->fsmonitor_last_update
);
247 istate
->sparse_index
= INDEX_COLLAPSED
;
248 trace2_region_leave("index", "convert_to_sparse", istate
->repo
);
252 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
254 ALLOC_GROW(istate
->cache
, nr
+ 1, istate
->cache_alloc
);
256 istate
->cache
[nr
] = ce
;
257 add_name_hash(istate
, ce
);
260 static int add_path_to_index(const struct object_id
*oid
,
261 struct strbuf
*base
, const char *path
,
262 unsigned int mode
, void *context
)
264 struct modify_index_context
*ctx
= (struct modify_index_context
*)context
;
265 struct cache_entry
*ce
;
266 size_t len
= base
->len
;
270 size_t baselen
= base
->len
;
272 return READ_TREE_RECURSIVE
;
275 * Have we expanded to a point outside of the sparse-checkout?
277 * Artificially pad the path name with a slash "/" to
278 * indicate it as a directory, and add an arbitrary file
279 * name ("-") so we can consider base->buf as a file name
280 * to match against the cone-mode patterns.
282 * If we compared just "path", then we would expand more
283 * than we should. Since every file at root is always
284 * included, we would expand every directory at root at
285 * least one level deep instead of using sparse directory
288 strbuf_addstr(base
, path
);
289 strbuf_add(base
, "/-", 2);
291 if (path_matches_pattern_list(base
->buf
, base
->len
,
293 ctx
->pl
, ctx
->write
)) {
294 strbuf_setlen(base
, baselen
);
295 return READ_TREE_RECURSIVE
;
299 * The path "{base}{path}/" is a sparse directory. Create the correct
300 * name for inserting the entry into the index.
302 strbuf_setlen(base
, base
->len
- 1);
304 strbuf_addstr(base
, path
);
307 ce
= make_cache_entry(ctx
->write
, mode
, oid
, base
->buf
, 0, 0);
308 ce
->ce_flags
|= CE_SKIP_WORKTREE
| CE_EXTENDED
;
309 set_index_entry(ctx
->write
, ctx
->write
->cache_nr
++, ce
);
311 strbuf_setlen(base
, len
);
315 void expand_index(struct index_state
*istate
, struct pattern_list
*pl
)
318 struct index_state
*full
;
319 struct strbuf base
= STRBUF_INIT
;
320 const char *tr_region
;
321 struct modify_index_context ctx
;
324 * If the index is already full, then keep it full. We will convert
325 * it to a sparse index on write, if possible.
327 if (istate
->sparse_index
== INDEX_EXPANDED
)
331 * If our index is sparse, but our new pattern set does not use
332 * cone mode patterns, then we need to expand the index before we
333 * continue. A NULL pattern set indicates a full expansion to a
336 if (pl
&& !pl
->use_cone_patterns
) {
340 * We might contract file entries into sparse-directory
341 * entries, and for that we will need the cache tree to
344 cache_tree_free(&istate
->cache_tree
);
347 * If there is a problem creating the cache tree, then we
348 * need to expand to a full index since we cannot satisfy
349 * the current request as a sparse index.
351 if (cache_tree_update(istate
, 0))
355 if (!pl
&& give_advice_on_expansion
) {
356 give_advice_on_expansion
= 0;
357 advise_if_enabled(ADVICE_SPARSE_INDEX_EXPANDED
,
362 * A NULL pattern set indicates we are expanding a full index, so
363 * we use a special region name that indicates the full expansion.
364 * This is used by test cases, but also helps to differentiate the
367 tr_region
= pl
? "expand_index" : "ensure_full_index";
368 trace2_region_enter("index", tr_region
, istate
->repo
);
370 /* initialize basics of new index */
371 full
= xcalloc(1, sizeof(struct index_state
));
372 memcpy(full
, istate
, sizeof(struct index_state
));
375 * This slightly-misnamed 'full' index might still be sparse if we
376 * are only modifying the list of sparse directories. This hinges
377 * on whether we have a non-NULL pattern list.
379 full
->sparse_index
= pl
? INDEX_PARTIALLY_SPARSE
: INDEX_EXPANDED
;
381 /* then change the necessary things */
382 full
->cache_alloc
= (3 * istate
->cache_alloc
) / 2;
384 ALLOC_ARRAY(full
->cache
, full
->cache_alloc
);
389 for (i
= 0; i
< istate
->cache_nr
; i
++) {
390 struct cache_entry
*ce
= istate
->cache
[i
];
395 if (!S_ISSPARSEDIR(ce
->ce_mode
)) {
396 set_index_entry(full
, full
->cache_nr
++, ce
);
400 /* We now have a sparse directory entry. Should we expand? */
402 path_matches_pattern_list(ce
->name
, ce
->ce_namelen
,
404 pl
, istate
) == NOT_MATCHED
) {
405 set_index_entry(full
, full
->cache_nr
++, ce
);
409 if (!(ce
->ce_flags
& CE_SKIP_WORKTREE
))
410 warning(_("index entry is a directory, but not sparse (%08x)"),
413 /* recursively walk into cd->name */
414 tree
= lookup_tree(istate
->repo
, &ce
->oid
);
416 memset(&ps
, 0, sizeof(ps
));
421 strbuf_setlen(&base
, 0);
422 strbuf_add(&base
, ce
->name
, strlen(ce
->name
));
424 read_tree_at(istate
->repo
, tree
, &base
, 0, &ps
,
425 add_path_to_index
, &ctx
);
427 /* free directory entries. full entries are re-used */
428 discard_cache_entry(ce
);
431 /* Copy back into original index. */
432 memcpy(&istate
->name_hash
, &full
->name_hash
, sizeof(full
->name_hash
));
433 memcpy(&istate
->dir_hash
, &full
->dir_hash
, sizeof(full
->dir_hash
));
434 istate
->sparse_index
= pl
? INDEX_PARTIALLY_SPARSE
: INDEX_EXPANDED
;
436 istate
->cache
= full
->cache
;
437 istate
->cache_nr
= full
->cache_nr
;
438 istate
->cache_alloc
= full
->cache_alloc
;
439 istate
->fsmonitor_has_run_once
= 0;
440 FREE_AND_NULL(istate
->fsmonitor_dirty
);
441 FREE_AND_NULL(istate
->fsmonitor_last_update
);
443 strbuf_release(&base
);
446 /* Clear and recompute the cache-tree */
447 cache_tree_free(&istate
->cache_tree
);
448 cache_tree_update(istate
, 0);
450 trace2_region_leave("index", tr_region
, istate
->repo
);
453 void ensure_full_index(struct index_state
*istate
)
456 BUG("ensure_full_index() must get an index!");
457 expand_index(istate
, NULL
);
460 void ensure_correct_sparsity(struct index_state
*istate
)
463 * If the index can be sparse, make it sparse. Otherwise,
464 * ensure the index is full.
466 if (is_sparse_index_allowed(istate
, 0))
467 convert_to_sparse(istate
, 0);
469 ensure_full_index(istate
);
472 struct path_found_data
{
474 * The path stored in 'dir', if non-empty, corresponds to the most-
475 * recent path that we checked where:
477 * 1. The path should be a directory, according to the index.
478 * 2. The path does not exist.
479 * 3. The parent path _does_ exist. (This may be the root of the
480 * working directory.)
486 #define PATH_FOUND_DATA_INIT { \
490 static void clear_path_found_data(struct path_found_data
*data
)
492 strbuf_release(&data
->dir
);
496 * Return the length of the longest common substring that ends in a
497 * slash ('/') to indicate the longest common parent directory. Returns
498 * zero if no common directory exists.
500 static size_t max_common_dir_prefix(const char *path1
, const char *path2
)
502 size_t common_prefix
= 0;
503 for (size_t i
= 0; path1
[i
] && path2
[i
]; i
++) {
504 if (path1
[i
] != path2
[i
])
508 * If they agree at a directory separator, then add one
509 * to make sure it is included in the common prefix string.
512 common_prefix
= i
+ 1;
515 return common_prefix
;
518 static int path_found(const char *path
, struct path_found_data
*data
)
521 size_t common_prefix
;
524 * If data->dir is non-empty, then it contains a path that doesn't
525 * exist, including an ending slash ('/'). If it is a prefix of 'path',
526 * then we can return 0.
528 if (data
->dir
.len
&& !memcmp(path
, data
->dir
.buf
, data
->dir
.len
))
532 * Otherwise, we must check if the current path exists. If it does, then
533 * return 1. The cached directory will be skipped until we come across
534 * a missing path again.
537 if (!lstat(path
, &st
))
541 * At this point, we know that 'path' doesn't exist, and we know that
542 * the parent directory of 'data->dir' does exist. Let's set 'data->dir'
543 * to be the top-most non-existing directory of 'path'. If the first
544 * parent of 'path' exists, then we will act as though 'path'
545 * corresponds to a directory (by adding a slash).
547 common_prefix
= max_common_dir_prefix(path
, data
->dir
.buf
);
550 * At this point, 'path' and 'data->dir' have a common existing parent
551 * directory given by path[0..common_prefix] (which could have length 0).
552 * We "grow" the data->dir buffer by checking for existing directories
556 strbuf_setlen(&data
->dir
, common_prefix
);
558 /* Find the next directory in 'path'. */
559 const char *rest
= path
+ data
->dir
.len
;
560 const char *next_slash
= strchr(rest
, '/');
563 * If there are no more slashes, then 'path' doesn't contain a
564 * non-existent _parent_ directory. Set 'data->dir' to be equal
565 * to 'path' plus an additional slash, so it can be used for
566 * caching in the future. The filename of 'path' is considered
567 * a non-existent directory.
569 * Note: if "{path}/" exists as a directory, then it will never
570 * appear as a prefix of other callers to this method, assuming
571 * the context from the clear_skip_worktree... methods. If this
572 * method is reused, then this must be reconsidered.
575 strbuf_addstr(&data
->dir
, rest
);
576 strbuf_addch(&data
->dir
, '/');
581 * Now that we have a slash, let's grow 'data->dir' to include
582 * this slash, then test if we should stop.
584 strbuf_add(&data
->dir
, rest
, next_slash
- rest
+ 1);
586 /* If the parent dir doesn't exist, then stop here. */
588 if (lstat(data
->dir
.buf
, &st
))
593 * At this point, 'data->dir' is equal to 'path' plus a slash character,
594 * and the parent directory of 'path' definitely exists. Moreover, we
595 * know that 'path' doesn't exist, or we would have returned 1 earlier.
600 static int clear_skip_worktree_from_present_files_sparse(struct index_state
*istate
)
602 struct path_found_data data
= PATH_FOUND_DATA_INIT
;
607 trace2_region_enter("index", "clear_skip_worktree_from_present_files_sparse",
609 for (int i
= 0; i
< istate
->cache_nr
; i
++) {
610 struct cache_entry
*ce
= istate
->cache
[i
];
612 if (ce_skip_worktree(ce
)) {
614 if (path_found(ce
->name
, &data
)) {
615 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
619 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
624 trace2_data_intmax("index", istate
->repo
,
625 "sparse_path_count", path_count
);
626 trace2_data_intmax("index", istate
->repo
,
627 "sparse_lstat_count", data
.lstat_count
);
628 trace2_region_leave("index", "clear_skip_worktree_from_present_files_sparse",
630 clear_path_found_data(&data
);
634 static void clear_skip_worktree_from_present_files_full(struct index_state
*istate
)
636 struct path_found_data data
= PATH_FOUND_DATA_INIT
;
640 trace2_region_enter("index", "clear_skip_worktree_from_present_files_full",
642 for (int i
= 0; i
< istate
->cache_nr
; i
++) {
643 struct cache_entry
*ce
= istate
->cache
[i
];
645 if (S_ISSPARSEDIR(ce
->ce_mode
))
646 BUG("ensure-full-index did not fully flatten?");
648 if (ce_skip_worktree(ce
)) {
650 if (path_found(ce
->name
, &data
))
651 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
655 trace2_data_intmax("index", istate
->repo
,
656 "full_path_count", path_count
);
657 trace2_data_intmax("index", istate
->repo
,
658 "full_lstat_count", data
.lstat_count
);
659 trace2_region_leave("index", "clear_skip_worktree_from_present_files_full",
661 clear_path_found_data(&data
);
664 void clear_skip_worktree_from_present_files(struct index_state
*istate
)
666 if (!core_apply_sparse_checkout
||
667 sparse_expect_files_outside_of_patterns
)
670 if (clear_skip_worktree_from_present_files_sparse(istate
)) {
671 ensure_full_index(istate
);
672 clear_skip_worktree_from_present_files_full(istate
);
677 * This static global helps avoid infinite recursion between
678 * expand_to_path() and index_file_exists().
680 static int in_expand_to_path
= 0;
682 void expand_to_path(struct index_state
*istate
,
683 const char *path
, size_t pathlen
, int icase
)
685 struct strbuf path_mutable
= STRBUF_INIT
;
688 /* prevent extra recursion */
689 if (in_expand_to_path
)
692 if (!istate
->sparse_index
)
695 in_expand_to_path
= 1;
698 * We only need to actually expand a region if the
699 * following are both true:
701 * 1. 'path' is not already in the index.
702 * 2. Some parent directory of 'path' is a sparse directory.
705 if (index_file_exists(istate
, path
, pathlen
, icase
))
708 strbuf_add(&path_mutable
, path
, pathlen
);
709 strbuf_addch(&path_mutable
, '/');
711 /* Check the name hash for all parent directories */
713 while (substr_len
< pathlen
) {
715 char *replace
= strchr(path_mutable
.buf
+ substr_len
, '/');
720 /* replace the character _after_ the slash */
724 substr_len
= replace
- path_mutable
.buf
;
725 if (index_file_exists(istate
, path_mutable
.buf
,
726 substr_len
, icase
)) {
728 * We found a parent directory in the name-hash
729 * hashtable, because only sparse directory entries
730 * have a trailing '/' character. Since "path" wasn't
731 * in the index, perhaps it exists within this
732 * sparse-directory. Expand accordingly.
734 ensure_full_index(istate
);
742 strbuf_release(&path_mutable
);
743 in_expand_to_path
= 0;