blob: 6a555f4d431f9f6dbf8dad06d75a4ec81a4254fd [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"
6
Junio C Hamano49031612006-10-30 15:29:53 -08007#ifndef DEBUG
Junio C Hamano74986462006-04-23 16:52:20 -07008#define DEBUG 0
Junio C Hamano49031612006-10-30 15:29:53 -08009#endif
Junio C Hamano74986462006-04-23 16:52:20 -070010
11struct cache_tree *cache_tree(void)
12{
13 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
14 it->entry_count = -1;
15 return it;
16}
17
Junio C Hamanobad68ec2006-04-24 21:18:58 -070018void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 16:52:20 -070019{
20 int i;
Junio C Hamanobad68ec2006-04-24 21:18:58 -070021 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 16:52:20 -070022
23 if (!it)
24 return;
25 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 15:40:16 -060026 if (it->down[i]) {
Junio C Hamano61fa3092006-04-25 17:40:02 -070027 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 15:40:16 -060028 free(it->down[i]);
29 }
Junio C Hamano74986462006-04-23 16:52:20 -070030 free(it->down);
31 free(it);
Junio C Hamanobad68ec2006-04-24 21:18:58 -070032 *it_p = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070033}
34
Junio C Hamano61fa3092006-04-25 17:40:02 -070035static int subtree_name_cmp(const char *one, int onelen,
36 const char *two, int twolen)
37{
38 if (onelen < twolen)
39 return -1;
40 if (twolen < onelen)
41 return 1;
42 return memcmp(one, two, onelen);
43}
44
45static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
46{
47 struct cache_tree_sub **down = it->down;
48 int lo, hi;
49 lo = 0;
50 hi = it->subtree_nr;
51 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -040052 int mi = lo + (hi - lo) / 2;
Junio C Hamano61fa3092006-04-25 17:40:02 -070053 struct cache_tree_sub *mdl = down[mi];
54 int cmp = subtree_name_cmp(path, pathlen,
55 mdl->name, mdl->namelen);
56 if (!cmp)
57 return mi;
58 if (cmp < 0)
59 hi = mi;
60 else
61 lo = mi + 1;
62 }
63 return -lo-1;
64}
65
Junio C Hamano74986462006-04-23 16:52:20 -070066static struct cache_tree_sub *find_subtree(struct cache_tree *it,
67 const char *path,
68 int pathlen,
69 int create)
70{
Junio C Hamano74986462006-04-23 16:52:20 -070071 struct cache_tree_sub *down;
Junio C Hamano61fa3092006-04-25 17:40:02 -070072 int pos = subtree_pos(it, path, pathlen);
73 if (0 <= pos)
74 return it->down[pos];
Junio C Hamano74986462006-04-23 16:52:20 -070075 if (!create)
76 return NULL;
Junio C Hamano61fa3092006-04-25 17:40:02 -070077
78 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-04 02:31:51 +040079 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-25 17:40:02 -070080 it->subtree_nr++;
81
Jeff King96ffc062016-02-22 17:44:32 -050082 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-25 17:40:02 -070083 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -070084 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-25 17:40:02 -070085
86 if (pos < it->subtree_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010087 MOVE_ARRAY(it->down + pos + 1, it->down + pos,
88 it->subtree_nr - pos - 1);
Junio C Hamano61fa3092006-04-25 17:40:02 -070089 it->down[pos] = down;
Junio C Hamano74986462006-04-23 16:52:20 -070090 return down;
91}
92
Junio C Hamano7927a552006-04-27 01:33:07 -070093struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
94{
95 int pathlen = strlen(path);
96 return find_subtree(it, path, pathlen, 1);
97}
98
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +070099static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 16:52:20 -0700100{
101 /* a/b/c
102 * ==> invalidate self
103 * ==> find "a", have it invalidate "b/c"
104 * a
105 * ==> invalidate self
106 * ==> if "a" exists as a subtree, remove it.
107 */
108 const char *slash;
109 int namelen;
110 struct cache_tree_sub *down;
111
Junio C Hamano00703e62006-05-03 16:10:45 -0700112#if DEBUG
113 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
114#endif
115
Junio C Hamano74986462006-04-23 16:52:20 -0700116 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700117 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800118 slash = strchrnul(path, '/');
119 namelen = slash - path;
Junio C Hamano74986462006-04-23 16:52:20 -0700120 it->entry_count = -1;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800121 if (!*slash) {
Junio C Hamano61fa3092006-04-25 17:40:02 -0700122 int pos;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700123 pos = subtree_pos(it, path, namelen);
124 if (0 <= pos) {
125 cache_tree_free(&it->down[pos]->cache_tree);
126 free(it->down[pos]);
Junio C Hamano74986462006-04-23 16:52:20 -0700127 /* 0 1 2 3 4 5
128 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-25 17:40:02 -0700129 * pos
Junio C Hamano74986462006-04-23 16:52:20 -0700130 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-25 17:40:02 -0700131 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 16:52:20 -0700132 */
René Scharfef331ab92017-07-15 22:00:45 +0200133 MOVE_ARRAY(it->down + pos, it->down + pos + 1,
134 it->subtree_nr - pos - 1);
Junio C Hamano74986462006-04-23 16:52:20 -0700135 it->subtree_nr--;
136 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700137 return 1;
Junio C Hamano74986462006-04-23 16:52:20 -0700138 }
Junio C Hamano74986462006-04-23 16:52:20 -0700139 down = find_subtree(it, path, namelen, 0);
140 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700141 do_invalidate_path(down->cache_tree, slash + 1);
142 return 1;
143}
144
145void cache_tree_invalidate_path(struct index_state *istate, const char *path)
146{
147 if (do_invalidate_path(istate->cache_tree, path))
148 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700149}
150
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700151static int verify_cache(struct cache_entry **cache,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700152 int entries, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700153{
154 int i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700155 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 16:52:20 -0700156
157 /* Verify that the tree is merged */
158 funny = 0;
159 for (i = 0; i < entries; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700160 const struct cache_entry *ce = cache[i];
Junio C Hamano3f6d56d2012-02-07 11:55:48 -0800161 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 18:43:37 +0100162 if (silent)
163 return -1;
Junio C Hamano74986462006-04-23 16:52:20 -0700164 if (10 < ++funny) {
165 fprintf(stderr, "...\n");
166 break;
167 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 11:15:25 +0700168 fprintf(stderr, "%s: unmerged (%s)\n",
brian m. carlson99d1a982016-09-05 20:07:52 +0000169 ce->name, oid_to_hex(&ce->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700170 }
171 }
172 if (funny)
173 return -1;
174
175 /* Also verify that the cache does not have path and path/file
176 * at the same time. At this point we know the cache has only
177 * stage 0 entries.
178 */
179 funny = 0;
180 for (i = 0; i < entries - 1; i++) {
181 /* path/file always comes after path because of the way
182 * the cache is sorted. Also path can appear only once,
183 * which means conflicting one would immediately follow.
184 */
185 const char *this_name = cache[i]->name;
186 const char *next_name = cache[i+1]->name;
187 int this_len = strlen(this_name);
188 if (this_len < strlen(next_name) &&
189 strncmp(this_name, next_name, this_len) == 0 &&
190 next_name[this_len] == '/') {
191 if (10 < ++funny) {
192 fprintf(stderr, "...\n");
193 break;
194 }
195 fprintf(stderr, "You have both %s and %s\n",
196 this_name, next_name);
197 }
198 }
199 if (funny)
200 return -1;
201 return 0;
202}
203
204static void discard_unused_subtrees(struct cache_tree *it)
205{
206 struct cache_tree_sub **down = it->down;
207 int nr = it->subtree_nr;
208 int dst, src;
209 for (dst = src = 0; src < nr; src++) {
210 struct cache_tree_sub *s = down[src];
211 if (s->used)
212 down[dst++] = s;
213 else {
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700214 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 16:52:20 -0700215 free(s);
216 it->subtree_nr--;
217 }
218 }
219}
220
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700221int cache_tree_fully_valid(struct cache_tree *it)
222{
223 int i;
224 if (!it)
225 return 0;
brian m. carlsone0a92802017-05-01 02:28:56 +0000226 if (it->entry_count < 0 || !has_sha1_file(it->oid.hash))
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700227 return 0;
228 for (i = 0; i < it->subtree_nr; i++) {
229 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
230 return 0;
231 }
232 return 1;
233}
234
Junio C Hamano74986462006-04-23 16:52:20 -0700235static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700236 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 16:52:20 -0700237 int entries,
238 const char *base,
239 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700240 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700241 int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700242{
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200243 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700244 int missing_ok = flags & WRITE_TREE_MISSING_OK;
245 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-05 21:06:56 -0700246 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700247 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700248 int i;
249
David Turneraecf5672014-07-05 21:06:56 -0700250 assert(!(dryrun && repair));
251
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700252 *skip_count = 0;
253
brian m. carlsone0a92802017-05-01 02:28:56 +0000254 if (0 <= it->entry_count && has_sha1_file(it->oid.hash))
Junio C Hamano74986462006-04-23 16:52:20 -0700255 return it->entry_count;
256
257 /*
258 * We first scan for subtrees and update them; we start by
259 * marking existing subtrees -- the ones that are unmarked
260 * should not be in the result.
261 */
262 for (i = 0; i < it->subtree_nr; i++)
263 it->down[i]->used = 0;
264
265 /*
266 * Find the subtrees and update them.
267 */
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700268 i = 0;
269 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700270 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 16:52:20 -0700271 struct cache_tree_sub *sub;
272 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700273 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700274
275 path = ce->name;
276 pathlen = ce_namelen(ce);
277 if (pathlen <= baselen || memcmp(base, path, baselen))
278 break; /* at the end of this level */
279
280 slash = strchr(path + baselen, '/');
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700281 if (!slash) {
282 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700283 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700284 }
Junio C Hamano74986462006-04-23 16:52:20 -0700285 /*
286 * a/bbb/c (base = a/, slash = /c)
287 * ==>
288 * path+baselen = bbb/c, sublen = 3
289 */
290 sublen = slash - (path + baselen);
291 sub = find_subtree(it, path + baselen, sublen, 1);
292 if (!sub->cache_tree)
293 sub->cache_tree = cache_tree();
294 subcnt = update_one(sub->cache_tree,
295 cache + i, entries - i,
296 path,
297 baselen + sublen + 1,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700298 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700299 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00 +0000300 if (subcnt < 0)
301 return subcnt;
Jeff King729dbbd2014-10-29 13:11:58 -0400302 if (!subcnt)
303 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700304 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700305 sub->count = subcnt; /* to be used in the next loop */
306 *skip_count += subskip;
Junio C Hamano74986462006-04-23 16:52:20 -0700307 sub->used = 1;
308 }
309
310 discard_unused_subtrees(it);
311
312 /*
313 * Then write out the tree object for this level.
314 */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200315 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 16:52:20 -0700316
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700317 i = 0;
318 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700319 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200320 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 16:52:20 -0700321 const char *path, *slash;
322 int pathlen, entlen;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000323 const struct object_id *oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700324 unsigned mode;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700325 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200326 int contains_ita = 0;
Junio C Hamano74986462006-04-23 16:52:20 -0700327
328 path = ce->name;
329 pathlen = ce_namelen(ce);
330 if (pathlen <= baselen || memcmp(base, path, baselen))
331 break; /* at the end of this level */
332
333 slash = strchr(path + baselen, '/');
334 if (slash) {
335 entlen = slash - (path + baselen);
336 sub = find_subtree(it, path + baselen, entlen, 0);
337 if (!sub)
338 die("cache-tree.c: '%.*s' in '%s' not found",
339 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700340 i += sub->count;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000341 oid = &sub->cache_tree->oid;
Junio C Hamano74986462006-04-23 16:52:20 -0700342 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200343 contains_ita = sub->cache_tree->entry_count < 0;
344 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700345 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700346 expected_missing = 1;
347 }
Junio C Hamano74986462006-04-23 16:52:20 -0700348 }
349 else {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000350 oid = &ce->oid;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800351 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 16:52:20 -0700352 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 11:15:26 +0700353 i++;
Junio C Hamano74986462006-04-23 16:52:20 -0700354 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -0400355
brian m. carlson6dcb4622018-03-12 02:27:24 +0000356 if (is_null_oid(oid) ||
357 (mode != S_IFGITLINK && !missing_ok && !has_object_file(oid))) {
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500358 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700359 if (expected_missing)
360 return -1;
Linus Torvaldsa3883732009-07-14 11:25:17 -0700361 return error("invalid object %06o %s for '%.*s'",
brian m. carlson6dcb4622018-03-12 02:27:24 +0000362 mode, oid_to_hex(oid), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-09 22:32:11 -0500363 }
Junio C Hamano74986462006-04-23 16:52:20 -0700364
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700365 /*
366 * CE_REMOVE entries are removed before the index is
367 * written to disk. Skip them to remain consistent
368 * with the future on-disk index.
369 */
370 if (ce->ce_flags & CE_REMOVE) {
371 *skip_count = *skip_count + 1;
372 continue;
373 }
374
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700375 /*
376 * CE_INTENT_TO_ADD entries exist on on-disk index but
377 * they are not part of generated trees. Invalidate up
378 * to root to force cache-tree users to read elsewhere.
379 */
Nguyễn Thái Ngọc Duyc041d542016-07-16 07:06:26 +0200380 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700381 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700382 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700383 }
Junio C Hamano74986462006-04-23 16:52:20 -0700384
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200385 /*
386 * "sub" can be an empty tree if all subentries are i-t-a.
387 */
brian m. carlson6dcb4622018-03-12 02:27:24 +0000388 if (contains_ita && !oidcmp(oid, &empty_tree_oid))
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 07:06:27 +0200389 continue;
390
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200391 strbuf_grow(&buffer, entlen + 100);
392 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
brian m. carlson6dcb4622018-03-12 02:27:24 +0000393 strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700394
395#if DEBUG
Junio C Hamano00703e62006-05-03 16:10:45 -0700396 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700397 mode, entlen, path + baselen);
398#endif
399 }
400
David Turneraecf5672014-07-05 21:06:56 -0700401 if (repair) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100402 struct object_id oid;
403 hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
brian m. carlson6dcb4622018-03-12 02:27:24 +0000404 if (has_object_file(&oid))
Patryk Obaraf070fac2018-01-28 01:13:13 +0100405 oidcpy(&it->oid, &oid);
David Turneraecf5672014-07-05 21:06:56 -0700406 else
407 to_invalidate = 1;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100408 } else if (dryrun) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100409 hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +0100410 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
411 &it->oid)) {
Junio C Hamanoedae5f02008-04-23 09:47:17 -0700412 strbuf_release(&buffer);
413 return -1;
414 }
415
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200416 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 11:15:28 +0700417 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Junio C Hamano74986462006-04-23 16:52:20 -0700418#if DEBUG
Junio C Hamano00703e62006-05-03 16:10:45 -0700419 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 16:52:20 -0700420 it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000421 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700422#endif
423 return i;
424}
425
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700426int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 16:52:20 -0700427{
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700428 struct cache_tree *it = istate->cache_tree;
429 struct cache_entry **cache = istate->cache;
430 int entries = istate->cache_nr;
431 int skip, i = verify_cache(cache, entries, flags);
432
Junio C Hamano74986462006-04-23 16:52:20 -0700433 if (i)
434 return i;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 11:15:27 +0700435 i = update_one(it, cache, entries, "", 0, &skip, flags);
Junio C Hamano74986462006-04-23 16:52:20 -0700436 if (i < 0)
437 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700438 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 16:52:20 -0700439 return 0;
440}
441
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200442static void write_one(struct strbuf *buffer, struct cache_tree *it,
443 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 16:52:20 -0700444{
445 int i;
446
447 /* One "cache-tree" entry consists of the following:
448 * path (NUL terminated)
449 * entry_count, subtree_nr ("%d %d\n")
450 * tree-sha1 (missing if invalid)
451 * subtree_nr "cache-tree" entries for subtrees.
452 */
Pierre Habouzit5242bcb2007-09-06 13:20:11 +0200453 strbuf_grow(buffer, pathlen + 100);
454 strbuf_add(buffer, path, pathlen);
455 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 16:52:20 -0700456
457#if DEBUG
458 if (0 <= it->entry_count)
459 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
460 pathlen, path, it->entry_count, it->subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000461 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700462 else
463 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
464 pathlen, path, it->subtree_nr);
465#endif
466
467 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000468 strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
Junio C Hamano74986462006-04-23 16:52:20 -0700469 }
470 for (i = 0; i < it->subtree_nr; i++) {
471 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-25 17:40:02 -0700472 if (i) {
473 struct cache_tree_sub *prev = it->down[i-1];
474 if (subtree_name_cmp(down->name, down->namelen,
475 prev->name, prev->namelen) <= 0)
476 die("fatal - unsorted cache subtree");
477 }
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200478 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 16:52:20 -0700479 }
Junio C Hamano74986462006-04-23 16:52:20 -0700480}
481
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200482void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 16:52:20 -0700483{
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +0200484 write_one(sb, root, "", 0);
Junio C Hamano74986462006-04-23 16:52:20 -0700485}
486
487static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
488{
489 const char *buf = *buffer;
490 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200491 const char *cp;
492 char *ep;
Junio C Hamano74986462006-04-23 16:52:20 -0700493 struct cache_tree *it;
494 int i, subtree_nr;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000495 const unsigned rawsz = the_hash_algo->rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700496
497 it = NULL;
498 /* skip name, but make sure name exists */
499 while (size && *buf) {
500 size--;
501 buf++;
502 }
503 if (!size)
504 goto free_return;
505 buf++; size--;
506 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 03:31:02 +0200507
508 cp = buf;
509 it->entry_count = strtol(cp, &ep, 10);
510 if (cp == ep)
511 goto free_return;
512 cp = ep;
513 subtree_nr = strtol(cp, &ep, 10);
514 if (cp == ep)
Junio C Hamano74986462006-04-23 16:52:20 -0700515 goto free_return;
516 while (size && *buf && *buf != '\n') {
517 size--;
518 buf++;
519 }
520 if (!size)
521 goto free_return;
522 buf++; size--;
523 if (0 <= it->entry_count) {
brian m. carlson6dcb4622018-03-12 02:27:24 +0000524 if (size < rawsz)
Junio C Hamano74986462006-04-23 16:52:20 -0700525 goto free_return;
brian m. carlson6dcb4622018-03-12 02:27:24 +0000526 memcpy(it->oid.hash, (const unsigned char*)buf, rawsz);
527 buf += rawsz;
528 size -= rawsz;
Junio C Hamano74986462006-04-23 16:52:20 -0700529 }
530
531#if DEBUG
532 if (0 <= it->entry_count)
533 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
534 *buffer, it->entry_count, subtree_nr,
brian m. carlsone0a92802017-05-01 02:28:56 +0000535 oid_to_hex(&it->oid));
Junio C Hamano74986462006-04-23 16:52:20 -0700536 else
537 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
538 *buffer, subtree_nr);
539#endif
540
541 /*
542 * Just a heuristic -- we do not add directories that often but
543 * we do not want to have to extend it immediately when we do,
544 * hence +2.
545 */
546 it->subtree_alloc = subtree_nr + 2;
547 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
548 for (i = 0; i < subtree_nr; i++) {
549 /* read each subtree */
550 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-25 17:40:02 -0700551 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 16:52:20 -0700552 const char *name = buf;
Junio C Hamano7927a552006-04-27 01:33:07 -0700553
Junio C Hamano74986462006-04-23 16:52:20 -0700554 sub = read_one(&buf, &size);
555 if (!sub)
556 goto free_return;
Junio C Hamano7927a552006-04-27 01:33:07 -0700557 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-25 17:40:02 -0700558 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 16:52:20 -0700559 }
560 if (subtree_nr != it->subtree_nr)
561 die("cache-tree: internal error");
562 *buffer = buf;
563 *size_p = size;
564 return it;
565
566 free_return:
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700567 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 16:52:20 -0700568 return NULL;
569}
570
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700571struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 16:52:20 -0700572{
Junio C Hamanobad68ec2006-04-24 21:18:58 -0700573 if (buffer[0])
Junio C Hamano74986462006-04-23 16:52:20 -0700574 return NULL; /* not the whole tree */
Junio C Hamano74986462006-04-23 16:52:20 -0700575 return read_one(&buffer, &size);
576}
Junio C Hamano6bd20352006-04-26 01:20:50 -0700577
Nanako Shiraishi7ba04d92008-07-16 19:42:10 +0900578static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 01:20:50 -0700579{
Junio C Hamanob87fc962009-05-20 15:53:57 -0700580 if (!it)
581 return NULL;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700582 while (*path) {
583 const char *slash;
584 struct cache_tree_sub *sub;
585
Michael Haggerty17e22dd2014-03-05 18:26:26 +0100586 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 18:26:27 +0100587 /*
588 * Between path and slash is the name of the subtree
589 * to look for.
Junio C Hamano6bd20352006-04-26 01:20:50 -0700590 */
591 sub = find_subtree(it, path, slash - path, 0);
592 if (!sub)
593 return NULL;
594 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 18:26:30 +0100595
Junio C Hamano6bd20352006-04-26 01:20:50 -0700596 path = slash;
Michael Haggerty34910472014-03-05 18:26:30 +0100597 while (*path == '/')
598 path++;
Junio C Hamano6bd20352006-04-26 01:20:50 -0700599 }
600 return it;
601}
Junio C Hamano45525bd2008-01-10 22:49:35 -0800602
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000603int 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 -0800604{
Martin Ågren2954e5e2017-10-05 22:32:08 +0200605 int entries, was_valid;
Jeff Kingbfffb482017-09-05 08:15:21 -0400606 struct lock_file lock_file = LOCK_INIT;
Jeff Kingc82c75b2017-09-05 08:14:07 -0400607 int ret = 0;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800608
Martin Ågren2954e5e2017-10-05 22:32:08 +0200609 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800610
Thomas Gummerera125a222018-01-07 22:30:13 +0000611 entries = read_index_from(index_state, index_path, get_git_dir());
Jeff Kingc82c75b2017-09-05 08:14:07 -0400612 if (entries < 0) {
613 ret = WRITE_TREE_UNREADABLE_INDEX;
614 goto out;
615 }
Junio C Hamanod11b8d32009-05-20 11:04:35 -0700616 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
Paul Tand23a5112015-08-04 21:51:40 +0800617 cache_tree_free(&index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800618
Paul Tand23a5112015-08-04 21:51:40 +0800619 if (!index_state->cache_tree)
620 index_state->cache_tree = cache_tree();
Junio C Hamano45525bd2008-01-10 22:49:35 -0800621
Paul Tand23a5112015-08-04 21:51:40 +0800622 was_valid = cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800623 if (!was_valid) {
Jeff Kingc82c75b2017-09-05 08:14:07 -0400624 if (cache_tree_update(index_state, flags) < 0) {
625 ret = WRITE_TREE_UNMERGED_INDEX;
626 goto out;
627 }
Martin Ågren2954e5e2017-10-05 22:32:08 +0200628 write_locked_index(index_state, &lock_file, COMMIT_LOCK);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800629 /* Not being able to write is fine -- we are only interested
630 * in updating the cache-tree part, and if the next caller
631 * ends up using the old index with unupdated cache-tree part
632 * it misses the work we did here, but that is just a
633 * performance penalty and not a big deal.
634 */
635 }
636
637 if (prefix) {
Paul Tand23a5112015-08-04 21:51:40 +0800638 struct cache_tree *subtree;
639 subtree = cache_tree_find(index_state->cache_tree, prefix);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400640 if (!subtree) {
641 ret = WRITE_TREE_PREFIX_ERROR;
642 goto out;
643 }
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000644 oidcpy(oid, &subtree->oid);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800645 }
646 else
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000647 oidcpy(oid, &index_state->cache_tree->oid);
Junio C Hamano45525bd2008-01-10 22:49:35 -0800648
Jeff Kingc82c75b2017-09-05 08:14:07 -0400649out:
Martin Ågren2954e5e2017-10-05 22:32:08 +0200650 rollback_lock_file(&lock_file);
Jeff Kingc82c75b2017-09-05 08:14:07 -0400651 return ret;
Junio C Hamano45525bd2008-01-10 22:49:35 -0800652}
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700653
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000654int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix)
Paul Tand23a5112015-08-04 21:51:40 +0800655{
brian m. carlsonfc5cb992018-03-12 02:27:23 +0000656 return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix);
Paul Tand23a5112015-08-04 21:51:40 +0800657}
658
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700659static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
660{
661 struct tree_desc desc;
662 struct name_entry entry;
663 int cnt;
664
brian m. carlsone0a92802017-05-01 02:28:56 +0000665 oidcpy(&it->oid, &tree->object.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700666 init_tree_desc(&desc, tree->buffer, tree->size);
667 cnt = 0;
668 while (tree_entry(&desc, &entry)) {
669 if (!S_ISDIR(entry.mode))
670 cnt++;
671 else {
672 struct cache_tree_sub *sub;
brian m. carlson740ee052017-05-06 22:10:17 +0000673 struct tree *subtree = lookup_tree(entry.oid);
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700674 if (!subtree->object.parsed)
675 parse_tree(subtree);
676 sub = cache_tree_sub(it, entry.path);
677 sub->cache_tree = cache_tree();
678 prime_cache_tree_rec(sub->cache_tree, subtree);
679 cnt += sub->cache_tree->entry_count;
680 }
681 }
682 it->entry_count = cnt;
683}
684
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700685void prime_cache_tree(struct index_state *istate, struct tree *tree)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700686{
Nguyễn Thái Ngọc Duye6c286e2014-06-13 19:19:33 +0700687 cache_tree_free(&istate->cache_tree);
688 istate->cache_tree = cache_tree();
689 prime_cache_tree_rec(istate->cache_tree, tree);
690 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700691}
Junio C Hamanob65982b2009-05-20 15:57:22 -0700692
693/*
694 * find the cache_tree that corresponds to the current level without
695 * exploding the full path into textual form. The root of the
696 * cache tree is given as "root", and our current level is "info".
697 * (1) When at root level, info->prev is NULL, so it is "root" itself.
698 * (2) Otherwise, find the cache_tree that corresponds to one level
699 * above us, and find ourselves in there.
700 */
701static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
702 struct traverse_info *info)
703{
704 struct cache_tree *our_parent;
705
706 if (!info->prev)
707 return root;
708 our_parent = find_cache_tree_from_traversal(root, info->prev);
709 return cache_tree_find(our_parent, info->name.path);
710}
711
712int cache_tree_matches_traversal(struct cache_tree *root,
713 struct name_entry *ent,
714 struct traverse_info *info)
715{
716 struct cache_tree *it;
717
718 it = find_cache_tree_from_traversal(root, info);
719 it = cache_tree_find(it, ent->path);
brian m. carlsone0a92802017-05-01 02:28:56 +0000720 if (it && it->entry_count > 0 && !oidcmp(ent->oid, &it->oid))
Junio C Hamanob65982b2009-05-20 15:57:22 -0700721 return it->entry_count;
722 return 0;
723}
Thomas Rast996277c2011-12-06 18:43:37 +0100724
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700725int update_main_cache_tree(int flags)
Thomas Rast996277c2011-12-06 18:43:37 +0100726{
727 if (!the_index.cache_tree)
728 the_index.cache_tree = cache_tree();
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +0700729 return cache_tree_update(&the_index, flags);
Thomas Rast996277c2011-12-06 18:43:37 +0100730}