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