🌐 AI搜索 & 代理 主页
blob: 56db0b5026b675a77a3962802ebf38c083ae107b [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"
Neeraj Singh4d33e2b2022-04-05 05:20:106#include "bulk-checkin.h"
Stefan Bellercbd53a22018-05-15 23:42:157#include "object-store.h"
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:288#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 13:40:319#include "promisor-remote.h"
Derrick Stolee6e773522021-03-30 13:10:5510#include "sparse-index.h"
Junio C Hamano74986462006-04-23 23:52:2011
Jeff Hostetlere9b9cc52019-06-19 21:05:5812#ifndef DEBUG_CACHE_TREE
13#define DEBUG_CACHE_TREE 0
Junio C Hamano49031612006-10-30 23:29:5314#endif
Junio C Hamano74986462006-04-23 23:52:2015
16struct cache_tree *cache_tree(void)
17{
18 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
19 it->entry_count = -1;
20 return it;
21}
22
Junio C Hamanobad68ec2006-04-25 04:18:5823void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 23:52:2024{
25 int i;
Junio C Hamanobad68ec2006-04-25 04:18:5826 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 23:52:2027
28 if (!it)
29 return;
30 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 21:40:1631 if (it->down[i]) {
Junio C Hamano61fa3092006-04-26 00:40:0232 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 21:40:1633 free(it->down[i]);
34 }
Junio C Hamano74986462006-04-23 23:52:2035 free(it->down);
36 free(it);
Junio C Hamanobad68ec2006-04-25 04:18:5837 *it_p = NULL;
Junio C Hamano74986462006-04-23 23:52:2038}
39
Junio C Hamano61fa3092006-04-26 00:40:0240static int subtree_name_cmp(const char *one, int onelen,
41 const char *two, int twolen)
42{
43 if (onelen < twolen)
44 return -1;
45 if (twolen < onelen)
46 return 1;
47 return memcmp(one, two, onelen);
48}
49
Derrick Stoleec80dd392021-01-23 19:58:1350int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
Junio C Hamano61fa3092006-04-26 00:40:0251{
52 struct cache_tree_sub **down = it->down;
53 int lo, hi;
54 lo = 0;
55 hi = it->subtree_nr;
56 while (lo < hi) {
Derrick Stolee19716b22017-10-08 18:29:3757 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-26 00:40:0258 struct cache_tree_sub *mdl = down[mi];
59 int cmp = subtree_name_cmp(path, pathlen,
60 mdl->name, mdl->namelen);
61 if (!cmp)
62 return mi;
63 if (cmp < 0)
64 hi = mi;
65 else
66 lo = mi + 1;
67 }
68 return -lo-1;
69}
70
Junio C Hamano74986462006-04-23 23:52:2071static struct cache_tree_sub *find_subtree(struct cache_tree *it,
72 const char *path,
73 int pathlen,
74 int create)
75{
Junio C Hamano74986462006-04-23 23:52:2076 struct cache_tree_sub *down;
Derrick Stoleec80dd392021-01-23 19:58:1377 int pos = cache_tree_subtree_pos(it, path, pathlen);
Junio C Hamano61fa3092006-04-26 00:40:0278 if (0 <= pos)
79 return it->down[pos];
Junio C Hamano74986462006-04-23 23:52:2080 if (!create)
81 return NULL;
Junio C Hamano61fa3092006-04-26 00:40:0282
83 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-03 22:31:5184 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-26 00:40:0285 it->subtree_nr++;
86
Jeff King96ffc062016-02-22 22:44:3287 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-26 00:40:0288 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 23:52:2089 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-26 00:40:0290
91 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 17:50:0992 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
93 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-26 00:40:0294 it->down[pos] = down;
Junio C Hamano74986462006-04-23 23:52:2095 return down;
96}
97
Junio C Hamano7927a552006-04-27 08:33:0798struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
99{
100 int pathlen = strlen(path);
101 return find_subtree(it, path, pathlen, 1);
102}
103
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31104static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 23:52:20105{
106 /* a/b/c
107 * ==> invalidate self
108 * ==> find "a", have it invalidate "b/c"
109 * a
110 * ==> invalidate self
111 * ==> if "a" exists as a subtree, remove it.
112 */
113 const char *slash;
114 int namelen;
115 struct cache_tree_sub *down;
116
Jeff Hostetlere9b9cc52019-06-19 21:05:58117#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 23:10:45118 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
119#endif
120
Junio C Hamano74986462006-04-23 23:52:20121 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31122 return 0;
Rohit Mani2c5495f2014-03-08 06:48:31123 slash = strchrnul(path, '/');
124 namelen = slash - path;
Junio C Hamano74986462006-04-23 23:52:20125 it->entry_count = -1;
Rohit Mani2c5495f2014-03-08 06:48:31126 if (!*slash) {
Junio C Hamano61fa3092006-04-26 00:40:02127 int pos;
Derrick Stoleec80dd392021-01-23 19:58:13128 pos = cache_tree_subtree_pos(it, path, namelen);
Junio C Hamano61fa3092006-04-26 00:40:02129 if (0 <= pos) {
130 cache_tree_free(&it->down[pos]->cache_tree);
131 free(it->down[pos]);
Junio C Hamano74986462006-04-23 23:52:20132 /* 0 1 2 3 4 5
133 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-26 00:40:02134 * pos
Junio C Hamano74986462006-04-23 23:52:20135 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-26 00:40:02136 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 23:52:20137 */
René Scharfef331ab92017-07-15 20:00:45138 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
139 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 23:52:20140 it->subtree_nr--;
141 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31142 return 1;
Junio C Hamano74986462006-04-23 23:52:20143 }
Junio C Hamano74986462006-04-23 23:52:20144 down = find_subtree(it, path, namelen, 0);
145 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31146 do_invalidate_path(down->cache_tree, slash + 1);
147 return 1;
148}
149
150void cache_tree_invalidate_path(struct index_state *istate, const char *path)
151{
152 if (do_invalidate_path(istate->cache_tree, path))
153 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 23:52:20154}
155
Derrick Stolee8d87e332021-01-23 19:58:12156static int verify_cache(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 23:52:20157{
Derrick Stolee8d87e332021-01-23 19:58:12158 unsigned i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46159 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 23:52:20160
161 /* Verify that the tree is merged */
162 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12163 for (i = 0; i < istate->cache_nr; i++) {
164 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano3f6d56d2012-02-07 19:55:48165 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 17:43:37166 if (silent)
167 return -1;
Junio C Hamano74986462006-04-23 23:52:20168 if (10 < ++funny) {
169 fprintf(stderr, "...\n");
170 break;
171 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 04:15:25172 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52173 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 23:52:20174 }
175 }
176 if (funny)
177 return -1;
178
179 /* Also verify that the cache does not have path and path/file
180 * at the same time. At this point we know the cache has only
181 * stage 0 entries.
182 */
183 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12184 for (i = 0; i + 1 < istate->cache_nr; i++) {
Junio C Hamano74986462006-04-23 23:52:20185 /* path/file always comes after path because of the way
186 * the cache is sorted. Also path can appear only once,
187 * which means conflicting one would immediately follow.
188 */
Derrick Stolee8d87e332021-01-23 19:58:12189 const struct cache_entry *this_ce = istate->cache[i];
190 const struct cache_entry *next_ce = istate->cache[i + 1];
René Scharfe0b725362021-01-07 16:32:10191 const char *this_name = this_ce->name;
192 const char *next_name = next_ce->name;
193 int this_len = ce_namelen(this_ce);
194 if (this_len < ce_namelen(next_ce) &&
Derrick Stoleea4b6d202021-01-07 16:32:11195 next_name[this_len] == '/' &&
196 strncmp(this_name, next_name, this_len) == 0) {
Junio C Hamano74986462006-04-23 23:52:20197 if (10 < ++funny) {
198 fprintf(stderr, "...\n");
199 break;
200 }
201 fprintf(stderr, "You have both %s and %s\n",
202 this_name, next_name);
203 }
204 }
205 if (funny)
206 return -1;
207 return 0;
208}
209
210static void discard_unused_subtrees(struct cache_tree *it)
211{
212 struct cache_tree_sub **down = it->down;
213 int nr = it->subtree_nr;
214 int dst, src;
215 for (dst = src = 0; src < nr; src++) {
216 struct cache_tree_sub *s = down[src];
217 if (s->used)
218 down[dst++] = s;
219 else {
Junio C Hamanobad68ec2006-04-25 04:18:58220 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 23:52:20221 free(s);
222 it->subtree_nr--;
223 }
224 }
225}
226
Junio C Hamanobad68ec2006-04-25 04:18:58227int cache_tree_fully_valid(struct cache_tree *it)
228{
229 int i;
230 if (!it)
231 return 0;
Jeff King98374a02019-01-07 08:37:54232 if (it->entry_count < 0 || !has_object_file(&it->oid))
Junio C Hamanobad68ec2006-04-25 04:18:58233 return 0;
234 for (i = 0; i < it->subtree_nr; i++) {
235 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
236 return 0;
237 }
238 return 1;
239}
240
Jonathan Tand3da2232021-07-23 18:52:23241static int must_check_existence(const struct cache_entry *ce)
242{
243 return !(has_promisor_remote() && ce_skip_worktree(ce));
244}
245
Junio C Hamano74986462006-04-23 23:52:20246static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32247 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 23:52:20248 int entries,
249 const char *base,
250 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27251 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46252 int flags)
Junio C Hamano74986462006-04-23 23:52:20253{
Pierre Habouzit5242bcb2007-09-06 11:20:11254 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46255 int missing_ok = flags & WRITE_TREE_MISSING_OK;
256 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-06 04:06:56257 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28258 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 23:52:20259 int i;
260
David Turneraecf5672014-07-06 04:06:56261 assert(!(dryrun && repair));
262
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27263 *skip_count = 0;
264
Derrick Stolee2de37c52021-03-30 13:11:02265 /*
266 * If the first entry of this region is a sparse directory
267 * entry corresponding exactly to 'base', then this cache_tree
268 * struct is a "leaf" in the data structure, pointing to the
269 * tree OID specified in the entry.
270 */
271 if (entries > 0) {
272 const struct cache_entry *ce = cache[0];
273
274 if (S_ISSPARSEDIR(ce->ce_mode) &&
275 ce->ce_namelen == baselen &&
276 !strncmp(ce->name, base, baselen)) {
277 it->entry_count = 1;
278 oidcpy(&it->oid, &ce->oid);
279 return 1;
280 }
281 }
282
Jeff King98374a02019-01-07 08:37:54283 if (0 <= it->entry_count && has_object_file(&it->oid))
Junio C Hamano74986462006-04-23 23:52:20284 return it->entry_count;
285
286 /*
287 * We first scan for subtrees and update them; we start by
288 * marking existing subtrees -- the ones that are unmarked
289 * should not be in the result.
290 */
291 for (i = 0; i < it->subtree_nr; i++)
292 it->down[i]->used = 0;
293
294 /*
295 * Find the subtrees and update them.
296 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26297 i = 0;
298 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00299 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 23:52:20300 struct cache_tree_sub *sub;
301 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27302 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 23:52:20303
304 path = ce->name;
305 pathlen = ce_namelen(ce);
306 if (pathlen <= baselen || memcmp(base, path, baselen))
307 break; /* at the end of this level */
308
309 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26310 if (!slash) {
311 i++;
Junio C Hamano74986462006-04-23 23:52:20312 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26313 }
Junio C Hamano74986462006-04-23 23:52:20314 /*
315 * a/bbb/c (base = a/, slash = /c)
316 * ==>
317 * path+baselen = bbb/c, sublen = 3
318 */
319 sublen = slash - (path + baselen);
320 sub = find_subtree(it, path + baselen, sublen, 1);
321 if (!sub->cache_tree)
322 sub->cache_tree = cache_tree();
323 subcnt = update_one(sub->cache_tree,
324 cache + i, entries - i,
325 path,
326 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27327 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46328 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00329 if (subcnt < 0)
330 return subcnt;
Jeff King729dbbd2014-10-29 17:11:58331 if (!subcnt)
332 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26333 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27334 sub->count = subcnt; /* to be used in the next loop */
335 *skip_count += subskip;
Junio C Hamano74986462006-04-23 23:52:20336 sub->used = 1;
337 }
338
339 discard_unused_subtrees(it);
340
341 /*
342 * Then write out the tree object for this level.
343 */
Pierre Habouzitf1696ee2007-09-10 10:35:04344 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 23:52:20345
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26346 i = 0;
347 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00348 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 05:06:26349 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 23:52:20350 const char *path, *slash;
351 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24352 const struct object_id *oid;
Junio C Hamano74986462006-04-23 23:52:20353 unsigned mode;
Junio C Hamano4ed115e2014-09-02 21:16:20354 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27355 int contains_ita = 0;
Jonathan Tan2f215ff2018-10-09 18:40:37356 int ce_missing_ok;
Junio C Hamano74986462006-04-23 23:52:20357
358 path = ce->name;
359 pathlen = ce_namelen(ce);
360 if (pathlen <= baselen || memcmp(base, path, baselen))
361 break; /* at the end of this level */
362
363 slash = strchr(path + baselen, '/');
364 if (slash) {
365 entlen = slash - (path + baselen);
366 sub = find_subtree(it, path + baselen, entlen, 0);
367 if (!sub)
368 die("cache-tree.c: '%.*s' in '%s' not found",
369 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27370 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24371 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 23:52:20372 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27373 contains_ita = sub->cache_tree->entry_count < 0;
374 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28375 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 21:16:20376 expected_missing = 1;
377 }
Junio C Hamano74986462006-04-23 23:52:20378 }
379 else {
brian m. carlson6dcb4622018-03-12 02:27:24380 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-15 00:03:17381 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 23:52:20382 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26383 i++;
Junio C Hamano74986462006-04-23 23:52:20384 }
Jeff Kinga96d3cc2017-04-21 18:46:17385
Jonathan Tan2f215ff2018-10-09 18:40:37386 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
Jonathan Tand3da2232021-07-23 18:52:23387 !must_check_existence(ce);
brian m. carlson6dcb4622018-03-12 02:27:24388 if (is_null_oid(oid) ||
Jonathan Tan2f215ff2018-10-09 18:40:37389 (!ce_missing_ok && !has_object_file(oid))) {
Jonathan Niederb6b56ac2010-08-10 03:32:11390 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 21:16:20391 if (expected_missing)
392 return -1;
Linus Torvaldsa3883732009-07-14 18:25:17393 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24394 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-10 03:32:11395 }
Junio C Hamano74986462006-04-23 23:52:20396
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27397 /*
398 * CE_REMOVE entries are removed before the index is
399 * written to disk. Skip them to remain consistent
400 * with the future on-disk index.
401 */
402 if (ce->ce_flags & CE_REMOVE) {
403 *skip_count = *skip_count + 1;
404 continue;
405 }
406
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28407 /*
408 * CE_INTENT_TO_ADD entries exist on on-disk index but
409 * they are not part of generated trees. Invalidate up
410 * to root to force cache-tree users to read elsewhere.
411 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 05:06:26412 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28413 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27414 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28415 }
Junio C Hamano74986462006-04-23 23:52:20416
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27417 /*
418 * "sub" can be an empty tree if all subentries are i-t-a.
419 */
brian m. carlsona0554932018-05-02 00:26:04420 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27421 continue;
422
Pierre Habouzit5242bcb2007-09-06 11:20:11423 strbuf_grow(&buffer, entlen + 100);
424 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24425 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 23:52:20426
Jeff Hostetlere9b9cc52019-06-19 21:05:58427#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 23:10:45428 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 23:52:20429 mode, entlen, path + baselen);
430#endif
431 }
432
David Turneraecf5672014-07-06 04:06:56433 if (repair) {
Patryk Obaraf070fac2018-01-28 00:13:13434 struct object_id oid;
Matheus Tavares2dcde202020-01-30 20:32:22435 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
Ævar Arnfjörð Bjarmason44439c12022-02-04 23:48:32436 OBJ_TREE, &oid);
Jonathan Tanf981ec12019-09-03 19:42:47437 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
Patryk Obaraf070fac2018-01-28 00:13:13438 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-06 04:06:56439 else
440 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 00:13:19441 } else if (dryrun) {
Matheus Tavares2dcde202020-01-30 20:32:22442 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
Ævar Arnfjörð Bjarmason44439c12022-02-04 23:48:32443 OBJ_TREE, &it->oid);
Ævar Arnfjörð Bjarmasonc80d2262022-02-04 23:48:26444 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 14:30:49445 &it->oid, flags & WRITE_TREE_SILENT
446 ? HASH_SILENT : 0)) {
Junio C Hamanoedae5f02008-04-23 16:47:17447 strbuf_release(&buffer);
448 return -1;
449 }
450
Pierre Habouzit5242bcb2007-09-06 11:20:11451 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28452 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Jeff Hostetlere9b9cc52019-06-19 21:05:58453#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 23:10:45454 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 23:52:20455 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56456 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 23:52:20457#endif
458 return i;
459}
460
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32461int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 23:52:20462{
Derrick Stoleefb088262021-01-23 19:58:11463 int skip, i;
464
Derrick Stolee8d87e332021-01-23 19:58:12465 i = verify_cache(istate, flags);
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32466
Junio C Hamano74986462006-04-23 23:52:20467 if (i)
468 return i;
Derrick Stoleefb088262021-01-23 19:58:11469
470 if (!istate->cache_tree)
471 istate->cache_tree = cache_tree();
472
Jonathan Tand3da2232021-07-23 18:52:23473 if (!(flags & WRITE_TREE_MISSING_OK) && has_promisor_remote())
474 prefetch_cache_entries(istate, must_check_existence);
475
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 14:41:23476 trace_performance_enter();
Derrick Stoleefa7ca5d2021-01-04 03:09:12477 trace2_region_enter("cache_tree", "update", the_repository);
Neeraj Singh4d33e2b2022-04-05 05:20:10478 begin_odb_transaction();
Derrick Stoleefb088262021-01-23 19:58:11479 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
480 "", 0, &skip, flags);
Neeraj Singh4d33e2b2022-04-05 05:20:10481 end_odb_transaction();
Derrick Stoleefa7ca5d2021-01-04 03:09:12482 trace2_region_leave("cache_tree", "update", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 14:41:23483 trace_performance_leave("cache_tree_update");
Junio C Hamano74986462006-04-23 23:52:20484 if (i < 0)
485 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32486 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 23:52:20487 return 0;
488}
489
Pierre Habouzit1dffb8f2007-09-25 08:22:44490static void write_one(struct strbuf *buffer, struct cache_tree *it,
Nguyễn Thái Ngọc Duyec36c422018-12-06 15:42:06491 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 23:52:20492{
493 int i;
494
495 /* One "cache-tree" entry consists of the following:
496 * path (NUL terminated)
497 * entry_count, subtree_nr ("%d %d\n")
498 * tree-sha1 (missing if invalid)
499 * subtree_nr "cache-tree" entries for subtrees.
500 */
Pierre Habouzit5242bcb2007-09-06 11:20:11501 strbuf_grow(buffer, pathlen + 100);
502 strbuf_add(buffer, path, pathlen);
503 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 23:52:20504
Jeff Hostetlere9b9cc52019-06-19 21:05:58505#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 23:52:20506 if (0 <= it->entry_count)
507 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
508 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56509 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 23:52:20510 else
511 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
512 pathlen, path, it->subtree_nr);
513#endif
514
515 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24516 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 23:52:20517 }
518 for (i = 0; i < it->subtree_nr; i++) {
519 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-26 00:40:02520 if (i) {
521 struct cache_tree_sub *prev = it->down[i-1];
522 if (subtree_name_cmp(down->name, down->namelen,
523 prev->name, prev->namelen) <= 0)
524 die("fatal - unsorted cache subtree");
525 }
Pierre Habouzit1dffb8f2007-09-25 08:22:44526 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 23:52:20527 }
Junio C Hamano74986462006-04-23 23:52:20528}
529
Pierre Habouzit1dffb8f2007-09-25 08:22:44530void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 23:52:20531{
Derrick Stolee4c3e1872021-01-04 03:09:13532 trace2_region_enter("cache_tree", "write", the_repository);
Pierre Habouzit1dffb8f2007-09-25 08:22:44533 write_one(sb, root, "", 0);
Derrick Stolee4c3e1872021-01-04 03:09:13534 trace2_region_leave("cache_tree", "write", the_repository);
Junio C Hamano74986462006-04-23 23:52:20535}
536
537static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
538{
539 const char *buf = *buffer;
540 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 01:31:02541 const char *cp;
542 char *ep;
Junio C Hamano74986462006-04-23 23:52:20543 struct cache_tree *it;
544 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24545 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 23:52:20546
547 it = NULL;
548 /* skip name, but make sure name exists */
549 while (size && *buf) {
550 size--;
551 buf++;
552 }
553 if (!size)
554 goto free_return;
555 buf++; size--;
556 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 01:31:02557
558 cp = buf;
559 it->entry_count = strtol(cp, &ep, 10);
560 if (cp == ep)
561 goto free_return;
562 cp = ep;
563 subtree_nr = strtol(cp, &ep, 10);
564 if (cp == ep)
Junio C Hamano74986462006-04-23 23:52:20565 goto free_return;
566 while (size && *buf && *buf != '\n') {
567 size--;
568 buf++;
569 }
570 if (!size)
571 goto free_return;
572 buf++; size--;
573 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24574 if (size < rawsz)
Junio C Hamano74986462006-04-23 23:52:20575 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29576 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24577 buf += rawsz;
578 size -= rawsz;
Junio C Hamano74986462006-04-23 23:52:20579 }
580
Jeff Hostetlere9b9cc52019-06-19 21:05:58581#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 23:52:20582 if (0 <= it->entry_count)
583 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
584 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56585 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 23:52:20586 else
587 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
588 *buffer, subtree_nr);
589#endif
590
591 /*
592 * Just a heuristic -- we do not add directories that often but
593 * we do not want to have to extend it immediately when we do,
594 * hence +2.
595 */
596 it->subtree_alloc = subtree_nr + 2;
René Scharfeca56dad2021-03-13 16:17:22597 CALLOC_ARRAY(it->down, it->subtree_alloc);
Junio C Hamano74986462006-04-23 23:52:20598 for (i = 0; i < subtree_nr; i++) {
599 /* read each subtree */
600 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-26 00:40:02601 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 23:52:20602 const char *name = buf;
Junio C Hamano7927a552006-04-27 08:33:07603
Junio C Hamano74986462006-04-23 23:52:20604 sub = read_one(&buf, &size);
605 if (!sub)
606 goto free_return;
Junio C Hamano7927a552006-04-27 08:33:07607 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-26 00:40:02608 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 23:52:20609 }
610 if (subtree_nr != it->subtree_nr)
611 die("cache-tree: internal error");
612 *buffer = buf;
613 *size_p = size;
614 return it;
615
616 free_return:
Junio C Hamanobad68ec2006-04-25 04:18:58617 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 23:52:20618 return NULL;
619}
620
Junio C Hamanobad68ec2006-04-25 04:18:58621struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 23:52:20622{
Derrick Stolee4c3e1872021-01-04 03:09:13623 struct cache_tree *result;
624
Junio C Hamanobad68ec2006-04-25 04:18:58625 if (buffer[0])
Junio C Hamano74986462006-04-23 23:52:20626 return NULL; /* not the whole tree */
Derrick Stolee4c3e1872021-01-04 03:09:13627
628 trace2_region_enter("cache_tree", "read", the_repository);
629 result = read_one(&buffer, &size);
630 trace2_region_leave("cache_tree", "read", the_repository);
631
632 return result;
Junio C Hamano74986462006-04-23 23:52:20633}
Junio C Hamano6bd20352006-04-26 08:20:50634
Nanako Shiraishi7ba04d92008-07-16 10:42:10635static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 08:20:50636{
Junio C Hamanob87fc962009-05-20 22:53:57637 if (!it)
638 return NULL;
Junio C Hamano6bd20352006-04-26 08:20:50639 while (*path) {
640 const char *slash;
641 struct cache_tree_sub *sub;
642
Michael Haggerty17e22dd2014-03-05 17:26:26643 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 17:26:27644 /*
645 * Between path and slash is the name of the subtree
646 * to look for.
Junio C Hamano6bd20352006-04-26 08:20:50647 */
648 sub = find_subtree(it, path, slash - path, 0);
649 if (!sub)
650 return NULL;
651 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 17:26:30652
Junio C Hamano6bd20352006-04-26 08:20:50653 path = slash;
Michael Haggerty34910472014-03-05 17:26:30654 while (*path == '/')
655 path++;
Junio C Hamano6bd20352006-04-26 08:20:50656 }
657 return it;
658}
Junio C Hamano45525bd2008-01-11 06:49:35659
Elijah Newren724dd762019-08-17 18:41:32660static int write_index_as_tree_internal(struct object_id *oid,
661 struct index_state *index_state,
662 int cache_tree_valid,
663 int flags,
664 const char *prefix)
665{
666 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
667 cache_tree_free(&index_state->cache_tree);
668 cache_tree_valid = 0;
669 }
670
Elijah Newren724dd762019-08-17 18:41:32671 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
672 return WRITE_TREE_UNMERGED_INDEX;
673
674 if (prefix) {
675 struct cache_tree *subtree;
676 subtree = cache_tree_find(index_state->cache_tree, prefix);
677 if (!subtree)
678 return WRITE_TREE_PREFIX_ERROR;
679 oidcpy(oid, &subtree->oid);
680 }
681 else
682 oidcpy(oid, &index_state->cache_tree->oid);
683
684 return 0;
685}
686
687struct tree* write_in_core_index_as_tree(struct repository *repo) {
688 struct object_id o;
689 int was_valid, ret;
690
691 struct index_state *index_state = repo->index;
692 was_valid = index_state->cache_tree &&
693 cache_tree_fully_valid(index_state->cache_tree);
694
695 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
696 if (ret == WRITE_TREE_UNMERGED_INDEX) {
697 int i;
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 12:25:37698 bug("there are unmerged index entries:");
Elijah Newren724dd762019-08-17 18:41:32699 for (i = 0; i < index_state->cache_nr; i++) {
700 const struct cache_entry *ce = index_state->cache[i];
701 if (ce_stage(ce))
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 12:25:37702 bug("%d %.*s", ce_stage(ce),
703 (int)ce_namelen(ce), ce->name);
Elijah Newren724dd762019-08-17 18:41:32704 }
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 12:25:37705 BUG("unmerged index entries when writing in-core index");
Elijah Newren724dd762019-08-17 18:41:32706 }
707
708 return lookup_tree(repo, &index_state->cache_tree->oid);
709}
710
711
brian m. carlsonfc5cb992018-03-12 02:27:23712int 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:35713{
Martin Ågren2954e5e2017-10-05 20:32:08714 int entries, was_valid;
Jeff Kingbfffb482017-09-05 12:15:21715 struct lock_file lock_file = LOCK_INIT;
Elijah Newren724dd762019-08-17 18:41:32716 int ret;
Junio C Hamano45525bd2008-01-11 06:49:35717
Martin Ågren2954e5e2017-10-05 20:32:08718 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-11 06:49:35719
Thomas Gummerera125a222018-01-07 22:30:13720 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 12:14:07721 if (entries < 0) {
722 ret = WRITE_TREE_UNREADABLE_INDEX;
723 goto out;
724 }
Junio C Hamano45525bd2008-01-11 06:49:35725
Elijah Newren724dd762019-08-17 18:41:32726 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
727 index_state->cache_tree &&
728 cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-11 06:49:35729
Elijah Newren724dd762019-08-17 18:41:32730 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
731 prefix);
732 if (!ret && !was_valid) {
Martin Ågren2954e5e2017-10-05 20:32:08733 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-11 06:49:35734 /* Not being able to write is fine -- we are only interested
735 * in updating the cache-tree part, and if the next caller
736 * ends up using the old index with unupdated cache-tree part
737 * it misses the work we did here, but that is just a
738 * performance penalty and not a big deal.
739 */
740 }
741
Jeff Kingc82c75b2017-09-05 12:14:07742out:
Martin Ågren2954e5e2017-10-05 20:32:08743 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 12:14:07744 return ret;
Junio C Hamano45525bd2008-01-11 06:49:35745}
Junio C Hamanob9d37a52009-04-20 10:58:18746
Victoria Dye20ec2d02021-11-29 15:52:41747static void prime_cache_tree_sparse_dir(struct cache_tree *it,
748 struct tree *tree)
749{
750
751 oidcpy(&it->oid, &tree->object.oid);
752 it->entry_count = 1;
753}
754
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02755static void prime_cache_tree_rec(struct repository *r,
756 struct cache_tree *it,
Victoria Dye20ec2d02021-11-29 15:52:41757 struct tree *tree,
758 struct strbuf *tree_path)
Junio C Hamanob9d37a52009-04-20 10:58:18759{
760 struct tree_desc desc;
761 struct name_entry entry;
762 int cnt;
Victoria Dye20ec2d02021-11-29 15:52:41763 int base_path_len = tree_path->len;
Junio C Hamanob9d37a52009-04-20 10:58:18764
brian m. carlsone0a92802017-05-01 02:28:56765 oidcpy(&it->oid, &tree->object.oid);
Victoria Dye20ec2d02021-11-29 15:52:41766
Junio C Hamanob9d37a52009-04-20 10:58:18767 init_tree_desc(&desc, tree->buffer, tree->size);
768 cnt = 0;
769 while (tree_entry(&desc, &entry)) {
770 if (!S_ISDIR(entry.mode))
771 cnt++;
772 else {
773 struct cache_tree_sub *sub;
brian m. carlsonea82b2a2019-01-15 00:39:44774 struct tree *subtree = lookup_tree(r, &entry.oid);
Victoria Dye20ec2d02021-11-29 15:52:41775
Junio C Hamanob9d37a52009-04-20 10:58:18776 if (!subtree->object.parsed)
777 parse_tree(subtree);
778 sub = cache_tree_sub(it, entry.path);
779 sub->cache_tree = cache_tree();
Victoria Dye20ec2d02021-11-29 15:52:41780
781 /*
782 * Recursively-constructed subtree path is only needed when working
783 * in a sparse index (where it's used to determine whether the
784 * subtree is a sparse directory in the index).
785 */
786 if (r->index->sparse_index) {
787 strbuf_setlen(tree_path, base_path_len);
788 strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
789 strbuf_add(tree_path, entry.path, entry.pathlen);
790 strbuf_addch(tree_path, '/');
791 }
792
793 /*
794 * If a sparse index is in use, the directory being processed may be
795 * sparse. To confirm that, we can check whether an entry with that
796 * exact name exists in the index. If it does, the created subtree
797 * should be sparse. Otherwise, cache tree expansion should continue
798 * as normal.
799 */
800 if (r->index->sparse_index &&
801 index_entry_exists(r->index, tree_path->buf, tree_path->len))
802 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
803 else
804 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
Junio C Hamanob9d37a52009-04-20 10:58:18805 cnt += sub->cache_tree->entry_count;
806 }
807 }
Victoria Dye20ec2d02021-11-29 15:52:41808
Junio C Hamanob9d37a52009-04-20 10:58:18809 it->entry_count = cnt;
810}
811
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02812void prime_cache_tree(struct repository *r,
813 struct index_state *istate,
814 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 10:58:18815{
Victoria Dye20ec2d02021-11-29 15:52:41816 struct strbuf tree_path = STRBUF_INIT;
817
Derrick Stolee0e5c9502021-01-04 03:09:14818 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 12:19:33819 cache_tree_free(&istate->cache_tree);
820 istate->cache_tree = cache_tree();
Derrick Stolee0e5c9502021-01-04 03:09:14821
Victoria Dye20ec2d02021-11-29 15:52:41822 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
823 strbuf_release(&tree_path);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 12:19:33824 istate->cache_changed |= CACHE_TREE_CHANGED;
Derrick Stolee0e5c9502021-01-04 03:09:14825 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
Junio C Hamanob9d37a52009-04-20 10:58:18826}
Junio C Hamanob65982b2009-05-20 22:57:22827
828/*
829 * find the cache_tree that corresponds to the current level without
830 * exploding the full path into textual form. The root of the
831 * cache tree is given as "root", and our current level is "info".
832 * (1) When at root level, info->prev is NULL, so it is "root" itself.
833 * (2) Otherwise, find the cache_tree that corresponds to one level
834 * above us, and find ourselves in there.
835 */
836static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
837 struct traverse_info *info)
838{
839 struct cache_tree *our_parent;
840
841 if (!info->prev)
842 return root;
843 our_parent = find_cache_tree_from_traversal(root, info->prev);
Jeff King90553842019-07-31 04:38:15844 return cache_tree_find(our_parent, info->name);
Junio C Hamanob65982b2009-05-20 22:57:22845}
846
847int cache_tree_matches_traversal(struct cache_tree *root,
848 struct name_entry *ent,
849 struct traverse_info *info)
850{
851 struct cache_tree *it;
852
853 it = find_cache_tree_from_traversal(root, info);
854 it = cache_tree_find(it, ent->path);
brian m. carlsonea82b2a2019-01-15 00:39:44855 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 22:57:22856 return it->entry_count;
857 return 0;
858}
Thomas Rast996277c2011-12-06 17:43:37859
Derrick Stolee9ad2d5e2021-03-30 13:11:03860static void verify_one_sparse(struct repository *r,
861 struct index_state *istate,
862 struct cache_tree *it,
863 struct strbuf *path,
864 int pos)
865{
866 struct cache_entry *ce = istate->cache[pos];
867
868 if (!S_ISSPARSEDIR(ce->ce_mode))
869 BUG("directory '%s' is present in index, but not sparse",
870 path->buf);
871}
872
Phillip Woodf7510972021-10-07 18:07:21873/*
874 * Returns:
875 * 0 - Verification completed.
876 * 1 - Restart verification - a call to ensure_full_index() freed the cache
877 * tree that is being verified and verification needs to be restarted from
878 * the new toplevel cache tree.
879 */
880static int verify_one(struct repository *r,
881 struct index_state *istate,
882 struct cache_tree *it,
883 struct strbuf *path)
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28884{
885 int i, pos, len = path->len;
886 struct strbuf tree_buf = STRBUF_INIT;
887 struct object_id new_oid;
888
889 for (i = 0; i < it->subtree_nr; i++) {
890 strbuf_addf(path, "%s/", it->down[i]->name);
Phillip Woodf7510972021-10-07 18:07:21891 if (verify_one(r, istate, it->down[i]->cache_tree, path))
892 return 1;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28893 strbuf_setlen(path, len);
894 }
895
896 if (it->entry_count < 0 ||
897 /* no verification on tests (t7003) that replace trees */
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02898 lookup_replace_object(r, &it->oid) != &it->oid)
Phillip Woodf7510972021-10-07 18:07:21899 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28900
901 if (path->len) {
Phillip Woodf7510972021-10-07 18:07:21902 /*
903 * If the index is sparse and the cache tree is not
904 * index_name_pos() may trigger ensure_full_index() which will
905 * free the tree that is being verified.
906 */
907 int is_sparse = istate->sparse_index;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28908 pos = index_name_pos(istate, path->buf, path->len);
Phillip Woodf7510972021-10-07 18:07:21909 if (is_sparse && !istate->sparse_index)
910 return 1;
Derrick Stolee9ad2d5e2021-03-30 13:11:03911
912 if (pos >= 0) {
913 verify_one_sparse(r, istate, it, path, pos);
Phillip Woodf7510972021-10-07 18:07:21914 return 0;
Derrick Stolee9ad2d5e2021-03-30 13:11:03915 }
916
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28917 pos = -pos - 1;
918 } else {
919 pos = 0;
920 }
921
922 i = 0;
923 while (i < it->entry_count) {
924 struct cache_entry *ce = istate->cache[pos + i];
925 const char *slash;
926 struct cache_tree_sub *sub = NULL;
927 const struct object_id *oid;
928 const char *name;
929 unsigned mode;
930 int entlen;
931
932 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
933 BUG("%s with flags 0x%x should not be in cache-tree",
934 ce->name, ce->ce_flags);
935 name = ce->name + path->len;
936 slash = strchr(name, '/');
937 if (slash) {
938 entlen = slash - name;
939 sub = find_subtree(it, ce->name + path->len, entlen, 0);
940 if (!sub || sub->cache_tree->entry_count < 0)
941 BUG("bad subtree '%.*s'", entlen, name);
942 oid = &sub->cache_tree->oid;
943 mode = S_IFDIR;
944 i += sub->cache_tree->entry_count;
945 } else {
946 oid = &ce->oid;
947 mode = ce->ce_mode;
948 entlen = ce_namelen(ce) - path->len;
949 i++;
950 }
951 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
Matheus Tavaresa6519462020-01-30 20:32:18952 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28953 }
Ævar Arnfjörð Bjarmason44439c12022-02-04 23:48:32954 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
Matheus Tavares2dcde202020-01-30 20:32:22955 &new_oid);
Jeff Kinge43d2dc2018-10-02 21:19:21956 if (!oideq(&new_oid, &it->oid))
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28957 BUG("cache-tree for path %.*s does not match. "
958 "Expected %s got %s", len, path->buf,
959 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
960 strbuf_setlen(path, len);
961 strbuf_release(&tree_buf);
Phillip Woodf7510972021-10-07 18:07:21962 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28963}
964
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:02965void cache_tree_verify(struct repository *r, struct index_state *istate)
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28966{
967 struct strbuf path = STRBUF_INIT;
968
969 if (!istate->cache_tree)
970 return;
Phillip Woodf7510972021-10-07 18:07:21971 if (verify_one(r, istate, istate->cache_tree, &path)) {
972 strbuf_reset(&path);
973 if (verify_one(r, istate, istate->cache_tree, &path))
974 BUG("ensure_full_index() called twice while verifying cache tree");
975 }
Nguyễn Thái Ngọc Duy4592e602018-08-18 14:41:28976 strbuf_release(&path);
977}