]> git.ipfire.org Git - thirdparty/git.git/blame - sparse-index.c
reftable: signal overflow
[thirdparty/git.git] / sparse-index.c
CommitLineData
3964fc2a
DS
1#include "cache.h"
2#include "repository.h"
3#include "sparse-index.h"
4300f844
DS
4#include "tree.h"
5#include "pathspec.h"
6#include "trace2.h"
6e773527
DS
7#include "cache-tree.h"
8#include "config.h"
9#include "dir.h"
10#include "fsmonitor.h"
11
12static struct cache_entry *construct_sparse_dir_entry(
13 struct index_state *istate,
14 const char *sparse_dir,
15 struct cache_tree *tree)
16{
17 struct cache_entry *de;
18
19 de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0);
20
21 de->ce_flags |= CE_SKIP_WORKTREE;
22 return de;
23}
24
25/*
26 * Returns the number of entries "inserted" into the index.
27 */
28static int convert_to_sparse_rec(struct index_state *istate,
29 int num_converted,
30 int start, int end,
31 const char *ct_path, size_t ct_pathlen,
32 struct cache_tree *ct)
33{
34 int i, can_convert = 1;
35 int start_converted = num_converted;
6e773527 36 struct strbuf child_path = STRBUF_INIT;
6e773527
DS
37
38 /*
39 * Is the current path outside of the sparse cone?
40 * Then check if the region can be replaced by a sparse
41 * directory entry (everything is sparse and merged).
42 */
02155c8c 43 if (path_in_sparse_checkout(ct_path, istate))
6e773527
DS
44 can_convert = 0;
45
46 for (i = start; can_convert && i < end; i++) {
47 struct cache_entry *ce = istate->cache[i];
48
49 if (ce_stage(ce) ||
f442313e 50 S_ISGITLINK(ce->ce_mode) ||
6e773527
DS
51 !(ce->ce_flags & CE_SKIP_WORKTREE))
52 can_convert = 0;
53 }
54
55 if (can_convert) {
56 struct cache_entry *se;
57 se = construct_sparse_dir_entry(istate, ct_path, ct);
58
59 istate->cache[num_converted++] = se;
60 return 1;
61 }
62
63 for (i = start; i < end; ) {
64 int count, span, pos = -1;
65 const char *base, *slash;
66 struct cache_entry *ce = istate->cache[i];
67
68 /*
69 * Detect if this is a normal entry outside of any subtree
70 * entry.
71 */
72 base = ce->name + ct_pathlen;
73 slash = strchr(base, '/');
74
75 if (slash)
76 pos = cache_tree_subtree_pos(ct, base, slash - base);
77
78 if (pos < 0) {
79 istate->cache[num_converted++] = ce;
80 i++;
81 continue;
82 }
83
84 strbuf_setlen(&child_path, 0);
85 strbuf_add(&child_path, ce->name, slash - ce->name + 1);
86
87 span = ct->down[pos]->cache_tree->entry_count;
88 count = convert_to_sparse_rec(istate,
89 num_converted, i, i + span,
90 child_path.buf, child_path.len,
91 ct->down[pos]->cache_tree);
92 num_converted += count;
93 i += span;
94 }
95
96 strbuf_release(&child_path);
97 return num_converted - start_converted;
98}
99
b79f9c07 100int set_sparse_index_config(struct repository *repo, int enable)
58300f47 101{
122ba1f7
DS
102 int res;
103 char *config_path = repo_git_path(repo, "config.worktree");
104 res = git_config_set_in_file_gently(config_path,
105 "index.sparse",
106 enable ? "true" : NULL);
107 free(config_path);
58300f47 108
122ba1f7
DS
109 prepare_repo_settings(repo);
110 repo->settings.sparse_index = enable;
111 return res;
58300f47
DS
112}
113
fc6609d1
DS
114static int index_has_unmerged_entries(struct index_state *istate)
115{
116 int i;
117 for (i = 0; i < istate->cache_nr; i++) {
118 if (ce_stage(istate->cache[i]))
119 return 1;
120 }
121
122 return 0;
123}
124
b93fea08 125static int is_sparse_index_allowed(struct index_state *istate, int flags)
6e773527 126{
b93fea08 127 if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
6e773527
DS
128 return 0;
129
58300f47
DS
130 if (!istate->repo)
131 istate->repo = the_repository;
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 */
139 if (istate->split_index)
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 */
180 if (istate->sparse_index || !istate->cache_nr ||
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
6e773527
DS
221 istate->sparse_index = 1;
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{
238 struct index_state *istate = (struct index_state *)context;
239 struct cache_entry *ce;
240 size_t len = base->len;
241
242 if (S_ISDIR(mode))
243 return READ_TREE_RECURSIVE;
244
245 strbuf_addstr(base, path);
246
247 ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
47410778 248 ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED;
4300f844
DS
249 set_index_entry(istate, istate->cache_nr++, ce);
250
251 strbuf_setlen(base, len);
252 return 0;
253}
3964fc2a
DS
254
255void ensure_full_index(struct index_state *istate)
256{
4300f844
DS
257 int i;
258 struct index_state *full;
259 struct strbuf base = STRBUF_INIT;
260
261 if (!istate || !istate->sparse_index)
262 return;
263
264 if (!istate->repo)
265 istate->repo = the_repository;
266
267 trace2_region_enter("index", "ensure_full_index", istate->repo);
268
269 /* initialize basics of new index */
270 full = xcalloc(1, sizeof(struct index_state));
271 memcpy(full, istate, sizeof(struct index_state));
272
273 /* then change the necessary things */
274 full->sparse_index = 0;
275 full->cache_alloc = (3 * istate->cache_alloc) / 2;
276 full->cache_nr = 0;
277 ALLOC_ARRAY(full->cache, full->cache_alloc);
278
279 for (i = 0; i < istate->cache_nr; i++) {
280 struct cache_entry *ce = istate->cache[i];
281 struct tree *tree;
282 struct pathspec ps;
283
284 if (!S_ISSPARSEDIR(ce->ce_mode)) {
285 set_index_entry(full, full->cache_nr++, ce);
286 continue;
287 }
288 if (!(ce->ce_flags & CE_SKIP_WORKTREE))
289 warning(_("index entry is a directory, but not sparse (%08x)"),
290 ce->ce_flags);
291
292 /* recursively walk into cd->name */
293 tree = lookup_tree(istate->repo, &ce->oid);
294
295 memset(&ps, 0, sizeof(ps));
296 ps.recursive = 1;
297 ps.has_wildcard = 1;
298 ps.max_depth = -1;
299
300 strbuf_setlen(&base, 0);
301 strbuf_add(&base, ce->name, strlen(ce->name));
302
303 read_tree_at(istate->repo, tree, &base, &ps,
304 add_path_to_index, full);
305
306 /* free directory entries. full entries are re-used */
307 discard_cache_entry(ce);
308 }
309
310 /* Copy back into original index. */
311 memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
d9e9b44d 312 memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash));
4300f844
DS
313 istate->sparse_index = 0;
314 free(istate->cache);
315 istate->cache = full->cache;
316 istate->cache_nr = full->cache_nr;
317 istate->cache_alloc = full->cache_alloc;
f8fe49e5
DS
318 istate->fsmonitor_has_run_once = 0;
319 FREE_AND_NULL(istate->fsmonitor_dirty);
320 FREE_AND_NULL(istate->fsmonitor_last_update);
4300f844
DS
321
322 strbuf_release(&base);
323 free(full);
324
2de37c53
DS
325 /* Clear and recompute the cache-tree */
326 cache_tree_free(&istate->cache_tree);
327 cache_tree_update(istate, 0);
328
4300f844 329 trace2_region_leave("index", "ensure_full_index", istate->repo);
3964fc2a 330}
71f82d03 331
b93fea08
VD
332void ensure_correct_sparsity(struct index_state *istate)
333{
334 /*
335 * If the index can be sparse, make it sparse. Otherwise,
336 * ensure the index is full.
337 */
338 if (is_sparse_index_allowed(istate, 0))
339 convert_to_sparse(istate, 0);
340 else
341 ensure_full_index(istate);
342}
343
71f82d03
DS
344/*
345 * This static global helps avoid infinite recursion between
346 * expand_to_path() and index_file_exists().
347 */
348static int in_expand_to_path = 0;
349
350void expand_to_path(struct index_state *istate,
351 const char *path, size_t pathlen, int icase)
352{
353 struct strbuf path_mutable = STRBUF_INIT;
354 size_t substr_len;
355
356 /* prevent extra recursion */
357 if (in_expand_to_path)
358 return;
359
360 if (!istate || !istate->sparse_index)
361 return;
362
363 if (!istate->repo)
364 istate->repo = the_repository;
365
366 in_expand_to_path = 1;
367
368 /*
369 * We only need to actually expand a region if the
370 * following are both true:
371 *
372 * 1. 'path' is not already in the index.
373 * 2. Some parent directory of 'path' is a sparse directory.
374 */
375
376 if (index_file_exists(istate, path, pathlen, icase))
377 goto cleanup;
378
379 strbuf_add(&path_mutable, path, pathlen);
380 strbuf_addch(&path_mutable, '/');
381
382 /* Check the name hash for all parent directories */
383 substr_len = 0;
384 while (substr_len < pathlen) {
385 char temp;
386 char *replace = strchr(path_mutable.buf + substr_len, '/');
387
388 if (!replace)
389 break;
390
391 /* replace the character _after_ the slash */
392 replace++;
393 temp = *replace;
394 *replace = '\0';
395 if (index_file_exists(istate, path_mutable.buf,
396 path_mutable.len, icase)) {
397 /*
398 * We found a parent directory in the name-hash
399 * hashtable, because only sparse directory entries
400 * have a trailing '/' character. Since "path" wasn't
401 * in the index, perhaps it exists within this
402 * sparse-directory. Expand accordingly.
403 */
404 ensure_full_index(istate);
405 break;
406 }
407
408 *replace = temp;
409 substr_len = replace - path_mutable.buf;
410 }
411
412cleanup:
413 strbuf_release(&path_mutable);
414 in_expand_to_path = 0;
415}