blob: 16ea022c46d3b281d04a3956f865d8a886a5b714 [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"
Junio C Hamano74986462006-04-23 16:52:20 -07007
Junio C Hamano49031612006-10-30 15:29:53 -08008#ifndef DEBUG
Junio C Hamano74986462006-04-23 16:52:20 -07009#define DEBUG 0
Junio C Hamano49031612006-10-30 15:29:53 -080010#endif
Junio C Hamano74986462006-04-23 16:52:20 -070011
12struct cache_tree *cache_tree(void)
13{
14 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
15 it->entry_count = -1;
16 return it;
17}
18
Junio C Hamanobad68ec2006-04-24 21:18:58 -070019void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 16:52:20 -070020{
21 int i;
Junio C Hamanobad68ec2006-04-24 21:18:58 -070022 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 16:52:20 -070023
24 if (!it)
25 return;
26 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 15:40:16 -060027 if (it->down[i]) {
Junio C Hamano61fa3092006-04-25 17:40:02 -070028 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 15:40:16 -060029 free(it->down[i]);
30 }
Junio C Hamano74986462006-04-23 16:52:20 -070031 free(it->down);
32 free(it);
Junio C Hamanobad68ec2006-04-24 21:18:58 -070033 *it_p = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070034}
35
Junio C Hamano61fa3092006-04-25 17:40:02 -070036static int subtree_name_cmp(const char *one, int onelen,
37 const char *two, int twolen)
38{
39 if (onelen < twolen)
40 return -1;
41 if (twolen < onelen)
42 return 1;
43 return memcmp(one, two, onelen);
44}
45
46static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
47{
48 struct cache_tree_sub **down = it->down;
49 int lo, hi;
50 lo = 0;
51 hi = it->subtree_nr;
52 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -040053 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-25 17:40:02 -070054 struct cache_tree_sub *mdl = down[mi];
55 int cmp = subtree_name_cmp(path, pathlen,
56 mdl->name, mdl->namelen);
57 if (!cmp)
58 return mi;
59 if (cmp < 0)
60 hi = mi;
61 else
62 lo = mi + 1;
63 }
64 return -lo-1;
65}
66
Junio C Hamano74986462006-04-23 16:52:20 -070067static struct cache_tree_sub *find_subtree(struct cache_tree *it,
68 const char *path,
69 int pathlen,
70 int create)
71{
Junio C Hamano74986462006-04-23 16:52:20 -070072 struct cache_tree_sub *down;
Junio C Hamano61fa3092006-04-25 17:40:02 -070073 int pos = subtree_pos(it, path, pathlen);
74 if (0 <= pos)
75 return it->down[pos];
Junio C Hamano74986462006-04-23 16:52:20 -070076 if (!create)
77 return NULL;
Junio C Hamano61fa3092006-04-25 17:40:02 -070078
79 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-04 02:31:51 +040080 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-25 17:40:02 -070081 it->subtree_nr++;
82
Jeff King96ffc062016-02-22 17:44:32 -050083 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070084 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070085 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-25 17:40:02 -070086
87 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010088 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
89 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-25 17:40:02 -070090 it->down[pos] = down;
Junio C Hamano74986462006-04-23 16:52:20 -070091 return down;
92}
93
Junio C Hamano7927a552006-04-27 01:33:07 -070094struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
95{
96 int pathlen = strlen(path);
97 return find_subtree(it, path, pathlen, 1);
98}
99
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700100static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 16:52:20 -0700101{
102 /* a/b/c
103 * ==> invalidate self
104 * ==> find "a", have it invalidate "b/c"
105 * a
106 * ==> invalidate self
107 * ==> if "a" exists as a subtree, remove it.
108 */
109 const char *slash;
110 int namelen;
111 struct cache_tree_sub *down;
112
Junio C Hamano00703e62006-05-03 16:10:45 -0700113#if DEBUG
114 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
115#endif
116
Junio C Hamano74986462006-04-23 16:52:20 -0700117 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700118 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800119 slash = strchrnul(path, '/');
120 namelen = slash - path;
Junio C Hamano74986462006-04-23 16:52:20 -0700121 it->entry_count = -1;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800122 if (!*slash) {
Junio C Hamano61fa3092006-04-25 17:40:02 -0700123 int pos;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700124 pos = subtree_pos(it, path, namelen);
125 if (0 <= pos) {
126 cache_tree_free(&it->down[pos]->cache_tree);
127 free(it->down[pos]);
Junio C Hamano74986462006-04-23 16:52:20 -0700128 /* 0 1 2 3 4 5
129 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-25 17:40:02 -0700130 * pos
Junio C Hamano74986462006-04-23 16:52:20 -0700131 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-25 17:40:02 -0700132 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 16:52:20 -0700133 */
René Scharfef331ab92017-07-15 22:00:45 +0200134 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
135 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 16:52:20 -0700136 it->subtree_nr--;
137 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700138 return 1;
Junio C Hamano74986462006-04-23 16:52:20 -0700139 }
Junio C Hamano74986462006-04-23 16:52:20 -0700140 down = find_subtree(it, path, namelen, 0);
141 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700142 do_invalidate_path(down->cache_tree, slash + 1);
143 return 1;
144}
145
146void cache_tree_invalidate_path(struct index_state *istate, const char *path)
147{
148 if (do_invalidate_path(istate->cache_tree, path))
149 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700150}
151
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700152static int verify_cache(struct cache_entry **cache,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700153 int entries, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700154{
155 int i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700156 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 16:52:20 -0700157
158 /* Verify that the tree is merged */
159 funny = 0;
160 for (i = 0; i < entries; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700161 const struct cache_entry *ce = cache[i];
Junio C Hamano3f6d56d2012-02-07 11:55:48 -0800162 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 18:43:37 +0100163 if (silent)
164 return -1;
Junio C Hamano74986462006-04-23 16:52:20 -0700165 if (10 < ++funny) {
166 fprintf(stderr, "...\n");
167 break;
168 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 11:15:25 +0700169 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52 +0000170 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700171 }
172 }
173 if (funny)
174 return -1;
175
176 /* Also verify that the cache does not have path and path/file
177 * at the same time. At this point we know the cache has only
178 * stage 0 entries.
179 */
180 funny = 0;
181 for (i = 0; i < entries - 1; i++) {
182 /* path/file always comes after path because of the way
183 * the cache is sorted. Also path can appear only once,
184 * which means conflicting one would immediately follow.
185 */
186 const char *this_name = cache[i]->name;
187 const char *next_name = cache[i+1]->name;
188 int this_len = strlen(this_name);
189 if (this_len < strlen(next_name) &&
190 strncmp(this_name, next_name, this_len) == 0 &&
191 next_name[this_len] == '/') {
192 if (10 < ++funny) {
193 fprintf(stderr, "...\n");
194 break;
195 }
196 fprintf(stderr, "You have both %s and %s\n",
197 this_name, next_name);
198 }
199 }
200 if (funny)
201 return -1;
202 return 0;
203}
204
205static void discard_unused_subtrees(struct cache_tree *it)
206{
207 struct cache_tree_sub **down = it->down;
208 int nr = it->subtree_nr;
209 int dst, src;
210 for (dst = src = 0; src < nr; src++) {
211 struct cache_tree_sub *s = down[src];
212 if (s->used)
213 down[dst++] = s;
214 else {
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700215 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 16:52:20 -0700216 free(s);
217 it->subtree_nr--;
218 }
219 }
220}
221
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700222int cache_tree_fully_valid(struct cache_tree *it)
223{
224 int i;
225 if (!it)
226 return 0;
brian m. carlsone0a92802017-05-01 02:28:56 +0000227 if (it->entry_count < 0 || !has_sha1_file(it->oid.hash))
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700228 return 0;
229 for (i = 0; i < it->subtree_nr; i++) {
230 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
231 return 0;
232 }
233 return 1;
234}
235
Junio C Hamano74986462006-04-23 16:52:20 -0700236static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700237 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 16:52:20 -0700238 int entries,
239 const char *base,
240 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700241 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700242 int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700243{
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200244 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700245 int missing_ok = flags & WRITE_TREE_MISSING_OK;
246 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-05 21:06:56 -0700247 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700248 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700249 int i;
250
David Turneraecf5672014-07-05 21:06:56 -0700251 assert(!(dryrun && repair));
252
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700253 *skip_count = 0;
254
brian m. carlsone0a92802017-05-01 02:28:56 +0000255 if (0 <= it->entry_count && has_sha1_file(it->oid.hash))
Junio C Hamano74986462006-04-23 16:52:20 -0700256 return it->entry_count;
257
258 /*
259 * We first scan for subtrees and update them; we start by
260 * marking existing subtrees -- the ones that are unmarked
261 * should not be in the result.
262 */
263 for (i = 0; i < it->subtree_nr; i++)
264 it->down[i]->used = 0;
265
266 /*
267 * Find the subtrees and update them.
268 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700269 i = 0;
270 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700271 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 16:52:20 -0700272 struct cache_tree_sub *sub;
273 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700274 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700275
276 path = ce->name;
277 pathlen = ce_namelen(ce);
278 if (pathlen <= baselen || memcmp(base, path, baselen))
279 break; /* at the end of this level */
280
281 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700282 if (!slash) {
283 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700284 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700285 }
Junio C Hamano74986462006-04-23 16:52:20 -0700286 /*
287 * a/bbb/c (base = a/, slash = /c)
288 * ==>
289 * path+baselen = bbb/c, sublen = 3
290 */
291 sublen = slash - (path + baselen);
292 sub = find_subtree(it, path + baselen, sublen, 1);
293 if (!sub->cache_tree)
294 sub->cache_tree = cache_tree();
295 subcnt = update_one(sub->cache_tree,
296 cache + i, entries - i,
297 path,
298 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700299 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700300 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00 +0000301 if (subcnt < 0)
302 return subcnt;
Jeff King729dbbd2014-10-29 13:11:58 -0400303 if (!subcnt)
304 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700305 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700306 sub->count = subcnt; /* to be used in the next loop */
307 *skip_count += subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700308 sub->used = 1;
309 }
310
311 discard_unused_subtrees(it);
312
313 /*
314 * Then write out the tree object for this level.
315 */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200316 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 16:52:20 -0700317
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700318 i = 0;
319 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700320 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200321 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -0700322 const char *path, *slash;
323 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000324 const struct object_id *oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700325 unsigned mode;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700326 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200327 int contains_ita = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700328
329 path = ce->name;
330 pathlen = ce_namelen(ce);
331 if (pathlen <= baselen || memcmp(base, path, baselen))
332 break; /* at the end of this level */
333
334 slash = strchr(path + baselen, '/');
335 if (slash) {
336 entlen = slash - (path + baselen);
337 sub = find_subtree(it, path + baselen, entlen, 0);
338 if (!sub)
339 die("cache-tree.c: '%.*s' in '%s' not found",
340 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700341 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000342 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700343 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200344 contains_ita = sub->cache_tree->entry_count < 0;
345 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700346 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700347 expected_missing = 1;
348 }
Junio C Hamano74986462006-04-23 16:52:20 -0700349 }
350 else {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000351 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800352 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 16:52:20 -0700353 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700354 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700355 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -0400356
brian m. carlson6dcb4622018-03-12 02:27:24 +0000357 if (is_null_oid(oid) ||
358 (mode != S_IFGITLINK && !missing_ok && !has_object_file(oid))) {
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500359 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700360 if (expected_missing)
361 return -1;
Linus Torvaldsa3883732009-07-14 11:25:17 -0700362 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24 +0000363 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500364 }
Junio C Hamano74986462006-04-23 16:52:20 -0700365
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700366 /*
367 * CE_REMOVE entries are removed before the index is
368 * written to disk. Skip them to remain consistent
369 * with the future on-disk index.
370 */
371 if (ce->ce_flags & CE_REMOVE) {
372 *skip_count = *skip_count + 1;
373 continue;
374 }
375
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700376 /*
377 * CE_INTENT_TO_ADD entries exist on on-disk index but
378 * they are not part of generated trees. Invalidate up
379 * to root to force cache-tree users to read elsewhere.
380 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200381 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700382 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700383 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700384 }
Junio C Hamano74986462006-04-23 16:52:20 -0700385
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200386 /*
387 * "sub" can be an empty tree if all subentries are i-t-a.
388 */
brian m. carlsona0554932018-05-02 00:26:04 +0000389 if (contains_ita && is_empty_tree_oid(oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200390 continue;
391
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200392 strbuf_grow(&buffer, entlen + 100);
393 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24 +0000394 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700395
396#if DEBUG
Junio C Hamano00703e62006-05-03 16:10:45 -0700397 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700398 mode, entlen, path + baselen);
399#endif
400 }
401
David Turneraecf5672014-07-05 21:06:56 -0700402 if (repair) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100403 struct object_id oid;
404 hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000405 if (has_object_file(&oid))
Patryk Obaraf070fac2018-01-28 01:13:13 +0100406 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-05 21:06:56 -0700407 else
408 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100409 } else if (dryrun) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100410 hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +0100411 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
412 &it->oid)) {
Junio C Hamanoedae5f02008-04-23 09:47:17 -0700413 strbuf_release(&buffer);
414 return -1;
415 }
416
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200417 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700418 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Junio C Hamano74986462006-04-23 16:52:20 -0700419#if DEBUG
Junio C Hamano00703e62006-05-03 16:10:45 -0700420 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700421 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000422 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700423#endif
424 return i;
425}
426
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700427int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700428{
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700429 struct cache_tree *it = istate->cache_tree;
430 struct cache_entry **cache = istate->cache;
431 int entries = istate->cache_nr;
432 int skip, i = verify_cache(cache, entries, flags);
433
Junio C Hamano74986462006-04-23 16:52:20 -0700434 if (i)
435 return i;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700436 i = update_one(it, cache, entries, "", 0, &skip, flags);
Junio C Hamano74986462006-04-23 16:52:20 -0700437 if (i < 0)
438 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700439 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700440 return 0;
441}
442
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200443static void write_one(struct strbuf *buffer, struct cache_tree *it,
444 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 16:52:20 -0700445{
446 int i;
447
448 /* One "cache-tree" entry consists of the following:
449 * path (NUL terminated)
450 * entry_count, subtree_nr ("%d %d\n")
451 * tree-sha1 (missing if invalid)
452 * subtree_nr "cache-tree" entries for subtrees.
453 */
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200454 strbuf_grow(buffer, pathlen + 100);
455 strbuf_add(buffer, path, pathlen);
456 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 16:52:20 -0700457
458#if DEBUG
459 if (0 <= it->entry_count)
460 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
461 pathlen, path, 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 else
464 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
465 pathlen, path, it->subtree_nr);
466#endif
467
468 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000469 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700470 }
471 for (i = 0; i < it->subtree_nr; i++) {
472 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-25 17:40:02 -0700473 if (i) {
474 struct cache_tree_sub *prev = it->down[i-1];
475 if (subtree_name_cmp(down->name, down->namelen,
476 prev->name, prev->namelen) <= 0)
477 die("fatal - unsorted cache subtree");
478 }
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200479 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 16:52:20 -0700480 }
Junio C Hamano74986462006-04-23 16:52:20 -0700481}
482
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200483void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 16:52:20 -0700484{
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200485 write_one(sb, root, "", 0);
Junio C Hamano74986462006-04-23 16:52:20 -0700486}
487
488static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
489{
490 const char *buf = *buffer;
491 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200492 const char *cp;
493 char *ep;
Junio C Hamano74986462006-04-23 16:52:20 -0700494 struct cache_tree *it;
495 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000496 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700497
498 it = NULL;
499 /* skip name, but make sure name exists */
500 while (size && *buf) {
501 size--;
502 buf++;
503 }
504 if (!size)
505 goto free_return;
506 buf++; size--;
507 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200508
509 cp = buf;
510 it->entry_count = strtol(cp, &ep, 10);
511 if (cp == ep)
512 goto free_return;
513 cp = ep;
514 subtree_nr = strtol(cp, &ep, 10);
515 if (cp == ep)
Junio C Hamano74986462006-04-23 16:52:20 -0700516 goto free_return;
517 while (size && *buf && *buf != '\n') {
518 size--;
519 buf++;
520 }
521 if (!size)
522 goto free_return;
523 buf++; size--;
524 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000525 if (size < rawsz)
Junio C Hamano74986462006-04-23 16:52:20 -0700526 goto free_return;
brian m. carlson69d12422018-05-02 00:25:29 +0000527 oidread(&it->oid, (const unsigned char *)buf);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000528 buf += rawsz;
529 size -= rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700530 }
531
532#if DEBUG
533 if (0 <= it->entry_count)
534 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
535 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000536 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700537 else
538 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
539 *buffer, subtree_nr);
540#endif
541
542 /*
543 * Just a heuristic -- we do not add directories that often but
544 * we do not want to have to extend it immediately when we do,
545 * hence +2.
546 */
547 it->subtree_alloc = subtree_nr + 2;
548 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
549 for (i = 0; i < subtree_nr; i++) {
550 /* read each subtree */
551 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700552 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 16:52:20 -0700553 const char *name = buf;
Junio C Hamano7927a552006-04-27 01:33:07 -0700554
Junio C Hamano74986462006-04-23 16:52:20 -0700555 sub = read_one(&buf, &size);
556 if (!sub)
557 goto free_return;
Junio C Hamano7927a552006-04-27 01:33:07 -0700558 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700559 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 16:52:20 -0700560 }
561 if (subtree_nr != it->subtree_nr)
562 die("cache-tree: internal error");
563 *buffer = buf;
564 *size_p = size;
565 return it;
566
567 free_return:
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700568 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 16:52:20 -0700569 return NULL;
570}
571
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700572struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 16:52:20 -0700573{
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700574 if (buffer[0])
Junio C Hamano74986462006-04-23 16:52:20 -0700575 return NULL; /* not the whole tree */
Junio C Hamano74986462006-04-23 16:52:20 -0700576 return read_one(&buffer, &size);
577}
Junio C Hamano6bd20352006-04-26 01:20:50 -0700578
Nanako Shiraishi7ba04d92008-07-16 19:42:10 +0900579static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 01:20:50 -0700580{
Junio C Hamanob87fc962009-05-20 15:53:57 -0700581 if (!it)
582 return NULL;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700583 while (*path) {
584 const char *slash;
585 struct cache_tree_sub *sub;
586
Michael Haggerty17e22dd2014-03-05 18:26:26 +0100587 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 18:26:27 +0100588 /*
589 * Between path and slash is the name of the subtree
590 * to look for.
Junio C Hamano6bd20352006-04-26 01:20:50 -0700591 */
592 sub = find_subtree(it, path, slash - path, 0);
593 if (!sub)
594 return NULL;
595 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 18:26:30 +0100596
Junio C Hamano6bd20352006-04-26 01:20:50 -0700597 path = slash;
Michael Haggerty34910472014-03-05 18:26:30 +0100598 while (*path == '/')
599 path++;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700600 }
601 return it;
602}
Junio C Hamano45525bd2008-01-10 22:49:35 -0800603
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000604int 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 -0800605{
Martin Ågren2954e5e2017-10-05 22:32:08 +0200606 int entries, was_valid;
Jeff Kingbfffb482017-09-05 08:15:21 -0400607 struct lock_file lock_file = LOCK_INIT;
Jeff Kingc82c75b2017-09-05 08:14:07 -0400608 int ret = 0;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800609
Martin Ågren2954e5e2017-10-05 22:32:08 +0200610 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800611
Thomas Gummerera125a222018-01-07 22:30:13 +0000612 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 08:14:07 -0400613 if (entries < 0) {
614 ret = WRITE_TREE_UNREADABLE_INDEX;
615 goto out;
616 }
Junio C Hamanod11b8d32009-05-20 11:04:35 -0700617 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
Paul Tand23a5112015-08-04 21:51:40 +0800618 cache_tree_free(&index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800619
Paul Tand23a5112015-08-04 21:51:40 +0800620 if (!index_state->cache_tree)
621 index_state->cache_tree = cache_tree();
Junio C Hamano45525bd2008-01-10 22:49:35 -0800622
Paul Tand23a5112015-08-04 21:51:40 +0800623 was_valid = cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800624 if (!was_valid) {
Jeff Kingc82c75b2017-09-05 08:14:07 -0400625 if (cache_tree_update(index_state, flags) < 0) {
626 ret = WRITE_TREE_UNMERGED_INDEX;
627 goto out;
628 }
Martin Ågren2954e5e2017-10-05 22:32:08 +0200629 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800630 /* Not being able to write is fine -- we are only interested
631 * in updating the cache-tree part, and if the next caller
632 * ends up using the old index with unupdated cache-tree part
633 * it misses the work we did here, but that is just a
634 * performance penalty and not a big deal.
635 */
636 }
637
638 if (prefix) {
Paul Tand23a5112015-08-04 21:51:40 +0800639 struct cache_tree *subtree;
640 subtree = cache_tree_find(index_state->cache_tree, prefix);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400641 if (!subtree) {
642 ret = WRITE_TREE_PREFIX_ERROR;
643 goto out;
644 }
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000645 oidcpy(oid, &subtree->oid);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800646 }
647 else
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000648 oidcpy(oid, &index_state->cache_tree->oid);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800649
Jeff Kingc82c75b2017-09-05 08:14:07 -0400650out:
Martin Ågren2954e5e2017-10-05 22:32:08 +0200651 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400652 return ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800653}
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700654
655static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
656{
657 struct tree_desc desc;
658 struct name_entry entry;
659 int cnt;
660
brian m. carlsone0a92802017-05-01 02:28:56 +0000661 oidcpy(&it->oid, &tree->object.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700662 init_tree_desc(&desc, tree->buffer, tree->size);
663 cnt = 0;
664 while (tree_entry(&desc, &entry)) {
665 if (!S_ISDIR(entry.mode))
666 cnt++;
667 else {
668 struct cache_tree_sub *sub;
Stefan Bellerf86bcc72018-06-28 18:21:56 -0700669 struct tree *subtree = lookup_tree(the_repository,
670 entry.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700671 if (!subtree->object.parsed)
672 parse_tree(subtree);
673 sub = cache_tree_sub(it, entry.path);
674 sub->cache_tree = cache_tree();
675 prime_cache_tree_rec(sub->cache_tree, subtree);
676 cnt += sub->cache_tree->entry_count;
677 }
678 }
679 it->entry_count = cnt;
680}
681
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700682void prime_cache_tree(struct index_state *istate, struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700683{
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700684 cache_tree_free(&istate->cache_tree);
685 istate->cache_tree = cache_tree();
686 prime_cache_tree_rec(istate->cache_tree, tree);
687 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700688}
Junio C Hamanob65982b2009-05-20 15:57:22 -0700689
690/*
691 * find the cache_tree that corresponds to the current level without
692 * exploding the full path into textual form. The root of the
693 * cache tree is given as "root", and our current level is "info".
694 * (1) When at root level, info->prev is NULL, so it is "root" itself.
695 * (2) Otherwise, find the cache_tree that corresponds to one level
696 * above us, and find ourselves in there.
697 */
698static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
699 struct traverse_info *info)
700{
701 struct cache_tree *our_parent;
702
703 if (!info->prev)
704 return root;
705 our_parent = find_cache_tree_from_traversal(root, info->prev);
706 return cache_tree_find(our_parent, info->name.path);
707}
708
709int cache_tree_matches_traversal(struct cache_tree *root,
710 struct name_entry *ent,
711 struct traverse_info *info)
712{
713 struct cache_tree *it;
714
715 it = find_cache_tree_from_traversal(root, info);
716 it = cache_tree_find(it, ent->path);
brian m. carlsone0a92802017-05-01 02:28:56 +0000717 if (it && it->entry_count > 0 && !oidcmp(ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 15:57:22 -0700718 return it->entry_count;
719 return 0;
720}