Merge branch 'pw/diff-no-index-from-named-pipes'
[alt-git.git] / sparse-index.c
blob90d046225683dd02b6593acc9413b1caecd0e12d
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "name-hash.h"
6 #include "read-cache-ll.h"
7 #include "repository.h"
8 #include "sparse-index.h"
9 #include "tree.h"
10 #include "pathspec.h"
11 #include "trace2.h"
12 #include "cache-tree.h"
13 #include "config.h"
14 #include "dir.h"
15 #include "fsmonitor-ll.h"
17 struct modify_index_context {
18 struct index_state *write;
19 struct pattern_list *pl;
22 static struct cache_entry *construct_sparse_dir_entry(
23 struct index_state *istate,
24 const char *sparse_dir,
25 struct cache_tree *tree)
27 struct cache_entry *de;
29 de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0);
31 de->ce_flags |= CE_SKIP_WORKTREE;
32 return de;
36 * Returns the number of entries "inserted" into the index.
38 static int convert_to_sparse_rec(struct index_state *istate,
39 int num_converted,
40 int start, int end,
41 const char *ct_path, size_t ct_pathlen,
42 struct cache_tree *ct)
44 int i, can_convert = 1;
45 int start_converted = num_converted;
46 struct strbuf child_path = STRBUF_INIT;
49 * Is the current path outside of the sparse cone?
50 * Then check if the region can be replaced by a sparse
51 * directory entry (everything is sparse and merged).
53 if (path_in_sparse_checkout(ct_path, istate))
54 can_convert = 0;
56 for (i = start; can_convert && i < end; i++) {
57 struct cache_entry *ce = istate->cache[i];
59 if (ce_stage(ce) ||
60 S_ISGITLINK(ce->ce_mode) ||
61 !(ce->ce_flags & CE_SKIP_WORKTREE))
62 can_convert = 0;
65 if (can_convert) {
66 struct cache_entry *se;
67 se = construct_sparse_dir_entry(istate, ct_path, ct);
69 istate->cache[num_converted++] = se;
70 return 1;
73 for (i = start; i < end; ) {
74 int count, span, pos = -1;
75 const char *base, *slash;
76 struct cache_entry *ce = istate->cache[i];
79 * Detect if this is a normal entry outside of any subtree
80 * entry.
82 base = ce->name + ct_pathlen;
83 slash = strchr(base, '/');
85 if (slash)
86 pos = cache_tree_subtree_pos(ct, base, slash - base);
88 if (pos < 0) {
89 istate->cache[num_converted++] = ce;
90 i++;
91 continue;
94 strbuf_setlen(&child_path, 0);
95 strbuf_add(&child_path, ce->name, slash - ce->name + 1);
97 span = ct->down[pos]->cache_tree->entry_count;
98 count = convert_to_sparse_rec(istate,
99 num_converted, i, i + span,
100 child_path.buf, child_path.len,
101 ct->down[pos]->cache_tree);
102 num_converted += count;
103 i += span;
106 strbuf_release(&child_path);
107 return num_converted - start_converted;
110 int set_sparse_index_config(struct repository *repo, int enable)
112 int res = repo_config_set_worktree_gently(repo,
113 "index.sparse",
114 enable ? "true" : "false");
115 prepare_repo_settings(repo);
116 repo->settings.sparse_index = enable;
117 return res;
120 static int index_has_unmerged_entries(struct index_state *istate)
122 int i;
123 for (i = 0; i < istate->cache_nr; i++) {
124 if (ce_stage(istate->cache[i]))
125 return 1;
128 return 0;
131 int is_sparse_index_allowed(struct index_state *istate, int flags)
133 if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
134 return 0;
136 if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
137 int test_env;
140 * The sparse index is not (yet) integrated with a split index.
142 if (istate->split_index || git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
143 return 0;
145 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
146 * index.sparse config variable to be on.
148 test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
149 if (test_env >= 0)
150 set_sparse_index_config(istate->repo, test_env);
153 * Only convert to sparse if index.sparse is set.
155 prepare_repo_settings(istate->repo);
156 if (!istate->repo->settings.sparse_index)
157 return 0;
160 if (init_sparse_checkout_patterns(istate))
161 return 0;
164 * We need cone-mode patterns to use sparse-index. If a user edits
165 * their sparse-checkout file manually, then we can detect during
166 * parsing that they are not actually using cone-mode patterns and
167 * hence we need to abort this conversion _without error_. Warnings
168 * already exist in the pattern parsing to inform the user of their
169 * bad patterns.
171 if (!istate->sparse_checkout_patterns->use_cone_patterns)
172 return 0;
174 return 1;
177 int convert_to_sparse(struct index_state *istate, int flags)
180 * If the index is already sparse, empty, or otherwise
181 * cannot be converted to sparse, do not convert.
183 if (istate->sparse_index == INDEX_COLLAPSED || !istate->cache_nr ||
184 !is_sparse_index_allowed(istate, flags))
185 return 0;
188 * NEEDSWORK: If we have unmerged entries, then stay full.
189 * Unmerged entries prevent the cache-tree extension from working.
191 if (index_has_unmerged_entries(istate))
192 return 0;
194 if (!cache_tree_fully_valid(istate->cache_tree)) {
195 /* Clear and recompute the cache-tree */
196 cache_tree_free(&istate->cache_tree);
199 * Silently return if there is a problem with the cache tree update,
200 * which might just be due to a conflict state in some entry.
202 * This might create new tree objects, so be sure to use
203 * WRITE_TREE_MISSING_OK.
205 if (cache_tree_update(istate, WRITE_TREE_MISSING_OK))
206 return 0;
209 remove_fsmonitor(istate);
211 trace2_region_enter("index", "convert_to_sparse", istate->repo);
212 istate->cache_nr = convert_to_sparse_rec(istate,
213 0, 0, istate->cache_nr,
214 "", 0, istate->cache_tree);
216 /* Clear and recompute the cache-tree */
217 cache_tree_free(&istate->cache_tree);
218 cache_tree_update(istate, 0);
220 istate->fsmonitor_has_run_once = 0;
221 FREE_AND_NULL(istate->fsmonitor_dirty);
222 FREE_AND_NULL(istate->fsmonitor_last_update);
224 istate->sparse_index = INDEX_COLLAPSED;
225 trace2_region_leave("index", "convert_to_sparse", istate->repo);
226 return 0;
229 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
231 ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
233 istate->cache[nr] = ce;
234 add_name_hash(istate, ce);
237 static int add_path_to_index(const struct object_id *oid,
238 struct strbuf *base, const char *path,
239 unsigned int mode, void *context)
241 struct modify_index_context *ctx = (struct modify_index_context *)context;
242 struct cache_entry *ce;
243 size_t len = base->len;
245 if (S_ISDIR(mode)) {
246 int dtype;
247 size_t baselen = base->len;
248 if (!ctx->pl)
249 return READ_TREE_RECURSIVE;
252 * Have we expanded to a point outside of the sparse-checkout?
254 * Artificially pad the path name with a slash "/" to
255 * indicate it as a directory, and add an arbitrary file
256 * name ("-") so we can consider base->buf as a file name
257 * to match against the cone-mode patterns.
259 * If we compared just "path", then we would expand more
260 * than we should. Since every file at root is always
261 * included, we would expand every directory at root at
262 * least one level deep instead of using sparse directory
263 * entries.
265 strbuf_addstr(base, path);
266 strbuf_add(base, "/-", 2);
268 if (path_matches_pattern_list(base->buf, base->len,
269 NULL, &dtype,
270 ctx->pl, ctx->write)) {
271 strbuf_setlen(base, baselen);
272 return READ_TREE_RECURSIVE;
276 * The path "{base}{path}/" is a sparse directory. Create the correct
277 * name for inserting the entry into the index.
279 strbuf_setlen(base, base->len - 1);
280 } else {
281 strbuf_addstr(base, path);
284 ce = make_cache_entry(ctx->write, mode, oid, base->buf, 0, 0);
285 ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED;
286 set_index_entry(ctx->write, ctx->write->cache_nr++, ce);
288 strbuf_setlen(base, len);
289 return 0;
292 void expand_index(struct index_state *istate, struct pattern_list *pl)
294 int i;
295 struct index_state *full;
296 struct strbuf base = STRBUF_INIT;
297 const char *tr_region;
298 struct modify_index_context ctx;
301 * If the index is already full, then keep it full. We will convert
302 * it to a sparse index on write, if possible.
304 if (istate->sparse_index == INDEX_EXPANDED)
305 return;
308 * If our index is sparse, but our new pattern set does not use
309 * cone mode patterns, then we need to expand the index before we
310 * continue. A NULL pattern set indicates a full expansion to a
311 * full index.
313 if (pl && !pl->use_cone_patterns) {
314 pl = NULL;
315 } else {
317 * We might contract file entries into sparse-directory
318 * entries, and for that we will need the cache tree to
319 * be recomputed.
321 cache_tree_free(&istate->cache_tree);
324 * If there is a problem creating the cache tree, then we
325 * need to expand to a full index since we cannot satisfy
326 * the current request as a sparse index.
328 if (cache_tree_update(istate, 0))
329 pl = NULL;
333 * A NULL pattern set indicates we are expanding a full index, so
334 * we use a special region name that indicates the full expansion.
335 * This is used by test cases, but also helps to differentiate the
336 * two cases.
338 tr_region = pl ? "expand_index" : "ensure_full_index";
339 trace2_region_enter("index", tr_region, istate->repo);
341 /* initialize basics of new index */
342 full = xcalloc(1, sizeof(struct index_state));
343 memcpy(full, istate, sizeof(struct index_state));
346 * This slightly-misnamed 'full' index might still be sparse if we
347 * are only modifying the list of sparse directories. This hinges
348 * on whether we have a non-NULL pattern list.
350 full->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
352 /* then change the necessary things */
353 full->cache_alloc = (3 * istate->cache_alloc) / 2;
354 full->cache_nr = 0;
355 ALLOC_ARRAY(full->cache, full->cache_alloc);
357 ctx.write = full;
358 ctx.pl = pl;
360 for (i = 0; i < istate->cache_nr; i++) {
361 struct cache_entry *ce = istate->cache[i];
362 struct tree *tree;
363 struct pathspec ps;
364 int dtype;
366 if (!S_ISSPARSEDIR(ce->ce_mode)) {
367 set_index_entry(full, full->cache_nr++, ce);
368 continue;
371 /* We now have a sparse directory entry. Should we expand? */
372 if (pl &&
373 path_matches_pattern_list(ce->name, ce->ce_namelen,
374 NULL, &dtype,
375 pl, istate) == NOT_MATCHED) {
376 set_index_entry(full, full->cache_nr++, ce);
377 continue;
380 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
381 warning(_("index entry is a directory, but not sparse (%08x)"),
382 ce->ce_flags);
384 /* recursively walk into cd->name */
385 tree = lookup_tree(istate->repo, &ce->oid);
387 memset(&ps, 0, sizeof(ps));
388 ps.recursive = 1;
389 ps.has_wildcard = 1;
390 ps.max_depth = -1;
392 strbuf_setlen(&base, 0);
393 strbuf_add(&base, ce->name, strlen(ce->name));
395 read_tree_at(istate->repo, tree, &base, &ps,
396 add_path_to_index, &ctx);
398 /* free directory entries. full entries are re-used */
399 discard_cache_entry(ce);
402 /* Copy back into original index. */
403 memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
404 memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash));
405 istate->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
406 free(istate->cache);
407 istate->cache = full->cache;
408 istate->cache_nr = full->cache_nr;
409 istate->cache_alloc = full->cache_alloc;
410 istate->fsmonitor_has_run_once = 0;
411 FREE_AND_NULL(istate->fsmonitor_dirty);
412 FREE_AND_NULL(istate->fsmonitor_last_update);
414 strbuf_release(&base);
415 free(full);
417 /* Clear and recompute the cache-tree */
418 cache_tree_free(&istate->cache_tree);
419 cache_tree_update(istate, 0);
421 trace2_region_leave("index", tr_region, istate->repo);
424 void ensure_full_index(struct index_state *istate)
426 if (!istate)
427 BUG("ensure_full_index() must get an index!");
428 expand_index(istate, NULL);
431 void ensure_correct_sparsity(struct index_state *istate)
434 * If the index can be sparse, make it sparse. Otherwise,
435 * ensure the index is full.
437 if (is_sparse_index_allowed(istate, 0))
438 convert_to_sparse(istate, 0);
439 else
440 ensure_full_index(istate);
443 static int path_found(const char *path, const char **dirname, size_t *dir_len,
444 int *dir_found)
446 struct stat st;
447 char *newdir;
448 char *tmp;
451 * If dirname corresponds to a directory that doesn't exist, and this
452 * path starts with dirname, then path can't exist.
454 if (!*dir_found && !memcmp(path, *dirname, *dir_len))
455 return 0;
458 * If path itself exists, return 1.
460 if (!lstat(path, &st))
461 return 1;
464 * Otherwise, path does not exist so we'll return 0...but we'll first
465 * determine some info about its parent directory so we can avoid
466 * lstat calls for future cache entries.
468 newdir = strrchr(path, '/');
469 if (!newdir)
470 return 0; /* Didn't find a parent dir; just return 0 now. */
473 * If path starts with directory (which we already lstat'ed and found),
474 * then no need to lstat parent directory again.
476 if (*dir_found && *dirname && memcmp(path, *dirname, *dir_len))
477 return 0;
479 /* Free previous dirname, and cache path's dirname */
480 *dirname = path;
481 *dir_len = newdir - path + 1;
483 tmp = xstrndup(path, *dir_len);
484 *dir_found = !lstat(tmp, &st);
485 free(tmp);
487 return 0;
490 void clear_skip_worktree_from_present_files(struct index_state *istate)
492 const char *last_dirname = NULL;
493 size_t dir_len = 0;
494 int dir_found = 1;
496 int i;
497 int path_count[2] = {0, 0};
498 int restarted = 0;
500 if (!core_apply_sparse_checkout ||
501 sparse_expect_files_outside_of_patterns)
502 return;
504 trace2_region_enter("index", "clear_skip_worktree_from_present_files",
505 istate->repo);
506 restart:
507 for (i = 0; i < istate->cache_nr; i++) {
508 struct cache_entry *ce = istate->cache[i];
510 if (ce_skip_worktree(ce)) {
511 path_count[restarted]++;
512 if (path_found(ce->name, &last_dirname, &dir_len, &dir_found)) {
513 if (S_ISSPARSEDIR(ce->ce_mode)) {
514 if (restarted)
515 BUG("ensure-full-index did not fully flatten?");
516 ensure_full_index(istate);
517 restarted = 1;
518 goto restart;
520 ce->ce_flags &= ~CE_SKIP_WORKTREE;
525 if (path_count[0])
526 trace2_data_intmax("index", istate->repo,
527 "sparse_path_count", path_count[0]);
528 if (restarted)
529 trace2_data_intmax("index", istate->repo,
530 "sparse_path_count_full", path_count[1]);
531 trace2_region_leave("index", "clear_skip_worktree_from_present_files",
532 istate->repo);
536 * This static global helps avoid infinite recursion between
537 * expand_to_path() and index_file_exists().
539 static int in_expand_to_path = 0;
541 void expand_to_path(struct index_state *istate,
542 const char *path, size_t pathlen, int icase)
544 struct strbuf path_mutable = STRBUF_INIT;
545 size_t substr_len;
547 /* prevent extra recursion */
548 if (in_expand_to_path)
549 return;
551 if (!istate->sparse_index)
552 return;
554 in_expand_to_path = 1;
557 * We only need to actually expand a region if the
558 * following are both true:
560 * 1. 'path' is not already in the index.
561 * 2. Some parent directory of 'path' is a sparse directory.
564 if (index_file_exists(istate, path, pathlen, icase))
565 goto cleanup;
567 strbuf_add(&path_mutable, path, pathlen);
568 strbuf_addch(&path_mutable, '/');
570 /* Check the name hash for all parent directories */
571 substr_len = 0;
572 while (substr_len < pathlen) {
573 char temp;
574 char *replace = strchr(path_mutable.buf + substr_len, '/');
576 if (!replace)
577 break;
579 /* replace the character _after_ the slash */
580 replace++;
581 temp = *replace;
582 *replace = '\0';
583 if (index_file_exists(istate, path_mutable.buf,
584 path_mutable.len, icase)) {
586 * We found a parent directory in the name-hash
587 * hashtable, because only sparse directory entries
588 * have a trailing '/' character. Since "path" wasn't
589 * in the index, perhaps it exists within this
590 * sparse-directory. Expand accordingly.
592 ensure_full_index(istate);
593 break;
596 *replace = temp;
597 substr_len = replace - path_mutable.buf;
600 cleanup:
601 strbuf_release(&path_mutable);
602 in_expand_to_path = 0;