]> git.ipfire.org Git - thirdparty/git.git/blame - sparse-index.c
treewide: remove unnecessary includes of cache.h
[thirdparty/git.git] / sparse-index.c
CommitLineData
3964fc2a 1#include "cache.h"
36bf1958 2#include "alloc.h"
f394e093 3#include "gettext.h"
3964fc2a
DS
4#include "repository.h"
5#include "sparse-index.h"
4300f844
DS
6#include "tree.h"
7#include "pathspec.h"
8#include "trace2.h"
6e773527
DS
9#include "cache-tree.h"
10#include "config.h"
11#include "dir.h"
12#include "fsmonitor.h"
13
0243930a
DS
14struct modify_index_context {
15 struct index_state *write;
16 struct pattern_list *pl;
17};
18
6e773527
DS
19static struct cache_entry *construct_sparse_dir_entry(
20 struct index_state *istate,
21 const char *sparse_dir,
22 struct cache_tree *tree)
23{
24 struct cache_entry *de;
25
26 de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0);
27
28 de->ce_flags |= CE_SKIP_WORKTREE;
29 return de;
30}
31
32/*
33 * Returns the number of entries "inserted" into the index.
34 */
35static int convert_to_sparse_rec(struct index_state *istate,
36 int num_converted,
37 int start, int end,
38 const char *ct_path, size_t ct_pathlen,
39 struct cache_tree *ct)
40{
41 int i, can_convert = 1;
42 int start_converted = num_converted;
6e773527 43 struct strbuf child_path = STRBUF_INIT;
6e773527
DS
44
45 /*
46 * Is the current path outside of the sparse cone?
47 * Then check if the region can be replaced by a sparse
48 * directory entry (everything is sparse and merged).
49 */
02155c8c 50 if (path_in_sparse_checkout(ct_path, istate))
6e773527
DS
51 can_convert = 0;
52
53 for (i = start; can_convert && i < end; i++) {
54 struct cache_entry *ce = istate->cache[i];
55
56 if (ce_stage(ce) ||
f442313e 57 S_ISGITLINK(ce->ce_mode) ||
6e773527
DS
58 !(ce->ce_flags & CE_SKIP_WORKTREE))
59 can_convert = 0;
60 }
61
62 if (can_convert) {
63 struct cache_entry *se;
64 se = construct_sparse_dir_entry(istate, ct_path, ct);
65
66 istate->cache[num_converted++] = se;
67 return 1;
68 }
69
70 for (i = start; i < end; ) {
71 int count, span, pos = -1;
72 const char *base, *slash;
73 struct cache_entry *ce = istate->cache[i];
74
75 /*
76 * Detect if this is a normal entry outside of any subtree
77 * entry.
78 */
79 base = ce->name + ct_pathlen;
80 slash = strchr(base, '/');
81
82 if (slash)
83 pos = cache_tree_subtree_pos(ct, base, slash - base);
84
85 if (pos < 0) {
86 istate->cache[num_converted++] = ce;
87 i++;
88 continue;
89 }
90
91 strbuf_setlen(&child_path, 0);
92 strbuf_add(&child_path, ce->name, slash - ce->name + 1);
93
94 span = ct->down[pos]->cache_tree->entry_count;
95 count = convert_to_sparse_rec(istate,
96 num_converted, i, i + span,
97 child_path.buf, child_path.len,
98 ct->down[pos]->cache_tree);
99 num_converted += count;
100 i += span;
101 }
102
103 strbuf_release(&child_path);
104 return num_converted - start_converted;
105}
106
b79f9c07 107int set_sparse_index_config(struct repository *repo, int enable)
58300f47 108{
7316dc5f
DS
109 int res = repo_config_set_worktree_gently(repo,
110 "index.sparse",
111 enable ? "true" : "false");
122ba1f7
DS
112 prepare_repo_settings(repo);
113 repo->settings.sparse_index = enable;
114 return res;
58300f47
DS
115}
116
fc6609d1
DS
117static int index_has_unmerged_entries(struct index_state *istate)
118{
119 int i;
120 for (i = 0; i < istate->cache_nr; i++) {
121 if (ce_stage(istate->cache[i]))
122 return 1;
123 }
124
125 return 0;
126}
127
cfde4cd6 128int is_sparse_index_allowed(struct index_state *istate, int flags)
6e773527 129{
b93fea08 130 if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
6e773527
DS
131 return 0;
132
ce7a9f01 133 if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
b93fea08
VD
134 int test_env;
135
ce7a9f01
DS
136 /*
137 * The sparse index is not (yet) integrated with a split index.
138 */
ae103c37 139 if (istate->split_index || git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
ce7a9f01
DS
140 return 0;
141 /*
142 * The GIT_TEST_SPARSE_INDEX environment variable triggers the
143 * index.sparse config variable to be on.
144 */
145 test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
146 if (test_env >= 0)
147 set_sparse_index_config(istate->repo, test_env);
6e773527 148
ce7a9f01
DS
149 /*
150 * Only convert to sparse if index.sparse is set.
151 */
152 prepare_repo_settings(istate->repo);
153 if (!istate->repo->settings.sparse_index)
6e773527
DS
154 return 0;
155 }
156
02155c8c
DS
157 if (init_sparse_checkout_patterns(istate))
158 return 0;
6e773527 159
e27eab45
DS
160 /*
161 * We need cone-mode patterns to use sparse-index. If a user edits
162 * their sparse-checkout file manually, then we can detect during
163 * parsing that they are not actually using cone-mode patterns and
164 * hence we need to abort this conversion _without error_. Warnings
165 * already exist in the pattern parsing to inform the user of their
166 * bad patterns.
167 */
168 if (!istate->sparse_checkout_patterns->use_cone_patterns)
169 return 0;
6e773527 170
b93fea08
VD
171 return 1;
172}
173
174int convert_to_sparse(struct index_state *istate, int flags)
175{
176 /*
177 * If the index is already sparse, empty, or otherwise
178 * cannot be converted to sparse, do not convert.
179 */
9fadb373 180 if (istate->sparse_index == INDEX_COLLAPSED || !istate->cache_nr ||
b93fea08
VD
181 !is_sparse_index_allowed(istate, flags))
182 return 0;
183
fc6609d1
DS
184 /*
185 * NEEDSWORK: If we have unmerged entries, then stay full.
186 * Unmerged entries prevent the cache-tree extension from working.
187 */
188 if (index_has_unmerged_entries(istate))
189 return 0;
190
13f69f30
VD
191 if (!cache_tree_fully_valid(istate->cache_tree)) {
192 /* Clear and recompute the cache-tree */
193 cache_tree_free(&istate->cache_tree);
194
195 /*
196 * Silently return if there is a problem with the cache tree update,
197 * which might just be due to a conflict state in some entry.
198 *
199 * This might create new tree objects, so be sure to use
200 * WRITE_TREE_MISSING_OK.
201 */
202 if (cache_tree_update(istate, WRITE_TREE_MISSING_OK))
203 return 0;
204 }
6e773527
DS
205
206 remove_fsmonitor(istate);
207
208 trace2_region_enter("index", "convert_to_sparse", istate->repo);
209 istate->cache_nr = convert_to_sparse_rec(istate,
210 0, 0, istate->cache_nr,
211 "", 0, istate->cache_tree);
2de37c53
DS
212
213 /* Clear and recompute the cache-tree */
214 cache_tree_free(&istate->cache_tree);
215 cache_tree_update(istate, 0);
216
f8fe49e5
DS
217 istate->fsmonitor_has_run_once = 0;
218 FREE_AND_NULL(istate->fsmonitor_dirty);
219 FREE_AND_NULL(istate->fsmonitor_last_update);
220
9fadb373 221 istate->sparse_index = INDEX_COLLAPSED;
6e773527
DS
222 trace2_region_leave("index", "convert_to_sparse", istate->repo);
223 return 0;
224}
4300f844
DS
225
226static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
227{
228 ALLOC_GROW(istate->cache, nr + 1, istate->cache_alloc);
229
230 istate->cache[nr] = ce;
231 add_name_hash(istate, ce);
232}
233
234static int add_path_to_index(const struct object_id *oid,
235 struct strbuf *base, const char *path,
236 unsigned int mode, void *context)
237{
0243930a 238 struct modify_index_context *ctx = (struct modify_index_context *)context;
4300f844
DS
239 struct cache_entry *ce;
240 size_t len = base->len;
241
0243930a
DS
242 if (S_ISDIR(mode)) {
243 int dtype;
244 size_t baselen = base->len;
245 if (!ctx->pl)
246 return READ_TREE_RECURSIVE;
4300f844 247
0243930a
DS
248 /*
249 * Have we expanded to a point outside of the sparse-checkout?
250 *
251 * Artificially pad the path name with a slash "/" to
252 * indicate it as a directory, and add an arbitrary file
253 * name ("-") so we can consider base->buf as a file name
254 * to match against the cone-mode patterns.
255 *
256 * If we compared just "path", then we would expand more
257 * than we should. Since every file at root is always
258 * included, we would expand every directory at root at
259 * least one level deep instead of using sparse directory
260 * entries.
261 */
262 strbuf_addstr(base, path);
263 strbuf_add(base, "/-", 2);
264
265 if (path_matches_pattern_list(base->buf, base->len,
266 NULL, &dtype,
267 ctx->pl, ctx->write)) {
268 strbuf_setlen(base, baselen);
269 return READ_TREE_RECURSIVE;
270 }
4300f844 271
0243930a
DS
272 /*
273 * The path "{base}{path}/" is a sparse directory. Create the correct
274 * name for inserting the entry into the index.
275 */
276 strbuf_setlen(base, base->len - 1);
277 } else {
278 strbuf_addstr(base, path);
279 }
4300f844 280
0243930a 281 ce = make_cache_entry(ctx->write, mode, oid, base->buf, 0, 0);
47410778 282 ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED;
0243930a 283 set_index_entry(ctx->write, ctx->write->cache_nr++, ce);
4300f844
DS
284
285 strbuf_setlen(base, len);
286 return 0;
287}
3964fc2a 288
dce241b0 289void expand_index(struct index_state *istate, struct pattern_list *pl)
3964fc2a 290{
4300f844
DS
291 int i;
292 struct index_state *full;
293 struct strbuf base = STRBUF_INIT;
dce241b0 294 const char *tr_region;
0243930a 295 struct modify_index_context ctx;
4300f844 296
dce241b0
DS
297 /*
298 * If the index is already full, then keep it full. We will convert
299 * it to a sparse index on write, if possible.
300 */
29fefafc 301 if (istate->sparse_index == INDEX_EXPANDED)
4300f844
DS
302 return;
303
dce241b0
DS
304 /*
305 * If our index is sparse, but our new pattern set does not use
306 * cone mode patterns, then we need to expand the index before we
307 * continue. A NULL pattern set indicates a full expansion to a
308 * full index.
309 */
ac8acb4f 310 if (pl && !pl->use_cone_patterns) {
dce241b0 311 pl = NULL;
ac8acb4f
DS
312 } else {
313 /*
314 * We might contract file entries into sparse-directory
315 * entries, and for that we will need the cache tree to
316 * be recomputed.
317 */
318 cache_tree_free(&istate->cache_tree);
319
320 /*
321 * If there is a problem creating the cache tree, then we
322 * need to expand to a full index since we cannot satisfy
323 * the current request as a sparse index.
324 */
325 if (cache_tree_update(istate, 0))
326 pl = NULL;
327 }
dce241b0 328
dce241b0
DS
329 /*
330 * A NULL pattern set indicates we are expanding a full index, so
331 * we use a special region name that indicates the full expansion.
332 * This is used by test cases, but also helps to differentiate the
333 * two cases.
334 */
335 tr_region = pl ? "expand_index" : "ensure_full_index";
336 trace2_region_enter("index", tr_region, istate->repo);
4300f844
DS
337
338 /* initialize basics of new index */
339 full = xcalloc(1, sizeof(struct index_state));
340 memcpy(full, istate, sizeof(struct index_state));
341
ac8acb4f
DS
342 /*
343 * This slightly-misnamed 'full' index might still be sparse if we
344 * are only modifying the list of sparse directories. This hinges
345 * on whether we have a non-NULL pattern list.
346 */
347 full->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
348
4300f844 349 /* then change the necessary things */
4300f844
DS
350 full->cache_alloc = (3 * istate->cache_alloc) / 2;
351 full->cache_nr = 0;
352 ALLOC_ARRAY(full->cache, full->cache_alloc);
353
0243930a
DS
354 ctx.write = full;
355 ctx.pl = pl;
356
4300f844
DS
357 for (i = 0; i < istate->cache_nr; i++) {
358 struct cache_entry *ce = istate->cache[i];
359 struct tree *tree;
360 struct pathspec ps;
ac8acb4f 361 int dtype;
4300f844
DS
362
363 if (!S_ISSPARSEDIR(ce->ce_mode)) {
364 set_index_entry(full, full->cache_nr++, ce);
365 continue;
366 }
ac8acb4f
DS
367
368 /* We now have a sparse directory entry. Should we expand? */
369 if (pl &&
370 path_matches_pattern_list(ce->name, ce->ce_namelen,
371 NULL, &dtype,
372 pl, istate) == NOT_MATCHED) {
373 set_index_entry(full, full->cache_nr++, ce);
374 continue;
375 }
376
4300f844
DS
377 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
378 warning(_("index entry is a directory, but not sparse (%08x)"),
379 ce->ce_flags);
380
381 /* recursively walk into cd->name */
382 tree = lookup_tree(istate->repo, &ce->oid);
383
384 memset(&ps, 0, sizeof(ps));
385 ps.recursive = 1;
386 ps.has_wildcard = 1;
387 ps.max_depth = -1;
388
389 strbuf_setlen(&base, 0);
390 strbuf_add(&base, ce->name, strlen(ce->name));
391
392 read_tree_at(istate->repo, tree, &base, &ps,
0243930a 393 add_path_to_index, &ctx);
4300f844
DS
394
395 /* free directory entries. full entries are re-used */
396 discard_cache_entry(ce);
397 }
398
399 /* Copy back into original index. */
400 memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
d9e9b44d 401 memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash));
ac8acb4f 402 istate->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
4300f844
DS
403 free(istate->cache);
404 istate->cache = full->cache;
405 istate->cache_nr = full->cache_nr;
406 istate->cache_alloc = full->cache_alloc;
f8fe49e5
DS
407 istate->fsmonitor_has_run_once = 0;
408 FREE_AND_NULL(istate->fsmonitor_dirty);
409 FREE_AND_NULL(istate->fsmonitor_last_update);
4300f844
DS
410
411 strbuf_release(&base);
412 free(full);
413
2de37c53
DS
414 /* Clear and recompute the cache-tree */
415 cache_tree_free(&istate->cache_tree);
416 cache_tree_update(istate, 0);
417
dce241b0
DS
418 trace2_region_leave("index", tr_region, istate->repo);
419}
420
421void ensure_full_index(struct index_state *istate)
422{
29fefafc
ÆAB
423 if (!istate)
424 BUG("ensure_full_index() must get an index!");
dce241b0 425 expand_index(istate, NULL);
3964fc2a 426}
71f82d03 427
b93fea08
VD
428void ensure_correct_sparsity(struct index_state *istate)
429{
430 /*
431 * If the index can be sparse, make it sparse. Otherwise,
432 * ensure the index is full.
433 */
434 if (is_sparse_index_allowed(istate, 0))
435 convert_to_sparse(istate, 0);
436 else
437 ensure_full_index(istate);
438}
439
d79d2993
EN
440static int path_found(const char *path, const char **dirname, size_t *dir_len,
441 int *dir_found)
442{
443 struct stat st;
444 char *newdir;
445 char *tmp;
446
447 /*
448 * If dirname corresponds to a directory that doesn't exist, and this
449 * path starts with dirname, then path can't exist.
450 */
451 if (!*dir_found && !memcmp(path, *dirname, *dir_len))
452 return 0;
453
454 /*
455 * If path itself exists, return 1.
456 */
457 if (!lstat(path, &st))
458 return 1;
459
460 /*
461 * Otherwise, path does not exist so we'll return 0...but we'll first
462 * determine some info about its parent directory so we can avoid
463 * lstat calls for future cache entries.
464 */
465 newdir = strrchr(path, '/');
466 if (!newdir)
467 return 0; /* Didn't find a parent dir; just return 0 now. */
468
469 /*
470 * If path starts with directory (which we already lstat'ed and found),
471 * then no need to lstat parent directory again.
472 */
473 if (*dir_found && *dirname && memcmp(path, *dirname, *dir_len))
474 return 0;
475
476 /* Free previous dirname, and cache path's dirname */
477 *dirname = path;
478 *dir_len = newdir - path + 1;
479
480 tmp = xstrndup(path, *dir_len);
481 *dir_found = !lstat(tmp, &st);
482 free(tmp);
483
484 return 0;
485}
486
af6a5187
EN
487void clear_skip_worktree_from_present_files(struct index_state *istate)
488{
d79d2993
EN
489 const char *last_dirname = NULL;
490 size_t dir_len = 0;
491 int dir_found = 1;
492
af6a5187 493 int i;
89aaab11
AL
494 int path_count[2] = {0, 0};
495 int restarted = 0;
d79d2993 496
ecc7c884
EN
497 if (!core_apply_sparse_checkout ||
498 sparse_expect_files_outside_of_patterns)
af6a5187
EN
499 return;
500
89aaab11
AL
501 trace2_region_enter("index", "clear_skip_worktree_from_present_files",
502 istate->repo);
af6a5187
EN
503restart:
504 for (i = 0; i < istate->cache_nr; i++) {
505 struct cache_entry *ce = istate->cache[i];
af6a5187 506
89aaab11
AL
507 if (ce_skip_worktree(ce)) {
508 path_count[restarted]++;
509 if (path_found(ce->name, &last_dirname, &dir_len, &dir_found)) {
510 if (S_ISSPARSEDIR(ce->ce_mode)) {
8c7abdc5
AL
511 if (restarted)
512 BUG("ensure-full-index did not fully flatten?");
89aaab11
AL
513 ensure_full_index(istate);
514 restarted = 1;
515 goto restart;
516 }
517 ce->ce_flags &= ~CE_SKIP_WORKTREE;
af6a5187 518 }
af6a5187
EN
519 }
520 }
89aaab11
AL
521
522 if (path_count[0])
523 trace2_data_intmax("index", istate->repo,
524 "sparse_path_count", path_count[0]);
525 if (restarted)
526 trace2_data_intmax("index", istate->repo,
527 "sparse_path_count_full", path_count[1]);
528 trace2_region_leave("index", "clear_skip_worktree_from_present_files",
529 istate->repo);
af6a5187
EN
530}
531
71f82d03
DS
532/*
533 * This static global helps avoid infinite recursion between
534 * expand_to_path() and index_file_exists().
535 */
536static int in_expand_to_path = 0;
537
538void expand_to_path(struct index_state *istate,
539 const char *path, size_t pathlen, int icase)
540{
541 struct strbuf path_mutable = STRBUF_INIT;
542 size_t substr_len;
543
544 /* prevent extra recursion */
545 if (in_expand_to_path)
546 return;
547
d2cdf2c2 548 if (!istate->sparse_index)
71f82d03
DS
549 return;
550
71f82d03
DS
551 in_expand_to_path = 1;
552
553 /*
554 * We only need to actually expand a region if the
555 * following are both true:
556 *
557 * 1. 'path' is not already in the index.
558 * 2. Some parent directory of 'path' is a sparse directory.
559 */
560
561 if (index_file_exists(istate, path, pathlen, icase))
562 goto cleanup;
563
564 strbuf_add(&path_mutable, path, pathlen);
565 strbuf_addch(&path_mutable, '/');
566
567 /* Check the name hash for all parent directories */
568 substr_len = 0;
569 while (substr_len < pathlen) {
570 char temp;
571 char *replace = strchr(path_mutable.buf + substr_len, '/');
572
573 if (!replace)
574 break;
575
576 /* replace the character _after_ the slash */
577 replace++;
578 temp = *replace;
579 *replace = '\0';
580 if (index_file_exists(istate, path_mutable.buf,
581 path_mutable.len, icase)) {
582 /*
583 * We found a parent directory in the name-hash
584 * hashtable, because only sparse directory entries
585 * have a trailing '/' character. Since "path" wasn't
586 * in the index, perhaps it exists within this
587 * sparse-directory. Expand accordingly.
588 */
589 ensure_full_index(istate);
590 break;
591 }
592
593 *replace = temp;
594 substr_len = replace - path_mutable.buf;
595 }
596
597cleanup:
598 strbuf_release(&path_mutable);
599 in_expand_to_path = 0;
600}