blob: 641427ed410af39be80be22460a103dbad2d3d35 [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00001#include "git-compat-util.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00002#include "environment.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00003#include "hex.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02004#include "lockfile.h"
Junio C Hamano74986462006-04-23 16:52:20 -07005#include "tree.h"
Junio C Hamanob9d37a52009-04-20 03:58:18 -07006#include "tree-walk.h"
Junio C Hamano74986462006-04-23 16:52:20 -07007#include "cache-tree.h"
Neeraj Singh4d33e2b2022-04-04 22:20:10 -07008#include "bulk-checkin.h"
Elijah Newren87bed172023-04-11 00:41:53 -07009#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000010#include "object-store-ll.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000011#include "read-cache-ll.h"
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +020012#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020013#include "promisor-remote.h"
Derrick Stolee6e773522021-03-30 13:10:55 +000014#include "sparse-index.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000015#include "trace.h"
16#include "trace2.h"
Junio C Hamano74986462006-04-23 16:52:20 -070017
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -070018#ifndef DEBUG_CACHE_TREE
19#define DEBUG_CACHE_TREE 0
Junio C Hamano49031612006-10-30 15:29:53 -080020#endif
Junio C Hamano74986462006-04-23 16:52:20 -070021
22struct cache_tree *cache_tree(void)
23{
24 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
25 it->entry_count = -1;
26 return it;
27}
28
Junio C Hamanobad68ec2006-04-24 21:18:58 -070029void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 16:52:20 -070030{
31 int i;
Junio C Hamanobad68ec2006-04-24 21:18:58 -070032 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 16:52:20 -070033
34 if (!it)
35 return;
36 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 15:40:16 -060037 if (it->down[i]) {
Junio C Hamano61fa3092006-04-25 17:40:02 -070038 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 15:40:16 -060039 free(it->down[i]);
40 }
Junio C Hamano74986462006-04-23 16:52:20 -070041 free(it->down);
42 free(it);
Junio C Hamanobad68ec2006-04-24 21:18:58 -070043 *it_p = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070044}
45
Junio C Hamano61fa3092006-04-25 17:40:02 -070046static int subtree_name_cmp(const char *one, int onelen,
47 const char *two, int twolen)
48{
49 if (onelen < twolen)
50 return -1;
51 if (twolen < onelen)
52 return 1;
53 return memcmp(one, two, onelen);
54}
55
Derrick Stoleec80dd392021-01-23 19:58:13 +000056int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
Junio C Hamano61fa3092006-04-25 17:40:02 -070057{
58 struct cache_tree_sub **down = it->down;
59 int lo, hi;
60 lo = 0;
61 hi = it->subtree_nr;
62 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -040063 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-25 17:40:02 -070064 struct cache_tree_sub *mdl = down[mi];
65 int cmp = subtree_name_cmp(path, pathlen,
66 mdl->name, mdl->namelen);
67 if (!cmp)
68 return mi;
69 if (cmp < 0)
70 hi = mi;
71 else
72 lo = mi + 1;
73 }
74 return -lo-1;
75}
76
Junio C Hamano74986462006-04-23 16:52:20 -070077static struct cache_tree_sub *find_subtree(struct cache_tree *it,
78 const char *path,
79 int pathlen,
80 int create)
81{
Junio C Hamano74986462006-04-23 16:52:20 -070082 struct cache_tree_sub *down;
Derrick Stoleec80dd392021-01-23 19:58:13 +000083 int pos = cache_tree_subtree_pos(it, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070084 if (0 <= pos)
85 return it->down[pos];
Junio C Hamano74986462006-04-23 16:52:20 -070086 if (!create)
87 return NULL;
Junio C Hamano61fa3092006-04-25 17:40:02 -070088
89 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-04 02:31:51 +040090 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-25 17:40:02 -070091 it->subtree_nr++;
92
Jeff King96ffc062016-02-22 17:44:32 -050093 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070094 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070095 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-25 17:40:02 -070096
97 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010098 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
99 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700100 it->down[pos] = down;
Junio C Hamano74986462006-04-23 16:52:20 -0700101 return down;
102}
103
Junio C Hamano7927a552006-04-27 01:33:07 -0700104struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
105{
106 int pathlen = strlen(path);
107 return find_subtree(it, path, pathlen, 1);
108}
109
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700110static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 16:52:20 -0700111{
112 /* a/b/c
113 * ==> invalidate self
114 * ==> find "a", have it invalidate "b/c"
115 * a
116 * ==> invalidate self
117 * ==> if "a" exists as a subtree, remove it.
118 */
119 const char *slash;
120 int namelen;
121 struct cache_tree_sub *down;
122
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700123#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700124 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
125#endif
126
Junio C Hamano74986462006-04-23 16:52:20 -0700127 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700128 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800129 slash = strchrnul(path, '/');
130 namelen = slash - path;
Junio C Hamano74986462006-04-23 16:52:20 -0700131 it->entry_count = -1;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800132 if (!*slash) {
Junio C Hamano61fa3092006-04-25 17:40:02 -0700133 int pos;
Derrick Stoleec80dd392021-01-23 19:58:13 +0000134 pos = cache_tree_subtree_pos(it, path, namelen);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700135 if (0 <= pos) {
136 cache_tree_free(&it->down[pos]->cache_tree);
137 free(it->down[pos]);
Junio C Hamano74986462006-04-23 16:52:20 -0700138 /* 0 1 2 3 4 5
139 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-25 17:40:02 -0700140 * pos
Junio C Hamano74986462006-04-23 16:52:20 -0700141 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-25 17:40:02 -0700142 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 16:52:20 -0700143 */
René Scharfef331ab92017-07-15 22:00:45 +0200144 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
145 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 16:52:20 -0700146 it->subtree_nr--;
147 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700148 return 1;
Junio C Hamano74986462006-04-23 16:52:20 -0700149 }
Junio C Hamano74986462006-04-23 16:52:20 -0700150 down = find_subtree(it, path, namelen, 0);
151 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700152 do_invalidate_path(down->cache_tree, slash + 1);
153 return 1;
154}
155
156void cache_tree_invalidate_path(struct index_state *istate, const char *path)
157{
158 if (do_invalidate_path(istate->cache_tree, path))
159 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700160}
161
Derrick Stolee8d87e332021-01-23 19:58:12 +0000162static int verify_cache(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700163{
Derrick Stolee8d87e332021-01-23 19:58:12 +0000164 unsigned i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700165 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 16:52:20 -0700166
167 /* Verify that the tree is merged */
168 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12 +0000169 for (i = 0; i < istate->cache_nr; i++) {
170 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano3f6d56d2012-02-07 11:55:48 -0800171 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 18:43:37 +0100172 if (silent)
173 return -1;
Junio C Hamano74986462006-04-23 16:52:20 -0700174 if (10 < ++funny) {
175 fprintf(stderr, "...\n");
176 break;
177 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 11:15:25 +0700178 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52 +0000179 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700180 }
181 }
182 if (funny)
183 return -1;
184
185 /* Also verify that the cache does not have path and path/file
186 * at the same time. At this point we know the cache has only
187 * stage 0 entries.
188 */
189 funny = 0;
Derrick Stolee8d87e332021-01-23 19:58:12 +0000190 for (i = 0; i + 1 < istate->cache_nr; i++) {
Junio C Hamano74986462006-04-23 16:52:20 -0700191 /* path/file always comes after path because of the way
192 * the cache is sorted. Also path can appear only once,
193 * which means conflicting one would immediately follow.
194 */
Derrick Stolee8d87e332021-01-23 19:58:12 +0000195 const struct cache_entry *this_ce = istate->cache[i];
196 const struct cache_entry *next_ce = istate->cache[i + 1];
René Scharfe0b725362021-01-07 16:32:10 +0000197 const char *this_name = this_ce->name;
198 const char *next_name = next_ce->name;
199 int this_len = ce_namelen(this_ce);
200 if (this_len < ce_namelen(next_ce) &&
Derrick Stoleea4b6d202021-01-07 16:32:11 +0000201 next_name[this_len] == '/' &&
202 strncmp(this_name, next_name, this_len) == 0) {
Junio C Hamano74986462006-04-23 16:52:20 -0700203 if (10 < ++funny) {
204 fprintf(stderr, "...\n");
205 break;
206 }
207 fprintf(stderr, "You have both %s and %s\n",
208 this_name, next_name);
209 }
210 }
211 if (funny)
212 return -1;
213 return 0;
214}
215
216static void discard_unused_subtrees(struct cache_tree *it)
217{
218 struct cache_tree_sub **down = it->down;
219 int nr = it->subtree_nr;
220 int dst, src;
221 for (dst = src = 0; src < nr; src++) {
222 struct cache_tree_sub *s = down[src];
223 if (s->used)
224 down[dst++] = s;
225 else {
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700226 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 16:52:20 -0700227 free(s);
228 it->subtree_nr--;
229 }
230 }
231}
232
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700233int cache_tree_fully_valid(struct cache_tree *it)
234{
235 int i;
236 if (!it)
237 return 0;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200238 if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700239 return 0;
240 for (i = 0; i < it->subtree_nr; i++) {
241 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
242 return 0;
243 }
244 return 1;
245}
246
Jonathan Tand3da2232021-07-23 11:52:23 -0700247static int must_check_existence(const struct cache_entry *ce)
248{
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200249 return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
Jonathan Tand3da2232021-07-23 11:52:23 -0700250}
251
Junio C Hamano74986462006-04-23 16:52:20 -0700252static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700253 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 16:52:20 -0700254 int entries,
255 const char *base,
256 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700257 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700258 int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700259{
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200260 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700261 int missing_ok = flags & WRITE_TREE_MISSING_OK;
262 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-05 21:06:56 -0700263 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700264 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700265 int i;
266
David Turneraecf5672014-07-05 21:06:56 -0700267 assert(!(dryrun && repair));
268
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700269 *skip_count = 0;
270
Derrick Stolee2de37c52021-03-30 13:11:02 +0000271 /*
272 * If the first entry of this region is a sparse directory
273 * entry corresponding exactly to 'base', then this cache_tree
274 * struct is a "leaf" in the data structure, pointing to the
275 * tree OID specified in the entry.
276 */
277 if (entries > 0) {
278 const struct cache_entry *ce = cache[0];
279
280 if (S_ISSPARSEDIR(ce->ce_mode) &&
281 ce->ce_namelen == baselen &&
282 !strncmp(ce->name, base, baselen)) {
283 it->entry_count = 1;
284 oidcpy(&it->oid, &ce->oid);
285 return 1;
286 }
287 }
288
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200289 if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
Junio C Hamano74986462006-04-23 16:52:20 -0700290 return it->entry_count;
291
292 /*
293 * We first scan for subtrees and update them; we start by
294 * marking existing subtrees -- the ones that are unmarked
295 * should not be in the result.
296 */
297 for (i = 0; i < it->subtree_nr; i++)
298 it->down[i]->used = 0;
299
300 /*
301 * Find the subtrees and update them.
302 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700303 i = 0;
304 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700305 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 16:52:20 -0700306 struct cache_tree_sub *sub;
307 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700308 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700309
310 path = ce->name;
311 pathlen = ce_namelen(ce);
312 if (pathlen <= baselen || memcmp(base, path, baselen))
313 break; /* at the end of this level */
314
315 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700316 if (!slash) {
317 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700318 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700319 }
Junio C Hamano74986462006-04-23 16:52:20 -0700320 /*
321 * a/bbb/c (base = a/, slash = /c)
322 * ==>
323 * path+baselen = bbb/c, sublen = 3
324 */
325 sublen = slash - (path + baselen);
326 sub = find_subtree(it, path + baselen, sublen, 1);
327 if (!sub->cache_tree)
328 sub->cache_tree = cache_tree();
329 subcnt = update_one(sub->cache_tree,
330 cache + i, entries - i,
331 path,
332 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700333 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700334 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00 +0000335 if (subcnt < 0)
336 return subcnt;
Jeff King729dbbd2014-10-29 13:11:58 -0400337 if (!subcnt)
338 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700339 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700340 sub->count = subcnt; /* to be used in the next loop */
341 *skip_count += subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700342 sub->used = 1;
343 }
344
345 discard_unused_subtrees(it);
346
347 /*
348 * Then write out the tree object for this level.
349 */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200350 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 16:52:20 -0700351
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700352 i = 0;
353 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700354 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200355 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -0700356 const char *path, *slash;
357 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000358 const struct object_id *oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700359 unsigned mode;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700360 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200361 int contains_ita = 0;
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700362 int ce_missing_ok;
Junio C Hamano74986462006-04-23 16:52:20 -0700363
364 path = ce->name;
365 pathlen = ce_namelen(ce);
366 if (pathlen <= baselen || memcmp(base, path, baselen))
367 break; /* at the end of this level */
368
369 slash = strchr(path + baselen, '/');
370 if (slash) {
371 entlen = slash - (path + baselen);
372 sub = find_subtree(it, path + baselen, entlen, 0);
373 if (!sub)
374 die("cache-tree.c: '%.*s' in '%s' not found",
375 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700376 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000377 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700378 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200379 contains_ita = sub->cache_tree->entry_count < 0;
380 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700381 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700382 expected_missing = 1;
383 }
Junio C Hamano74986462006-04-23 16:52:20 -0700384 }
385 else {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000386 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800387 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 16:52:20 -0700388 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700389 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700390 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -0400391
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700392 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
Jonathan Tand3da2232021-07-23 11:52:23 -0700393 !must_check_existence(ce);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000394 if (is_null_oid(oid) ||
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200395 (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500396 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700397 if (expected_missing)
398 return -1;
Linus Torvaldsa3883732009-07-14 11:25:17 -0700399 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24 +0000400 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500401 }
Junio C Hamano74986462006-04-23 16:52:20 -0700402
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700403 /*
404 * CE_REMOVE entries are removed before the index is
405 * written to disk. Skip them to remain consistent
406 * with the future on-disk index.
407 */
408 if (ce->ce_flags & CE_REMOVE) {
409 *skip_count = *skip_count + 1;
410 continue;
411 }
412
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700413 /*
Andrei Rybakb39a8412023-01-07 14:56:55 +0100414 * CE_INTENT_TO_ADD entries exist in on-disk index but
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700415 * they are not part of generated trees. Invalidate up
416 * to root to force cache-tree users to read elsewhere.
417 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200418 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700419 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700420 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700421 }
Junio C Hamano74986462006-04-23 16:52:20 -0700422
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200423 /*
424 * "sub" can be an empty tree if all subentries are i-t-a.
425 */
brian m. carlsona0554932018-05-02 00:26:04 +0000426 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200427 continue;
428
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200429 strbuf_grow(&buffer, entlen + 100);
430 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24 +0000431 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700432
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700433#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700434 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700435 mode, entlen, path + baselen);
436#endif
437 }
438
David Turneraecf5672014-07-05 21:06:56 -0700439 if (repair) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100440 struct object_id oid;
Matheus Tavares2dcde202020-01-30 17:32:22 -0300441 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100442 OBJ_TREE, &oid);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200443 if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
Patryk Obaraf070fac2018-01-28 01:13:13 +0100444 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-05 21:06:56 -0700445 else
446 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100447 } else if (dryrun) {
Matheus Tavares2dcde202020-01-30 17:32:22 -0300448 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100449 OBJ_TREE, &it->oid);
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100450 } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +0200451 &it->oid, flags & WRITE_TREE_SILENT
452 ? HASH_SILENT : 0)) {
Junio C Hamanoedae5f02008-04-23 09:47:17 -0700453 strbuf_release(&buffer);
454 return -1;
455 }
456
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200457 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700458 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700459#if DEBUG_CACHE_TREE
Junio C Hamano00703e62006-05-03 16:10:45 -0700460 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700461 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000462 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700463#endif
464 return i;
465}
466
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700467int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700468{
Derrick Stoleefb088262021-01-23 19:58:11 +0000469 int skip, i;
470
Derrick Stolee8d87e332021-01-23 19:58:12 +0000471 i = verify_cache(istate, flags);
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700472
Junio C Hamano74986462006-04-23 16:52:20 -0700473 if (i)
474 return i;
Derrick Stoleefb088262021-01-23 19:58:11 +0000475
476 if (!istate->cache_tree)
477 istate->cache_tree = cache_tree();
478
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200479 if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
Jonathan Tand3da2232021-07-23 11:52:23 -0700480 prefetch_cache_entries(istate, must_check_existence);
481
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200482 trace_performance_enter();
Derrick Stoleefa7ca5d2021-01-04 03:09:12 +0000483 trace2_region_enter("cache_tree", "update", the_repository);
Neeraj Singh4d33e2b2022-04-04 22:20:10 -0700484 begin_odb_transaction();
Derrick Stoleefb088262021-01-23 19:58:11 +0000485 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
486 "", 0, &skip, flags);
Neeraj Singh4d33e2b2022-04-04 22:20:10 -0700487 end_odb_transaction();
Derrick Stoleefa7ca5d2021-01-04 03:09:12 +0000488 trace2_region_leave("cache_tree", "update", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200489 trace_performance_leave("cache_tree_update");
Junio C Hamano74986462006-04-23 16:52:20 -0700490 if (i < 0)
491 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700492 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700493 return 0;
494}
495
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200496static void write_one(struct strbuf *buffer, struct cache_tree *it,
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +0100497 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 16:52:20 -0700498{
499 int i;
500
501 /* One "cache-tree" entry consists of the following:
502 * path (NUL terminated)
503 * entry_count, subtree_nr ("%d %d\n")
504 * tree-sha1 (missing if invalid)
505 * subtree_nr "cache-tree" entries for subtrees.
506 */
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200507 strbuf_grow(buffer, pathlen + 100);
508 strbuf_add(buffer, path, pathlen);
509 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 16:52:20 -0700510
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700511#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 16:52:20 -0700512 if (0 <= it->entry_count)
513 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
514 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000515 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700516 else
517 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
518 pathlen, path, it->subtree_nr);
519#endif
520
521 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000522 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700523 }
524 for (i = 0; i < it->subtree_nr; i++) {
525 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-25 17:40:02 -0700526 if (i) {
527 struct cache_tree_sub *prev = it->down[i-1];
528 if (subtree_name_cmp(down->name, down->namelen,
529 prev->name, prev->namelen) <= 0)
530 die("fatal - unsorted cache subtree");
531 }
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200532 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 16:52:20 -0700533 }
Junio C Hamano74986462006-04-23 16:52:20 -0700534}
535
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200536void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 16:52:20 -0700537{
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000538 trace2_region_enter("cache_tree", "write", the_repository);
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200539 write_one(sb, root, "", 0);
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000540 trace2_region_leave("cache_tree", "write", the_repository);
Junio C Hamano74986462006-04-23 16:52:20 -0700541}
542
543static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
544{
545 const char *buf = *buffer;
546 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200547 const char *cp;
548 char *ep;
Junio C Hamano74986462006-04-23 16:52:20 -0700549 struct cache_tree *it;
550 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000551 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700552
553 it = NULL;
554 /* skip name, but make sure name exists */
555 while (size && *buf) {
556 size--;
557 buf++;
558 }
559 if (!size)
560 goto free_return;
561 buf++; size--;
562 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200563
564 cp = buf;
565 it->entry_count = strtol(cp, &ep, 10);
566 if (cp == ep)
567 goto free_return;
568 cp = ep;
569 subtree_nr = strtol(cp, &ep, 10);
570 if (cp == ep)
Junio C Hamano74986462006-04-23 16:52:20 -0700571 goto free_return;
572 while (size && *buf && *buf != '\n') {
573 size--;
574 buf++;
575 }
576 if (!size)
577 goto free_return;
578 buf++; size--;
579 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000580 if (size < rawsz)
Junio C Hamano74986462006-04-23 16:52:20 -0700581 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29 +0000582 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000583 buf += rawsz;
584 size -= rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700585 }
586
Jeff Hostetlere9b9cc52019-06-19 14:05:58 -0700587#if DEBUG_CACHE_TREE
Junio C Hamano74986462006-04-23 16:52:20 -0700588 if (0 <= it->entry_count)
589 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
590 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000591 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700592 else
593 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
594 *buffer, subtree_nr);
595#endif
596
597 /*
598 * Just a heuristic -- we do not add directories that often but
599 * we do not want to have to extend it immediately when we do,
600 * hence +2.
601 */
602 it->subtree_alloc = subtree_nr + 2;
René Scharfeca56dad2021-03-13 17:17:22 +0100603 CALLOC_ARRAY(it->down, it->subtree_alloc);
Junio C Hamano74986462006-04-23 16:52:20 -0700604 for (i = 0; i < subtree_nr; i++) {
605 /* read each subtree */
606 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700607 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 16:52:20 -0700608 const char *name = buf;
Junio C Hamano7927a552006-04-27 01:33:07 -0700609
Junio C Hamano74986462006-04-23 16:52:20 -0700610 sub = read_one(&buf, &size);
611 if (!sub)
612 goto free_return;
Junio C Hamano7927a552006-04-27 01:33:07 -0700613 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700614 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 16:52:20 -0700615 }
616 if (subtree_nr != it->subtree_nr)
617 die("cache-tree: internal error");
618 *buffer = buf;
619 *size_p = size;
620 return it;
621
622 free_return:
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700623 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 16:52:20 -0700624 return NULL;
625}
626
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700627struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 16:52:20 -0700628{
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000629 struct cache_tree *result;
630
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700631 if (buffer[0])
Junio C Hamano74986462006-04-23 16:52:20 -0700632 return NULL; /* not the whole tree */
Derrick Stolee4c3e1872021-01-04 03:09:13 +0000633
634 trace2_region_enter("cache_tree", "read", the_repository);
635 result = read_one(&buffer, &size);
636 trace2_region_leave("cache_tree", "read", the_repository);
637
638 return result;
Junio C Hamano74986462006-04-23 16:52:20 -0700639}
Junio C Hamano6bd20352006-04-26 01:20:50 -0700640
Nanako Shiraishi7ba04d92008-07-16 19:42:10 +0900641static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 01:20:50 -0700642{
Junio C Hamanob87fc962009-05-20 15:53:57 -0700643 if (!it)
644 return NULL;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700645 while (*path) {
646 const char *slash;
647 struct cache_tree_sub *sub;
648
Michael Haggerty17e22dd2014-03-05 18:26:26 +0100649 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 18:26:27 +0100650 /*
651 * Between path and slash is the name of the subtree
652 * to look for.
Junio C Hamano6bd20352006-04-26 01:20:50 -0700653 */
654 sub = find_subtree(it, path, slash - path, 0);
655 if (!sub)
656 return NULL;
657 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 18:26:30 +0100658
Junio C Hamano6bd20352006-04-26 01:20:50 -0700659 path = slash;
Michael Haggerty34910472014-03-05 18:26:30 +0100660 while (*path == '/')
661 path++;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700662 }
663 return it;
664}
Junio C Hamano45525bd2008-01-10 22:49:35 -0800665
Elijah Newren724dd762019-08-17 11:41:32 -0700666static int write_index_as_tree_internal(struct object_id *oid,
667 struct index_state *index_state,
668 int cache_tree_valid,
669 int flags,
670 const char *prefix)
671{
672 if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
673 cache_tree_free(&index_state->cache_tree);
674 cache_tree_valid = 0;
675 }
676
Elijah Newren724dd762019-08-17 11:41:32 -0700677 if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
678 return WRITE_TREE_UNMERGED_INDEX;
679
680 if (prefix) {
681 struct cache_tree *subtree;
682 subtree = cache_tree_find(index_state->cache_tree, prefix);
683 if (!subtree)
684 return WRITE_TREE_PREFIX_ERROR;
685 oidcpy(oid, &subtree->oid);
686 }
687 else
688 oidcpy(oid, &index_state->cache_tree->oid);
689
690 return 0;
691}
692
693struct tree* write_in_core_index_as_tree(struct repository *repo) {
694 struct object_id o;
695 int was_valid, ret;
696
697 struct index_state *index_state = repo->index;
698 was_valid = index_state->cache_tree &&
699 cache_tree_fully_valid(index_state->cache_tree);
700
701 ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
702 if (ret == WRITE_TREE_UNMERGED_INDEX) {
703 int i;
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 14:25:37 +0200704 bug("there are unmerged index entries:");
Elijah Newren724dd762019-08-17 11:41:32 -0700705 for (i = 0; i < index_state->cache_nr; i++) {
706 const struct cache_entry *ce = index_state->cache[i];
707 if (ce_stage(ce))
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 14:25:37 +0200708 bug("%d %.*s", ce_stage(ce),
709 (int)ce_namelen(ce), ce->name);
Elijah Newren724dd762019-08-17 11:41:32 -0700710 }
Ævar Arnfjörð Bjarmason6d40f0a2022-06-02 14:25:37 +0200711 BUG("unmerged index entries when writing in-core index");
Elijah Newren724dd762019-08-17 11:41:32 -0700712 }
713
714 return lookup_tree(repo, &index_state->cache_tree->oid);
715}
716
717
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000718int 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 -0800719{
Martin Ågren2954e5e2017-10-05 22:32:08 +0200720 int entries, was_valid;
Jeff Kingbfffb482017-09-05 08:15:21 -0400721 struct lock_file lock_file = LOCK_INIT;
Elijah Newren724dd762019-08-17 11:41:32 -0700722 int ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800723
Martin Ågren2954e5e2017-10-05 22:32:08 +0200724 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800725
Thomas Gummerera125a222018-01-07 22:30:13 +0000726 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 08:14:07 -0400727 if (entries < 0) {
728 ret = WRITE_TREE_UNREADABLE_INDEX;
729 goto out;
730 }
Junio C Hamano45525bd2008-01-10 22:49:35 -0800731
Elijah Newren724dd762019-08-17 11:41:32 -0700732 was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
733 index_state->cache_tree &&
734 cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800735
Elijah Newren724dd762019-08-17 11:41:32 -0700736 ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
737 prefix);
738 if (!ret && !was_valid) {
Martin Ågren2954e5e2017-10-05 22:32:08 +0200739 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800740 /* Not being able to write is fine -- we are only interested
741 * in updating the cache-tree part, and if the next caller
742 * ends up using the old index with unupdated cache-tree part
743 * it misses the work we did here, but that is just a
744 * performance penalty and not a big deal.
745 */
746 }
747
Jeff Kingc82c75b2017-09-05 08:14:07 -0400748out:
Martin Ågren2954e5e2017-10-05 22:32:08 +0200749 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400750 return ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800751}
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700752
Victoria Dye20ec2d02021-11-29 15:52:41 +0000753static void prime_cache_tree_sparse_dir(struct cache_tree *it,
754 struct tree *tree)
755{
756
757 oidcpy(&it->oid, &tree->object.oid);
758 it->entry_count = 1;
759}
760
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100761static void prime_cache_tree_rec(struct repository *r,
762 struct cache_tree *it,
Victoria Dye20ec2d02021-11-29 15:52:41 +0000763 struct tree *tree,
764 struct strbuf *tree_path)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700765{
766 struct tree_desc desc;
767 struct name_entry entry;
768 int cnt;
René Scharfe93ea1182023-02-10 21:20:30 +0100769 size_t base_path_len = tree_path->len;
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700770
brian m. carlsone0a92802017-05-01 02:28:56 +0000771 oidcpy(&it->oid, &tree->object.oid);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000772
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700773 init_tree_desc(&desc, tree->buffer, tree->size);
774 cnt = 0;
775 while (tree_entry(&desc, &entry)) {
776 if (!S_ISDIR(entry.mode))
777 cnt++;
778 else {
779 struct cache_tree_sub *sub;
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000780 struct tree *subtree = lookup_tree(r, &entry.oid);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000781
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700782 if (!subtree->object.parsed)
783 parse_tree(subtree);
784 sub = cache_tree_sub(it, entry.path);
785 sub->cache_tree = cache_tree();
Victoria Dye20ec2d02021-11-29 15:52:41 +0000786
787 /*
788 * Recursively-constructed subtree path is only needed when working
789 * in a sparse index (where it's used to determine whether the
790 * subtree is a sparse directory in the index).
791 */
792 if (r->index->sparse_index) {
793 strbuf_setlen(tree_path, base_path_len);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000794 strbuf_add(tree_path, entry.path, entry.pathlen);
795 strbuf_addch(tree_path, '/');
796 }
797
798 /*
799 * If a sparse index is in use, the directory being processed may be
800 * sparse. To confirm that, we can check whether an entry with that
801 * exact name exists in the index. If it does, the created subtree
802 * should be sparse. Otherwise, cache tree expansion should continue
803 * as normal.
804 */
805 if (r->index->sparse_index &&
806 index_entry_exists(r->index, tree_path->buf, tree_path->len))
807 prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
808 else
809 prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700810 cnt += sub->cache_tree->entry_count;
811 }
812 }
Victoria Dye20ec2d02021-11-29 15:52:41 +0000813
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700814 it->entry_count = cnt;
815}
816
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100817void prime_cache_tree(struct repository *r,
818 struct index_state *istate,
819 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700820{
Victoria Dye20ec2d02021-11-29 15:52:41 +0000821 struct strbuf tree_path = STRBUF_INIT;
822
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200823 trace2_region_enter("cache-tree", "prime_cache_tree", r);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700824 cache_tree_free(&istate->cache_tree);
825 istate->cache_tree = cache_tree();
Derrick Stolee0e5c9502021-01-04 03:09:14 +0000826
Victoria Dye20ec2d02021-11-29 15:52:41 +0000827 prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
828 strbuf_release(&tree_path);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700829 istate->cache_changed |= CACHE_TREE_CHANGED;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200830 trace2_region_leave("cache-tree", "prime_cache_tree", r);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700831}
Junio C Hamanob65982b2009-05-20 15:57:22 -0700832
833/*
834 * find the cache_tree that corresponds to the current level without
835 * exploding the full path into textual form. The root of the
836 * cache tree is given as "root", and our current level is "info".
837 * (1) When at root level, info->prev is NULL, so it is "root" itself.
838 * (2) Otherwise, find the cache_tree that corresponds to one level
839 * above us, and find ourselves in there.
840 */
841static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
842 struct traverse_info *info)
843{
844 struct cache_tree *our_parent;
845
846 if (!info->prev)
847 return root;
848 our_parent = find_cache_tree_from_traversal(root, info->prev);
Jeff King90553842019-07-31 00:38:15 -0400849 return cache_tree_find(our_parent, info->name);
Junio C Hamanob65982b2009-05-20 15:57:22 -0700850}
851
852int cache_tree_matches_traversal(struct cache_tree *root,
853 struct name_entry *ent,
854 struct traverse_info *info)
855{
856 struct cache_tree *it;
857
858 it = find_cache_tree_from_traversal(root, info);
859 it = cache_tree_find(it, ent->path);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000860 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 15:57:22 -0700861 return it->entry_count;
862 return 0;
863}
Thomas Rast996277c2011-12-06 18:43:37 +0100864
Jeff King5db8e592022-08-20 05:02:48 -0400865static void verify_one_sparse(struct index_state *istate,
Derrick Stolee9ad2d5e2021-03-30 13:11:03 +0000866 struct strbuf *path,
867 int pos)
868{
869 struct cache_entry *ce = istate->cache[pos];
870
871 if (!S_ISSPARSEDIR(ce->ce_mode))
872 BUG("directory '%s' is present in index, but not sparse",
873 path->buf);
874}
875
Phillip Woodf7510972021-10-07 18:07:21 +0000876/*
877 * Returns:
878 * 0 - Verification completed.
879 * 1 - Restart verification - a call to ensure_full_index() freed the cache
880 * tree that is being verified and verification needs to be restarted from
881 * the new toplevel cache tree.
882 */
883static int verify_one(struct repository *r,
884 struct index_state *istate,
885 struct cache_tree *it,
886 struct strbuf *path)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200887{
888 int i, pos, len = path->len;
889 struct strbuf tree_buf = STRBUF_INIT;
890 struct object_id new_oid;
891
892 for (i = 0; i < it->subtree_nr; i++) {
893 strbuf_addf(path, "%s/", it->down[i]->name);
Phillip Woodf7510972021-10-07 18:07:21 +0000894 if (verify_one(r, istate, it->down[i]->cache_tree, path))
895 return 1;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200896 strbuf_setlen(path, len);
897 }
898
899 if (it->entry_count < 0 ||
900 /* no verification on tests (t7003) that replace trees */
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100901 lookup_replace_object(r, &it->oid) != &it->oid)
Phillip Woodf7510972021-10-07 18:07:21 +0000902 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200903
904 if (path->len) {
Phillip Woodf7510972021-10-07 18:07:21 +0000905 /*
906 * If the index is sparse and the cache tree is not
907 * index_name_pos() may trigger ensure_full_index() which will
908 * free the tree that is being verified.
909 */
910 int is_sparse = istate->sparse_index;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200911 pos = index_name_pos(istate, path->buf, path->len);
Phillip Woodf7510972021-10-07 18:07:21 +0000912 if (is_sparse && !istate->sparse_index)
913 return 1;
Derrick Stolee9ad2d5e2021-03-30 13:11:03 +0000914
915 if (pos >= 0) {
Jeff King5db8e592022-08-20 05:02:48 -0400916 verify_one_sparse(istate, path, pos);
Phillip Woodf7510972021-10-07 18:07:21 +0000917 return 0;
Derrick Stolee9ad2d5e2021-03-30 13:11:03 +0000918 }
919
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200920 pos = -pos - 1;
921 } else {
922 pos = 0;
923 }
924
925 i = 0;
926 while (i < it->entry_count) {
927 struct cache_entry *ce = istate->cache[pos + i];
928 const char *slash;
929 struct cache_tree_sub *sub = NULL;
930 const struct object_id *oid;
931 const char *name;
932 unsigned mode;
933 int entlen;
934
935 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
936 BUG("%s with flags 0x%x should not be in cache-tree",
937 ce->name, ce->ce_flags);
938 name = ce->name + path->len;
939 slash = strchr(name, '/');
940 if (slash) {
941 entlen = slash - name;
942 sub = find_subtree(it, ce->name + path->len, entlen, 0);
943 if (!sub || sub->cache_tree->entry_count < 0)
944 BUG("bad subtree '%.*s'", entlen, name);
945 oid = &sub->cache_tree->oid;
946 mode = S_IFDIR;
947 i += sub->cache_tree->entry_count;
948 } else {
949 oid = &ce->oid;
950 mode = ce->ce_mode;
951 entlen = ce_namelen(ce) - path->len;
952 i++;
953 }
954 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
Matheus Tavaresa6519462020-01-30 17:32:18 -0300955 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200956 }
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100957 hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
Matheus Tavares2dcde202020-01-30 17:32:22 -0300958 &new_oid);
Jeff Kinge43d2dc2018-10-02 17:19:21 -0400959 if (!oideq(&new_oid, &it->oid))
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200960 BUG("cache-tree for path %.*s does not match. "
961 "Expected %s got %s", len, path->buf,
962 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
963 strbuf_setlen(path, len);
964 strbuf_release(&tree_buf);
Phillip Woodf7510972021-10-07 18:07:21 +0000965 return 0;
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200966}
967
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100968void cache_tree_verify(struct repository *r, struct index_state *istate)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200969{
970 struct strbuf path = STRBUF_INIT;
971
972 if (!istate->cache_tree)
973 return;
Phillip Woodf7510972021-10-07 18:07:21 +0000974 if (verify_one(r, istate, istate->cache_tree, &path)) {
975 strbuf_reset(&path);
976 if (verify_one(r, istate, istate->cache_tree, &path))
977 BUG("ensure_full_index() called twice while verifying cache tree");
978 }
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200979 strbuf_release(&path);
980}