Line | Count | Source |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
3 | | |
4 | | #include "git-compat-util.h" |
5 | | #include "gettext.h" |
6 | | #include "hex.h" |
7 | | #include "lockfile.h" |
8 | | #include "tree.h" |
9 | | #include "tree-walk.h" |
10 | | #include "cache-tree.h" |
11 | | #include "object-file.h" |
12 | | #include "odb.h" |
13 | | #include "read-cache-ll.h" |
14 | | #include "replace-object.h" |
15 | | #include "repository.h" |
16 | | #include "promisor-remote.h" |
17 | | #include "trace.h" |
18 | | #include "trace2.h" |
19 | | |
20 | | #ifndef DEBUG_CACHE_TREE |
21 | | #define DEBUG_CACHE_TREE 0 |
22 | | #endif |
23 | | |
24 | | struct cache_tree *cache_tree(void) |
25 | 0 | { |
26 | 0 | struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree)); |
27 | 0 | it->entry_count = -1; |
28 | 0 | return it; |
29 | 0 | } |
30 | | |
31 | | void cache_tree_free(struct cache_tree **it_p) |
32 | 0 | { |
33 | 0 | int i; |
34 | 0 | struct cache_tree *it = *it_p; |
35 | |
|
36 | 0 | if (!it) |
37 | 0 | return; |
38 | 0 | for (i = 0; i < it->subtree_nr; i++) |
39 | 0 | if (it->down[i]) { |
40 | 0 | cache_tree_free(&it->down[i]->cache_tree); |
41 | 0 | free(it->down[i]); |
42 | 0 | } |
43 | 0 | free(it->down); |
44 | 0 | free(it); |
45 | 0 | *it_p = NULL; |
46 | 0 | } |
47 | | |
48 | | static int subtree_name_cmp(const char *one, int onelen, |
49 | | const char *two, int twolen) |
50 | 0 | { |
51 | 0 | if (onelen < twolen) |
52 | 0 | return -1; |
53 | 0 | if (twolen < onelen) |
54 | 0 | return 1; |
55 | 0 | return memcmp(one, two, onelen); |
56 | 0 | } |
57 | | |
58 | | int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen) |
59 | 0 | { |
60 | 0 | struct cache_tree_sub **down = it->down; |
61 | 0 | int lo, hi; |
62 | 0 | lo = 0; |
63 | 0 | hi = it->subtree_nr; |
64 | 0 | while (lo < hi) { |
65 | 0 | int mi = lo + (hi - lo) / 2; |
66 | 0 | struct cache_tree_sub *mdl = down[mi]; |
67 | 0 | int cmp = subtree_name_cmp(path, pathlen, |
68 | 0 | mdl->name, mdl->namelen); |
69 | 0 | if (!cmp) |
70 | 0 | return mi; |
71 | 0 | if (cmp < 0) |
72 | 0 | hi = mi; |
73 | 0 | else |
74 | 0 | lo = mi + 1; |
75 | 0 | } |
76 | 0 | return -lo-1; |
77 | 0 | } |
78 | | |
79 | | static struct cache_tree_sub *find_subtree(struct cache_tree *it, |
80 | | const char *path, |
81 | | int pathlen, |
82 | | int create) |
83 | 0 | { |
84 | 0 | struct cache_tree_sub *down; |
85 | 0 | int pos = cache_tree_subtree_pos(it, path, pathlen); |
86 | 0 | if (0 <= pos) |
87 | 0 | return it->down[pos]; |
88 | 0 | if (!create) |
89 | 0 | return NULL; |
90 | | |
91 | 0 | pos = -pos-1; |
92 | 0 | ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc); |
93 | 0 | it->subtree_nr++; |
94 | |
|
95 | 0 | FLEX_ALLOC_MEM(down, name, path, pathlen); |
96 | 0 | down->cache_tree = NULL; |
97 | 0 | down->namelen = pathlen; |
98 | |
|
99 | 0 | if (pos < it->subtree_nr) |
100 | 0 | MOVE_ARRAY(it->down + pos + 1, it->down + pos, |
101 | 0 | it->subtree_nr - pos - 1); |
102 | 0 | it->down[pos] = down; |
103 | 0 | return down; |
104 | 0 | } |
105 | | |
106 | | struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path) |
107 | 0 | { |
108 | 0 | int pathlen = strlen(path); |
109 | 0 | return find_subtree(it, path, pathlen, 1); |
110 | 0 | } |
111 | | |
112 | | static int do_invalidate_path(struct cache_tree *it, const char *path) |
113 | 0 | { |
114 | | /* a/b/c |
115 | | * ==> invalidate self |
116 | | * ==> find "a", have it invalidate "b/c" |
117 | | * a |
118 | | * ==> invalidate self |
119 | | * ==> if "a" exists as a subtree, remove it. |
120 | | */ |
121 | 0 | const char *slash; |
122 | 0 | int namelen; |
123 | 0 | struct cache_tree_sub *down; |
124 | |
|
125 | | #if DEBUG_CACHE_TREE |
126 | | fprintf(stderr, "cache-tree invalidate <%s>\n", path); |
127 | | #endif |
128 | |
|
129 | 0 | if (!it) |
130 | 0 | return 0; |
131 | 0 | slash = strchrnul(path, '/'); |
132 | 0 | namelen = slash - path; |
133 | 0 | it->entry_count = -1; |
134 | 0 | if (!*slash) { |
135 | 0 | int pos; |
136 | 0 | pos = cache_tree_subtree_pos(it, path, namelen); |
137 | 0 | if (0 <= pos) { |
138 | 0 | cache_tree_free(&it->down[pos]->cache_tree); |
139 | 0 | free(it->down[pos]); |
140 | | /* 0 1 2 3 4 5 |
141 | | * ^ ^subtree_nr = 6 |
142 | | * pos |
143 | | * move 4 and 5 up one place (2 entries) |
144 | | * 2 = 6 - 3 - 1 = subtree_nr - pos - 1 |
145 | | */ |
146 | 0 | MOVE_ARRAY(it->down + pos, it->down + pos + 1, |
147 | 0 | it->subtree_nr - pos - 1); |
148 | 0 | it->subtree_nr--; |
149 | 0 | } |
150 | 0 | return 1; |
151 | 0 | } |
152 | 0 | down = find_subtree(it, path, namelen, 0); |
153 | 0 | if (down) |
154 | 0 | do_invalidate_path(down->cache_tree, slash + 1); |
155 | 0 | return 1; |
156 | 0 | } |
157 | | |
158 | | void cache_tree_invalidate_path(struct index_state *istate, const char *path) |
159 | 0 | { |
160 | 0 | if (do_invalidate_path(istate->cache_tree, path)) |
161 | 0 | istate->cache_changed |= CACHE_TREE_CHANGED; |
162 | 0 | } |
163 | | |
164 | | static int verify_cache(struct index_state *istate, int flags) |
165 | 0 | { |
166 | 0 | unsigned i, funny; |
167 | 0 | int silent = flags & WRITE_TREE_SILENT; |
168 | | |
169 | | /* Verify that the tree is merged */ |
170 | 0 | funny = 0; |
171 | 0 | for (i = 0; i < istate->cache_nr; i++) { |
172 | 0 | const struct cache_entry *ce = istate->cache[i]; |
173 | 0 | if (ce_stage(ce)) { |
174 | 0 | if (silent) |
175 | 0 | return -1; |
176 | 0 | if (10 < ++funny) { |
177 | 0 | fprintf(stderr, "...\n"); |
178 | 0 | break; |
179 | 0 | } |
180 | 0 | fprintf(stderr, "%s: unmerged (%s)\n", |
181 | 0 | ce->name, oid_to_hex(&ce->oid)); |
182 | 0 | } |
183 | 0 | } |
184 | 0 | if (funny) |
185 | 0 | return -1; |
186 | | |
187 | | /* Also verify that the cache does not have path and path/file |
188 | | * at the same time. At this point we know the cache has only |
189 | | * stage 0 entries. |
190 | | */ |
191 | 0 | funny = 0; |
192 | 0 | for (i = 0; i + 1 < istate->cache_nr; i++) { |
193 | | /* path/file always comes after path because of the way |
194 | | * the cache is sorted. Also path can appear only once, |
195 | | * which means conflicting one would immediately follow. |
196 | | */ |
197 | 0 | const struct cache_entry *this_ce = istate->cache[i]; |
198 | 0 | const struct cache_entry *next_ce = istate->cache[i + 1]; |
199 | 0 | const char *this_name = this_ce->name; |
200 | 0 | const char *next_name = next_ce->name; |
201 | 0 | int this_len = ce_namelen(this_ce); |
202 | 0 | if (this_len < ce_namelen(next_ce) && |
203 | 0 | next_name[this_len] == '/' && |
204 | 0 | strncmp(this_name, next_name, this_len) == 0) { |
205 | 0 | if (10 < ++funny) { |
206 | 0 | fprintf(stderr, "...\n"); |
207 | 0 | break; |
208 | 0 | } |
209 | 0 | fprintf(stderr, "You have both %s and %s\n", |
210 | 0 | this_name, next_name); |
211 | 0 | } |
212 | 0 | } |
213 | 0 | if (funny) |
214 | 0 | return -1; |
215 | 0 | return 0; |
216 | 0 | } |
217 | | |
218 | | static void discard_unused_subtrees(struct cache_tree *it) |
219 | 0 | { |
220 | 0 | struct cache_tree_sub **down = it->down; |
221 | 0 | int nr = it->subtree_nr; |
222 | 0 | int dst, src; |
223 | 0 | for (dst = src = 0; src < nr; src++) { |
224 | 0 | struct cache_tree_sub *s = down[src]; |
225 | 0 | if (s->used) |
226 | 0 | down[dst++] = s; |
227 | 0 | else { |
228 | 0 | cache_tree_free(&s->cache_tree); |
229 | 0 | free(s); |
230 | 0 | it->subtree_nr--; |
231 | 0 | } |
232 | 0 | } |
233 | 0 | } |
234 | | |
235 | | int cache_tree_fully_valid(struct cache_tree *it) |
236 | 0 | { |
237 | 0 | int i; |
238 | 0 | if (!it) |
239 | 0 | return 0; |
240 | 0 | if (it->entry_count < 0 || |
241 | 0 | odb_has_object(the_repository->objects, &it->oid, |
242 | 0 | HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) |
243 | 0 | return 0; |
244 | 0 | for (i = 0; i < it->subtree_nr; i++) { |
245 | 0 | if (!cache_tree_fully_valid(it->down[i]->cache_tree)) |
246 | 0 | return 0; |
247 | 0 | } |
248 | 0 | return 1; |
249 | 0 | } |
250 | | |
251 | | static int must_check_existence(const struct cache_entry *ce) |
252 | 0 | { |
253 | 0 | return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce)); |
254 | 0 | } |
255 | | |
256 | | static int update_one(struct cache_tree *it, |
257 | | struct cache_entry **cache, |
258 | | int entries, |
259 | | const char *base, |
260 | | int baselen, |
261 | | int *skip_count, |
262 | | int flags) |
263 | 0 | { |
264 | 0 | struct strbuf buffer; |
265 | 0 | int missing_ok = flags & WRITE_TREE_MISSING_OK; |
266 | 0 | int dryrun = flags & WRITE_TREE_DRY_RUN; |
267 | 0 | int repair = flags & WRITE_TREE_REPAIR; |
268 | 0 | int to_invalidate = 0; |
269 | 0 | int i; |
270 | |
|
271 | 0 | assert(!(dryrun && repair)); |
272 | |
|
273 | 0 | *skip_count = 0; |
274 | | |
275 | | /* |
276 | | * If the first entry of this region is a sparse directory |
277 | | * entry corresponding exactly to 'base', then this cache_tree |
278 | | * struct is a "leaf" in the data structure, pointing to the |
279 | | * tree OID specified in the entry. |
280 | | */ |
281 | 0 | if (entries > 0) { |
282 | 0 | const struct cache_entry *ce = cache[0]; |
283 | |
|
284 | 0 | if (S_ISSPARSEDIR(ce->ce_mode) && |
285 | 0 | ce->ce_namelen == baselen && |
286 | 0 | !strncmp(ce->name, base, baselen)) { |
287 | 0 | it->entry_count = 1; |
288 | 0 | oidcpy(&it->oid, &ce->oid); |
289 | 0 | return 1; |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | 0 | if (0 <= it->entry_count && |
294 | 0 | odb_has_object(the_repository->objects, &it->oid, |
295 | 0 | HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) |
296 | 0 | return it->entry_count; |
297 | | |
298 | | /* |
299 | | * We first scan for subtrees and update them; we start by |
300 | | * marking existing subtrees -- the ones that are unmarked |
301 | | * should not be in the result. |
302 | | */ |
303 | 0 | for (i = 0; i < it->subtree_nr; i++) |
304 | 0 | it->down[i]->used = 0; |
305 | | |
306 | | /* |
307 | | * Find the subtrees and update them. |
308 | | */ |
309 | 0 | i = 0; |
310 | 0 | while (i < entries) { |
311 | 0 | const struct cache_entry *ce = cache[i]; |
312 | 0 | struct cache_tree_sub *sub; |
313 | 0 | const char *path, *slash; |
314 | 0 | int pathlen, sublen, subcnt, subskip; |
315 | |
|
316 | 0 | path = ce->name; |
317 | 0 | pathlen = ce_namelen(ce); |
318 | 0 | if (pathlen <= baselen || memcmp(base, path, baselen)) |
319 | 0 | break; /* at the end of this level */ |
320 | | |
321 | 0 | slash = strchr(path + baselen, '/'); |
322 | 0 | if (!slash) { |
323 | 0 | i++; |
324 | 0 | continue; |
325 | 0 | } |
326 | | /* |
327 | | * a/bbb/c (base = a/, slash = /c) |
328 | | * ==> |
329 | | * path+baselen = bbb/c, sublen = 3 |
330 | | */ |
331 | 0 | sublen = slash - (path + baselen); |
332 | 0 | sub = find_subtree(it, path + baselen, sublen, 1); |
333 | 0 | if (!sub->cache_tree) |
334 | 0 | sub->cache_tree = cache_tree(); |
335 | 0 | subcnt = update_one(sub->cache_tree, |
336 | 0 | cache + i, entries - i, |
337 | 0 | path, |
338 | 0 | baselen + sublen + 1, |
339 | 0 | &subskip, |
340 | 0 | flags); |
341 | 0 | if (subcnt < 0) |
342 | 0 | return subcnt; |
343 | 0 | if (!subcnt) |
344 | 0 | die("index cache-tree records empty sub-tree"); |
345 | 0 | i += subcnt; |
346 | 0 | sub->count = subcnt; /* to be used in the next loop */ |
347 | 0 | *skip_count += subskip; |
348 | 0 | sub->used = 1; |
349 | 0 | } |
350 | | |
351 | 0 | discard_unused_subtrees(it); |
352 | | |
353 | | /* |
354 | | * Then write out the tree object for this level. |
355 | | */ |
356 | 0 | strbuf_init(&buffer, 8192); |
357 | |
|
358 | 0 | i = 0; |
359 | 0 | while (i < entries) { |
360 | 0 | const struct cache_entry *ce = cache[i]; |
361 | 0 | struct cache_tree_sub *sub = NULL; |
362 | 0 | const char *path, *slash; |
363 | 0 | int pathlen, entlen; |
364 | 0 | const struct object_id *oid; |
365 | 0 | unsigned mode; |
366 | 0 | int expected_missing = 0; |
367 | 0 | int contains_ita = 0; |
368 | 0 | int ce_missing_ok; |
369 | |
|
370 | 0 | path = ce->name; |
371 | 0 | pathlen = ce_namelen(ce); |
372 | 0 | if (pathlen <= baselen || memcmp(base, path, baselen)) |
373 | 0 | break; /* at the end of this level */ |
374 | | |
375 | 0 | slash = strchr(path + baselen, '/'); |
376 | 0 | if (slash) { |
377 | 0 | entlen = slash - (path + baselen); |
378 | 0 | sub = find_subtree(it, path + baselen, entlen, 0); |
379 | 0 | if (!sub) |
380 | 0 | die("cache-tree.c: '%.*s' in '%s' not found", |
381 | 0 | entlen, path + baselen, path); |
382 | 0 | i += sub->count; |
383 | 0 | oid = &sub->cache_tree->oid; |
384 | 0 | mode = S_IFDIR; |
385 | 0 | contains_ita = sub->cache_tree->entry_count < 0; |
386 | 0 | if (contains_ita) { |
387 | 0 | to_invalidate = 1; |
388 | 0 | expected_missing = 1; |
389 | 0 | } |
390 | 0 | } |
391 | 0 | else { |
392 | 0 | oid = &ce->oid; |
393 | 0 | mode = ce->ce_mode; |
394 | 0 | entlen = pathlen - baselen; |
395 | 0 | i++; |
396 | 0 | } |
397 | | |
398 | 0 | ce_missing_ok = mode == S_IFGITLINK || missing_ok || |
399 | 0 | !must_check_existence(ce); |
400 | 0 | if (is_null_oid(oid) || |
401 | 0 | (!ce_missing_ok && |
402 | 0 | !odb_has_object(the_repository->objects, oid, |
403 | 0 | HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))) { |
404 | 0 | strbuf_release(&buffer); |
405 | 0 | if (expected_missing) |
406 | 0 | return -1; |
407 | 0 | return error("invalid object %06o %s for '%.*s'", |
408 | 0 | mode, oid_to_hex(oid), entlen+baselen, path); |
409 | 0 | } |
410 | | |
411 | | /* |
412 | | * CE_REMOVE entries are removed before the index is |
413 | | * written to disk. Skip them to remain consistent |
414 | | * with the future on-disk index. |
415 | | */ |
416 | 0 | if (ce->ce_flags & CE_REMOVE) { |
417 | 0 | *skip_count = *skip_count + 1; |
418 | 0 | continue; |
419 | 0 | } |
420 | | |
421 | | /* |
422 | | * CE_INTENT_TO_ADD entries exist in on-disk index but |
423 | | * they are not part of generated trees. Invalidate up |
424 | | * to root to force cache-tree users to read elsewhere. |
425 | | */ |
426 | 0 | if (!sub && ce_intent_to_add(ce)) { |
427 | 0 | to_invalidate = 1; |
428 | 0 | continue; |
429 | 0 | } |
430 | | |
431 | | /* |
432 | | * "sub" can be an empty tree if all subentries are i-t-a. |
433 | | */ |
434 | 0 | if (contains_ita && is_empty_tree_oid(oid, the_repository->hash_algo)) |
435 | 0 | continue; |
436 | | |
437 | 0 | strbuf_grow(&buffer, entlen + 100); |
438 | 0 | strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); |
439 | 0 | strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz); |
440 | |
|
441 | | #if DEBUG_CACHE_TREE |
442 | | fprintf(stderr, "cache-tree update-one %o %.*s\n", |
443 | | mode, entlen, path + baselen); |
444 | | #endif |
445 | 0 | } |
446 | | |
447 | 0 | if (repair) { |
448 | 0 | struct object_id oid; |
449 | 0 | hash_object_file(the_hash_algo, buffer.buf, buffer.len, |
450 | 0 | OBJ_TREE, &oid); |
451 | 0 | if (odb_has_object(the_repository->objects, &oid, HAS_OBJECT_RECHECK_PACKED)) |
452 | 0 | oidcpy(&it->oid, &oid); |
453 | 0 | else |
454 | 0 | to_invalidate = 1; |
455 | 0 | } else if (dryrun) { |
456 | 0 | hash_object_file(the_hash_algo, buffer.buf, buffer.len, |
457 | 0 | OBJ_TREE, &it->oid); |
458 | 0 | } else if (odb_write_object_ext(the_repository->objects, buffer.buf, buffer.len, OBJ_TREE, |
459 | 0 | &it->oid, NULL, flags & WRITE_TREE_SILENT ? WRITE_OBJECT_SILENT : 0)) { |
460 | 0 | strbuf_release(&buffer); |
461 | 0 | return -1; |
462 | 0 | } |
463 | | |
464 | 0 | strbuf_release(&buffer); |
465 | 0 | it->entry_count = to_invalidate ? -1 : i - *skip_count; |
466 | | #if DEBUG_CACHE_TREE |
467 | | fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", |
468 | | it->entry_count, it->subtree_nr, |
469 | | oid_to_hex(&it->oid)); |
470 | | #endif |
471 | 0 | return i; |
472 | 0 | } |
473 | | |
474 | | int cache_tree_update(struct index_state *istate, int flags) |
475 | 0 | { |
476 | 0 | struct odb_transaction *transaction; |
477 | 0 | int skip, i; |
478 | |
|
479 | 0 | i = verify_cache(istate, flags); |
480 | |
|
481 | 0 | if (i) |
482 | 0 | return i; |
483 | | |
484 | 0 | if (!istate->cache_tree) |
485 | 0 | istate->cache_tree = cache_tree(); |
486 | |
|
487 | 0 | if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository)) |
488 | 0 | prefetch_cache_entries(istate, must_check_existence); |
489 | |
|
490 | 0 | trace_performance_enter(); |
491 | 0 | trace2_region_enter("cache_tree", "update", the_repository); |
492 | 0 | transaction = odb_transaction_begin(the_repository->objects); |
493 | 0 | i = update_one(istate->cache_tree, istate->cache, istate->cache_nr, |
494 | 0 | "", 0, &skip, flags); |
495 | 0 | odb_transaction_commit(transaction); |
496 | 0 | trace2_region_leave("cache_tree", "update", the_repository); |
497 | 0 | trace_performance_leave("cache_tree_update"); |
498 | 0 | if (i < 0) |
499 | 0 | return i; |
500 | 0 | istate->cache_changed |= CACHE_TREE_CHANGED; |
501 | 0 | return 0; |
502 | 0 | } |
503 | | |
504 | | static void write_one(struct strbuf *buffer, struct cache_tree *it, |
505 | | const char *path, int pathlen) |
506 | 0 | { |
507 | 0 | int i; |
508 | | |
509 | | /* One "cache-tree" entry consists of the following: |
510 | | * path (NUL terminated) |
511 | | * entry_count, subtree_nr ("%d %d\n") |
512 | | * tree-sha1 (missing if invalid) |
513 | | * subtree_nr "cache-tree" entries for subtrees. |
514 | | */ |
515 | 0 | strbuf_grow(buffer, pathlen + 100); |
516 | 0 | strbuf_add(buffer, path, pathlen); |
517 | 0 | strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr); |
518 | |
|
519 | | #if DEBUG_CACHE_TREE |
520 | | if (0 <= it->entry_count) |
521 | | fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n", |
522 | | pathlen, path, it->entry_count, it->subtree_nr, |
523 | | oid_to_hex(&it->oid)); |
524 | | else |
525 | | fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n", |
526 | | pathlen, path, it->subtree_nr); |
527 | | #endif |
528 | |
|
529 | 0 | if (0 <= it->entry_count) { |
530 | 0 | strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz); |
531 | 0 | } |
532 | 0 | for (i = 0; i < it->subtree_nr; i++) { |
533 | 0 | struct cache_tree_sub *down = it->down[i]; |
534 | 0 | if (i) { |
535 | 0 | struct cache_tree_sub *prev = it->down[i-1]; |
536 | 0 | if (subtree_name_cmp(down->name, down->namelen, |
537 | 0 | prev->name, prev->namelen) <= 0) |
538 | 0 | die("fatal - unsorted cache subtree"); |
539 | 0 | } |
540 | 0 | write_one(buffer, down->cache_tree, down->name, down->namelen); |
541 | 0 | } |
542 | 0 | } |
543 | | |
544 | | void cache_tree_write(struct strbuf *sb, struct cache_tree *root) |
545 | 0 | { |
546 | 0 | trace2_region_enter("cache_tree", "write", the_repository); |
547 | 0 | write_one(sb, root, "", 0); |
548 | 0 | trace2_region_leave("cache_tree", "write", the_repository); |
549 | 0 | } |
550 | | |
551 | | static int parse_int(const char **ptr, unsigned long *len_p, int *out) |
552 | 0 | { |
553 | 0 | const char *s = *ptr; |
554 | 0 | unsigned long len = *len_p; |
555 | 0 | int ret = 0; |
556 | 0 | int sign = 1; |
557 | |
|
558 | 0 | while (len && *s == '-') { |
559 | 0 | sign *= -1; |
560 | 0 | s++; |
561 | 0 | len--; |
562 | 0 | } |
563 | |
|
564 | 0 | while (len) { |
565 | 0 | if (!isdigit(*s)) |
566 | 0 | break; |
567 | 0 | ret *= 10; |
568 | 0 | ret += *s - '0'; |
569 | 0 | s++; |
570 | 0 | len--; |
571 | 0 | } |
572 | |
|
573 | 0 | if (s == *ptr) |
574 | 0 | return -1; |
575 | | |
576 | 0 | *ptr = s; |
577 | 0 | *len_p = len; |
578 | 0 | *out = sign * ret; |
579 | 0 | return 0; |
580 | 0 | } |
581 | | |
582 | | static struct cache_tree *read_one(const char **buffer, unsigned long *size_p) |
583 | 0 | { |
584 | 0 | const char *buf = *buffer; |
585 | 0 | unsigned long size = *size_p; |
586 | 0 | struct cache_tree *it; |
587 | 0 | int i, subtree_nr; |
588 | 0 | const unsigned rawsz = the_hash_algo->rawsz; |
589 | |
|
590 | 0 | it = NULL; |
591 | | /* skip name, but make sure name exists */ |
592 | 0 | while (size && *buf) { |
593 | 0 | size--; |
594 | 0 | buf++; |
595 | 0 | } |
596 | 0 | if (!size) |
597 | 0 | goto free_return; |
598 | 0 | buf++; size--; |
599 | 0 | it = cache_tree(); |
600 | |
|
601 | 0 | if (parse_int(&buf, &size, &it->entry_count) < 0) |
602 | 0 | goto free_return; |
603 | 0 | if (!size || *buf != ' ') |
604 | 0 | goto free_return; |
605 | 0 | buf++; size--; |
606 | 0 | if (parse_int(&buf, &size, &subtree_nr) < 0) |
607 | 0 | goto free_return; |
608 | 0 | if (!size || *buf != '\n') |
609 | 0 | goto free_return; |
610 | 0 | buf++; size--; |
611 | 0 | if (0 <= it->entry_count) { |
612 | 0 | if (size < rawsz) |
613 | 0 | goto free_return; |
614 | 0 | oidread(&it->oid, (const unsigned char *)buf, |
615 | 0 | the_repository->hash_algo); |
616 | 0 | buf += rawsz; |
617 | 0 | size -= rawsz; |
618 | 0 | } |
619 | | |
620 | | #if DEBUG_CACHE_TREE |
621 | | if (0 <= it->entry_count) |
622 | | fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n", |
623 | | *buffer, it->entry_count, subtree_nr, |
624 | | oid_to_hex(&it->oid)); |
625 | | else |
626 | | fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n", |
627 | | *buffer, subtree_nr); |
628 | | #endif |
629 | | |
630 | | /* |
631 | | * Just a heuristic -- we do not add directories that often but |
632 | | * we do not want to have to extend it immediately when we do, |
633 | | * hence +2. |
634 | | */ |
635 | 0 | it->subtree_alloc = subtree_nr + 2; |
636 | 0 | CALLOC_ARRAY(it->down, it->subtree_alloc); |
637 | 0 | for (i = 0; i < subtree_nr; i++) { |
638 | | /* read each subtree */ |
639 | 0 | struct cache_tree *sub; |
640 | 0 | struct cache_tree_sub *subtree; |
641 | 0 | const char *name = buf; |
642 | |
|
643 | 0 | sub = read_one(&buf, &size); |
644 | 0 | if (!sub) |
645 | 0 | goto free_return; |
646 | 0 | subtree = cache_tree_sub(it, name); |
647 | 0 | subtree->cache_tree = sub; |
648 | 0 | } |
649 | 0 | if (subtree_nr != it->subtree_nr) |
650 | 0 | die("cache-tree: internal error"); |
651 | 0 | *buffer = buf; |
652 | 0 | *size_p = size; |
653 | 0 | return it; |
654 | | |
655 | 0 | free_return: |
656 | 0 | cache_tree_free(&it); |
657 | 0 | return NULL; |
658 | 0 | } |
659 | | |
660 | | struct cache_tree *cache_tree_read(const char *buffer, unsigned long size) |
661 | 0 | { |
662 | 0 | struct cache_tree *result; |
663 | |
|
664 | 0 | if (buffer[0]) |
665 | 0 | return NULL; /* not the whole tree */ |
666 | | |
667 | 0 | trace2_region_enter("cache_tree", "read", the_repository); |
668 | 0 | result = read_one(&buffer, &size); |
669 | 0 | trace2_region_leave("cache_tree", "read", the_repository); |
670 | |
|
671 | 0 | return result; |
672 | 0 | } |
673 | | |
674 | | static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path) |
675 | 0 | { |
676 | 0 | if (!it) |
677 | 0 | return NULL; |
678 | 0 | while (*path) { |
679 | 0 | const char *slash; |
680 | 0 | struct cache_tree_sub *sub; |
681 | |
|
682 | 0 | slash = strchrnul(path, '/'); |
683 | | /* |
684 | | * Between path and slash is the name of the subtree |
685 | | * to look for. |
686 | | */ |
687 | 0 | sub = find_subtree(it, path, slash - path, 0); |
688 | 0 | if (!sub) |
689 | 0 | return NULL; |
690 | 0 | it = sub->cache_tree; |
691 | |
|
692 | 0 | path = slash; |
693 | 0 | while (*path == '/') |
694 | 0 | path++; |
695 | 0 | } |
696 | 0 | return it; |
697 | 0 | } |
698 | | |
699 | | static int write_index_as_tree_internal(struct object_id *oid, |
700 | | struct index_state *index_state, |
701 | | int cache_tree_valid, |
702 | | int flags, |
703 | | const char *prefix) |
704 | 0 | { |
705 | 0 | if (flags & WRITE_TREE_IGNORE_CACHE_TREE) { |
706 | 0 | cache_tree_free(&index_state->cache_tree); |
707 | 0 | cache_tree_valid = 0; |
708 | 0 | } |
709 | |
|
710 | 0 | if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0) |
711 | 0 | return WRITE_TREE_UNMERGED_INDEX; |
712 | | |
713 | 0 | if (prefix) { |
714 | 0 | struct cache_tree *subtree; |
715 | 0 | subtree = cache_tree_find(index_state->cache_tree, prefix); |
716 | 0 | if (!subtree) |
717 | 0 | return WRITE_TREE_PREFIX_ERROR; |
718 | 0 | oidcpy(oid, &subtree->oid); |
719 | 0 | } |
720 | 0 | else |
721 | 0 | oidcpy(oid, &index_state->cache_tree->oid); |
722 | | |
723 | 0 | return 0; |
724 | 0 | } |
725 | | |
726 | 0 | struct tree* write_in_core_index_as_tree(struct repository *repo) { |
727 | 0 | struct object_id o; |
728 | 0 | int was_valid, ret; |
729 | |
|
730 | 0 | struct index_state *index_state = repo->index; |
731 | 0 | was_valid = index_state->cache_tree && |
732 | 0 | cache_tree_fully_valid(index_state->cache_tree); |
733 | |
|
734 | 0 | ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL); |
735 | 0 | if (ret == WRITE_TREE_UNMERGED_INDEX) { |
736 | 0 | int i; |
737 | 0 | bug("there are unmerged index entries:"); |
738 | 0 | for (i = 0; i < index_state->cache_nr; i++) { |
739 | 0 | const struct cache_entry *ce = index_state->cache[i]; |
740 | 0 | if (ce_stage(ce)) |
741 | 0 | bug("%d %.*s", ce_stage(ce), |
742 | 0 | (int)ce_namelen(ce), ce->name); |
743 | 0 | } |
744 | 0 | BUG("unmerged index entries when writing in-core index"); |
745 | 0 | } |
746 | | |
747 | 0 | return lookup_tree(repo, &index_state->cache_tree->oid); |
748 | 0 | } |
749 | | |
750 | | |
751 | | int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix) |
752 | 0 | { |
753 | 0 | int entries, was_valid; |
754 | 0 | struct lock_file lock_file = LOCK_INIT; |
755 | 0 | int ret; |
756 | |
|
757 | 0 | hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR); |
758 | |
|
759 | 0 | entries = read_index_from(index_state, index_path, |
760 | 0 | repo_get_git_dir(the_repository)); |
761 | 0 | if (entries < 0) { |
762 | 0 | ret = WRITE_TREE_UNREADABLE_INDEX; |
763 | 0 | goto out; |
764 | 0 | } |
765 | | |
766 | 0 | was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) && |
767 | 0 | index_state->cache_tree && |
768 | 0 | cache_tree_fully_valid(index_state->cache_tree); |
769 | |
|
770 | 0 | ret = write_index_as_tree_internal(oid, index_state, was_valid, flags, |
771 | 0 | prefix); |
772 | 0 | if (!ret && !was_valid) { |
773 | 0 | write_locked_index(index_state, &lock_file, COMMIT_LOCK); |
774 | | /* Not being able to write is fine -- we are only interested |
775 | | * in updating the cache-tree part, and if the next caller |
776 | | * ends up using the old index with unupdated cache-tree part |
777 | | * it misses the work we did here, but that is just a |
778 | | * performance penalty and not a big deal. |
779 | | */ |
780 | 0 | } |
781 | |
|
782 | 0 | out: |
783 | 0 | rollback_lock_file(&lock_file); |
784 | 0 | return ret; |
785 | 0 | } |
786 | | |
787 | | static void prime_cache_tree_sparse_dir(struct cache_tree *it, |
788 | | struct tree *tree) |
789 | 0 | { |
790 | |
|
791 | 0 | oidcpy(&it->oid, &tree->object.oid); |
792 | 0 | it->entry_count = 1; |
793 | 0 | } |
794 | | |
795 | | static void prime_cache_tree_rec(struct repository *r, |
796 | | struct cache_tree *it, |
797 | | struct tree *tree, |
798 | | struct strbuf *tree_path) |
799 | 0 | { |
800 | 0 | struct tree_desc desc; |
801 | 0 | struct name_entry entry; |
802 | 0 | int cnt; |
803 | 0 | size_t base_path_len = tree_path->len; |
804 | |
|
805 | 0 | oidcpy(&it->oid, &tree->object.oid); |
806 | |
|
807 | 0 | init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size); |
808 | 0 | cnt = 0; |
809 | 0 | while (tree_entry(&desc, &entry)) { |
810 | 0 | if (!S_ISDIR(entry.mode)) |
811 | 0 | cnt++; |
812 | 0 | else { |
813 | 0 | struct cache_tree_sub *sub; |
814 | 0 | struct tree *subtree = lookup_tree(r, &entry.oid); |
815 | |
|
816 | 0 | if (parse_tree(subtree) < 0) |
817 | 0 | exit(128); |
818 | 0 | sub = cache_tree_sub(it, entry.path); |
819 | 0 | sub->cache_tree = cache_tree(); |
820 | | |
821 | | /* |
822 | | * Recursively-constructed subtree path is only needed when working |
823 | | * in a sparse index (where it's used to determine whether the |
824 | | * subtree is a sparse directory in the index). |
825 | | */ |
826 | 0 | if (r->index->sparse_index) { |
827 | 0 | strbuf_setlen(tree_path, base_path_len); |
828 | 0 | strbuf_add(tree_path, entry.path, entry.pathlen); |
829 | 0 | strbuf_addch(tree_path, '/'); |
830 | 0 | } |
831 | | |
832 | | /* |
833 | | * If a sparse index is in use, the directory being processed may be |
834 | | * sparse. To confirm that, we can check whether an entry with that |
835 | | * exact name exists in the index. If it does, the created subtree |
836 | | * should be sparse. Otherwise, cache tree expansion should continue |
837 | | * as normal. |
838 | | */ |
839 | 0 | if (r->index->sparse_index && |
840 | 0 | index_entry_exists(r->index, tree_path->buf, tree_path->len)) |
841 | 0 | prime_cache_tree_sparse_dir(sub->cache_tree, subtree); |
842 | 0 | else |
843 | 0 | prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path); |
844 | 0 | cnt += sub->cache_tree->entry_count; |
845 | 0 | } |
846 | 0 | } |
847 | | |
848 | 0 | it->entry_count = cnt; |
849 | 0 | } |
850 | | |
851 | | void prime_cache_tree(struct repository *r, |
852 | | struct index_state *istate, |
853 | | struct tree *tree) |
854 | 0 | { |
855 | 0 | struct strbuf tree_path = STRBUF_INIT; |
856 | |
|
857 | 0 | trace2_region_enter("cache-tree", "prime_cache_tree", r); |
858 | 0 | cache_tree_free(&istate->cache_tree); |
859 | 0 | istate->cache_tree = cache_tree(); |
860 | |
|
861 | 0 | prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path); |
862 | 0 | strbuf_release(&tree_path); |
863 | 0 | istate->cache_changed |= CACHE_TREE_CHANGED; |
864 | 0 | trace2_region_leave("cache-tree", "prime_cache_tree", r); |
865 | 0 | } |
866 | | |
867 | | /* |
868 | | * find the cache_tree that corresponds to the current level without |
869 | | * exploding the full path into textual form. The root of the |
870 | | * cache tree is given as "root", and our current level is "info". |
871 | | * (1) When at root level, info->prev is NULL, so it is "root" itself. |
872 | | * (2) Otherwise, find the cache_tree that corresponds to one level |
873 | | * above us, and find ourselves in there. |
874 | | */ |
875 | | static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root, |
876 | | struct traverse_info *info) |
877 | 0 | { |
878 | 0 | struct cache_tree *our_parent; |
879 | |
|
880 | 0 | if (!info->prev) |
881 | 0 | return root; |
882 | 0 | our_parent = find_cache_tree_from_traversal(root, info->prev); |
883 | 0 | return cache_tree_find(our_parent, info->name); |
884 | 0 | } |
885 | | |
886 | | int cache_tree_matches_traversal(struct cache_tree *root, |
887 | | struct name_entry *ent, |
888 | | struct traverse_info *info) |
889 | 0 | { |
890 | 0 | struct cache_tree *it; |
891 | |
|
892 | 0 | it = find_cache_tree_from_traversal(root, info); |
893 | 0 | it = cache_tree_find(it, ent->path); |
894 | 0 | if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid)) |
895 | 0 | return it->entry_count; |
896 | 0 | return 0; |
897 | 0 | } |
898 | | |
899 | | static int verify_one_sparse(struct index_state *istate, |
900 | | struct strbuf *path, |
901 | | int pos) |
902 | 0 | { |
903 | 0 | struct cache_entry *ce = istate->cache[pos]; |
904 | 0 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
905 | 0 | return error(_("directory '%s' is present in index, but not sparse"), |
906 | 0 | path->buf); |
907 | 0 | return 0; |
908 | 0 | } |
909 | | |
910 | | /* |
911 | | * Returns: |
912 | | * 0 - Verification completed. |
913 | | * 1 - Restart verification - a call to ensure_full_index() freed the cache |
914 | | * tree that is being verified and verification needs to be restarted from |
915 | | * the new toplevel cache tree. |
916 | | * -1 - Verification failed. |
917 | | */ |
918 | | static int verify_one(struct repository *r, |
919 | | struct index_state *istate, |
920 | | struct cache_tree *it, |
921 | | struct strbuf *path) |
922 | 0 | { |
923 | 0 | int i, pos, len = path->len; |
924 | 0 | struct strbuf tree_buf = STRBUF_INIT; |
925 | 0 | struct object_id new_oid; |
926 | 0 | int ret; |
927 | |
|
928 | 0 | for (i = 0; i < it->subtree_nr; i++) { |
929 | 0 | strbuf_addf(path, "%s/", it->down[i]->name); |
930 | 0 | ret = verify_one(r, istate, it->down[i]->cache_tree, path); |
931 | 0 | if (ret) |
932 | 0 | goto out; |
933 | | |
934 | 0 | strbuf_setlen(path, len); |
935 | 0 | } |
936 | | |
937 | 0 | if (it->entry_count < 0 || |
938 | | /* no verification on tests (t7003) that replace trees */ |
939 | 0 | lookup_replace_object(r, &it->oid) != &it->oid) { |
940 | 0 | ret = 0; |
941 | 0 | goto out; |
942 | 0 | } |
943 | | |
944 | 0 | if (path->len) { |
945 | | /* |
946 | | * If the index is sparse and the cache tree is not |
947 | | * index_name_pos() may trigger ensure_full_index() which will |
948 | | * free the tree that is being verified. |
949 | | */ |
950 | 0 | int is_sparse = istate->sparse_index; |
951 | 0 | pos = index_name_pos(istate, path->buf, path->len); |
952 | 0 | if (is_sparse && !istate->sparse_index) { |
953 | 0 | ret = 1; |
954 | 0 | goto out; |
955 | 0 | } |
956 | | |
957 | 0 | if (pos >= 0) { |
958 | 0 | ret = verify_one_sparse(istate, path, pos); |
959 | 0 | goto out; |
960 | 0 | } |
961 | | |
962 | 0 | pos = -pos - 1; |
963 | 0 | } else { |
964 | 0 | pos = 0; |
965 | 0 | } |
966 | | |
967 | 0 | if (it->entry_count + pos > istate->cache_nr) { |
968 | 0 | ret = error(_("corrupted cache-tree has entries not present in index")); |
969 | 0 | goto out; |
970 | 0 | } |
971 | | |
972 | 0 | i = 0; |
973 | 0 | while (i < it->entry_count) { |
974 | 0 | struct cache_entry *ce = istate->cache[pos + i]; |
975 | 0 | const char *slash; |
976 | 0 | struct cache_tree_sub *sub = NULL; |
977 | 0 | const struct object_id *oid; |
978 | 0 | const char *name; |
979 | 0 | unsigned mode; |
980 | 0 | int entlen; |
981 | |
|
982 | 0 | if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) { |
983 | 0 | ret = error(_("%s with flags 0x%x should not be in cache-tree"), |
984 | 0 | ce->name, ce->ce_flags); |
985 | 0 | goto out; |
986 | 0 | } |
987 | | |
988 | 0 | name = ce->name + path->len; |
989 | 0 | slash = strchr(name, '/'); |
990 | 0 | if (slash) { |
991 | 0 | entlen = slash - name; |
992 | |
|
993 | 0 | sub = find_subtree(it, ce->name + path->len, entlen, 0); |
994 | 0 | if (!sub || sub->cache_tree->entry_count < 0) { |
995 | 0 | ret = error(_("bad subtree '%.*s'"), entlen, name); |
996 | 0 | goto out; |
997 | 0 | } |
998 | | |
999 | 0 | oid = &sub->cache_tree->oid; |
1000 | 0 | mode = S_IFDIR; |
1001 | 0 | i += sub->cache_tree->entry_count; |
1002 | 0 | } else { |
1003 | 0 | oid = &ce->oid; |
1004 | 0 | mode = ce->ce_mode; |
1005 | 0 | entlen = ce_namelen(ce) - path->len; |
1006 | 0 | i++; |
1007 | 0 | } |
1008 | 0 | strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0'); |
1009 | 0 | strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz); |
1010 | 0 | } |
1011 | | |
1012 | 0 | hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE, |
1013 | 0 | &new_oid); |
1014 | |
|
1015 | 0 | if (!oideq(&new_oid, &it->oid)) { |
1016 | 0 | ret = error(_("cache-tree for path %.*s does not match. " |
1017 | 0 | "Expected %s got %s"), len, path->buf, |
1018 | 0 | oid_to_hex(&new_oid), oid_to_hex(&it->oid)); |
1019 | 0 | goto out; |
1020 | 0 | } |
1021 | | |
1022 | 0 | ret = 0; |
1023 | 0 | out: |
1024 | 0 | strbuf_setlen(path, len); |
1025 | 0 | strbuf_release(&tree_buf); |
1026 | 0 | return ret; |
1027 | 0 | } |
1028 | | |
1029 | | int cache_tree_verify(struct repository *r, struct index_state *istate) |
1030 | 0 | { |
1031 | 0 | struct strbuf path = STRBUF_INIT; |
1032 | 0 | int ret; |
1033 | |
|
1034 | 0 | if (!istate->cache_tree) { |
1035 | 0 | ret = 0; |
1036 | 0 | goto out; |
1037 | 0 | } |
1038 | | |
1039 | 0 | ret = verify_one(r, istate, istate->cache_tree, &path); |
1040 | 0 | if (ret < 0) |
1041 | 0 | goto out; |
1042 | 0 | if (ret > 0) { |
1043 | 0 | strbuf_reset(&path); |
1044 | |
|
1045 | 0 | ret = verify_one(r, istate, istate->cache_tree, &path); |
1046 | 0 | if (ret < 0) |
1047 | 0 | goto out; |
1048 | 0 | if (ret > 0) |
1049 | 0 | BUG("ensure_full_index() called twice while verifying cache tree"); |
1050 | 0 | } |
1051 | | |
1052 | 0 | ret = 0; |
1053 | |
|
1054 | 0 | out: |
1055 | 0 | strbuf_release(&path); |
1056 | 0 | return ret; |
1057 | 0 | } |