🌐 AI搜索 & 代理 主页
blob: 65ca99336136f1f50695f06c6b404859b13cc0a3 [file] [log] [blame]
Junio C Hamano74986462006-04-23 23:52:201#include "cache.h"
Michael Haggerty697cc8e2014-10-01 10:28:422#include "lockfile.h"
Junio C Hamano74986462006-04-23 23:52:203#include "tree.h"
Junio C Hamanob9d37a52009-04-20 10:58:184#include "tree-walk.h"
Junio C Hamano74986462006-04-23 23:52:205#include "cache-tree.h"
Stefan Bellercbd53a22018-05-15 23:42:156#include "object-store.h"
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:287#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 13:40:318#include "promisor-remote.h"
Derrick Stolee6e773522021-03-30 13:10:559#include "sparse-index.h"
Junio C Hamano74986462006-04-23 23:52:2010
Jeff Hostetlere9b9cc52019-06-19 21:05:5811#ifndef DEBUG_CACHE_TREE
12#define DEBUG_CACHE_TREE 0
Junio C Hamano49031612006-10-30 23:29:5313#endif
Junio C Hamano74986462006-04-23 23:52:2014
15struct cache_tree *cache_tree(void)
16{
17 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
18 it->entry_count = -1;
19 return it;
20}
21
Junio C Hamanobad68ec2006-04-25 04:18:5822void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 23:52:2023{
24 int i;
Junio C Hamanobad68ec2006-04-25 04:18:5825 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 23:52:2026
27 if (!it)
28 return;
29 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 21:40:1630 if (it->down[i]) {
Junio C Hamano61fa3092006-04-26 00:40:0231 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 21:40:1632 free(it->down[i]);
33 }
Junio C Hamano74986462006-04-23 23:52:2034 free(it->down);
35 free(it);
Junio C Hamanobad68ec2006-04-25 04:18:5836 *it_p = NULL;
Junio C Hamano74986462006-04-23 23:52:2037}
38
Junio C Hamano61fa3092006-04-26 00:40:0239static int subtree_name_cmp(const char *one, int onelen,
40 const char *two, int twolen)
41{
42 if (onelen < twolen)
43 return -1;
44 if (twolen < onelen)
45 return 1;
46 return memcmp(one, two, onelen);
47}
48
Derrick Stoleec80dd392021-01-23 19:58:1349int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
Junio C Hamano61fa3092006-04-26 00:40:0250{
51 struct cache_tree_sub **down = it->down;
52 int lo, hi;
53 lo = 0;
54 hi = it->subtree_nr;
55 while (lo < hi) {
Derrick Stolee19716b22017-10-08 18:29:3756 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-26 00:40:0257 struct cache_tree_sub *mdl = down[mi];
58 int cmp = subtree_name_cmp(path, pathlen,
59 mdl->name, mdl->namelen);
60 if (!cmp)
61 return mi;
62 if (cmp < 0)
63 hi = mi;
64 else
65 lo = mi + 1;
66 }
67 return -lo-1;
68}
69
Junio C Hamano74986462006-04-23 23:52:2070static struct cache_tree_sub *find_subtree(struct cache_tree *it,
71 const char *path,
72 int pathlen,
73 int create)
74{
Junio C Hamano74986462006-04-23 23:52:2075 struct cache_tree_sub *down;
Derrick Stoleec80dd392021-01-23 19:58:1376 int pos = cache_tree_subtree_pos(it, path, pathlen);
Junio C Hamano61fa3092006-04-26 00:40:0277 if (0 <= pos)
78 return it->down[pos];
Junio C Hamano74986462006-04-23 23:52:2079 if (!create)
80 return NULL;
Junio C Hamano61fa3092006-04-26 00:40:0281
82 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-03 22:31:5183 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-26 00:40:0284 it->subtree_nr++;
85
Jeff King96ffc062016-02-22 22:44:3286 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-26 00:40:0287 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 23:52:2088 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-26 00:40:0289
90 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 17:50:0991 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
92 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-26 00:40:0293 it->down[pos] = down;
Junio C Hamano74986462006-04-23 23:52:2094 return down;
95}
96
Junio C Hamano7927a552006-04-27 08:33:0797struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
98{
99 int pathlen = strlen(path);
100 return find_subtree(it, path, pathlen, 1);
101}
102
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31103static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 23:52:20104{
105 /* a/b/c
106 * ==> invalidate self
107 * ==> find "a", have it invalidate "b/c"
108 * a
109 * ==> invalidate self
110 * ==> if "a" exists as a subtree, remove it.
111 */
112 const char *slash;
113 int namelen;
114 struct cache_tree_sub *down;
115
Jeff Hostetlere9b9cc52019-06-19 21:05:58116#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 23:10:45117 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
118#endif
119
Junio C Hamano74986462006-04-23 23:52:20120 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31121 return 0;
Rohit Mani2c5495f2014-03-08 06:48:31122 slash = strchrnul(path, '/');
123 namelen = slash - path;
Junio C Hamano74986462006-04-23 23:52:20124 it->entry_count = -1;
Rohit Mani2c5495f2014-03-08 06:48:31125 if (!*slash) {
Junio C Hamano61fa3092006-04-26 00:40:02126 int pos;
Derrick Stoleec80dd392021-01-23 19:58:13127 pos = cache_tree_subtree_pos(it, path, namelen);
Junio C Hamano61fa3092006-04-26 00:40:02128 if (0 <= pos) {
129 cache_tree_free(&it->down[pos]->cache_tree);
130 free(it->down[pos]);
Junio C Hamano74986462006-04-23 23:52:20131 /* 0 1 2 3 4 5
132 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-26 00:40:02133 * pos
Junio C Hamano74986462006-04-23 23:52:20134 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-26 00:40:02135 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 23:52:20136 */
René Scharfef331ab92017-07-15 20:00:45137 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
138 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 23:52:20139 it->subtree_nr--;
140 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31141 return 1;
Junio C Hamano74986462006-04-23 23:52:20142 }
Junio C Hamano74986462006-04-23 23:52:20143 down = find_subtree(it, path, namelen, 0);
144 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31145 do_invalidate_path(down->cache_tree, slash + 1);
146 return 1;
147}
148
149void cache_tree_invalidate_path(struct index_state *istate, const char *path)
150{
151 if (do_invalidate_path(istate->cache_tree, path))
152 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 23:52:20153}
154
Derrick Stolee8d87e332021-01-23 19:58:12155static int verify_cache(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 23:52:20156{
Derrick Stolee8d87e332021-01-23 19:58:12157 unsigned i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46158 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 23:52:20159
160 /* Verify that the tree is merged */
161 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12162 for (i = 0; i < istate->cache_nr; i++) {
163 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano3f6d56d2012-02-07 19:55:48164 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 17:43:37165 if (silent)
166 return -1;
Junio C Hamano74986462006-04-23 23:52:20167 if (10 < ++funny) {
168 fprintf(stderr, "...\n");
169 break;
170 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 04:15:25171 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52172 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 23:52:20173 }
174 }
175 if (funny)
176 return -1;
177
178 /* Also verify that the cache does not have path and path/file
179 * at the same time. At this point we know the cache has only
180 * stage 0 entries.
181 */
182 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12183 for (i = 0; i + 1 < istate->cache_nr; i++) {
Junio C Hamano74986462006-04-23 23:52:20184 /* path/file always comes after path because of the way
185 * the cache is sorted. Also path can appear only once,
186 * which means conflicting one would immediately follow.
187 */
Derrick Stolee8d87e332021-01-23 19:58:12188 const struct cache_entry *this_ce = istate->cache[i];
189 const struct cache_entry *next_ce = istate->cache[i + 1];
René Scharfe0b725362021-01-07 16:32:10190 const char *this_name = this_ce->name;
191 const char *next_name = next_ce->name;
192 int this_len = ce_namelen(this_ce);
193 if (this_len < ce_namelen(next_ce) &&
Derrick Stoleea4b6d202021-01-07 16:32:11194 next_name[this_len] == '/' &&
195 strncmp(this_name, next_name, this_len) == 0) {
Junio C Hamano74986462006-04-23 23:52:20196 if (10 < ++funny) {
197 fprintf(stderr, "...\n");
198 break;
199 }
200 fprintf(stderr, "You have both %s and %s\n",
201 this_name, next_name);
202 }
203 }
204 if (funny)
205 return -1;
206 return 0;
207}
208
209static void discard_unused_subtrees(struct cache_tree *it)
210{
211 struct cache_tree_sub **down = it->down;
212 int nr = it->subtree_nr;
213 int dst, src;
214 for (dst = src = 0; src < nr; src++) {
215 struct cache_tree_sub *s = down[src];
216 if (s->used)
217 down[dst++] = s;
218 else {
Junio C Hamanobad68ec2006-04-25 04:18:58219 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 23:52:20220 free(s);
221 it->subtree_nr--;
222 }
223 }
224}
225
Junio C Hamanobad68ec2006-04-25 04:18:58226int cache_tree_fully_valid(struct cache_tree *it)
227{
228 int i;
229 if (!it)
230 return 0;
Jeff King98374a02019-01-07 08:37:54231 if (it->entry_count < 0 || !has_object_file(&it->oid))
Junio C Hamanobad68ec2006-04-25 04:18:58232 return 0;
233 for (i = 0; i < it->subtree_nr; i++) {
234 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
235 return 0;
236 }
237 return 1;
238}
239
Jonathan Tand3da2232021-07-23 18:52:23240static int must_check_existence(const struct cache_entry *ce)
241{
242 return !(has_promisor_remote() && ce_skip_worktree(ce));
243}
244
Junio C Hamano74986462006-04-23 23:52:20245static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32246 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 23:52:20247 int entries,
248 const char *base,
249 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27250 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46251 int flags)
Junio C Hamano74986462006-04-23 23:52:20252{
Pierre Habouzit5242bcb2007-09-06 11:20:11253 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46254 int missing_ok = flags & WRITE_TREE_MISSING_OK;
255 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-06 04:06:56256 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28257 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 23:52:20258 int i;
259
David Turneraecf5672014-07-06 04:06:56260 assert(!(dryrun && repair));
261
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27262 *skip_count = 0;
263
Derrick Stolee2de37c52021-03-30 13:11:02264 /*
265 * If the first entry of this region is a sparse directory
266 * entry corresponding exactly to 'base', then this cache_tree
267 * struct is a "leaf" in the data structure, pointing to the
268 * tree OID specified in the entry.
269 */
270 if (entries > 0) {
271 const struct cache_entry *ce = cache[0];
272
273 if (S_ISSPARSEDIR(ce->ce_mode) &&
274 ce->ce_namelen == baselen &&
275 !strncmp(ce->name, base, baselen)) {
276 it->entry_count = 1;
277 oidcpy(&it->oid, &ce->oid);
278 return 1;
279 }
280 }
281
Jeff King98374a02019-01-07 08:37:54282 if (0 <= it->entry_count && has_object_file(&it->oid))
Junio C Hamano74986462006-04-23 23:52:20283 return it->entry_count;
284
285 /*
286 * We first scan for subtrees and update them; we start by
287 * marking existing subtrees -- the ones that are unmarked
288 * should not be in the result.
289 */
290 for (i = 0; i < it->subtree_nr; i++)
291 it->down[i]->used = 0;
292
293 /*
294 * Find the subtrees and update them.
295 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26296 i = 0;
297 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00298 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 23:52:20299 struct cache_tree_sub *sub;
300 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27301 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 23:52:20302
303 path = ce->name;
304 pathlen = ce_namelen(ce);
305 if (pathlen <= baselen || memcmp(base, path, baselen))
306 break; /* at the end of this level */
307
308 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26309 if (!slash) {
310 i++;
Junio C Hamano74986462006-04-23 23:52:20311 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26312 }
Junio C Hamano74986462006-04-23 23:52:20313 /*
314 * a/bbb/c (base = a/, slash = /c)
315 * ==>
316 * path+baselen = bbb/c, sublen = 3
317 */
318 sublen = slash - (path + baselen);
319 sub = find_subtree(it, path + baselen, sublen, 1);
320 if (!sub->cache_tree)
321 sub->cache_tree = cache_tree();
322 subcnt = update_one(sub->cache_tree,
323 cache + i, entries - i,
324 path,
325 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27326 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46327 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00328 if (subcnt < 0)
329 return subcnt;
Jeff King729dbbd2014-10-29 17:11:58330 if (!subcnt)
331 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26332 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27333 sub->count = subcnt; /* to be used in the next loop */
334 *skip_count += subskip;
Junio C Hamano74986462006-04-23 23:52:20335 sub->used = 1;
336 }
337
338 discard_unused_subtrees(it);
339
340 /*
341 * Then write out the tree object for this level.
342 */
Pierre Habouzitf1696ee2007-09-10 10:35:04343 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 23:52:20344
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26345 i = 0;
346 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00347 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 05:06:26348 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 23:52:20349 const char *path, *slash;
350 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24351 const struct object_id *oid;
Junio C Hamano74986462006-04-23 23:52:20352 unsigned mode;
Junio C Hamano4ed115e2014-09-02 21:16:20353 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27354 int contains_ita = 0;
Jonathan Tan2f215ff2018-10-09 18:40:37355 int ce_missing_ok;
Junio C Hamano74986462006-04-23 23:52:20356
357 path = ce->name;
358 pathlen = ce_namelen(ce);
359 if (pathlen <= baselen || memcmp(base, path, baselen))
360 break; /* at the end of this level */
361
362 slash = strchr(path + baselen, '/');
363 if (slash) {
364 entlen = slash - (path + baselen);
365 sub = find_subtree(it, path + baselen, entlen, 0);
366 if (!sub)
367 die("cache-tree.c: '%.*s' in '%s' not found",
368 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27369 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24370 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 23:52:20371 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27372 contains_ita = sub->cache_tree->entry_count < 0;
373 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28374 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 21:16:20375 expected_missing = 1;
376 }
Junio C Hamano74986462006-04-23 23:52:20377 }
378 else {
brian m. carlson6dcb4622018-03-12 02:27:24379 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-15 00:03:17380 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 23:52:20381 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26382 i++;
Junio C Hamano74986462006-04-23 23:52:20383 }
Jeff Kinga96d3cc2017-04-21 18:46:17384
Jonathan Tan2f215ff2018-10-09 18:40:37385 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
Jonathan Tand3da2232021-07-23 18:52:23386 !must_check_existence(ce);
brian m. carlson6dcb4622018-03-12 02:27:24387 if (is_null_oid(oid) ||
Jonathan Tan2f215ff2018-10-09 18:40:37388 (!ce_missing_ok && !has_object_file(oid))) {
Jonathan Niederb6b56ac2010-08-10 03:32:11389 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 21:16:20390 if (expected_missing)
391 return -1;
Linus Torvaldsa3883732009-07-14 18:25:17392 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24393 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-10 03:32:11394 }
Junio C Hamano74986462006-04-23 23:52:20395
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27396 /*
397 * CE_REMOVE entries are removed before the index is
398 * written to disk. Skip them to remain consistent
399 * with the future on-disk index.
400 */
401 if (ce->ce_flags & CE_REMOVE) {
402 *skip_count = *skip_count + 1;
403 continue;
404 }
405
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28406 /*
407 * CE_INTENT_TO_ADD entries exist on on-disk index but
408 * they are not part of generated trees. Invalidate up
409 * to root to force cache-tree users to read elsewhere.
410 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 05:06:26411 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28412 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27413 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28414 }
Junio C Hamano74986462006-04-23 23:52:20415
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27416 /*
417 * "sub" can be an empty tree if all subentries are i-t-a.
418 */
brian m. carlsona0554932018-05-02 00:26:04419 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27420 continue;
421
Pierre Habouzit5242bcb2007-09-06 11:20:11422 strbuf_grow(&buffer, entlen + 100);
423 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24424 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 23:52:20425
Jeff Hostetlere9b9cc52019-06-19 21:05:58426#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 23:10:45427 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 23:52:20428 mode, entlen, path + baselen);
429#endif
430 }
431
David Turneraecf5672014-07-06 04:06:56432 if (repair) {
Patryk Obaraf070fac2018-01-28 00:13:13433 struct object_id oid;
Matheus Tavares2dcde202020-01-30 20:32:22434 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
435 tree_type, &oid);
Jonathan Tanf981ec12019-09-03 19:42:47436 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
Patryk Obaraf070fac2018-01-28 00:13:13437 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-06 04:06:56438 else
439 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 00:13:19440 } else if (dryrun) {
Matheus Tavares2dcde202020-01-30 20:32:22441 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
442 tree_type, &it->oid);
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 14:30:49443 } else if (write_object_file_flags(buffer.buf, buffer.len, tree_type,
444 &it->oid, flags & WRITE_TREE_SILENT
445 ? HASH_SILENT : 0)) {
Junio C Hamanoedae5f02008-04-23 16:47:17446 strbuf_release(&buffer);
447 return -1;
448 }
449
Pierre Habouzit5242bcb2007-09-06 11:20:11450 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28451 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Jeff Hostetlere9b9cc52019-06-19 21:05:58452#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 23:10:45453 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 23:52:20454 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56455 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 23:52:20456#endif
457 return i;
458}
459
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32460int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 23:52:20461{
Derrick Stoleefb088262021-01-23 19:58:11462 int skip, i;
463
Derrick Stolee8d87e332021-01-23 19:58:12464 i = verify_cache(istate, flags);
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32465
Junio C Hamano74986462006-04-23 23:52:20466 if (i)
467 return i;
Derrick Stoleefb088262021-01-23 19:58:11468
469 if (!istate->cache_tree)
470 istate->cache_tree = cache_tree();
471
Jonathan Tand3da2232021-07-23 18:52:23472 if (!(flags & WRITE_TREE_MISSING_OK) && has_promisor_remote())
473 prefetch_cache_entries(istate, must_check_existence);
474
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 14:41:23475 trace_performance_enter();
Derrick Stoleefa7ca5d2021-01-04 03:09:12476 trace2_region_enter("cache_tree", "update", the_repository);
Derrick Stoleefb088262021-01-23 19:58:11477 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
478 "", 0, &skip, flags);
Derrick Stoleefa7ca5d2021-01-04 03:09:12479 trace2_region_leave("cache_tree", "update", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 14:41:23480 trace_performance_leave("cache_tree_update");
Junio C Hamano74986462006-04-23 23:52:20481 if (i < 0)
482 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32483 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 23:52:20484 return 0;
485}
486
Pierre Habouzit1dffb8f2007-09-25 08:22:44487static void write_one(struct strbuf *buffer, struct cache_tree *it,
Nguyễn Thái Ngọc Duyec36c422018-12-06 15:42:06488 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 23:52:20489{
490 int i;
491
492 /* One "cache-tree" entry consists of the following:
493 * path (NUL terminated)
494 * entry_count, subtree_nr ("%d %d\n")
495 * tree-sha1 (missing if invalid)
496 * subtree_nr "cache-tree" entries for subtrees.
497 */
Pierre Habouzit5242bcb2007-09-06 11:20:11498 strbuf_grow(buffer, pathlen + 100);
499 strbuf_add(buffer, path, pathlen);
500 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 23:52:20501
Jeff Hostetlere9b9cc52019-06-19 21:05:58502#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 23:52:20503 if (0 <= it->entry_count)
504 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
505 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56506 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 23:52:20507 else
508 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
509 pathlen, path, it->subtree_nr);
510#endif
511
512 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24513 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 23:52:20514 }
515 for (i = 0; i < it->subtree_nr; i++) {
516 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-26 00:40:02517 if (i) {
518 struct cache_tree_sub *prev = it->down[i-1];
519 if (subtree_name_cmp(down->name, down->namelen,
520 prev->name, prev->namelen) <= 0)
521 die("fatal - unsorted cache subtree");
522 }
Pierre Habouzit1dffb8f2007-09-25 08:22:44523 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 23:52:20524 }
Junio C Hamano74986462006-04-23 23:52:20525}
526
Pierre Habouzit1dffb8f2007-09-25 08:22:44527void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 23:52:20528{
Derrick Stolee4c3e1872021-01-04 03:09:13529 trace2_region_enter("cache_tree", "write", the_repository);
Pierre Habouzit1dffb8f2007-09-25 08:22:44530 write_one(sb, root, "", 0);
Derrick Stolee4c3e1872021-01-04 03:09:13531 trace2_region_leave("cache_tree", "write", the_repository);
Junio C Hamano74986462006-04-23 23:52:20532}
533
534static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
535{
536 const char *buf = *buffer;
537 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 01:31:02538 const char *cp;
539 char *ep;
Junio C Hamano74986462006-04-23 23:52:20540 struct cache_tree *it;
541 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24542 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 23:52:20543
544 it = NULL;
545 /* skip name, but make sure name exists */
546 while (size && *buf) {
547 size--;
548 buf++;
549 }
550 if (!size)
551 goto free_return;
552 buf++; size--;
553 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 01:31:02554
555 cp = buf;
556 it->entry_count = strtol(cp, &ep, 10);
557 if (cp == ep)
558 goto free_return;
559 cp = ep;
560 subtree_nr = strtol(cp, &ep, 10);
561 if (cp == ep)
Junio C Hamano74986462006-04-23 23:52:20562 goto free_return;
563 while (size && *buf && *buf != '\n') {
564 size--;
565 buf++;
566 }
567 if (!size)
568 goto free_return;
569 buf++; size--;
570 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24571 if (size < rawsz)
Junio C Hamano74986462006-04-23 23:52:20572 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29573 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24574 buf += rawsz;
575 size -= rawsz;
Junio C Hamano74986462006-04-23 23:52:20576 }
577
Jeff Hostetlere9b9cc52019-06-19 21:05:58578#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 23:52:20579 if (0 <= it->entry_count)
580 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
581 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56582 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 23:52:20583 else
584 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
585 *buffer, subtree_nr);
586#endif
587
588 /*
589 * Just a heuristic -- we do not add directories that often but
590 * we do not want to have to extend it immediately when we do,
591 * hence +2.
592 */
593 it->subtree_alloc = subtree_nr + 2;
René Scharfeca56dad2021-03-13 16:17:22594 CALLOC_ARRAY(it->down, it->subtree_alloc);
Junio C Hamano74986462006-04-23 23:52:20595 for (i = 0; i < subtree_nr; i++) {
596 /* read each subtree */
597 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-26 00:40:02598 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 23:52:20599 const char *name = buf;
Junio C Hamano7927a552006-04-27 08:33:07600
Junio C Hamano74986462006-04-23 23:52:20601 sub = read_one(&buf, &size);
602 if (!sub)
603 goto free_return;
Junio C Hamano7927a552006-04-27 08:33:07604 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-26 00:40:02605 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 23:52:20606 }
607 if (subtree_nr != it->subtree_nr)
608 die("cache-tree: internal error");
609 *buffer = buf;
610 *size_p = size;
611 return it;
612
613 free_return:
Junio C Hamanobad68ec2006-04-25 04:18:58614 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 23:52:20615 return NULL;
616}
617
Junio C Hamanobad68ec2006-04-25 04:18:58618struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 23:52:20619{
Derrick Stolee4c3e1872021-01-04 03:09:13620 struct cache_tree *result;
621
Junio C Hamanobad68ec2006-04-25 04:18:58622 if (buffer[0])
Junio C Hamano74986462006-04-23 23:52:20623 return NULL; /* not the whole tree */
Derrick Stolee4c3e1872021-01-04 03:09:13624
625 trace2_region_enter("cache_tree", "read", the_repository);
626 result = read_one(&buffer, &size);
627 trace2_region_leave("cache_tree", "read", the_repository);
628
629 return result;
Junio C Hamano74986462006-04-23 23:52:20630}
Junio C Hamano6bd20352006-04-26 08:20:50631
Nanako Shiraishi7ba04d92008-07-16 10:42:10632static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 08:20:50633{
Junio C Hamanob87fc962009-05-20 22:53:57634 if (!it)
635 return NULL;
Junio C Hamano6bd20352006-04-26 08:20:50636 while (*path) {
637 const char *slash;
638 struct cache_tree_sub *sub;
639
Michael Haggerty17e22dd2014-03-05 17:26:26640 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 17:26:27641 /*
642 * Between path and slash is the name of the subtree
643 * to look for.
Junio C Hamano6bd20352006-04-26 08:20:50644 */
645 sub = find_subtree(it, path, slash - path, 0);
646 if (!sub)
647 return NULL;
648 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 17:26:30649
Junio C Hamano6bd20352006-04-26 08:20:50650 path = slash;
Michael Haggerty34910472014-03-05 17:26:30651 while (*path == '/')
652 path++;
Junio C Hamano6bd20352006-04-26 08:20:50653 }
654 return it;
655}
Junio C Hamano45525bd2008-01-11 06:49:35656
Elijah Newren724dd762019-08-17 18:41:32657static int write_index_as_tree_internal(struct object_id *oid,
658 struct index_state *index_state,
659 int cache_tree_valid,
660 int flags,
661 const char *prefix)
662{
663 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
664 cache_tree_free(&index_state->cache_tree);
665 cache_tree_valid = 0;
666 }
667
Elijah Newren724dd762019-08-17 18:41:32668 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
669 return WRITE_TREE_UNMERGED_INDEX;
670
671 if (prefix) {
672 struct cache_tree *subtree;
673 subtree = cache_tree_find(index_state->cache_tree, prefix);
674 if (!subtree)
675 return WRITE_TREE_PREFIX_ERROR;
676 oidcpy(oid, &subtree->oid);
677 }
678 else
679 oidcpy(oid, &index_state->cache_tree->oid);
680
681 return 0;
682}
683
684struct tree* write_in_core_index_as_tree(struct repository *repo) {
685 struct object_id o;
686 int was_valid, ret;
687
688 struct index_state *index_state = repo->index;
689 was_valid = index_state->cache_tree &&
690 cache_tree_fully_valid(index_state->cache_tree);
691
692 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
693 if (ret == WRITE_TREE_UNMERGED_INDEX) {
694 int i;
695 fprintf(stderr, "BUG: There are unmerged index entries:\n");
696 for (i = 0; i < index_state->cache_nr; i++) {
697 const struct cache_entry *ce = index_state->cache[i];
698 if (ce_stage(ce))
699 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
700 (int)ce_namelen(ce), ce->name);
701 }
702 BUG("unmerged index entries when writing inmemory index");
703 }
704
705 return lookup_tree(repo, &index_state->cache_tree->oid);
706}
707
708
brian m. carlsonfc5cb992018-03-12 02:27:23709int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
Junio C Hamano45525bd2008-01-11 06:49:35710{
Martin Ågren2954e5e2017-10-05 20:32:08711 int entries, was_valid;
Jeff Kingbfffb482017-09-05 12:15:21712 struct lock_file lock_file = LOCK_INIT;
Elijah Newren724dd762019-08-17 18:41:32713 int ret;
Junio C Hamano45525bd2008-01-11 06:49:35714
Martin Ågren2954e5e2017-10-05 20:32:08715 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-11 06:49:35716
Thomas Gummerera125a222018-01-07 22:30:13717 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 12:14:07718 if (entries < 0) {
719 ret = WRITE_TREE_UNREADABLE_INDEX;
720 goto out;
721 }
Junio C Hamano45525bd2008-01-11 06:49:35722
Elijah Newren724dd762019-08-17 18:41:32723 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
724 index_state->cache_tree &&
725 cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-11 06:49:35726
Elijah Newren724dd762019-08-17 18:41:32727 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
728 prefix);
729 if (!ret && !was_valid) {
Martin Ågren2954e5e2017-10-05 20:32:08730 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-11 06:49:35731 /* Not being able to write is fine -- we are only interested
732 * in updating the cache-tree part, and if the next caller
733 * ends up using the old index with unupdated cache-tree part
734 * it misses the work we did here, but that is just a
735 * performance penalty and not a big deal.
736 */
737 }
738
Jeff Kingc82c75b2017-09-05 12:14:07739out:
Martin Ågren2954e5e2017-10-05 20:32:08740 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 12:14:07741 return ret;
Junio C Hamano45525bd2008-01-11 06:49:35742}
Junio C Hamanob9d37a52009-04-20 10:58:18743
Victoria Dye20ec2d02021-11-29 15:52:41744static void prime_cache_tree_sparse_dir(struct cache_tree *it,
745 struct tree *tree)
746{
747
748 oidcpy(&it->oid, &tree->object.oid);
749 it->entry_count = 1;
750}
751
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02752static void prime_cache_tree_rec(struct repository *r,
753 struct cache_tree *it,
Victoria Dye20ec2d02021-11-29 15:52:41754 struct tree *tree,
755 struct strbuf *tree_path)
Junio C Hamanob9d37a52009-04-20 10:58:18756{
757 struct tree_desc desc;
758 struct name_entry entry;
759 int cnt;
Victoria Dye20ec2d02021-11-29 15:52:41760 int base_path_len = tree_path->len;
Junio C Hamanob9d37a52009-04-20 10:58:18761
brian m. carlsone0a92802017-05-01 02:28:56762 oidcpy(&it->oid, &tree->object.oid);
Victoria Dye20ec2d02021-11-29 15:52:41763
Junio C Hamanob9d37a52009-04-20 10:58:18764 init_tree_desc(&desc, tree->buffer, tree->size);
765 cnt = 0;
766 while (tree_entry(&desc, &entry)) {
767 if (!S_ISDIR(entry.mode))
768 cnt++;
769 else {
770 struct cache_tree_sub *sub;
brian m. carlsonea82b2a2019-01-15 00:39:44771 struct tree *subtree = lookup_tree(r, &entry.oid);
Victoria Dye20ec2d02021-11-29 15:52:41772
Junio C Hamanob9d37a52009-04-20 10:58:18773 if (!subtree->object.parsed)
774 parse_tree(subtree);
775 sub = cache_tree_sub(it, entry.path);
776 sub->cache_tree = cache_tree();
Victoria Dye20ec2d02021-11-29 15:52:41777
778 /*
779 * Recursively-constructed subtree path is only needed when working
780 * in a sparse index (where it's used to determine whether the
781 * subtree is a sparse directory in the index).
782 */
783 if (r->index->sparse_index) {
784 strbuf_setlen(tree_path, base_path_len);
785 strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
786 strbuf_add(tree_path, entry.path, entry.pathlen);
787 strbuf_addch(tree_path, '/');
788 }
789
790 /*
791 * If a sparse index is in use, the directory being processed may be
792 * sparse. To confirm that, we can check whether an entry with that
793 * exact name exists in the index. If it does, the created subtree
794 * should be sparse. Otherwise, cache tree expansion should continue
795 * as normal.
796 */
797 if (r->index->sparse_index &&
798 index_entry_exists(r->index, tree_path->buf, tree_path->len))
799 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
800 else
801 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
Junio C Hamanob9d37a52009-04-20 10:58:18802 cnt += sub->cache_tree->entry_count;
803 }
804 }
Victoria Dye20ec2d02021-11-29 15:52:41805
Junio C Hamanob9d37a52009-04-20 10:58:18806 it->entry_count = cnt;
807}
808
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02809void prime_cache_tree(struct repository *r,
810 struct index_state *istate,
811 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 10:58:18812{
Victoria Dye20ec2d02021-11-29 15:52:41813 struct strbuf tree_path = STRBUF_INIT;
814
Derrick Stolee0e5c9502021-01-04 03:09:14815 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 12:19:33816 cache_tree_free(&istate->cache_tree);
817 istate->cache_tree = cache_tree();
Derrick Stolee0e5c9502021-01-04 03:09:14818
Victoria Dye20ec2d02021-11-29 15:52:41819 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
820 strbuf_release(&tree_path);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 12:19:33821 istate->cache_changed |= CACHE_TREE_CHANGED;
Derrick Stolee0e5c9502021-01-04 03:09:14822 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
Junio C Hamanob9d37a52009-04-20 10:58:18823}
Junio C Hamanob65982b2009-05-20 22:57:22824
825/*
826 * find the cache_tree that corresponds to the current level without
827 * exploding the full path into textual form. The root of the
828 * cache tree is given as "root", and our current level is "info".
829 * (1) When at root level, info->prev is NULL, so it is "root" itself.
830 * (2) Otherwise, find the cache_tree that corresponds to one level
831 * above us, and find ourselves in there.
832 */
833static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
834 struct traverse_info *info)
835{
836 struct cache_tree *our_parent;
837
838 if (!info->prev)
839 return root;
840 our_parent = find_cache_tree_from_traversal(root, info->prev);
Jeff King90553842019-07-31 04:38:15841 return cache_tree_find(our_parent, info->name);
Junio C Hamanob65982b2009-05-20 22:57:22842}
843
844int cache_tree_matches_traversal(struct cache_tree *root,
845 struct name_entry *ent,
846 struct traverse_info *info)
847{
848 struct cache_tree *it;
849
850 it = find_cache_tree_from_traversal(root, info);
851 it = cache_tree_find(it, ent->path);
brian m. carlsonea82b2a2019-01-15 00:39:44852 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 22:57:22853 return it->entry_count;
854 return 0;
855}
Thomas Rast996277c2011-12-06 17:43:37856
Derrick Stolee9ad2d5e2021-03-30 13:11:03857static void verify_one_sparse(struct repository *r,
858 struct index_state *istate,
859 struct cache_tree *it,
860 struct strbuf *path,
861 int pos)
862{
863 struct cache_entry *ce = istate->cache[pos];
864
865 if (!S_ISSPARSEDIR(ce->ce_mode))
866 BUG("directory '%s' is present in index, but not sparse",
867 path->buf);
868}
869
Phillip Woodf7510972021-10-07 18:07:21870/*
871 * Returns:
872 * 0 - Verification completed.
873 * 1 - Restart verification - a call to ensure_full_index() freed the cache
874 * tree that is being verified and verification needs to be restarted from
875 * the new toplevel cache tree.
876 */
877static int verify_one(struct repository *r,
878 struct index_state *istate,
879 struct cache_tree *it,
880 struct strbuf *path)
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28881{
882 int i, pos, len = path->len;
883 struct strbuf tree_buf = STRBUF_INIT;
884 struct object_id new_oid;
885
886 for (i = 0; i < it->subtree_nr; i++) {
887 strbuf_addf(path, "%s/", it->down[i]->name);
Phillip Woodf7510972021-10-07 18:07:21888 if (verify_one(r, istate, it->down[i]->cache_tree, path))
889 return 1;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28890 strbuf_setlen(path, len);
891 }
892
893 if (it->entry_count < 0 ||
894 /* no verification on tests (t7003) that replace trees */
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02895 lookup_replace_object(r, &it->oid) != &it->oid)
Phillip Woodf7510972021-10-07 18:07:21896 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28897
898 if (path->len) {
Phillip Woodf7510972021-10-07 18:07:21899 /*
900 * If the index is sparse and the cache tree is not
901 * index_name_pos() may trigger ensure_full_index() which will
902 * free the tree that is being verified.
903 */
904 int is_sparse = istate->sparse_index;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28905 pos = index_name_pos(istate, path->buf, path->len);
Phillip Woodf7510972021-10-07 18:07:21906 if (is_sparse && !istate->sparse_index)
907 return 1;
Derrick Stolee9ad2d5e2021-03-30 13:11:03908
909 if (pos >= 0) {
910 verify_one_sparse(r, istate, it, path, pos);
Phillip Woodf7510972021-10-07 18:07:21911 return 0;
Derrick Stolee9ad2d5e2021-03-30 13:11:03912 }
913
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28914 pos = -pos - 1;
915 } else {
916 pos = 0;
917 }
918
919 i = 0;
920 while (i < it->entry_count) {
921 struct cache_entry *ce = istate->cache[pos + i];
922 const char *slash;
923 struct cache_tree_sub *sub = NULL;
924 const struct object_id *oid;
925 const char *name;
926 unsigned mode;
927 int entlen;
928
929 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
930 BUG("%s with flags 0x%x should not be in cache-tree",
931 ce->name, ce->ce_flags);
932 name = ce->name + path->len;
933 slash = strchr(name, '/');
934 if (slash) {
935 entlen = slash - name;
936 sub = find_subtree(it, ce->name + path->len, entlen, 0);
937 if (!sub || sub->cache_tree->entry_count < 0)
938 BUG("bad subtree '%.*s'", entlen, name);
939 oid = &sub->cache_tree->oid;
940 mode = S_IFDIR;
941 i += sub->cache_tree->entry_count;
942 } else {
943 oid = &ce->oid;
944 mode = ce->ce_mode;
945 entlen = ce_namelen(ce) - path->len;
946 i++;
947 }
948 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
Matheus Tavaresa6519462020-01-30 20:32:18949 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28950 }
Matheus Tavares2dcde202020-01-30 20:32:22951 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
952 &new_oid);
Jeff Kinge43d2dc2018-10-02 21:19:21953 if (!oideq(&new_oid, &it->oid))
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28954 BUG("cache-tree for path %.*s does not match. "
955 "Expected %s got %s", len, path->buf,
956 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
957 strbuf_setlen(path, len);
958 strbuf_release(&tree_buf);
Phillip Woodf7510972021-10-07 18:07:21959 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28960}
961
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02962void cache_tree_verify(struct repository *r, struct index_state *istate)
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28963{
964 struct strbuf path = STRBUF_INIT;
965
966 if (!istate->cache_tree)
967 return;
Phillip Woodf7510972021-10-07 18:07:21968 if (verify_one(r, istate, istate->cache_tree, &path)) {
969 strbuf_reset(&path);
970 if (verify_one(r, istate, istate->cache_tree, &path))
971 BUG("ensure_full_index() called twice while verifying cache tree");
972 }
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28973 strbuf_release(&path);
974}