blob: b13bfaf71e9e1554173beca4cfd8acf5929bc9ce [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"
Junio C Hamano74986462006-04-23 16:52:20 -07008
Junio C Hamano49031612006-10-30 15:29:53 -08009#ifndef DEBUG
Junio C Hamano74986462006-04-23 16:52:20 -070010#define DEBUG 0
Junio C Hamano49031612006-10-30 15:29:53 -080011#endif
Junio C Hamano74986462006-04-23 16:52:20 -070012
13struct cache_tree *cache_tree(void)
14{
15 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
16 it->entry_count = -1;
17 return it;
18}
19
Junio C Hamanobad68ec2006-04-24 21:18:58 -070020void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 16:52:20 -070021{
22 int i;
Junio C Hamanobad68ec2006-04-24 21:18:58 -070023 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 16:52:20 -070024
25 if (!it)
26 return;
27 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 15:40:16 -060028 if (it->down[i]) {
Junio C Hamano61fa3092006-04-25 17:40:02 -070029 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 15:40:16 -060030 free(it->down[i]);
31 }
Junio C Hamano74986462006-04-23 16:52:20 -070032 free(it->down);
33 free(it);
Junio C Hamanobad68ec2006-04-24 21:18:58 -070034 *it_p = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070035}
36
Junio C Hamano61fa3092006-04-25 17:40:02 -070037static int subtree_name_cmp(const char *one, int onelen,
38 const char *two, int twolen)
39{
40 if (onelen < twolen)
41 return -1;
42 if (twolen < onelen)
43 return 1;
44 return memcmp(one, two, onelen);
45}
46
47static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
48{
49 struct cache_tree_sub **down = it->down;
50 int lo, hi;
51 lo = 0;
52 hi = it->subtree_nr;
53 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -040054 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-25 17:40:02 -070055 struct cache_tree_sub *mdl = down[mi];
56 int cmp = subtree_name_cmp(path, pathlen,
57 mdl->name, mdl->namelen);
58 if (!cmp)
59 return mi;
60 if (cmp < 0)
61 hi = mi;
62 else
63 lo = mi + 1;
64 }
65 return -lo-1;
66}
67
Junio C Hamano74986462006-04-23 16:52:20 -070068static struct cache_tree_sub *find_subtree(struct cache_tree *it,
69 const char *path,
70 int pathlen,
71 int create)
72{
Junio C Hamano74986462006-04-23 16:52:20 -070073 struct cache_tree_sub *down;
Junio C Hamano61fa3092006-04-25 17:40:02 -070074 int pos = subtree_pos(it, path, pathlen);
75 if (0 <= pos)
76 return it->down[pos];
Junio C Hamano74986462006-04-23 16:52:20 -070077 if (!create)
78 return NULL;
Junio C Hamano61fa3092006-04-25 17:40:02 -070079
80 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-04 02:31:51 +040081 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-25 17:40:02 -070082 it->subtree_nr++;
83
Jeff King96ffc062016-02-22 17:44:32 -050084 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070085 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070086 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-25 17:40:02 -070087
88 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010089 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
90 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-25 17:40:02 -070091 it->down[pos] = down;
Junio C Hamano74986462006-04-23 16:52:20 -070092 return down;
93}
94
Junio C Hamano7927a552006-04-27 01:33:07 -070095struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
96{
97 int pathlen = strlen(path);
98 return find_subtree(it, path, pathlen, 1);
99}
100
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700101static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 16:52:20 -0700102{
103 /* a/b/c
104 * ==> invalidate self
105 * ==> find "a", have it invalidate "b/c"
106 * a
107 * ==> invalidate self
108 * ==> if "a" exists as a subtree, remove it.
109 */
110 const char *slash;
111 int namelen;
112 struct cache_tree_sub *down;
113
Junio C Hamano00703e62006-05-03 16:10:45 -0700114#if DEBUG
115 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
116#endif
117
Junio C Hamano74986462006-04-23 16:52:20 -0700118 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700119 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800120 slash = strchrnul(path, '/');
121 namelen = slash - path;
Junio C Hamano74986462006-04-23 16:52:20 -0700122 it->entry_count = -1;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800123 if (!*slash) {
Junio C Hamano61fa3092006-04-25 17:40:02 -0700124 int pos;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700125 pos = subtree_pos(it, path, namelen);
126 if (0 <= pos) {
127 cache_tree_free(&it->down[pos]->cache_tree);
128 free(it->down[pos]);
Junio C Hamano74986462006-04-23 16:52:20 -0700129 /* 0 1 2 3 4 5
130 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-25 17:40:02 -0700131 * pos
Junio C Hamano74986462006-04-23 16:52:20 -0700132 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-25 17:40:02 -0700133 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 16:52:20 -0700134 */
René Scharfef331ab92017-07-15 22:00:45 +0200135 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
136 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 16:52:20 -0700137 it->subtree_nr--;
138 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700139 return 1;
Junio C Hamano74986462006-04-23 16:52:20 -0700140 }
Junio C Hamano74986462006-04-23 16:52:20 -0700141 down = find_subtree(it, path, namelen, 0);
142 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700143 do_invalidate_path(down->cache_tree, slash + 1);
144 return 1;
145}
146
147void cache_tree_invalidate_path(struct index_state *istate, const char *path)
148{
149 if (do_invalidate_path(istate->cache_tree, path))
150 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700151}
152
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700153static int verify_cache(struct cache_entry **cache,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700154 int entries, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700155{
156 int 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;
161 for (i = 0; i < entries; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700162 const struct cache_entry *ce = 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;
182 for (i = 0; i < entries - 1; i++) {
183 /* 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 */
187 const char *this_name = cache[i]->name;
188 const char *next_name = cache[i+1]->name;
189 int this_len = strlen(this_name);
190 if (this_len < strlen(next_name) &&
191 strncmp(this_name, next_name, this_len) == 0 &&
192 next_name[this_len] == '/') {
193 if (10 < ++funny) {
194 fprintf(stderr, "...\n");
195 break;
196 }
197 fprintf(stderr, "You have both %s and %s\n",
198 this_name, next_name);
199 }
200 }
201 if (funny)
202 return -1;
203 return 0;
204}
205
206static void discard_unused_subtrees(struct cache_tree *it)
207{
208 struct cache_tree_sub **down = it->down;
209 int nr = it->subtree_nr;
210 int dst, src;
211 for (dst = src = 0; src < nr; src++) {
212 struct cache_tree_sub *s = down[src];
213 if (s->used)
214 down[dst++] = s;
215 else {
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700216 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 16:52:20 -0700217 free(s);
218 it->subtree_nr--;
219 }
220 }
221}
222
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700223int cache_tree_fully_valid(struct cache_tree *it)
224{
225 int i;
226 if (!it)
227 return 0;
Jeff King98374a02019-01-07 03:37:54 -0500228 if (it->entry_count < 0 || !has_object_file(&it->oid))
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700229 return 0;
230 for (i = 0; i < it->subtree_nr; i++) {
231 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
232 return 0;
233 }
234 return 1;
235}
236
Junio C Hamano74986462006-04-23 16:52:20 -0700237static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700238 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 16:52:20 -0700239 int entries,
240 const char *base,
241 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700242 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700243 int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700244{
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200245 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700246 int missing_ok = flags & WRITE_TREE_MISSING_OK;
247 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-05 21:06:56 -0700248 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700249 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700250 int i;
251
David Turneraecf5672014-07-05 21:06:56 -0700252 assert(!(dryrun && repair));
253
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700254 *skip_count = 0;
255
Jeff King98374a02019-01-07 03:37:54 -0500256 if (0 <= it->entry_count && has_object_file(&it->oid))
Junio C Hamano74986462006-04-23 16:52:20 -0700257 return it->entry_count;
258
259 /*
260 * We first scan for subtrees and update them; we start by
261 * marking existing subtrees -- the ones that are unmarked
262 * should not be in the result.
263 */
264 for (i = 0; i < it->subtree_nr; i++)
265 it->down[i]->used = 0;
266
267 /*
268 * Find the subtrees and update them.
269 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700270 i = 0;
271 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700272 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 16:52:20 -0700273 struct cache_tree_sub *sub;
274 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700275 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700276
277 path = ce->name;
278 pathlen = ce_namelen(ce);
279 if (pathlen <= baselen || memcmp(base, path, baselen))
280 break; /* at the end of this level */
281
282 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700283 if (!slash) {
284 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700285 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700286 }
Junio C Hamano74986462006-04-23 16:52:20 -0700287 /*
288 * a/bbb/c (base = a/, slash = /c)
289 * ==>
290 * path+baselen = bbb/c, sublen = 3
291 */
292 sublen = slash - (path + baselen);
293 sub = find_subtree(it, path + baselen, sublen, 1);
294 if (!sub->cache_tree)
295 sub->cache_tree = cache_tree();
296 subcnt = update_one(sub->cache_tree,
297 cache + i, entries - i,
298 path,
299 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700300 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700301 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00 +0000302 if (subcnt < 0)
303 return subcnt;
Jeff King729dbbd2014-10-29 13:11:58 -0400304 if (!subcnt)
305 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700306 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700307 sub->count = subcnt; /* to be used in the next loop */
308 *skip_count += subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700309 sub->used = 1;
310 }
311
312 discard_unused_subtrees(it);
313
314 /*
315 * Then write out the tree object for this level.
316 */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200317 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 16:52:20 -0700318
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700319 i = 0;
320 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700321 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200322 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -0700323 const char *path, *slash;
324 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000325 const struct object_id *oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700326 unsigned mode;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700327 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200328 int contains_ita = 0;
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700329 int ce_missing_ok;
Junio C Hamano74986462006-04-23 16:52:20 -0700330
331 path = ce->name;
332 pathlen = ce_namelen(ce);
333 if (pathlen <= baselen || memcmp(base, path, baselen))
334 break; /* at the end of this level */
335
336 slash = strchr(path + baselen, '/');
337 if (slash) {
338 entlen = slash - (path + baselen);
339 sub = find_subtree(it, path + baselen, entlen, 0);
340 if (!sub)
341 die("cache-tree.c: '%.*s' in '%s' not found",
342 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700343 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000344 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700345 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200346 contains_ita = sub->cache_tree->entry_count < 0;
347 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700348 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700349 expected_missing = 1;
350 }
Junio C Hamano74986462006-04-23 16:52:20 -0700351 }
352 else {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000353 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800354 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 16:52:20 -0700355 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700356 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700357 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -0400358
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700359 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
360 (repository_format_partial_clone &&
361 ce_skip_worktree(ce));
brian m. carlson6dcb4622018-03-12 02:27:24 +0000362 if (is_null_oid(oid) ||
Jonathan Tan2f215ff2018-10-09 11:40:37 -0700363 (!ce_missing_ok && !has_object_file(oid))) {
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500364 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700365 if (expected_missing)
366 return -1;
Linus Torvaldsa3883732009-07-14 11:25:17 -0700367 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24 +0000368 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500369 }
Junio C Hamano74986462006-04-23 16:52:20 -0700370
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700371 /*
372 * CE_REMOVE entries are removed before the index is
373 * written to disk. Skip them to remain consistent
374 * with the future on-disk index.
375 */
376 if (ce->ce_flags & CE_REMOVE) {
377 *skip_count = *skip_count + 1;
378 continue;
379 }
380
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700381 /*
382 * CE_INTENT_TO_ADD entries exist on on-disk index but
383 * they are not part of generated trees. Invalidate up
384 * to root to force cache-tree users to read elsewhere.
385 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200386 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700387 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700388 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700389 }
Junio C Hamano74986462006-04-23 16:52:20 -0700390
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200391 /*
392 * "sub" can be an empty tree if all subentries are i-t-a.
393 */
brian m. carlsona0554932018-05-02 00:26:04 +0000394 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200395 continue;
396
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200397 strbuf_grow(&buffer, entlen + 100);
398 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24 +0000399 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700400
401#if DEBUG
Junio C Hamano00703e62006-05-03 16:10:45 -0700402 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700403 mode, entlen, path + baselen);
404#endif
405 }
406
David Turneraecf5672014-07-05 21:06:56 -0700407 if (repair) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100408 struct object_id oid;
409 hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000410 if (has_object_file(&oid))
Patryk Obaraf070fac2018-01-28 01:13:13 +0100411 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-05 21:06:56 -0700412 else
413 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100414 } else if (dryrun) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100415 hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +0100416 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
417 &it->oid)) {
Junio C Hamanoedae5f02008-04-23 09:47:17 -0700418 strbuf_release(&buffer);
419 return -1;
420 }
421
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200422 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700423 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Junio C Hamano74986462006-04-23 16:52:20 -0700424#if DEBUG
Junio C Hamano00703e62006-05-03 16:10:45 -0700425 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700426 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000427 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700428#endif
429 return i;
430}
431
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700432int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700433{
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700434 struct cache_tree *it = istate->cache_tree;
435 struct cache_entry **cache = istate->cache;
436 int entries = istate->cache_nr;
437 int skip, i = verify_cache(cache, entries, flags);
438
Junio C Hamano74986462006-04-23 16:52:20 -0700439 if (i)
440 return i;
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200441 trace_performance_enter();
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700442 i = update_one(it, cache, entries, "", 0, &skip, flags);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200443 trace_performance_leave("cache_tree_update");
Junio C Hamano74986462006-04-23 16:52:20 -0700444 if (i < 0)
445 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700446 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700447 return 0;
448}
449
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200450static void write_one(struct strbuf *buffer, struct cache_tree *it,
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +0100451 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 16:52:20 -0700452{
453 int i;
454
455 /* One "cache-tree" entry consists of the following:
456 * path (NUL terminated)
457 * entry_count, subtree_nr ("%d %d\n")
458 * tree-sha1 (missing if invalid)
459 * subtree_nr "cache-tree" entries for subtrees.
460 */
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200461 strbuf_grow(buffer, pathlen + 100);
462 strbuf_add(buffer, path, pathlen);
463 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 16:52:20 -0700464
465#if DEBUG
466 if (0 <= it->entry_count)
467 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
468 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000469 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700470 else
471 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
472 pathlen, path, it->subtree_nr);
473#endif
474
475 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000476 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700477 }
478 for (i = 0; i < it->subtree_nr; i++) {
479 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-25 17:40:02 -0700480 if (i) {
481 struct cache_tree_sub *prev = it->down[i-1];
482 if (subtree_name_cmp(down->name, down->namelen,
483 prev->name, prev->namelen) <= 0)
484 die("fatal - unsorted cache subtree");
485 }
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200486 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 16:52:20 -0700487 }
Junio C Hamano74986462006-04-23 16:52:20 -0700488}
489
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200490void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 16:52:20 -0700491{
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200492 write_one(sb, root, "", 0);
Junio C Hamano74986462006-04-23 16:52:20 -0700493}
494
495static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
496{
497 const char *buf = *buffer;
498 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200499 const char *cp;
500 char *ep;
Junio C Hamano74986462006-04-23 16:52:20 -0700501 struct cache_tree *it;
502 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000503 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700504
505 it = NULL;
506 /* skip name, but make sure name exists */
507 while (size && *buf) {
508 size--;
509 buf++;
510 }
511 if (!size)
512 goto free_return;
513 buf++; size--;
514 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200515
516 cp = buf;
517 it->entry_count = strtol(cp, &ep, 10);
518 if (cp == ep)
519 goto free_return;
520 cp = ep;
521 subtree_nr = strtol(cp, &ep, 10);
522 if (cp == ep)
Junio C Hamano74986462006-04-23 16:52:20 -0700523 goto free_return;
524 while (size && *buf && *buf != '\n') {
525 size--;
526 buf++;
527 }
528 if (!size)
529 goto free_return;
530 buf++; size--;
531 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000532 if (size < rawsz)
Junio C Hamano74986462006-04-23 16:52:20 -0700533 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29 +0000534 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000535 buf += rawsz;
536 size -= rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700537 }
538
539#if DEBUG
540 if (0 <= it->entry_count)
541 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
542 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000543 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700544 else
545 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
546 *buffer, subtree_nr);
547#endif
548
549 /*
550 * Just a heuristic -- we do not add directories that often but
551 * we do not want to have to extend it immediately when we do,
552 * hence +2.
553 */
554 it->subtree_alloc = subtree_nr + 2;
555 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
556 for (i = 0; i < subtree_nr; i++) {
557 /* read each subtree */
558 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700559 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 16:52:20 -0700560 const char *name = buf;
Junio C Hamano7927a552006-04-27 01:33:07 -0700561
Junio C Hamano74986462006-04-23 16:52:20 -0700562 sub = read_one(&buf, &size);
563 if (!sub)
564 goto free_return;
Junio C Hamano7927a552006-04-27 01:33:07 -0700565 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700566 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 16:52:20 -0700567 }
568 if (subtree_nr != it->subtree_nr)
569 die("cache-tree: internal error");
570 *buffer = buf;
571 *size_p = size;
572 return it;
573
574 free_return:
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700575 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 16:52:20 -0700576 return NULL;
577}
578
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700579struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 16:52:20 -0700580{
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700581 if (buffer[0])
Junio C Hamano74986462006-04-23 16:52:20 -0700582 return NULL; /* not the whole tree */
Junio C Hamano74986462006-04-23 16:52:20 -0700583 return read_one(&buffer, &size);
584}
Junio C Hamano6bd20352006-04-26 01:20:50 -0700585
Nanako Shiraishi7ba04d92008-07-16 19:42:10 +0900586static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 01:20:50 -0700587{
Junio C Hamanob87fc962009-05-20 15:53:57 -0700588 if (!it)
589 return NULL;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700590 while (*path) {
591 const char *slash;
592 struct cache_tree_sub *sub;
593
Michael Haggerty17e22dd2014-03-05 18:26:26 +0100594 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 18:26:27 +0100595 /*
596 * Between path and slash is the name of the subtree
597 * to look for.
Junio C Hamano6bd20352006-04-26 01:20:50 -0700598 */
599 sub = find_subtree(it, path, slash - path, 0);
600 if (!sub)
601 return NULL;
602 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 18:26:30 +0100603
Junio C Hamano6bd20352006-04-26 01:20:50 -0700604 path = slash;
Michael Haggerty34910472014-03-05 18:26:30 +0100605 while (*path == '/')
606 path++;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700607 }
608 return it;
609}
Junio C Hamano45525bd2008-01-10 22:49:35 -0800610
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000611int 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 -0800612{
Martin Ågren2954e5e2017-10-05 22:32:08 +0200613 int entries, was_valid;
Jeff Kingbfffb482017-09-05 08:15:21 -0400614 struct lock_file lock_file = LOCK_INIT;
Jeff Kingc82c75b2017-09-05 08:14:07 -0400615 int ret = 0;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800616
Martin Ågren2954e5e2017-10-05 22:32:08 +0200617 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800618
Thomas Gummerera125a222018-01-07 22:30:13 +0000619 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 08:14:07 -0400620 if (entries < 0) {
621 ret = WRITE_TREE_UNREADABLE_INDEX;
622 goto out;
623 }
Junio C Hamanod11b8d32009-05-20 11:04:35 -0700624 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
Paul Tand23a5112015-08-04 21:51:40 +0800625 cache_tree_free(&index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800626
Paul Tand23a5112015-08-04 21:51:40 +0800627 if (!index_state->cache_tree)
628 index_state->cache_tree = cache_tree();
Junio C Hamano45525bd2008-01-10 22:49:35 -0800629
Paul Tand23a5112015-08-04 21:51:40 +0800630 was_valid = cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800631 if (!was_valid) {
Jeff Kingc82c75b2017-09-05 08:14:07 -0400632 if (cache_tree_update(index_state, flags) < 0) {
633 ret = WRITE_TREE_UNMERGED_INDEX;
634 goto out;
635 }
Martin Ågren2954e5e2017-10-05 22:32:08 +0200636 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800637 /* Not being able to write is fine -- we are only interested
638 * in updating the cache-tree part, and if the next caller
639 * ends up using the old index with unupdated cache-tree part
640 * it misses the work we did here, but that is just a
641 * performance penalty and not a big deal.
642 */
643 }
644
645 if (prefix) {
Paul Tand23a5112015-08-04 21:51:40 +0800646 struct cache_tree *subtree;
647 subtree = cache_tree_find(index_state->cache_tree, prefix);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400648 if (!subtree) {
649 ret = WRITE_TREE_PREFIX_ERROR;
650 goto out;
651 }
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000652 oidcpy(oid, &subtree->oid);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800653 }
654 else
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000655 oidcpy(oid, &index_state->cache_tree->oid);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800656
Jeff Kingc82c75b2017-09-05 08:14:07 -0400657out:
Martin Ågren2954e5e2017-10-05 22:32:08 +0200658 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400659 return ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800660}
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700661
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100662static void prime_cache_tree_rec(struct repository *r,
663 struct cache_tree *it,
664 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700665{
666 struct tree_desc desc;
667 struct name_entry entry;
668 int cnt;
669
brian m. carlsone0a92802017-05-01 02:28:56 +0000670 oidcpy(&it->oid, &tree->object.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700671 init_tree_desc(&desc, tree->buffer, tree->size);
672 cnt = 0;
673 while (tree_entry(&desc, &entry)) {
674 if (!S_ISDIR(entry.mode))
675 cnt++;
676 else {
677 struct cache_tree_sub *sub;
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000678 struct tree *subtree = lookup_tree(r, &entry.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700679 if (!subtree->object.parsed)
680 parse_tree(subtree);
681 sub = cache_tree_sub(it, entry.path);
682 sub->cache_tree = cache_tree();
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100683 prime_cache_tree_rec(r, sub->cache_tree, subtree);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700684 cnt += sub->cache_tree->entry_count;
685 }
686 }
687 it->entry_count = cnt;
688}
689
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100690void prime_cache_tree(struct repository *r,
691 struct index_state *istate,
692 struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700693{
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700694 cache_tree_free(&istate->cache_tree);
695 istate->cache_tree = cache_tree();
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100696 prime_cache_tree_rec(r, istate->cache_tree, tree);
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700697 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700698}
Junio C Hamanob65982b2009-05-20 15:57:22 -0700699
700/*
701 * find the cache_tree that corresponds to the current level without
702 * exploding the full path into textual form. The root of the
703 * cache tree is given as "root", and our current level is "info".
704 * (1) When at root level, info->prev is NULL, so it is "root" itself.
705 * (2) Otherwise, find the cache_tree that corresponds to one level
706 * above us, and find ourselves in there.
707 */
708static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
709 struct traverse_info *info)
710{
711 struct cache_tree *our_parent;
712
713 if (!info->prev)
714 return root;
715 our_parent = find_cache_tree_from_traversal(root, info->prev);
716 return cache_tree_find(our_parent, info->name.path);
717}
718
719int cache_tree_matches_traversal(struct cache_tree *root,
720 struct name_entry *ent,
721 struct traverse_info *info)
722{
723 struct cache_tree *it;
724
725 it = find_cache_tree_from_traversal(root, info);
726 it = cache_tree_find(it, ent->path);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000727 if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 15:57:22 -0700728 return it->entry_count;
729 return 0;
730}
Thomas Rast996277c2011-12-06 18:43:37 +0100731
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100732static void verify_one(struct repository *r,
733 struct index_state *istate,
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200734 struct cache_tree *it,
735 struct strbuf *path)
736{
737 int i, pos, len = path->len;
738 struct strbuf tree_buf = STRBUF_INIT;
739 struct object_id new_oid;
740
741 for (i = 0; i < it->subtree_nr; i++) {
742 strbuf_addf(path, "%s/", it->down[i]->name);
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100743 verify_one(r, istate, it->down[i]->cache_tree, path);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200744 strbuf_setlen(path, len);
745 }
746
747 if (it->entry_count < 0 ||
748 /* no verification on tests (t7003) that replace trees */
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100749 lookup_replace_object(r, &it->oid) != &it->oid)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200750 return;
751
752 if (path->len) {
753 pos = index_name_pos(istate, path->buf, path->len);
754 pos = -pos - 1;
755 } else {
756 pos = 0;
757 }
758
759 i = 0;
760 while (i < it->entry_count) {
761 struct cache_entry *ce = istate->cache[pos + i];
762 const char *slash;
763 struct cache_tree_sub *sub = NULL;
764 const struct object_id *oid;
765 const char *name;
766 unsigned mode;
767 int entlen;
768
769 if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE))
770 BUG("%s with flags 0x%x should not be in cache-tree",
771 ce->name, ce->ce_flags);
772 name = ce->name + path->len;
773 slash = strchr(name, '/');
774 if (slash) {
775 entlen = slash - name;
776 sub = find_subtree(it, ce->name + path->len, entlen, 0);
777 if (!sub || sub->cache_tree->entry_count < 0)
778 BUG("bad subtree '%.*s'", entlen, name);
779 oid = &sub->cache_tree->oid;
780 mode = S_IFDIR;
781 i += sub->cache_tree->entry_count;
782 } else {
783 oid = &ce->oid;
784 mode = ce->ce_mode;
785 entlen = ce_namelen(ce) - path->len;
786 i++;
787 }
788 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
789 strbuf_add(&tree_buf, oid->hash, the_hash_algo->rawsz);
790 }
791 hash_object_file(tree_buf.buf, tree_buf.len, tree_type, &new_oid);
Jeff Kinge43d2dc2018-10-02 17:19:21 -0400792 if (!oideq(&new_oid, &it->oid))
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200793 BUG("cache-tree for path %.*s does not match. "
794 "Expected %s got %s", len, path->buf,
795 oid_to_hex(&new_oid), oid_to_hex(&it->oid));
796 strbuf_setlen(path, len);
797 strbuf_release(&tree_buf);
798}
799
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100800void cache_tree_verify(struct repository *r, struct index_state *istate)
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200801{
802 struct strbuf path = STRBUF_INIT;
803
804 if (!istate->cache_tree)
805 return;
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100806 verify_one(r, istate, istate->cache_tree, &path);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +0200807 strbuf_release(&path);
808}