blob: 2fb483d3c0838998677d53327100db7bc134e2f3 [file] [log] [blame]
Junio C Hamano74986462006-04-23 16:52:20 -07001#include "cache.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02002#include "lockfile.h"
Junio C Hamano74986462006-04-23 16:52:20 -07003#include "tree.h"
Junio C Hamanob9d37a52009-04-20 03:58:18 -07004#include "tree-walk.h"
Junio C Hamano74986462006-04-23 16:52:20 -07005#include "cache-tree.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07006#include "object-store.h"
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02007#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +02008#include "promisor-remote.h"
Junio C Hamano74986462006-04-23 16:52:20 -07009
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -070010#ifndef DEBUG_CACHE_TREE
11#define DEBUG_CACHE_TREE 0
Junio C Hamano49031612006-10-30 15:29:53 -080012#endif
Junio C Hamano74986462006-04-23 16:52:20 -070013
14struct cache_tree *cache_tree(void)
15{
16 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
17 it->entry_count = -1;
18 return it;
19}
20
Junio C Hamanobad68ec2006-04-24 21:18:58 -070021void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 16:52:20 -070022{
23 int i;
Junio C Hamanobad68ec2006-04-24 21:18:58 -070024 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 16:52:20 -070025
26 if (!it)
27 return;
28 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 15:40:16 -060029 if (it->down[i]) {
Junio C Hamano61fa3092006-04-25 17:40:02 -070030 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 15:40:16 -060031 free(it->down[i]);
32 }
Junio C Hamano74986462006-04-23 16:52:20 -070033 free(it->down);
34 free(it);
Junio C Hamanobad68ec2006-04-24 21:18:58 -070035 *it_p = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070036}
37
Junio C Hamano61fa3092006-04-25 17:40:02 -070038static int subtree_name_cmp(const char *one, int onelen,
39 const char *two, int twolen)
40{
41 if (onelen < twolen)
42 return -1;
43 if (twolen < onelen)
44 return 1;
45 return memcmp(one, two, onelen);
46}
47
Derrick Stoleec80dd392021-01-23 19:58:13 +000048int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
Junio C Hamano61fa3092006-04-25 17:40:02 -070049{
50 struct cache_tree_sub **down = it->down;
51 int lo, hi;
52 lo = 0;
53 hi = it->subtree_nr;
54 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -040055 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-25 17:40:02 -070056 struct cache_tree_sub *mdl = down[mi];
57 int cmp = subtree_name_cmp(path, pathlen,
58 mdl->name, mdl->namelen);
59 if (!cmp)
60 return mi;
61 if (cmp < 0)
62 hi = mi;
63 else
64 lo = mi + 1;
65 }
66 return -lo-1;
67}
68
Junio C Hamano74986462006-04-23 16:52:20 -070069static struct cache_tree_sub *find_subtree(struct cache_tree *it,
70 const char *path,
71 int pathlen,
72 int create)
73{
Junio C Hamano74986462006-04-23 16:52:20 -070074 struct cache_tree_sub *down;
Derrick Stoleec80dd392021-01-23 19:58:13 +000075 int pos = cache_tree_subtree_pos(it, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070076 if (0 <= pos)
77 return it->down[pos];
Junio C Hamano74986462006-04-23 16:52:20 -070078 if (!create)
79 return NULL;
Junio C Hamano61fa3092006-04-25 17:40:02 -070080
81 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-04 02:31:51 +040082 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-25 17:40:02 -070083 it->subtree_nr++;
84
Jeff King96ffc062016-02-22 17:44:32 -050085 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070086 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070087 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-25 17:40:02 -070088
89 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010090 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
91 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-25 17:40:02 -070092 it->down[pos] = down;
Junio C Hamano74986462006-04-23 16:52:20 -070093 return down;
94}
95
Junio C Hamano7927a552006-04-27 01:33:07 -070096struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
97{
98 int pathlen = strlen(path);
99 return find_subtree(it, path, pathlen, 1);
100}
101
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700102static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 16:52:20 -0700103{
104 /* a/b/c
105 * ==> invalidate self
106 * ==> find "a", have it invalidate "b/c"
107 * a
108 * ==> invalidate self
109 * ==> if "a" exists as a subtree, remove it.
110 */
111 const char *slash;
112 int namelen;
113 struct cache_tree_sub *down;
114
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700115#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700116 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
117#endif
118
Junio C Hamano74986462006-04-23 16:52:20 -0700119 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700120 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800121 slash = strchrnul(path, '/');
122 namelen = slash - path;
Junio C Hamano74986462006-04-23 16:52:20 -0700123 it->entry_count = -1;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800124 if (!*slash) {
Junio C Hamano61fa3092006-04-25 17:40:02 -0700125 int pos;
Derrick Stoleec80dd392021-01-23 19:58:13 +0000126 pos = cache_tree_subtree_pos(it, path, namelen);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700127 if (0 <= pos) {
128 cache_tree_free(&it->down[pos]->cache_tree);
129 free(it->down[pos]);
Junio C Hamano74986462006-04-23 16:52:20 -0700130 /* 0 1 2 3 4 5
131 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-25 17:40:02 -0700132 * pos
Junio C Hamano74986462006-04-23 16:52:20 -0700133 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-25 17:40:02 -0700134 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 16:52:20 -0700135 */
René Scharfef331ab92017-07-15 22:00:45 +0200136 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
137 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 16:52:20 -0700138 it->subtree_nr--;
139 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700140 return 1;
Junio C Hamano74986462006-04-23 16:52:20 -0700141 }
Junio C Hamano74986462006-04-23 16:52:20 -0700142 down = find_subtree(it, path, namelen, 0);
143 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700144 do_invalidate_path(down->cache_tree, slash + 1);
145 return 1;
146}
147
148void cache_tree_invalidate_path(struct index_state *istate, const char *path)
149{
150 if (do_invalidate_path(istate->cache_tree, path))
151 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700152}
153
Derrick Stolee8d87e332021-01-23 19:58:12 +0000154static int verify_cache(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700155{
Derrick Stolee8d87e332021-01-23 19:58:12 +0000156 unsigned i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700157 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 16:52:20 -0700158
159 /* Verify that the tree is merged */
160 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12 +0000161 for (i = 0; i < istate->cache_nr; i++) {
162 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano3f6d56d2012-02-07 11:55:48 -0800163 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 18:43:37 +0100164 if (silent)
165 return -1;
Junio C Hamano74986462006-04-23 16:52:20 -0700166 if (10 < ++funny) {
167 fprintf(stderr, "...\n");
168 break;
169 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 11:15:25 +0700170 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52 +0000171 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700172 }
173 }
174 if (funny)
175 return -1;
176
177 /* Also verify that the cache does not have path and path/file
178 * at the same time. At this point we know the cache has only
179 * stage 0 entries.
180 */
181 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12 +0000182 for (i = 0; i + 1 < istate->cache_nr; i++) {
Junio C Hamano74986462006-04-23 16:52:20 -0700183 /* path/file always comes after path because of the way
184 * the cache is sorted. Also path can appear only once,
185 * which means conflicting one would immediately follow.
186 */
Derrick Stolee8d87e332021-01-23 19:58:12 +0000187 const struct cache_entry *this_ce = istate->cache[i];
188 const struct cache_entry *next_ce = istate->cache[i + 1];
René Scharfe0b725362021-01-07 16:32:10 +0000189 const char *this_name = this_ce->name;
190 const char *next_name = next_ce->name;
191 int this_len = ce_namelen(this_ce);
192 if (this_len < ce_namelen(next_ce) &&
Derrick Stoleea4b6d202021-01-07 16:32:11 +0000193 next_name[this_len] == '/' &&
194 strncmp(this_name, next_name, this_len) == 0) {
Junio C Hamano74986462006-04-23 16:52:20 -0700195 if (10 < ++funny) {
196 fprintf(stderr, "...\n");
197 break;
198 }
199 fprintf(stderr, "You have both %s and %s\n",
200 this_name, next_name);
201 }
202 }
203 if (funny)
204 return -1;
205 return 0;
206}
207
208static void discard_unused_subtrees(struct cache_tree *it)
209{
210 struct cache_tree_sub **down = it->down;
211 int nr = it->subtree_nr;
212 int dst, src;
213 for (dst = src = 0; src < nr; src++) {
214 struct cache_tree_sub *s = down[src];
215 if (s->used)
216 down[dst++] = s;
217 else {
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700218 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 16:52:20 -0700219 free(s);
220 it->subtree_nr--;
221 }
222 }
223}
224
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700225int cache_tree_fully_valid(struct cache_tree *it)
226{
227 int i;
228 if (!it)
229 return 0;
Jeff King98374a02019-01-07 03:37:54 -0500230 if (it->entry_count < 0 || !has_object_file(&it->oid))
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700231 return 0;
232 for (i = 0; i < it->subtree_nr; i++) {
233 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
234 return 0;
235 }
236 return 1;
237}
238
Junio C Hamano74986462006-04-23 16:52:20 -0700239static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700240 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 16:52:20 -0700241 int entries,
242 const char *base,
243 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700244 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700245 int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700246{
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200247 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700248 int missing_ok = flags & WRITE_TREE_MISSING_OK;
249 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-05 21:06:56 -0700250 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700251 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700252 int i;
253
David Turneraecf5672014-07-05 21:06:56 -0700254 assert(!(dryrun && repair));
255
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700256 *skip_count = 0;
257
Jeff King98374a02019-01-07 03:37:54 -0500258 if (0 <= it->entry_count && has_object_file(&it->oid))
Junio C Hamano74986462006-04-23 16:52:20 -0700259 return it->entry_count;
260
261 /*
262 * We first scan for subtrees and update them; we start by
263 * marking existing subtrees -- the ones that are unmarked
264 * should not be in the result.
265 */
266 for (i = 0; i < it->subtree_nr; i++)
267 it->down[i]->used = 0;
268
269 /*
270 * Find the subtrees and update them.
271 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700272 i = 0;
273 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700274 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 16:52:20 -0700275 struct cache_tree_sub *sub;
276 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700277 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700278
279 path = ce->name;
280 pathlen = ce_namelen(ce);
281 if (pathlen <= baselen || memcmp(base, path, baselen))
282 break; /* at the end of this level */
283
284 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700285 if (!slash) {
286 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700287 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700288 }
Junio C Hamano74986462006-04-23 16:52:20 -0700289 /*
290 * a/bbb/c (base = a/, slash = /c)
291 * ==>
292 * path+baselen = bbb/c, sublen = 3
293 */
294 sublen = slash - (path + baselen);
295 sub = find_subtree(it, path + baselen, sublen, 1);
296 if (!sub->cache_tree)
297 sub->cache_tree = cache_tree();
298 subcnt = update_one(sub->cache_tree,
299 cache + i, entries - i,
300 path,
301 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700302 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700303 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00 +0000304 if (subcnt < 0)
305 return subcnt;
Jeff King729dbbd2014-10-29 13:11:58 -0400306 if (!subcnt)
307 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700308 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700309 sub->count = subcnt; /* to be used in the next loop */
310 *skip_count += subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700311 sub->used = 1;
312 }
313
314 discard_unused_subtrees(it);
315
316 /*
317 * Then write out the tree object for this level.
318 */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200319 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 16:52:20 -0700320
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700321 i = 0;
322 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700323 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200324 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -0700325 const char *path, *slash;
326 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000327 const struct object_id *oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700328 unsigned mode;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700329 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200330 int contains_ita = 0;
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700331 int ce_missing_ok;
Junio C Hamano74986462006-04-23 16:52:20 -0700332
333 path = ce->name;
334 pathlen = ce_namelen(ce);
335 if (pathlen <= baselen || memcmp(base, path, baselen))
336 break; /* at the end of this level */
337
338 slash = strchr(path + baselen, '/');
339 if (slash) {
340 entlen = slash - (path + baselen);
341 sub = find_subtree(it, path + baselen, entlen, 0);
342 if (!sub)
343 die("cache-tree.c: '%.*s' in '%s' not found",
344 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700345 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000346 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700347 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200348 contains_ita = sub->cache_tree->entry_count < 0;
349 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700350 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700351 expected_missing = 1;
352 }
Junio C Hamano74986462006-04-23 16:52:20 -0700353 }
354 else {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000355 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800356 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 16:52:20 -0700357 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700358 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700359 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -0400360
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700361 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
Christian Couderb14ed5a2019-06-25 15:40:31 +0200362 (has_promisor_remote() &&
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700363 ce_skip_worktree(ce));
brian m. carlson6dcb4622018-03-12 02:27:24 +0000364 if (is_null_oid(oid) ||
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700365 (!ce_missing_ok && !has_object_file(oid))) {
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500366 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700367 if (expected_missing)
368 return -1;
Linus Torvaldsa3883732009-07-14 11:25:17 -0700369 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24 +0000370 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500371 }
Junio C Hamano74986462006-04-23 16:52:20 -0700372
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700373 /*
374 * CE_REMOVE entries are removed before the index is
375 * written to disk. Skip them to remain consistent
376 * with the future on-disk index.
377 */
378 if (ce->ce_flags & CE_REMOVE) {
379 *skip_count = *skip_count + 1;
380 continue;
381 }
382
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700383 /*
384 * CE_INTENT_TO_ADD entries exist on on-disk index but
385 * they are not part of generated trees. Invalidate up
386 * to root to force cache-tree users to read elsewhere.
387 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200388 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700389 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700390 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700391 }
Junio C Hamano74986462006-04-23 16:52:20 -0700392
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200393 /*
394 * "sub" can be an empty tree if all subentries are i-t-a.
395 */
brian m. carlsona0554932018-05-02 00:26:04 +0000396 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200397 continue;
398
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200399 strbuf_grow(&buffer, entlen + 100);
400 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24 +0000401 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700402
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700403#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700404 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700405 mode, entlen, path + baselen);
406#endif
407 }
408
David Turneraecf5672014-07-05 21:06:56 -0700409 if (repair) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100410 struct object_id oid;
Matheus Tavares2dcde202020-01-30 17:32:22 -0300411 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
412 tree_type, &oid);
Jonathan Tanf981ec12019-09-03 12:42:47 -0700413 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
Patryk Obaraf070fac2018-01-28 01:13:13 +0100414 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-05 21:06:56 -0700415 else
416 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100417 } else if (dryrun) {
Matheus Tavares2dcde202020-01-30 17:32:22 -0300418 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
419 tree_type, &it->oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +0100420 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
421 &it->oid)) {
Junio C Hamanoedae5f02008-04-23 09:47:17 -0700422 strbuf_release(&buffer);
423 return -1;
424 }
425
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200426 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700427 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700428#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700429 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700430 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000431 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700432#endif
433 return i;
434}
435
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700436int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700437{
Derrick Stoleefb088262021-01-23 19:58:11 +0000438 int skip, i;
439
Derrick Stolee8d87e332021-01-23 19:58:12 +0000440 i = verify_cache(istate, flags);
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700441
Junio C Hamano74986462006-04-23 16:52:20 -0700442 if (i)
443 return i;
Derrick Stoleefb088262021-01-23 19:58:11 +0000444
445 if (!istate->cache_tree)
446 istate->cache_tree = cache_tree();
447
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200448 trace_performance_enter();
Derrick Stoleefa7ca5d2021-01-04 03:09:12 +0000449 trace2_region_enter("cache_tree", "update", the_repository);
Derrick Stoleefb088262021-01-23 19:58:11 +0000450 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
451 "", 0, &skip, flags);
Derrick Stoleefa7ca5d2021-01-04 03:09:12 +0000452 trace2_region_leave("cache_tree", "update", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200453 trace_performance_leave("cache_tree_update");
Junio C Hamano74986462006-04-23 16:52:20 -0700454 if (i < 0)
455 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700456 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700457 return 0;
458}
459
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200460static void write_one(struct strbuf *buffer, struct cache_tree *it,
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +0100461 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 16:52:20 -0700462{
463 int i;
464
465 /* One "cache-tree" entry consists of the following:
466 * path (NUL terminated)
467 * entry_count, subtree_nr ("%d %d\n")
468 * tree-sha1 (missing if invalid)
469 * subtree_nr "cache-tree" entries for subtrees.
470 */
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200471 strbuf_grow(buffer, pathlen + 100);
472 strbuf_add(buffer, path, pathlen);
473 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 16:52:20 -0700474
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700475#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 16:52:20 -0700476 if (0 <= it->entry_count)
477 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
478 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000479 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700480 else
481 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
482 pathlen, path, it->subtree_nr);
483#endif
484
485 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000486 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700487 }
488 for (i = 0; i < it->subtree_nr; i++) {
489 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-25 17:40:02 -0700490 if (i) {
491 struct cache_tree_sub *prev = it->down[i-1];
492 if (subtree_name_cmp(down->name, down->namelen,
493 prev->name, prev->namelen) <= 0)
494 die("fatal - unsorted cache subtree");
495 }
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200496 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 16:52:20 -0700497 }
Junio C Hamano74986462006-04-23 16:52:20 -0700498}
499
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200500void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 16:52:20 -0700501{
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000502 trace2_region_enter("cache_tree", "write", the_repository);
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200503 write_one(sb, root, "", 0);
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000504 trace2_region_leave("cache_tree", "write", the_repository);
Junio C Hamano74986462006-04-23 16:52:20 -0700505}
506
507static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
508{
509 const char *buf = *buffer;
510 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200511 const char *cp;
512 char *ep;
Junio C Hamano74986462006-04-23 16:52:20 -0700513 struct cache_tree *it;
514 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000515 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700516
517 it = NULL;
518 /* skip name, but make sure name exists */
519 while (size && *buf) {
520 size--;
521 buf++;
522 }
523 if (!size)
524 goto free_return;
525 buf++; size--;
526 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200527
528 cp = buf;
529 it->entry_count = strtol(cp, &ep, 10);
530 if (cp == ep)
531 goto free_return;
532 cp = ep;
533 subtree_nr = strtol(cp, &ep, 10);
534 if (cp == ep)
Junio C Hamano74986462006-04-23 16:52:20 -0700535 goto free_return;
536 while (size && *buf && *buf != '\n') {
537 size--;
538 buf++;
539 }
540 if (!size)
541 goto free_return;
542 buf++; size--;
543 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000544 if (size < rawsz)
Junio C Hamano74986462006-04-23 16:52:20 -0700545 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29 +0000546 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000547 buf += rawsz;
548 size -= rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700549 }
550
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700551#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 16:52:20 -0700552 if (0 <= it->entry_count)
553 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
554 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000555 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700556 else
557 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
558 *buffer, subtree_nr);
559#endif
560
561 /*
562 * Just a heuristic -- we do not add directories that often but
563 * we do not want to have to extend it immediately when we do,
564 * hence +2.
565 */
566 it->subtree_alloc = subtree_nr + 2;
567 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
568 for (i = 0; i < subtree_nr; i++) {
569 /* read each subtree */
570 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700571 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 16:52:20 -0700572 const char *name = buf;
Junio C Hamano7927a552006-04-27 01:33:07 -0700573
Junio C Hamano74986462006-04-23 16:52:20 -0700574 sub = read_one(&buf, &size);
575 if (!sub)
576 goto free_return;
Junio C Hamano7927a552006-04-27 01:33:07 -0700577 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700578 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 16:52:20 -0700579 }
580 if (subtree_nr != it->subtree_nr)
581 die("cache-tree: internal error");
582 *buffer = buf;
583 *size_p = size;
584 return it;
585
586 free_return:
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700587 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 16:52:20 -0700588 return NULL;
589}
590
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700591struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 16:52:20 -0700592{
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000593 struct cache_tree *result;
594
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700595 if (buffer[0])
Junio C Hamano74986462006-04-23 16:52:20 -0700596 return NULL; /* not the whole tree */
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000597
598 trace2_region_enter("cache_tree", "read", the_repository);
599 result = read_one(&buffer, &size);
600 trace2_region_leave("cache_tree", "read", the_repository);
601
602 return result;
Junio C Hamano74986462006-04-23 16:52:20 -0700603}
Junio C Hamano6bd20352006-04-26 01:20:50 -0700604
Nanako Shiraishi7ba04d92008-07-16 19:42:10 +0900605static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 01:20:50 -0700606{
Junio C Hamanob87fc962009-05-20 15:53:57 -0700607 if (!it)
608 return NULL;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700609 while (*path) {
610 const char *slash;
611 struct cache_tree_sub *sub;
612
Michael Haggerty17e22dd2014-03-05 18:26:26 +0100613 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 18:26:27 +0100614 /*
615 * Between path and slash is the name of the subtree
616 * to look for.
Junio C Hamano6bd20352006-04-26 01:20:50 -0700617 */
618 sub = find_subtree(it, path, slash - path, 0);
619 if (!sub)
620 return NULL;
621 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 18:26:30 +0100622
Junio C Hamano6bd20352006-04-26 01:20:50 -0700623 path = slash;
Michael Haggerty34910472014-03-05 18:26:30 +0100624 while (*path == '/')
625 path++;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700626 }
627 return it;
628}
Junio C Hamano45525bd2008-01-10 22:49:35 -0800629
Elijah Newren724dd762019-08-17 11:41:32 -0700630static int write_index_as_tree_internal(struct object_id *oid,
631 struct index_state *index_state,
632 int cache_tree_valid,
633 int flags,
634 const char *prefix)
635{
636 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
637 cache_tree_free(&index_state->cache_tree);
638 cache_tree_valid = 0;
639 }
640
Elijah Newren724dd762019-08-17 11:41:32 -0700641 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
642 return WRITE_TREE_UNMERGED_INDEX;
643
644 if (prefix) {
645 struct cache_tree *subtree;
646 subtree = cache_tree_find(index_state->cache_tree, prefix);
647 if (!subtree)
648 return WRITE_TREE_PREFIX_ERROR;
649 oidcpy(oid, &subtree->oid);
650 }
651 else
652 oidcpy(oid, &index_state->cache_tree->oid);
653
654 return 0;
655}
656
657struct tree* write_in_core_index_as_tree(struct repository *repo) {
658 struct object_id o;
659 int was_valid, ret;
660
661 struct index_state *index_state = repo->index;
662 was_valid = index_state->cache_tree &&
663 cache_tree_fully_valid(index_state->cache_tree);
664
665 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
666 if (ret == WRITE_TREE_UNMERGED_INDEX) {
667 int i;
668 fprintf(stderr, "BUG: There are unmerged index entries:\n");
669 for (i = 0; i < index_state->cache_nr; i++) {
670 const struct cache_entry *ce = index_state->cache[i];
671 if (ce_stage(ce))
672 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
673 (int)ce_namelen(ce), ce->name);
674 }
675 BUG("unmerged index entries when writing inmemory index");
676 }
677
678 return lookup_tree(repo, &index_state->cache_tree->oid);
679}
680
681
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000682int 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-10 22:49:35 -0800683{
Martin Ågren2954e5e2017-10-05 22:32:08 +0200684 int entries, was_valid;
Jeff Kingbfffb482017-09-05 08:15:21 -0400685 struct lock_file lock_file = LOCK_INIT;
Elijah Newren724dd762019-08-17 11:41:32 -0700686 int ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800687
Martin Ågren2954e5e2017-10-05 22:32:08 +0200688 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800689
Thomas Gummerera125a222018-01-07 22:30:13 +0000690 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 08:14:07 -0400691 if (entries < 0) {
692 ret = WRITE_TREE_UNREADABLE_INDEX;
693 goto out;
694 }
Junio C Hamano45525bd2008-01-10 22:49:35 -0800695
Elijah Newren724dd762019-08-17 11:41:32 -0700696 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
697 index_state->cache_tree &&
698 cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800699
Elijah Newren724dd762019-08-17 11:41:32 -0700700 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
701 prefix);
702 if (!ret && !was_valid) {
Martin Ågren2954e5e2017-10-05 22:32:08 +0200703 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800704 /* Not being able to write is fine -- we are only interested
705 * in updating the cache-tree part, and if the next caller
706 * ends up using the old index with unupdated cache-tree part
707 * it misses the work we did here, but that is just a
708 * performance penalty and not a big deal.
709 */
710 }
711
Jeff Kingc82c75b2017-09-05 08:14:07 -0400712out:
Martin Ågren2954e5e2017-10-05 22:32:08 +0200713 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400714 return ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800715}
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700716
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100717static void prime_cache_tree_rec(struct repository *r,
718 struct cache_tree *it,
719 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700720{
721 struct tree_desc desc;
722 struct name_entry entry;
723 int cnt;
724
brian m. carlsone0a92802017-05-01 02:28:56 +0000725 oidcpy(&it->oid, &tree->object.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700726 init_tree_desc(&desc, tree->buffer, tree->size);
727 cnt = 0;
728 while (tree_entry(&desc, &entry)) {
729 if (!S_ISDIR(entry.mode))
730 cnt++;
731 else {
732 struct cache_tree_sub *sub;
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000733 struct tree *subtree = lookup_tree(r, &entry.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700734 if (!subtree->object.parsed)
735 parse_tree(subtree);
736 sub = cache_tree_sub(it, entry.path);
737 sub->cache_tree = cache_tree();
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100738 prime_cache_tree_rec(r, sub->cache_tree, subtree);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700739 cnt += sub->cache_tree->entry_count;
740 }
741 }
742 it->entry_count = cnt;
743}
744
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100745void prime_cache_tree(struct repository *r,
746 struct index_state *istate,
747 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700748{
Derrick Stolee0e5c9502021-01-04 03:09:14 +0000749 trace2_region_enter("cache-tree", "prime_cache_tree", the_repository);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700750 cache_tree_free(&istate->cache_tree);
751 istate->cache_tree = cache_tree();
Derrick Stolee0e5c9502021-01-04 03:09:14 +0000752
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100753 prime_cache_tree_rec(r, istate->cache_tree, tree);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700754 istate->cache_changed |= CACHE_TREE_CHANGED;
Derrick Stolee0e5c9502021-01-04 03:09:14 +0000755 trace2_region_leave("cache-tree", "prime_cache_tree", the_repository);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700756}
Junio C Hamanob65982b2009-05-20 15:57:22 -0700757
758/*
759 * find the cache_tree that corresponds to the current level without
760 * exploding the full path into textual form. The root of the
761 * cache tree is given as "root", and our current level is "info".
762 * (1) When at root level, info->prev is NULL, so it is "root" itself.
763 * (2) Otherwise, find the cache_tree that corresponds to one level
764 * above us, and find ourselves in there.
765 */
766static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
767 struct traverse_info *info)
768{
769 struct cache_tree *our_parent;
770
771 if (!info->prev)
772 return root;
773 our_parent = find_cache_tree_from_traversal(root, info->prev);
Jeff King90553842019-07-31 00:38:15 -0400774 return cache_tree_find(our_parent, info->name);
Junio C Hamanob65982b2009-05-20 15:57:22 -0700775}
776
777int cache_tree_matches_traversal(struct cache_tree *root,
778 struct name_entry *ent,
779 struct traverse_info *info)
780{
781 struct cache_tree *it;
782
783 it = find_cache_tree_from_traversal(root, info);
784 it = cache_tree_find(it, ent->path);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000785 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 15:57:22 -0700786 return it->entry_count;
787 return 0;
788}
Thomas Rast996277c2011-12-06 18:43:37 +0100789
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100790static void verify_one(struct repository *r,
791 struct index_state *istate,
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200792 struct cache_tree *it,
793 struct strbuf *path)
794{
795 int i, pos, len = path->len;
796 struct strbuf tree_buf = STRBUF_INIT;
797 struct object_id new_oid;
798
799 for (i = 0; i < it->subtree_nr; i++) {
800 strbuf_addf(path, "%s/", it->down[i]->name);
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100801 verify_one(r, istate, it->down[i]->cache_tree, path);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200802 strbuf_setlen(path, len);
803 }
804
805 if (it->entry_count < 0 ||
806 /* no verification on tests (t7003) that replace trees */
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100807 lookup_replace_object(r, &it->oid) != &it->oid)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200808 return;
809
810 if (path->len) {
811 pos = index_name_pos(istate, path->buf, path->len);
812 pos = -pos - 1;
813 } else {
814 pos = 0;
815 }
816
817 i = 0;
818 while (i < it->entry_count) {
819 struct cache_entry *ce = istate->cache[pos + i];
820 const char *slash;
821 struct cache_tree_sub *sub = NULL;
822 const struct object_id *oid;
823 const char *name;
824 unsigned mode;
825 int entlen;
826
827 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
828 BUG("%s with flags 0x%x should not be in cache-tree",
829 ce->name, ce->ce_flags);
830 name = ce->name + path->len;
831 slash = strchr(name, '/');
832 if (slash) {
833 entlen = slash - name;
834 sub = find_subtree(it, ce->name + path->len, entlen, 0);
835 if (!sub || sub->cache_tree->entry_count < 0)
836 BUG("bad subtree '%.*s'", entlen, name);
837 oid = &sub->cache_tree->oid;
838 mode = S_IFDIR;
839 i += sub->cache_tree->entry_count;
840 } else {
841 oid = &ce->oid;
842 mode = ce->ce_mode;
843 entlen = ce_namelen(ce) - path->len;
844 i++;
845 }
846 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
Matheus Tavaresa6519462020-01-30 17:32:18 -0300847 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200848 }
Matheus Tavares2dcde202020-01-30 17:32:22 -0300849 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
850 &new_oid);
Jeff Kinge43d2dc2018-10-02 17:19:21 -0400851 if (!oideq(&new_oid, &it->oid))
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200852 BUG("cache-tree for path %.*s does not match. "
853 "Expected %s got %s", len, path->buf,
854 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
855 strbuf_setlen(path, len);
856 strbuf_release(&tree_buf);
857}
858
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100859void cache_tree_verify(struct repository *r, struct index_state *istate)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200860{
861 struct strbuf path = STRBUF_INIT;
862
863 if (!istate->cache_tree)
864 return;
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100865 verify_one(r, istate, istate->cache_tree, &path);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200866 strbuf_release(&path);
867}