🌐 AI搜索 & 代理 主页
blob: f28b1f45a49b842c2f7a372b0c0c595bebc56b5d [file] [log] [blame]
Junio C Hamano74986462006-04-23 23:52:201#include "cache.h"
Michael Haggerty697cc8e2014-10-01 10:28:422#include "lockfile.h"
Junio C Hamano74986462006-04-23 23:52:203#include "tree.h"
Junio C Hamanob9d37a52009-04-20 10:58:184#include "tree-walk.h"
Junio C Hamano74986462006-04-23 23:52:205#include "cache-tree.h"
6
Junio C Hamano49031612006-10-30 23:29:537#ifndef DEBUG
Junio C Hamano74986462006-04-23 23:52:208#define DEBUG 0
Junio C Hamano49031612006-10-30 23:29:539#endif
Junio C Hamano74986462006-04-23 23:52:2010
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-25 04:18:5818void cache_tree_free(struct cache_tree **it_p)
Junio C Hamano74986462006-04-23 23:52:2019{
20 int i;
Junio C Hamanobad68ec2006-04-25 04:18:5821 struct cache_tree *it = *it_p;
Junio C Hamano74986462006-04-23 23:52:2022
23 if (!it)
24 return;
25 for (i = 0; i < it->subtree_nr; i++)
Elijah Newrene92fa512010-09-06 21:40:1626 if (it->down[i]) {
Junio C Hamano61fa3092006-04-26 00:40:0227 cache_tree_free(&it->down[i]->cache_tree);
Elijah Newrene92fa512010-09-06 21:40:1628 free(it->down[i]);
29 }
Junio C Hamano74986462006-04-23 23:52:2030 free(it->down);
31 free(it);
Junio C Hamanobad68ec2006-04-25 04:18:5832 *it_p = NULL;
Junio C Hamano74986462006-04-23 23:52:2033}
34
Junio C Hamano61fa3092006-04-26 00:40:0235static 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) {
52 int mi = (lo + hi) / 2;
53 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 23:52:2066static 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 23:52:2071 struct cache_tree_sub *down;
Junio C Hamano61fa3092006-04-26 00:40:0272 int pos = subtree_pos(it, path, pathlen);
73 if (0 <= pos)
74 return it->down[pos];
Junio C Hamano74986462006-04-23 23:52:2075 if (!create)
76 return NULL;
Junio C Hamano61fa3092006-04-26 00:40:0277
78 pos = -pos-1;
Dmitry S. Dolzhenkobcc7a032014-03-03 22:31:5179 ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
Junio C Hamano61fa3092006-04-26 00:40:0280 it->subtree_nr++;
81
Jeff King96ffc062016-02-22 22:44:3282 FLEX_ALLOC_MEM(down, name, path, pathlen);
Junio C Hamano61fa3092006-04-26 00:40:0283 down->cache_tree = NULL;
Junio C Hamano74986462006-04-23 23:52:2084 down->namelen = pathlen;
Junio C Hamano61fa3092006-04-26 00:40:0285
86 if (pos < it->subtree_nr)
87 memmove(it->down + pos + 1,
88 it->down + pos,
89 sizeof(down) * (it->subtree_nr - pos - 1));
90 it->down[pos] = down;
Junio C Hamano74986462006-04-23 23:52:2091 return down;
92}
93
Junio C Hamano7927a552006-04-27 08:33:0794struct 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 12:19:31100static int do_invalidate_path(struct cache_tree *it, const char *path)
Junio C Hamano74986462006-04-23 23:52:20101{
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 23:10:45113#if DEBUG
114 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
115#endif
116
Junio C Hamano74986462006-04-23 23:52:20117 if (!it)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31118 return 0;
Rohit Mani2c5495f2014-03-08 06:48:31119 slash = strchrnul(path, '/');
120 namelen = slash - path;
Junio C Hamano74986462006-04-23 23:52:20121 it->entry_count = -1;
Rohit Mani2c5495f2014-03-08 06:48:31122 if (!*slash) {
Junio C Hamano61fa3092006-04-26 00:40:02123 int pos;
Junio C Hamano61fa3092006-04-26 00:40:02124 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 23:52:20128 /* 0 1 2 3 4 5
129 * ^ ^subtree_nr = 6
Junio C Hamano61fa3092006-04-26 00:40:02130 * pos
Junio C Hamano74986462006-04-23 23:52:20131 * move 4 and 5 up one place (2 entries)
Junio C Hamano61fa3092006-04-26 00:40:02132 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
Junio C Hamano74986462006-04-23 23:52:20133 */
Junio C Hamano61fa3092006-04-26 00:40:02134 memmove(it->down+pos, it->down+pos+1,
Junio C Hamano74986462006-04-23 23:52:20135 sizeof(struct cache_tree_sub *) *
Junio C Hamano61fa3092006-04-26 00:40:02136 (it->subtree_nr - pos - 1));
Junio C Hamano74986462006-04-23 23:52:20137 it->subtree_nr--;
138 }
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31139 return 1;
Junio C Hamano74986462006-04-23 23:52:20140 }
Junio C Hamano74986462006-04-23 23:52:20141 down = find_subtree(it, path, namelen, 0);
142 if (down)
Nguyễn Thái Ngọc Duya5400ef2014-06-13 12:19:31143 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 23:52:20151}
152
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32153static int verify_cache(struct cache_entry **cache,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46154 int entries, int flags)
Junio C Hamano74986462006-04-23 23:52:20155{
156 int i, funny;
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46157 int silent = flags & WRITE_TREE_SILENT;
Junio C Hamano74986462006-04-23 23:52:20158
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 15:29:00162 const struct cache_entry *ce = cache[i];
Junio C Hamano3f6d56d2012-02-07 19:55:48163 if (ce_stage(ce)) {
Thomas Rast996277c2011-12-06 17:43:37164 if (silent)
165 return -1;
Junio C Hamano74986462006-04-23 23:52:20166 if (10 < ++funny) {
167 fprintf(stderr, "...\n");
168 break;
169 }
Nguyễn Thái Ngọc Duydbc39042012-12-16 04:15:25170 fprintf(stderr, "%s: unmerged (%s)\n",
171 ce->name, sha1_to_hex(ce->sha1));
Junio C Hamano74986462006-04-23 23:52:20172 }
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-25 04:18:58216 cache_tree_free(&s->cache_tree);
Junio C Hamano74986462006-04-23 23:52:20217 free(s);
218 it->subtree_nr--;
219 }
220 }
221}
222
Junio C Hamanobad68ec2006-04-25 04:18:58223int cache_tree_fully_valid(struct cache_tree *it)
224{
225 int i;
226 if (!it)
227 return 0;
228 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
229 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 23:52:20237static int update_one(struct cache_tree *it,
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32238 struct cache_entry **cache,
Junio C Hamano74986462006-04-23 23:52:20239 int entries,
240 const char *base,
241 int baselen,
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27242 int *skip_count,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46243 int flags)
Junio C Hamano74986462006-04-23 23:52:20244{
Pierre Habouzit5242bcb2007-09-06 11:20:11245 struct strbuf buffer;
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46246 int missing_ok = flags & WRITE_TREE_MISSING_OK;
247 int dryrun = flags & WRITE_TREE_DRY_RUN;
David Turneraecf5672014-07-06 04:06:56248 int repair = flags & WRITE_TREE_REPAIR;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28249 int to_invalidate = 0;
Junio C Hamano74986462006-04-23 23:52:20250 int i;
251
David Turneraecf5672014-07-06 04:06:56252 assert(!(dryrun && repair));
253
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27254 *skip_count = 0;
255
Junio C Hamanodd0c34c2006-04-24 22:12:42256 if (0 <= it->entry_count && has_sha1_file(it->sha1))
Junio C Hamano74986462006-04-23 23:52:20257 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 04:15:26270 i = 0;
271 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00272 const struct cache_entry *ce = cache[i];
Junio C Hamano74986462006-04-23 23:52:20273 struct cache_tree_sub *sub;
274 const char *path, *slash;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27275 int pathlen, sublen, subcnt, subskip;
Junio C Hamano74986462006-04-23 23:52:20276
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 04:15:26283 if (!slash) {
284 i++;
Junio C Hamano74986462006-04-23 23:52:20285 continue;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26286 }
Junio C Hamano74986462006-04-23 23:52:20287 /*
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 04:15:27300 &subskip,
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46301 flags);
Johannes Sixt3d12d0c2006-11-13 13:50:00302 if (subcnt < 0)
303 return subcnt;
Jeff King729dbbd2014-10-29 17:11:58304 if (!subcnt)
305 die("index cache-tree records empty sub-tree");
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26306 i += subcnt;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27307 sub->count = subcnt; /* to be used in the next loop */
308 *skip_count += subskip;
Junio C Hamano74986462006-04-23 23:52:20309 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 10:35:04317 strbuf_init(&buffer, 8192);
Junio C Hamano74986462006-04-23 23:52:20318
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26319 i = 0;
320 while (i < entries) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00321 const struct cache_entry *ce = cache[i];
Nguyễn Thái Ngọc Duyc041d542016-07-16 05:06:26322 struct cache_tree_sub *sub = NULL;
Junio C Hamano74986462006-04-23 23:52:20323 const char *path, *slash;
324 int pathlen, entlen;
325 const unsigned char *sha1;
326 unsigned mode;
Junio C Hamano4ed115e2014-09-02 21:16:20327 int expected_missing = 0;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27328 int contains_ita = 0;
Junio C Hamano74986462006-04-23 23:52:20329
330 path = ce->name;
331 pathlen = ce_namelen(ce);
332 if (pathlen <= baselen || memcmp(base, path, baselen))
333 break; /* at the end of this level */
334
335 slash = strchr(path + baselen, '/');
336 if (slash) {
337 entlen = slash - (path + baselen);
338 sub = find_subtree(it, path + baselen, entlen, 0);
339 if (!sub)
340 die("cache-tree.c: '%.*s' in '%s' not found",
341 entlen, path + baselen, path);
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27342 i += sub->count;
Junio C Hamano74986462006-04-23 23:52:20343 sha1 = sub->cache_tree->sha1;
344 mode = S_IFDIR;
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27345 contains_ita = sub->cache_tree->entry_count < 0;
346 if (contains_ita) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28347 to_invalidate = 1;
Junio C Hamano4ed115e2014-09-02 21:16:20348 expected_missing = 1;
349 }
Junio C Hamano74986462006-04-23 23:52:20350 }
351 else {
352 sha1 = ce->sha1;
Linus Torvalds7a51ed62008-01-15 00:03:17353 mode = ce->ce_mode;
Junio C Hamano74986462006-04-23 23:52:20354 entlen = pathlen - baselen;
Nguyễn Thái Ngọc Duy386cc8b2012-12-16 04:15:26355 i++;
Junio C Hamano74986462006-04-23 23:52:20356 }
Jonathan Niederb6b56ac2010-08-10 03:32:11357 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
358 strbuf_release(&buffer);
Junio C Hamano4ed115e2014-09-02 21:16:20359 if (expected_missing)
360 return -1;
Linus Torvaldsa3883732009-07-14 18:25:17361 return error("invalid object %06o %s for '%.*s'",
362 mode, sha1_to_hex(sha1), entlen+baselen, path);
Jonathan Niederb6b56ac2010-08-10 03:32:11363 }
Junio C Hamano74986462006-04-23 23:52:20364
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27365 /*
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 04:15:28375 /*
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 05:06:26380 if (!sub && ce_intent_to_add(ce)) {
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28381 to_invalidate = 1;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27382 continue;
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28383 }
Junio C Hamano74986462006-04-23 23:52:20384
Nguyễn Thái Ngọc Duy6d6a7822016-07-16 05:06:27385 /*
386 * "sub" can be an empty tree if all subentries are i-t-a.
387 */
388 if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
389 continue;
390
Pierre Habouzit5242bcb2007-09-06 11:20:11391 strbuf_grow(&buffer, entlen + 100);
392 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
393 strbuf_add(&buffer, sha1, 20);
Junio C Hamano74986462006-04-23 23:52:20394
395#if DEBUG
Junio C Hamano00703e62006-05-03 23:10:45396 fprintf(stderr, "cache-tree update-one %o %.*s\n",
Junio C Hamano74986462006-04-23 23:52:20397 mode, entlen, path + baselen);
398#endif
399 }
400
David Turneraecf5672014-07-06 04:06:56401 if (repair) {
402 unsigned char sha1[20];
403 hash_sha1_file(buffer.buf, buffer.len, tree_type, sha1);
404 if (has_sha1_file(sha1))
405 hashcpy(it->sha1, sha1);
406 else
407 to_invalidate = 1;
408 } else if (dryrun)
Pierre Habouzit5242bcb2007-09-06 11:20:11409 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
Junio C Hamanoedae5f02008-04-23 16:47:17410 else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
411 strbuf_release(&buffer);
412 return -1;
413 }
414
Pierre Habouzit5242bcb2007-09-06 11:20:11415 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duyeec3e7e2012-12-16 04:15:28416 it->entry_count = to_invalidate ? -1 : i - *skip_count;
Junio C Hamano74986462006-04-23 23:52:20417#if DEBUG
Junio C Hamano00703e62006-05-03 23:10:45418 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
Junio C Hamano74986462006-04-23 23:52:20419 it->entry_count, it->subtree_nr,
420 sha1_to_hex(it->sha1));
421#endif
422 return i;
423}
424
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32425int cache_tree_update(struct index_state *istate, int flags)
Junio C Hamano74986462006-04-23 23:52:20426{
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32427 struct cache_tree *it = istate->cache_tree;
428 struct cache_entry **cache = istate->cache;
429 int entries = istate->cache_nr;
430 int skip, i = verify_cache(cache, entries, flags);
431
Junio C Hamano74986462006-04-23 23:52:20432 if (i)
433 return i;
Nguyễn Thái Ngọc Duy3cf773e2012-12-16 04:15:27434 i = update_one(it, cache, entries, "", 0, &skip, flags);
Junio C Hamano74986462006-04-23 23:52:20435 if (i < 0)
436 return i;
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32437 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamano74986462006-04-23 23:52:20438 return 0;
439}
440
Pierre Habouzit1dffb8f2007-09-25 08:22:44441static void write_one(struct strbuf *buffer, struct cache_tree *it,
442 const char *path, int pathlen)
Junio C Hamano74986462006-04-23 23:52:20443{
444 int i;
445
446 /* One "cache-tree" entry consists of the following:
447 * path (NUL terminated)
448 * entry_count, subtree_nr ("%d %d\n")
449 * tree-sha1 (missing if invalid)
450 * subtree_nr "cache-tree" entries for subtrees.
451 */
Pierre Habouzit5242bcb2007-09-06 11:20:11452 strbuf_grow(buffer, pathlen + 100);
453 strbuf_add(buffer, path, pathlen);
454 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
Junio C Hamano74986462006-04-23 23:52:20455
456#if DEBUG
457 if (0 <= it->entry_count)
458 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
459 pathlen, path, it->entry_count, it->subtree_nr,
460 sha1_to_hex(it->sha1));
461 else
462 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
463 pathlen, path, it->subtree_nr);
464#endif
465
466 if (0 <= it->entry_count) {
Pierre Habouzit5242bcb2007-09-06 11:20:11467 strbuf_add(buffer, it->sha1, 20);
Junio C Hamano74986462006-04-23 23:52:20468 }
469 for (i = 0; i < it->subtree_nr; i++) {
470 struct cache_tree_sub *down = it->down[i];
Junio C Hamano61fa3092006-04-26 00:40:02471 if (i) {
472 struct cache_tree_sub *prev = it->down[i-1];
473 if (subtree_name_cmp(down->name, down->namelen,
474 prev->name, prev->namelen) <= 0)
475 die("fatal - unsorted cache subtree");
476 }
Pierre Habouzit1dffb8f2007-09-25 08:22:44477 write_one(buffer, down->cache_tree, down->name, down->namelen);
Junio C Hamano74986462006-04-23 23:52:20478 }
Junio C Hamano74986462006-04-23 23:52:20479}
480
Pierre Habouzit1dffb8f2007-09-25 08:22:44481void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
Junio C Hamano74986462006-04-23 23:52:20482{
Pierre Habouzit1dffb8f2007-09-25 08:22:44483 write_one(sb, root, "", 0);
Junio C Hamano74986462006-04-23 23:52:20484}
485
486static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
487{
488 const char *buf = *buffer;
489 unsigned long size = *size_p;
Johannes Schindelin0111ea32006-05-02 01:31:02490 const char *cp;
491 char *ep;
Junio C Hamano74986462006-04-23 23:52:20492 struct cache_tree *it;
493 int i, subtree_nr;
494
495 it = NULL;
496 /* skip name, but make sure name exists */
497 while (size && *buf) {
498 size--;
499 buf++;
500 }
501 if (!size)
502 goto free_return;
503 buf++; size--;
504 it = cache_tree();
Johannes Schindelin0111ea32006-05-02 01:31:02505
506 cp = buf;
507 it->entry_count = strtol(cp, &ep, 10);
508 if (cp == ep)
509 goto free_return;
510 cp = ep;
511 subtree_nr = strtol(cp, &ep, 10);
512 if (cp == ep)
Junio C Hamano74986462006-04-23 23:52:20513 goto free_return;
514 while (size && *buf && *buf != '\n') {
515 size--;
516 buf++;
517 }
518 if (!size)
519 goto free_return;
520 buf++; size--;
521 if (0 <= it->entry_count) {
522 if (size < 20)
523 goto free_return;
Junio C Hamano63daae42007-06-23 06:19:43524 hashcpy(it->sha1, (const unsigned char*)buf);
Junio C Hamano74986462006-04-23 23:52:20525 buf += 20;
526 size -= 20;
527 }
528
529#if DEBUG
530 if (0 <= it->entry_count)
531 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
532 *buffer, it->entry_count, subtree_nr,
533 sha1_to_hex(it->sha1));
534 else
535 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
536 *buffer, subtree_nr);
537#endif
538
539 /*
540 * Just a heuristic -- we do not add directories that often but
541 * we do not want to have to extend it immediately when we do,
542 * hence +2.
543 */
544 it->subtree_alloc = subtree_nr + 2;
545 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
546 for (i = 0; i < subtree_nr; i++) {
547 /* read each subtree */
548 struct cache_tree *sub;
Junio C Hamano61fa3092006-04-26 00:40:02549 struct cache_tree_sub *subtree;
Junio C Hamano74986462006-04-23 23:52:20550 const char *name = buf;
Junio C Hamano7927a552006-04-27 08:33:07551
Junio C Hamano74986462006-04-23 23:52:20552 sub = read_one(&buf, &size);
553 if (!sub)
554 goto free_return;
Junio C Hamano7927a552006-04-27 08:33:07555 subtree = cache_tree_sub(it, name);
Junio C Hamano61fa3092006-04-26 00:40:02556 subtree->cache_tree = sub;
Junio C Hamano74986462006-04-23 23:52:20557 }
558 if (subtree_nr != it->subtree_nr)
559 die("cache-tree: internal error");
560 *buffer = buf;
561 *size_p = size;
562 return it;
563
564 free_return:
Junio C Hamanobad68ec2006-04-25 04:18:58565 cache_tree_free(&it);
Junio C Hamano74986462006-04-23 23:52:20566 return NULL;
567}
568
Junio C Hamanobad68ec2006-04-25 04:18:58569struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
Junio C Hamano74986462006-04-23 23:52:20570{
Junio C Hamanobad68ec2006-04-25 04:18:58571 if (buffer[0])
Junio C Hamano74986462006-04-23 23:52:20572 return NULL; /* not the whole tree */
Junio C Hamano74986462006-04-23 23:52:20573 return read_one(&buffer, &size);
574}
Junio C Hamano6bd20352006-04-26 08:20:50575
Nanako Shiraishi7ba04d92008-07-16 10:42:10576static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
Junio C Hamano6bd20352006-04-26 08:20:50577{
Junio C Hamanob87fc962009-05-20 22:53:57578 if (!it)
579 return NULL;
Junio C Hamano6bd20352006-04-26 08:20:50580 while (*path) {
581 const char *slash;
582 struct cache_tree_sub *sub;
583
Michael Haggerty17e22dd2014-03-05 17:26:26584 slash = strchrnul(path, '/');
Michael Haggerty79192b82014-03-05 17:26:27585 /*
586 * Between path and slash is the name of the subtree
587 * to look for.
Junio C Hamano6bd20352006-04-26 08:20:50588 */
589 sub = find_subtree(it, path, slash - path, 0);
590 if (!sub)
591 return NULL;
592 it = sub->cache_tree;
Michael Haggerty34910472014-03-05 17:26:30593
Junio C Hamano6bd20352006-04-26 08:20:50594 path = slash;
Michael Haggerty34910472014-03-05 17:26:30595 while (*path == '/')
596 path++;
Junio C Hamano6bd20352006-04-26 08:20:50597 }
598 return it;
599}
Junio C Hamano45525bd2008-01-11 06:49:35600
Paul Tand23a5112015-08-04 13:51:40601int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
Junio C Hamano45525bd2008-01-11 06:49:35602{
603 int entries, was_valid, newfd;
Junio C Hamanod11b8d32009-05-20 18:04:35604 struct lock_file *lock_file;
Junio C Hamano45525bd2008-01-11 06:49:35605
606 /*
607 * We can't free this memory, it becomes part of a linked list
608 * parsed atexit()
609 */
Junio C Hamanod11b8d32009-05-20 18:04:35610 lock_file = xcalloc(1, sizeof(struct lock_file));
Junio C Hamano45525bd2008-01-11 06:49:35611
Paul Tand23a5112015-08-04 13:51:40612 newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);
Junio C Hamano45525bd2008-01-11 06:49:35613
Paul Tand23a5112015-08-04 13:51:40614 entries = read_index_from(index_state, index_path);
Junio C Hamano45525bd2008-01-11 06:49:35615 if (entries < 0)
616 return WRITE_TREE_UNREADABLE_INDEX;
Junio C Hamanod11b8d32009-05-20 18:04:35617 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
Paul Tand23a5112015-08-04 13:51:40618 cache_tree_free(&index_state->cache_tree);
Junio C Hamano45525bd2008-01-11 06:49:35619
Paul Tand23a5112015-08-04 13:51:40620 if (!index_state->cache_tree)
621 index_state->cache_tree = cache_tree();
Junio C Hamano45525bd2008-01-11 06:49:35622
Paul Tand23a5112015-08-04 13:51:40623 was_valid = cache_tree_fully_valid(index_state->cache_tree);
Junio C Hamano45525bd2008-01-11 06:49:35624 if (!was_valid) {
Paul Tand23a5112015-08-04 13:51:40625 if (cache_tree_update(index_state, flags) < 0)
Junio C Hamano45525bd2008-01-11 06:49:35626 return WRITE_TREE_UNMERGED_INDEX;
627 if (0 <= newfd) {
Paul Tand23a5112015-08-04 13:51:40628 if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
Junio C Hamano45525bd2008-01-11 06:49:35629 newfd = -1;
630 }
631 /* Not being able to write is fine -- we are only interested
632 * in updating the cache-tree part, and if the next caller
633 * ends up using the old index with unupdated cache-tree part
634 * it misses the work we did here, but that is just a
635 * performance penalty and not a big deal.
636 */
637 }
638
639 if (prefix) {
Paul Tand23a5112015-08-04 13:51:40640 struct cache_tree *subtree;
641 subtree = cache_tree_find(index_state->cache_tree, prefix);
Junio C Hamano45525bd2008-01-11 06:49:35642 if (!subtree)
643 return WRITE_TREE_PREFIX_ERROR;
644 hashcpy(sha1, subtree->sha1);
645 }
646 else
Paul Tand23a5112015-08-04 13:51:40647 hashcpy(sha1, index_state->cache_tree->sha1);
Junio C Hamano45525bd2008-01-11 06:49:35648
649 if (0 <= newfd)
650 rollback_lock_file(lock_file);
651
652 return 0;
653}
Junio C Hamanob9d37a52009-04-20 10:58:18654
Paul Tand23a5112015-08-04 13:51:40655int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
656{
657 return write_index_as_tree(sha1, &the_index, get_index_file(), flags, prefix);
658}
659
Junio C Hamanob9d37a52009-04-20 10:58:18660static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
661{
662 struct tree_desc desc;
663 struct name_entry entry;
664 int cnt;
665
brian m. carlsoned1c9972015-11-10 02:22:29666 hashcpy(it->sha1, tree->object.oid.hash);
Junio C Hamanob9d37a52009-04-20 10:58:18667 init_tree_desc(&desc, tree->buffer, tree->size);
668 cnt = 0;
669 while (tree_entry(&desc, &entry)) {
670 if (!S_ISDIR(entry.mode))
671 cnt++;
672 else {
673 struct cache_tree_sub *sub;
brian m. carlson7d924c92016-04-17 23:10:39674 struct tree *subtree = lookup_tree(entry.oid->hash);
Junio C Hamanob9d37a52009-04-20 10:58:18675 if (!subtree->object.parsed)
676 parse_tree(subtree);
677 sub = cache_tree_sub(it, entry.path);
678 sub->cache_tree = cache_tree();
679 prime_cache_tree_rec(sub->cache_tree, subtree);
680 cnt += sub->cache_tree->entry_count;
681 }
682 }
683 it->entry_count = cnt;
684}
685
Nguyễn Thái Ngọc Duye6c286e2014-06-13 12:19:33686void prime_cache_tree(struct index_state *istate, struct tree *tree)
Junio C Hamanob9d37a52009-04-20 10:58:18687{
Nguyễn Thái Ngọc Duye6c286e2014-06-13 12:19:33688 cache_tree_free(&istate->cache_tree);
689 istate->cache_tree = cache_tree();
690 prime_cache_tree_rec(istate->cache_tree, tree);
691 istate->cache_changed |= CACHE_TREE_CHANGED;
Junio C Hamanob9d37a52009-04-20 10:58:18692}
Junio C Hamanob65982b2009-05-20 22:57:22693
694/*
695 * find the cache_tree that corresponds to the current level without
696 * exploding the full path into textual form. The root of the
697 * cache tree is given as "root", and our current level is "info".
698 * (1) When at root level, info->prev is NULL, so it is "root" itself.
699 * (2) Otherwise, find the cache_tree that corresponds to one level
700 * above us, and find ourselves in there.
701 */
702static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
703 struct traverse_info *info)
704{
705 struct cache_tree *our_parent;
706
707 if (!info->prev)
708 return root;
709 our_parent = find_cache_tree_from_traversal(root, info->prev);
710 return cache_tree_find(our_parent, info->name.path);
711}
712
713int cache_tree_matches_traversal(struct cache_tree *root,
714 struct name_entry *ent,
715 struct traverse_info *info)
716{
717 struct cache_tree *it;
718
719 it = find_cache_tree_from_traversal(root, info);
720 it = cache_tree_find(it, ent->path);
brian m. carlson7d924c92016-04-17 23:10:39721 if (it && it->entry_count > 0 && !hashcmp(ent->oid->hash, it->sha1))
Junio C Hamanob65982b2009-05-20 22:57:22722 return it->entry_count;
723 return 0;
724}
Thomas Rast996277c2011-12-06 17:43:37725
Nguyễn Thái Ngọc Duye859c692012-01-16 02:36:46726int update_main_cache_tree(int flags)
Thomas Rast996277c2011-12-06 17:43:37727{
728 if (!the_index.cache_tree)
729 the_index.cache_tree = cache_tree();
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 12:19:32730 return cache_tree_update(&the_index, flags);
Thomas Rast996277c2011-12-06 17:43:37731}