blob: ff14b527da38acc6968d88df65a1e1ebfd854b5b [file] [log] [blame]
Elijah Newrena6dc3d32023-03-21 06:25:53 +00001#include "cache.h"
Elijah Newren36bf1952023-02-24 00:09:24 +00002#include "alloc.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00003#include "environment.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02005#include "lockfile.h"
Junio C Hamano74986462006-04-23 16:52:20 -07006#include "tree.h"
Junio C Hamanob9d37a52009-04-20 03:58:18 -07007#include "tree-walk.h"
Junio C Hamano74986462006-04-23 16:52:20 -07008#include "cache-tree.h"
Neeraj Singh4d33e2b2022-04-04 22:20:10 -07009#include "bulk-checkin.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070010#include "object-store.h"
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +020011#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020012#include "promisor-remote.h"
Derrick Stolee6e773522021-03-30 13:10:55 +000013#include "sparse-index.h"
Junio C Hamano74986462006-04-23 16:52:20 -070014
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -070015#ifndef DEBUG_CACHE_TREE
16#define DEBUG_CACHE_TREE 0
Junio C Hamano49031612006-10-30 15:29:53 -080017#endif
Junio C Hamano74986462006-04-23 16:52:20 -070018
19struct cache_tree *cache_tree(void)
20{
21 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
22 it->entry_count = -1;
23 return it;
24}
25
Junio C Hamanobad68ec2006-04-24 21:18:58 -070026void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 16:52:20 -070027{
28 int i;
Junio C Hamanobad68ec2006-04-24 21:18:58 -070029 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 16:52:20 -070030
31 if (!it)
32 return;
33 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 15:40:16 -060034 if (it->down[i]) {
Junio C Hamano61fa3092006-04-25 17:40:02 -070035 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 15:40:16 -060036 free(it->down[i]);
37 }
Junio C Hamano74986462006-04-23 16:52:20 -070038 free(it->down);
39 free(it);
Junio C Hamanobad68ec2006-04-24 21:18:58 -070040 *it_p = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070041}
42
Junio C Hamano61fa3092006-04-25 17:40:02 -070043static int subtree_name_cmp(const char *one, int onelen,
44 const char *two, int twolen)
45{
46 if (onelen < twolen)
47 return -1;
48 if (twolen < onelen)
49 return 1;
50 return memcmp(one, two, onelen);
51}
52
Derrick Stoleec80dd392021-01-23 19:58:13 +000053int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
Junio C Hamano61fa3092006-04-25 17:40:02 -070054{
55 struct cache_tree_sub **down = it->down;
56 int lo, hi;
57 lo = 0;
58 hi = it->subtree_nr;
59 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -040060 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-25 17:40:02 -070061 struct cache_tree_sub *mdl = down[mi];
62 int cmp = subtree_name_cmp(path, pathlen,
63 mdl->name, mdl->namelen);
64 if (!cmp)
65 return mi;
66 if (cmp < 0)
67 hi = mi;
68 else
69 lo = mi + 1;
70 }
71 return -lo-1;
72}
73
Junio C Hamano74986462006-04-23 16:52:20 -070074static struct cache_tree_sub *find_subtree(struct cache_tree *it,
75 const char *path,
76 int pathlen,
77 int create)
78{
Junio C Hamano74986462006-04-23 16:52:20 -070079 struct cache_tree_sub *down;
Derrick Stoleec80dd392021-01-23 19:58:13 +000080 int pos = cache_tree_subtree_pos(it, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070081 if (0 <= pos)
82 return it->down[pos];
Junio C Hamano74986462006-04-23 16:52:20 -070083 if (!create)
84 return NULL;
Junio C Hamano61fa3092006-04-25 17:40:02 -070085
86 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-04 02:31:51 +040087 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-25 17:40:02 -070088 it->subtree_nr++;
89
Jeff King96ffc062016-02-22 17:44:32 -050090 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070091 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070092 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-25 17:40:02 -070093
94 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010095 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
96 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-25 17:40:02 -070097 it->down[pos] = down;
Junio C Hamano74986462006-04-23 16:52:20 -070098 return down;
99}
100
Junio C Hamano7927a552006-04-27 01:33:07 -0700101struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
102{
103 int pathlen = strlen(path);
104 return find_subtree(it, path, pathlen, 1);
105}
106
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700107static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 16:52:20 -0700108{
109 /* a/b/c
110 * ==> invalidate self
111 * ==> find "a", have it invalidate "b/c"
112 * a
113 * ==> invalidate self
114 * ==> if "a" exists as a subtree, remove it.
115 */
116 const char *slash;
117 int namelen;
118 struct cache_tree_sub *down;
119
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700120#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700121 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
122#endif
123
Junio C Hamano74986462006-04-23 16:52:20 -0700124 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700125 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800126 slash = strchrnul(path, '/');
127 namelen = slash - path;
Junio C Hamano74986462006-04-23 16:52:20 -0700128 it->entry_count = -1;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800129 if (!*slash) {
Junio C Hamano61fa3092006-04-25 17:40:02 -0700130 int pos;
Derrick Stoleec80dd392021-01-23 19:58:13 +0000131 pos = cache_tree_subtree_pos(it, path, namelen);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700132 if (0 <= pos) {
133 cache_tree_free(&it->down[pos]->cache_tree);
134 free(it->down[pos]);
Junio C Hamano74986462006-04-23 16:52:20 -0700135 /* 0 1 2 3 4 5
136 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-25 17:40:02 -0700137 * pos
Junio C Hamano74986462006-04-23 16:52:20 -0700138 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-25 17:40:02 -0700139 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 16:52:20 -0700140 */
René Scharfef331ab92017-07-15 22:00:45 +0200141 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
142 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 16:52:20 -0700143 it->subtree_nr--;
144 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700145 return 1;
Junio C Hamano74986462006-04-23 16:52:20 -0700146 }
Junio C Hamano74986462006-04-23 16:52:20 -0700147 down = find_subtree(it, path, namelen, 0);
148 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700149 do_invalidate_path(down->cache_tree, slash + 1);
150 return 1;
151}
152
153void cache_tree_invalidate_path(struct index_state *istate, const char *path)
154{
155 if (do_invalidate_path(istate->cache_tree, path))
156 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700157}
158
Derrick Stolee8d87e332021-01-23 19:58:12 +0000159static int verify_cache(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700160{
Derrick Stolee8d87e332021-01-23 19:58:12 +0000161 unsigned i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700162 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 16:52:20 -0700163
164 /* Verify that the tree is merged */
165 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12 +0000166 for (i = 0; i < istate->cache_nr; i++) {
167 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano3f6d56d2012-02-07 11:55:48 -0800168 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 18:43:37 +0100169 if (silent)
170 return -1;
Junio C Hamano74986462006-04-23 16:52:20 -0700171 if (10 < ++funny) {
172 fprintf(stderr, "...\n");
173 break;
174 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 11:15:25 +0700175 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52 +0000176 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700177 }
178 }
179 if (funny)
180 return -1;
181
182 /* Also verify that the cache does not have path and path/file
183 * at the same time. At this point we know the cache has only
184 * stage 0 entries.
185 */
186 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12 +0000187 for (i = 0; i + 1 < istate->cache_nr; i++) {
Junio C Hamano74986462006-04-23 16:52:20 -0700188 /* path/file always comes after path because of the way
189 * the cache is sorted. Also path can appear only once,
190 * which means conflicting one would immediately follow.
191 */
Derrick Stolee8d87e332021-01-23 19:58:12 +0000192 const struct cache_entry *this_ce = istate->cache[i];
193 const struct cache_entry *next_ce = istate->cache[i + 1];
René Scharfe0b725362021-01-07 16:32:10 +0000194 const char *this_name = this_ce->name;
195 const char *next_name = next_ce->name;
196 int this_len = ce_namelen(this_ce);
197 if (this_len < ce_namelen(next_ce) &&
Derrick Stoleea4b6d202021-01-07 16:32:11 +0000198 next_name[this_len] == '/' &&
199 strncmp(this_name, next_name, this_len) == 0) {
Junio C Hamano74986462006-04-23 16:52:20 -0700200 if (10 < ++funny) {
201 fprintf(stderr, "...\n");
202 break;
203 }
204 fprintf(stderr, "You have both %s and %s\n",
205 this_name, next_name);
206 }
207 }
208 if (funny)
209 return -1;
210 return 0;
211}
212
213static void discard_unused_subtrees(struct cache_tree *it)
214{
215 struct cache_tree_sub **down = it->down;
216 int nr = it->subtree_nr;
217 int dst, src;
218 for (dst = src = 0; src < nr; src++) {
219 struct cache_tree_sub *s = down[src];
220 if (s->used)
221 down[dst++] = s;
222 else {
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700223 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 16:52:20 -0700224 free(s);
225 it->subtree_nr--;
226 }
227 }
228}
229
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700230int cache_tree_fully_valid(struct cache_tree *it)
231{
232 int i;
233 if (!it)
234 return 0;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200235 if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700236 return 0;
237 for (i = 0; i < it->subtree_nr; i++) {
238 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
239 return 0;
240 }
241 return 1;
242}
243
Jonathan Tand3da2232021-07-23 11:52:23 -0700244static int must_check_existence(const struct cache_entry *ce)
245{
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200246 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
Jonathan Tand3da2232021-07-23 11:52:23 -0700247}
248
Junio C Hamano74986462006-04-23 16:52:20 -0700249static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700250 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 16:52:20 -0700251 int entries,
252 const char *base,
253 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700254 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700255 int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700256{
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200257 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700258 int missing_ok = flags & WRITE_TREE_MISSING_OK;
259 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-05 21:06:56 -0700260 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700261 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700262 int i;
263
David Turneraecf5672014-07-05 21:06:56 -0700264 assert(!(dryrun && repair));
265
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700266 *skip_count = 0;
267
Derrick Stolee2de37c52021-03-30 13:11:02 +0000268 /*
269 * If the first entry of this region is a sparse directory
270 * entry corresponding exactly to 'base', then this cache_tree
271 * struct is a "leaf" in the data structure, pointing to the
272 * tree OID specified in the entry.
273 */
274 if (entries > 0) {
275 const struct cache_entry *ce = cache[0];
276
277 if (S_ISSPARSEDIR(ce->ce_mode) &&
278 ce->ce_namelen == baselen &&
279 !strncmp(ce->name, base, baselen)) {
280 it->entry_count = 1;
281 oidcpy(&it->oid, &ce->oid);
282 return 1;
283 }
284 }
285
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200286 if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
Junio C Hamano74986462006-04-23 16:52:20 -0700287 return it->entry_count;
288
289 /*
290 * We first scan for subtrees and update them; we start by
291 * marking existing subtrees -- the ones that are unmarked
292 * should not be in the result.
293 */
294 for (i = 0; i < it->subtree_nr; i++)
295 it->down[i]->used = 0;
296
297 /*
298 * Find the subtrees and update them.
299 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700300 i = 0;
301 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700302 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 16:52:20 -0700303 struct cache_tree_sub *sub;
304 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700305 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700306
307 path = ce->name;
308 pathlen = ce_namelen(ce);
309 if (pathlen <= baselen || memcmp(base, path, baselen))
310 break; /* at the end of this level */
311
312 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700313 if (!slash) {
314 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700315 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700316 }
Junio C Hamano74986462006-04-23 16:52:20 -0700317 /*
318 * a/bbb/c (base = a/, slash = /c)
319 * ==>
320 * path+baselen = bbb/c, sublen = 3
321 */
322 sublen = slash - (path + baselen);
323 sub = find_subtree(it, path + baselen, sublen, 1);
324 if (!sub->cache_tree)
325 sub->cache_tree = cache_tree();
326 subcnt = update_one(sub->cache_tree,
327 cache + i, entries - i,
328 path,
329 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700330 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700331 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00 +0000332 if (subcnt < 0)
333 return subcnt;
Jeff King729dbbd2014-10-29 13:11:58 -0400334 if (!subcnt)
335 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700336 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700337 sub->count = subcnt; /* to be used in the next loop */
338 *skip_count += subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700339 sub->used = 1;
340 }
341
342 discard_unused_subtrees(it);
343
344 /*
345 * Then write out the tree object for this level.
346 */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200347 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 16:52:20 -0700348
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700349 i = 0;
350 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700351 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200352 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -0700353 const char *path, *slash;
354 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000355 const struct object_id *oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700356 unsigned mode;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700357 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200358 int contains_ita = 0;
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700359 int ce_missing_ok;
Junio C Hamano74986462006-04-23 16:52:20 -0700360
361 path = ce->name;
362 pathlen = ce_namelen(ce);
363 if (pathlen <= baselen || memcmp(base, path, baselen))
364 break; /* at the end of this level */
365
366 slash = strchr(path + baselen, '/');
367 if (slash) {
368 entlen = slash - (path + baselen);
369 sub = find_subtree(it, path + baselen, entlen, 0);
370 if (!sub)
371 die("cache-tree.c: '%.*s' in '%s' not found",
372 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700373 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000374 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700375 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200376 contains_ita = sub->cache_tree->entry_count < 0;
377 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700378 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700379 expected_missing = 1;
380 }
Junio C Hamano74986462006-04-23 16:52:20 -0700381 }
382 else {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000383 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800384 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 16:52:20 -0700385 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700386 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700387 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -0400388
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700389 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
Jonathan Tand3da2232021-07-23 11:52:23 -0700390 !must_check_existence(ce);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000391 if (is_null_oid(oid) ||
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200392 (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500393 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700394 if (expected_missing)
395 return -1;
Linus Torvaldsa3883732009-07-14 11:25:17 -0700396 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24 +0000397 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500398 }
Junio C Hamano74986462006-04-23 16:52:20 -0700399
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700400 /*
401 * CE_REMOVE entries are removed before the index is
402 * written to disk. Skip them to remain consistent
403 * with the future on-disk index.
404 */
405 if (ce->ce_flags & CE_REMOVE) {
406 *skip_count = *skip_count + 1;
407 continue;
408 }
409
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700410 /*
Andrei Rybakb39a8412023-01-07 14:56:55 +0100411 * CE_INTENT_TO_ADD entries exist in on-disk index but
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700412 * they are not part of generated trees. Invalidate up
413 * to root to force cache-tree users to read elsewhere.
414 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200415 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700416 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700417 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700418 }
Junio C Hamano74986462006-04-23 16:52:20 -0700419
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200420 /*
421 * "sub" can be an empty tree if all subentries are i-t-a.
422 */
brian m. carlsona0554932018-05-02 00:26:04 +0000423 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200424 continue;
425
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200426 strbuf_grow(&buffer, entlen + 100);
427 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24 +0000428 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700429
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700430#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700431 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700432 mode, entlen, path + baselen);
433#endif
434 }
435
David Turneraecf5672014-07-05 21:06:56 -0700436 if (repair) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100437 struct object_id oid;
Matheus Tavares2dcde202020-01-30 17:32:22 -0300438 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100439 OBJ_TREE, &oid);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200440 if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
Patryk Obaraf070fac2018-01-28 01:13:13 +0100441 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-05 21:06:56 -0700442 else
443 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100444 } else if (dryrun) {
Matheus Tavares2dcde202020-01-30 17:32:22 -0300445 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100446 OBJ_TREE, &it->oid);
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100447 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +0200448 &it->oid, flags & WRITE_TREE_SILENT
449 ? HASH_SILENT : 0)) {
Junio C Hamanoedae5f02008-04-23 09:47:17 -0700450 strbuf_release(&buffer);
451 return -1;
452 }
453
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200454 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700455 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700456#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700457 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700458 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000459 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700460#endif
461 return i;
462}
463
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700464int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700465{
Derrick Stoleefb088262021-01-23 19:58:11 +0000466 int skip, i;
467
Derrick Stolee8d87e332021-01-23 19:58:12 +0000468 i = verify_cache(istate, flags);
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700469
Junio C Hamano74986462006-04-23 16:52:20 -0700470 if (i)
471 return i;
Derrick Stoleefb088262021-01-23 19:58:11 +0000472
473 if (!istate->cache_tree)
474 istate->cache_tree = cache_tree();
475
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200476 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
Jonathan Tand3da2232021-07-23 11:52:23 -0700477 prefetch_cache_entries(istate, must_check_existence);
478
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200479 trace_performance_enter();
Derrick Stoleefa7ca5d2021-01-04 03:09:12 +0000480 trace2_region_enter("cache_tree", "update", the_repository);
Neeraj Singh4d33e2b2022-04-04 22:20:10 -0700481 begin_odb_transaction();
Derrick Stoleefb088262021-01-23 19:58:11 +0000482 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
483 "", 0, &skip, flags);
Neeraj Singh4d33e2b2022-04-04 22:20:10 -0700484 end_odb_transaction();
Derrick Stoleefa7ca5d2021-01-04 03:09:12 +0000485 trace2_region_leave("cache_tree", "update", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200486 trace_performance_leave("cache_tree_update");
Junio C Hamano74986462006-04-23 16:52:20 -0700487 if (i < 0)
488 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700489 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700490 return 0;
491}
492
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200493static void write_one(struct strbuf *buffer, struct cache_tree *it,
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +0100494 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 16:52:20 -0700495{
496 int i;
497
498 /* One "cache-tree" entry consists of the following:
499 * path (NUL terminated)
500 * entry_count, subtree_nr ("%d %d\n")
501 * tree-sha1 (missing if invalid)
502 * subtree_nr "cache-tree" entries for subtrees.
503 */
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200504 strbuf_grow(buffer, pathlen + 100);
505 strbuf_add(buffer, path, pathlen);
506 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 16:52:20 -0700507
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700508#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 16:52:20 -0700509 if (0 <= it->entry_count)
510 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
511 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000512 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700513 else
514 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
515 pathlen, path, it->subtree_nr);
516#endif
517
518 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000519 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700520 }
521 for (i = 0; i < it->subtree_nr; i++) {
522 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-25 17:40:02 -0700523 if (i) {
524 struct cache_tree_sub *prev = it->down[i-1];
525 if (subtree_name_cmp(down->name, down->namelen,
526 prev->name, prev->namelen) <= 0)
527 die("fatal - unsorted cache subtree");
528 }
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200529 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 16:52:20 -0700530 }
Junio C Hamano74986462006-04-23 16:52:20 -0700531}
532
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200533void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 16:52:20 -0700534{
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000535 trace2_region_enter("cache_tree", "write", the_repository);
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200536 write_one(sb, root, "", 0);
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000537 trace2_region_leave("cache_tree", "write", the_repository);
Junio C Hamano74986462006-04-23 16:52:20 -0700538}
539
540static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
541{
542 const char *buf = *buffer;
543 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200544 const char *cp;
545 char *ep;
Junio C Hamano74986462006-04-23 16:52:20 -0700546 struct cache_tree *it;
547 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000548 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700549
550 it = NULL;
551 /* skip name, but make sure name exists */
552 while (size && *buf) {
553 size--;
554 buf++;
555 }
556 if (!size)
557 goto free_return;
558 buf++; size--;
559 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200560
561 cp = buf;
562 it->entry_count = strtol(cp, &ep, 10);
563 if (cp == ep)
564 goto free_return;
565 cp = ep;
566 subtree_nr = strtol(cp, &ep, 10);
567 if (cp == ep)
Junio C Hamano74986462006-04-23 16:52:20 -0700568 goto free_return;
569 while (size && *buf && *buf != '\n') {
570 size--;
571 buf++;
572 }
573 if (!size)
574 goto free_return;
575 buf++; size--;
576 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000577 if (size < rawsz)
Junio C Hamano74986462006-04-23 16:52:20 -0700578 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29 +0000579 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000580 buf += rawsz;
581 size -= rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700582 }
583
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700584#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 16:52:20 -0700585 if (0 <= it->entry_count)
586 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
587 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000588 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700589 else
590 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
591 *buffer, subtree_nr);
592#endif
593
594 /*
595 * Just a heuristic -- we do not add directories that often but
596 * we do not want to have to extend it immediately when we do,
597 * hence +2.
598 */
599 it->subtree_alloc = subtree_nr + 2;
René Scharfeca56dad2021-03-13 17:17:22 +0100600 CALLOC_ARRAY(it->down, it->subtree_alloc);
Junio C Hamano74986462006-04-23 16:52:20 -0700601 for (i = 0; i < subtree_nr; i++) {
602 /* read each subtree */
603 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700604 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 16:52:20 -0700605 const char *name = buf;
Junio C Hamano7927a552006-04-27 01:33:07 -0700606
Junio C Hamano74986462006-04-23 16:52:20 -0700607 sub = read_one(&buf, &size);
608 if (!sub)
609 goto free_return;
Junio C Hamano7927a552006-04-27 01:33:07 -0700610 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700611 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 16:52:20 -0700612 }
613 if (subtree_nr != it->subtree_nr)
614 die("cache-tree: internal error");
615 *buffer = buf;
616 *size_p = size;
617 return it;
618
619 free_return:
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700620 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 16:52:20 -0700621 return NULL;
622}
623
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700624struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 16:52:20 -0700625{
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000626 struct cache_tree *result;
627
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700628 if (buffer[0])
Junio C Hamano74986462006-04-23 16:52:20 -0700629 return NULL; /* not the whole tree */
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000630
631 trace2_region_enter("cache_tree", "read", the_repository);
632 result = read_one(&buffer, &size);
633 trace2_region_leave("cache_tree", "read", the_repository);
634
635 return result;
Junio C Hamano74986462006-04-23 16:52:20 -0700636}
Junio C Hamano6bd20352006-04-26 01:20:50 -0700637
Nanako Shiraishi7ba04d92008-07-16 19:42:10 +0900638static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 01:20:50 -0700639{
Junio C Hamanob87fc962009-05-20 15:53:57 -0700640 if (!it)
641 return NULL;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700642 while (*path) {
643 const char *slash;
644 struct cache_tree_sub *sub;
645
Michael Haggerty17e22dd2014-03-05 18:26:26 +0100646 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 18:26:27 +0100647 /*
648 * Between path and slash is the name of the subtree
649 * to look for.
Junio C Hamano6bd20352006-04-26 01:20:50 -0700650 */
651 sub = find_subtree(it, path, slash - path, 0);
652 if (!sub)
653 return NULL;
654 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 18:26:30 +0100655
Junio C Hamano6bd20352006-04-26 01:20:50 -0700656 path = slash;
Michael Haggerty34910472014-03-05 18:26:30 +0100657 while (*path == '/')
658 path++;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700659 }
660 return it;
661}
Junio C Hamano45525bd2008-01-10 22:49:35 -0800662
Elijah Newren724dd762019-08-17 11:41:32 -0700663static int write_index_as_tree_internal(struct object_id *oid,
664 struct index_state *index_state,
665 int cache_tree_valid,
666 int flags,
667 const char *prefix)
668{
669 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
670 cache_tree_free(&index_state->cache_tree);
671 cache_tree_valid = 0;
672 }
673
Elijah Newren724dd762019-08-17 11:41:32 -0700674 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
675 return WRITE_TREE_UNMERGED_INDEX;
676
677 if (prefix) {
678 struct cache_tree *subtree;
679 subtree = cache_tree_find(index_state->cache_tree, prefix);
680 if (!subtree)
681 return WRITE_TREE_PREFIX_ERROR;
682 oidcpy(oid, &subtree->oid);
683 }
684 else
685 oidcpy(oid, &index_state->cache_tree->oid);
686
687 return 0;
688}
689
690struct tree* write_in_core_index_as_tree(struct repository *repo) {
691 struct object_id o;
692 int was_valid, ret;
693
694 struct index_state *index_state = repo->index;
695 was_valid = index_state->cache_tree &&
696 cache_tree_fully_valid(index_state->cache_tree);
697
698 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
699 if (ret == WRITE_TREE_UNMERGED_INDEX) {
700 int i;
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 14:25:37 +0200701 bug("there are unmerged index entries:");
Elijah Newren724dd762019-08-17 11:41:32 -0700702 for (i = 0; i < index_state->cache_nr; i++) {
703 const struct cache_entry *ce = index_state->cache[i];
704 if (ce_stage(ce))
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 14:25:37 +0200705 bug("%d %.*s", ce_stage(ce),
706 (int)ce_namelen(ce), ce->name);
Elijah Newren724dd762019-08-17 11:41:32 -0700707 }
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 14:25:37 +0200708 BUG("unmerged index entries when writing in-core index");
Elijah Newren724dd762019-08-17 11:41:32 -0700709 }
710
711 return lookup_tree(repo, &index_state->cache_tree->oid);
712}
713
714
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000715int 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 -0800716{
Martin Ågren2954e5e2017-10-05 22:32:08 +0200717 int entries, was_valid;
Jeff Kingbfffb482017-09-05 08:15:21 -0400718 struct lock_file lock_file = LOCK_INIT;
Elijah Newren724dd762019-08-17 11:41:32 -0700719 int ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800720
Martin Ågren2954e5e2017-10-05 22:32:08 +0200721 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800722
Thomas Gummerera125a222018-01-07 22:30:13 +0000723 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 08:14:07 -0400724 if (entries < 0) {
725 ret = WRITE_TREE_UNREADABLE_INDEX;
726 goto out;
727 }
Junio C Hamano45525bd2008-01-10 22:49:35 -0800728
Elijah Newren724dd762019-08-17 11:41:32 -0700729 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
730 index_state->cache_tree &&
731 cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800732
Elijah Newren724dd762019-08-17 11:41:32 -0700733 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
734 prefix);
735 if (!ret && !was_valid) {
Martin Ågren2954e5e2017-10-05 22:32:08 +0200736 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800737 /* Not being able to write is fine -- we are only interested
738 * in updating the cache-tree part, and if the next caller
739 * ends up using the old index with unupdated cache-tree part
740 * it misses the work we did here, but that is just a
741 * performance penalty and not a big deal.
742 */
743 }
744
Jeff Kingc82c75b2017-09-05 08:14:07 -0400745out:
Martin Ågren2954e5e2017-10-05 22:32:08 +0200746 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400747 return ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800748}
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700749
Victoria Dye20ec2d02021-11-29 15:52:41 +0000750static void prime_cache_tree_sparse_dir(struct cache_tree *it,
751 struct tree *tree)
752{
753
754 oidcpy(&it->oid, &tree->object.oid);
755 it->entry_count = 1;
756}
757
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100758static void prime_cache_tree_rec(struct repository *r,
759 struct cache_tree *it,
Victoria Dye20ec2d02021-11-29 15:52:41 +0000760 struct tree *tree,
761 struct strbuf *tree_path)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700762{
763 struct tree_desc desc;
764 struct name_entry entry;
765 int cnt;
René Scharfe93ea1182023-02-10 21:20:30 +0100766 size_t base_path_len = tree_path->len;
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700767
brian m. carlsone0a92802017-05-01 02:28:56 +0000768 oidcpy(&it->oid, &tree->object.oid);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000769
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700770 init_tree_desc(&desc, tree->buffer, tree->size);
771 cnt = 0;
772 while (tree_entry(&desc, &entry)) {
773 if (!S_ISDIR(entry.mode))
774 cnt++;
775 else {
776 struct cache_tree_sub *sub;
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000777 struct tree *subtree = lookup_tree(r, &entry.oid);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000778
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700779 if (!subtree->object.parsed)
780 parse_tree(subtree);
781 sub = cache_tree_sub(it, entry.path);
782 sub->cache_tree = cache_tree();
Victoria Dye20ec2d02021-11-29 15:52:41 +0000783
784 /*
785 * Recursively-constructed subtree path is only needed when working
786 * in a sparse index (where it's used to determine whether the
787 * subtree is a sparse directory in the index).
788 */
789 if (r->index->sparse_index) {
790 strbuf_setlen(tree_path, base_path_len);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000791 strbuf_add(tree_path, entry.path, entry.pathlen);
792 strbuf_addch(tree_path, '/');
793 }
794
795 /*
796 * If a sparse index is in use, the directory being processed may be
797 * sparse. To confirm that, we can check whether an entry with that
798 * exact name exists in the index. If it does, the created subtree
799 * should be sparse. Otherwise, cache tree expansion should continue
800 * as normal.
801 */
802 if (r->index->sparse_index &&
803 index_entry_exists(r->index, tree_path->buf, tree_path->len))
804 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
805 else
806 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700807 cnt += sub->cache_tree->entry_count;
808 }
809 }
Victoria Dye20ec2d02021-11-29 15:52:41 +0000810
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700811 it->entry_count = cnt;
812}
813
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100814void prime_cache_tree(struct repository *r,
815 struct index_state *istate,
816 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700817{
Victoria Dye20ec2d02021-11-29 15:52:41 +0000818 struct strbuf tree_path = STRBUF_INIT;
819
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200820 trace2_region_enter("cache-tree", "prime_cache_tree", r);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700821 cache_tree_free(&istate->cache_tree);
822 istate->cache_tree = cache_tree();
Derrick Stolee0e5c9502021-01-04 03:09:14 +0000823
Victoria Dye20ec2d02021-11-29 15:52:41 +0000824 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
825 strbuf_release(&tree_path);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700826 istate->cache_changed |= CACHE_TREE_CHANGED;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200827 trace2_region_leave("cache-tree", "prime_cache_tree", r);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700828}
Junio C Hamanob65982b2009-05-20 15:57:22 -0700829
830/*
831 * find the cache_tree that corresponds to the current level without
832 * exploding the full path into textual form. The root of the
833 * cache tree is given as "root", and our current level is "info".
834 * (1) When at root level, info->prev is NULL, so it is "root" itself.
835 * (2) Otherwise, find the cache_tree that corresponds to one level
836 * above us, and find ourselves in there.
837 */
838static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
839 struct traverse_info *info)
840{
841 struct cache_tree *our_parent;
842
843 if (!info->prev)
844 return root;
845 our_parent = find_cache_tree_from_traversal(root, info->prev);
Jeff King90553842019-07-31 00:38:15 -0400846 return cache_tree_find(our_parent, info->name);
Junio C Hamanob65982b2009-05-20 15:57:22 -0700847}
848
849int cache_tree_matches_traversal(struct cache_tree *root,
850 struct name_entry *ent,
851 struct traverse_info *info)
852{
853 struct cache_tree *it;
854
855 it = find_cache_tree_from_traversal(root, info);
856 it = cache_tree_find(it, ent->path);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000857 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 15:57:22 -0700858 return it->entry_count;
859 return 0;
860}
Thomas Rast996277c2011-12-06 18:43:37 +0100861
Jeff King5db8e592022-08-20 05:02:48 -0400862static void verify_one_sparse(struct index_state *istate,
Derrick Stolee9ad2d5e2021-03-30 13:11:03 +0000863 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:21 +0000873/*
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 16:41:28 +0200884{
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:21 +0000891 if (verify_one(r, istate, it->down[i]->cache_tree, path))
892 return 1;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200893 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 06:49:02 +0100898 lookup_replace_object(r, &it->oid) != &it->oid)
Phillip Woodf7510972021-10-07 18:07:21 +0000899 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200900
901 if (path->len) {
Phillip Woodf7510972021-10-07 18:07:21 +0000902 /*
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 16:41:28 +0200908 pos = index_name_pos(istate, path->buf, path->len);
Phillip Woodf7510972021-10-07 18:07:21 +0000909 if (is_sparse && !istate->sparse_index)
910 return 1;
Derrick Stolee9ad2d5e2021-03-30 13:11:03 +0000911
912 if (pos >= 0) {
Jeff King5db8e592022-08-20 05:02:48 -0400913 verify_one_sparse(istate, path, pos);
Phillip Woodf7510972021-10-07 18:07:21 +0000914 return 0;
Derrick Stolee9ad2d5e2021-03-30 13:11:03 +0000915 }
916
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200917 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 17:32:18 -0300952 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200953 }
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100954 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
Matheus Tavares2dcde202020-01-30 17:32:22 -0300955 &new_oid);
Jeff Kinge43d2dc2018-10-02 17:19:21 -0400956 if (!oideq(&new_oid, &it->oid))
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200957 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:21 +0000962 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200963}
964
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100965void cache_tree_verify(struct repository *r, struct index_state *istate)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200966{
967 struct strbuf path = STRBUF_INIT;
968
969 if (!istate->cache_tree)
970 return;
Phillip Woodf7510972021-10-07 18:07:21 +0000971 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 16:41:28 +0200976 strbuf_release(&path);
977}