]> git.ipfire.org Git - thirdparty/git.git/blame - cache-tree.c
test-lib.sh: A command dying due to a signal is an unexpected failure.
[thirdparty/git.git] / cache-tree.c
CommitLineData
74986462
JH
1#include "cache.h"
2#include "tree.h"
3#include "cache-tree.h"
4
4903161f 5#ifndef DEBUG
74986462 6#define DEBUG 0
4903161f 7#endif
74986462
JH
8
9struct cache_tree *cache_tree(void)
10{
11 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
12 it->entry_count = -1;
13 return it;
14}
15
bad68ec9 16void cache_tree_free(struct cache_tree **it_p)
74986462
JH
17{
18 int i;
bad68ec9 19 struct cache_tree *it = *it_p;
74986462
JH
20
21 if (!it)
22 return;
23 for (i = 0; i < it->subtree_nr; i++)
61fa3097
JH
24 if (it->down[i])
25 cache_tree_free(&it->down[i]->cache_tree);
74986462
JH
26 free(it->down);
27 free(it);
bad68ec9 28 *it_p = NULL;
74986462
JH
29}
30
61fa3097
JH
31static int subtree_name_cmp(const char *one, int onelen,
32 const char *two, int twolen)
33{
34 if (onelen < twolen)
35 return -1;
36 if (twolen < onelen)
37 return 1;
38 return memcmp(one, two, onelen);
39}
40
41static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
42{
43 struct cache_tree_sub **down = it->down;
44 int lo, hi;
45 lo = 0;
46 hi = it->subtree_nr;
47 while (lo < hi) {
48 int mi = (lo + hi) / 2;
49 struct cache_tree_sub *mdl = down[mi];
50 int cmp = subtree_name_cmp(path, pathlen,
51 mdl->name, mdl->namelen);
52 if (!cmp)
53 return mi;
54 if (cmp < 0)
55 hi = mi;
56 else
57 lo = mi + 1;
58 }
59 return -lo-1;
60}
61
74986462
JH
62static struct cache_tree_sub *find_subtree(struct cache_tree *it,
63 const char *path,
64 int pathlen,
65 int create)
66{
74986462 67 struct cache_tree_sub *down;
61fa3097
JH
68 int pos = subtree_pos(it, path, pathlen);
69 if (0 <= pos)
70 return it->down[pos];
74986462
JH
71 if (!create)
72 return NULL;
61fa3097
JH
73
74 pos = -pos-1;
74986462
JH
75 if (it->subtree_alloc <= it->subtree_nr) {
76 it->subtree_alloc = alloc_nr(it->subtree_alloc);
77 it->down = xrealloc(it->down, it->subtree_alloc *
78 sizeof(*it->down));
79 }
61fa3097
JH
80 it->subtree_nr++;
81
74986462 82 down = xmalloc(sizeof(*down) + pathlen + 1);
61fa3097 83 down->cache_tree = NULL;
74986462
JH
84 down->namelen = pathlen;
85 memcpy(down->name, path, pathlen);
61fa3097
JH
86 down->name[pathlen] = 0;
87
88 if (pos < it->subtree_nr)
89 memmove(it->down + pos + 1,
90 it->down + pos,
91 sizeof(down) * (it->subtree_nr - pos - 1));
92 it->down[pos] = down;
74986462
JH
93 return down;
94}
95
7927a55d
JH
96struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
97{
98 int pathlen = strlen(path);
99 return find_subtree(it, path, pathlen, 1);
100}
101
74986462
JH
102void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
103{
104 /* a/b/c
105 * ==> invalidate self
106 * ==> find "a", have it invalidate "b/c"
107 * a
108 * ==> invalidate self
109 * ==> if "a" exists as a subtree, remove it.
110 */
111 const char *slash;
112 int namelen;
113 struct cache_tree_sub *down;
114
00703e6d
JH
115#if DEBUG
116 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
117#endif
118
74986462
JH
119 if (!it)
120 return;
121 slash = strchr(path, '/');
122 it->entry_count = -1;
123 if (!slash) {
61fa3097 124 int pos;
74986462 125 namelen = strlen(path);
61fa3097
JH
126 pos = subtree_pos(it, path, namelen);
127 if (0 <= pos) {
128 cache_tree_free(&it->down[pos]->cache_tree);
129 free(it->down[pos]);
74986462
JH
130 /* 0 1 2 3 4 5
131 * ^ ^subtree_nr = 6
61fa3097 132 * pos
74986462 133 * move 4 and 5 up one place (2 entries)
61fa3097 134 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
74986462 135 */
61fa3097 136 memmove(it->down+pos, it->down+pos+1,
74986462 137 sizeof(struct cache_tree_sub *) *
61fa3097 138 (it->subtree_nr - pos - 1));
74986462
JH
139 it->subtree_nr--;
140 }
141 return;
142 }
143 namelen = slash - path;
144 down = find_subtree(it, path, namelen, 0);
145 if (down)
146 cache_tree_invalidate_path(down->cache_tree, slash + 1);
147}
148
149static int verify_cache(struct cache_entry **cache,
150 int entries)
151{
152 int i, funny;
153
154 /* Verify that the tree is merged */
155 funny = 0;
156 for (i = 0; i < entries; i++) {
157 struct cache_entry *ce = cache[i];
158 if (ce_stage(ce)) {
159 if (10 < ++funny) {
160 fprintf(stderr, "...\n");
161 break;
162 }
163 fprintf(stderr, "%s: unmerged (%s)\n",
164 ce->name, sha1_to_hex(ce->sha1));
165 }
166 }
167 if (funny)
168 return -1;
169
170 /* Also verify that the cache does not have path and path/file
171 * at the same time. At this point we know the cache has only
172 * stage 0 entries.
173 */
174 funny = 0;
175 for (i = 0; i < entries - 1; i++) {
176 /* path/file always comes after path because of the way
177 * the cache is sorted. Also path can appear only once,
178 * which means conflicting one would immediately follow.
179 */
180 const char *this_name = cache[i]->name;
181 const char *next_name = cache[i+1]->name;
182 int this_len = strlen(this_name);
183 if (this_len < strlen(next_name) &&
184 strncmp(this_name, next_name, this_len) == 0 &&
185 next_name[this_len] == '/') {
186 if (10 < ++funny) {
187 fprintf(stderr, "...\n");
188 break;
189 }
190 fprintf(stderr, "You have both %s and %s\n",
191 this_name, next_name);
192 }
193 }
194 if (funny)
195 return -1;
196 return 0;
197}
198
199static void discard_unused_subtrees(struct cache_tree *it)
200{
201 struct cache_tree_sub **down = it->down;
202 int nr = it->subtree_nr;
203 int dst, src;
204 for (dst = src = 0; src < nr; src++) {
205 struct cache_tree_sub *s = down[src];
206 if (s->used)
207 down[dst++] = s;
208 else {
bad68ec9 209 cache_tree_free(&s->cache_tree);
74986462
JH
210 free(s);
211 it->subtree_nr--;
212 }
213 }
214}
215
bad68ec9
JH
216int cache_tree_fully_valid(struct cache_tree *it)
217{
218 int i;
219 if (!it)
220 return 0;
221 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
222 return 0;
223 for (i = 0; i < it->subtree_nr; i++) {
224 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
225 return 0;
226 }
227 return 1;
228}
229
74986462
JH
230static int update_one(struct cache_tree *it,
231 struct cache_entry **cache,
232 int entries,
233 const char *base,
234 int baselen,
2956dd3b
JH
235 int missing_ok,
236 int dryrun)
74986462
JH
237{
238 unsigned long size, offset;
239 char *buffer;
240 int i;
241
dd0c34c4 242 if (0 <= it->entry_count && has_sha1_file(it->sha1))
74986462
JH
243 return it->entry_count;
244
245 /*
246 * We first scan for subtrees and update them; we start by
247 * marking existing subtrees -- the ones that are unmarked
248 * should not be in the result.
249 */
250 for (i = 0; i < it->subtree_nr; i++)
251 it->down[i]->used = 0;
252
253 /*
254 * Find the subtrees and update them.
255 */
256 for (i = 0; i < entries; i++) {
257 struct cache_entry *ce = cache[i];
258 struct cache_tree_sub *sub;
259 const char *path, *slash;
260 int pathlen, sublen, subcnt;
261
262 path = ce->name;
263 pathlen = ce_namelen(ce);
264 if (pathlen <= baselen || memcmp(base, path, baselen))
265 break; /* at the end of this level */
266
267 slash = strchr(path + baselen, '/');
268 if (!slash)
269 continue;
270 /*
271 * a/bbb/c (base = a/, slash = /c)
272 * ==>
273 * path+baselen = bbb/c, sublen = 3
274 */
275 sublen = slash - (path + baselen);
276 sub = find_subtree(it, path + baselen, sublen, 1);
277 if (!sub->cache_tree)
278 sub->cache_tree = cache_tree();
279 subcnt = update_one(sub->cache_tree,
280 cache + i, entries - i,
281 path,
282 baselen + sublen + 1,
2956dd3b
JH
283 missing_ok,
284 dryrun);
74986462
JH
285 i += subcnt - 1;
286 sub->used = 1;
287 }
288
289 discard_unused_subtrees(it);
290
291 /*
292 * Then write out the tree object for this level.
293 */
294 size = 8192;
295 buffer = xmalloc(size);
296 offset = 0;
297
298 for (i = 0; i < entries; i++) {
299 struct cache_entry *ce = cache[i];
300 struct cache_tree_sub *sub;
301 const char *path, *slash;
302 int pathlen, entlen;
303 const unsigned char *sha1;
304 unsigned mode;
305
306 path = ce->name;
307 pathlen = ce_namelen(ce);
308 if (pathlen <= baselen || memcmp(base, path, baselen))
309 break; /* at the end of this level */
310
311 slash = strchr(path + baselen, '/');
312 if (slash) {
313 entlen = slash - (path + baselen);
314 sub = find_subtree(it, path + baselen, entlen, 0);
315 if (!sub)
316 die("cache-tree.c: '%.*s' in '%s' not found",
317 entlen, path + baselen, path);
318 i += sub->cache_tree->entry_count - 1;
319 sha1 = sub->cache_tree->sha1;
320 mode = S_IFDIR;
321 }
322 else {
323 sha1 = ce->sha1;
324 mode = ntohl(ce->ce_mode);
325 entlen = pathlen - baselen;
326 }
327 if (!missing_ok && !has_sha1_file(sha1))
328 return error("invalid object %s", sha1_to_hex(sha1));
329
330 if (!ce->ce_mode)
331 continue; /* entry being removed */
332
333 if (size < offset + entlen + 100) {
334 size = alloc_nr(offset + entlen + 100);
335 buffer = xrealloc(buffer, size);
336 }
337 offset += sprintf(buffer + offset,
338 "%o %.*s", mode, entlen, path + baselen);
339 buffer[offset++] = 0;
e702496e 340 hashcpy((unsigned char*)buffer + offset, sha1);
74986462
JH
341 offset += 20;
342
343#if DEBUG
00703e6d 344 fprintf(stderr, "cache-tree update-one %o %.*s\n",
74986462
JH
345 mode, entlen, path + baselen);
346#endif
347 }
348
abdc3fc8
RS
349 if (dryrun)
350 hash_sha1_file(buffer, offset, tree_type, it->sha1);
2956dd3b
JH
351 else
352 write_sha1_file(buffer, offset, tree_type, it->sha1);
74986462
JH
353 free(buffer);
354 it->entry_count = i;
355#if DEBUG
00703e6d 356 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
74986462
JH
357 it->entry_count, it->subtree_nr,
358 sha1_to_hex(it->sha1));
359#endif
360 return i;
361}
362
363int cache_tree_update(struct cache_tree *it,
364 struct cache_entry **cache,
365 int entries,
2956dd3b
JH
366 int missing_ok,
367 int dryrun)
74986462
JH
368{
369 int i;
370 i = verify_cache(cache, entries);
371 if (i)
372 return i;
2956dd3b 373 i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
74986462
JH
374 if (i < 0)
375 return i;
376 return 0;
377}
378
379static void *write_one(struct cache_tree *it,
380 char *path,
381 int pathlen,
382 char *buffer,
383 unsigned long *size,
384 unsigned long *offset)
385{
386 int i;
387
388 /* One "cache-tree" entry consists of the following:
389 * path (NUL terminated)
390 * entry_count, subtree_nr ("%d %d\n")
391 * tree-sha1 (missing if invalid)
392 * subtree_nr "cache-tree" entries for subtrees.
393 */
394 if (*size < *offset + pathlen + 100) {
395 *size = alloc_nr(*offset + pathlen + 100);
396 buffer = xrealloc(buffer, *size);
397 }
398 *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
399 pathlen, path, 0,
400 it->entry_count, it->subtree_nr);
401
402#if DEBUG
403 if (0 <= it->entry_count)
404 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
405 pathlen, path, it->entry_count, it->subtree_nr,
406 sha1_to_hex(it->sha1));
407 else
408 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
409 pathlen, path, it->subtree_nr);
410#endif
411
412 if (0 <= it->entry_count) {
e702496e 413 hashcpy((unsigned char*)buffer + *offset, it->sha1);
74986462
JH
414 *offset += 20;
415 }
416 for (i = 0; i < it->subtree_nr; i++) {
417 struct cache_tree_sub *down = it->down[i];
61fa3097
JH
418 if (i) {
419 struct cache_tree_sub *prev = it->down[i-1];
420 if (subtree_name_cmp(down->name, down->namelen,
421 prev->name, prev->namelen) <= 0)
422 die("fatal - unsorted cache subtree");
423 }
74986462
JH
424 buffer = write_one(down->cache_tree, down->name, down->namelen,
425 buffer, size, offset);
426 }
427 return buffer;
428}
429
bad68ec9 430void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
74986462
JH
431{
432 char path[PATH_MAX];
433 unsigned long size = 8192;
434 char *buffer = xmalloc(size);
435
bad68ec9 436 *size_p = 0;
74986462 437 path[0] = 0;
bad68ec9 438 return write_one(root, path, 0, buffer, &size, size_p);
74986462
JH
439}
440
441static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
442{
443 const char *buf = *buffer;
444 unsigned long size = *size_p;
0111ea38
JS
445 const char *cp;
446 char *ep;
74986462
JH
447 struct cache_tree *it;
448 int i, subtree_nr;
449
450 it = NULL;
451 /* skip name, but make sure name exists */
452 while (size && *buf) {
453 size--;
454 buf++;
455 }
456 if (!size)
457 goto free_return;
458 buf++; size--;
459 it = cache_tree();
0111ea38
JS
460
461 cp = buf;
462 it->entry_count = strtol(cp, &ep, 10);
463 if (cp == ep)
464 goto free_return;
465 cp = ep;
466 subtree_nr = strtol(cp, &ep, 10);
467 if (cp == ep)
74986462
JH
468 goto free_return;
469 while (size && *buf && *buf != '\n') {
470 size--;
471 buf++;
472 }
473 if (!size)
474 goto free_return;
475 buf++; size--;
476 if (0 <= it->entry_count) {
477 if (size < 20)
478 goto free_return;
e702496e 479 hashcpy(it->sha1, (unsigned char*)buf);
74986462
JH
480 buf += 20;
481 size -= 20;
482 }
483
484#if DEBUG
485 if (0 <= it->entry_count)
486 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
487 *buffer, it->entry_count, subtree_nr,
488 sha1_to_hex(it->sha1));
489 else
490 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
491 *buffer, subtree_nr);
492#endif
493
494 /*
495 * Just a heuristic -- we do not add directories that often but
496 * we do not want to have to extend it immediately when we do,
497 * hence +2.
498 */
499 it->subtree_alloc = subtree_nr + 2;
500 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
501 for (i = 0; i < subtree_nr; i++) {
502 /* read each subtree */
503 struct cache_tree *sub;
61fa3097 504 struct cache_tree_sub *subtree;
74986462 505 const char *name = buf;
7927a55d 506
74986462
JH
507 sub = read_one(&buf, &size);
508 if (!sub)
509 goto free_return;
7927a55d 510 subtree = cache_tree_sub(it, name);
61fa3097 511 subtree->cache_tree = sub;
74986462
JH
512 }
513 if (subtree_nr != it->subtree_nr)
514 die("cache-tree: internal error");
515 *buffer = buf;
516 *size_p = size;
517 return it;
518
519 free_return:
bad68ec9 520 cache_tree_free(&it);
74986462
JH
521 return NULL;
522}
523
bad68ec9 524struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
74986462 525{
bad68ec9 526 if (buffer[0])
74986462 527 return NULL; /* not the whole tree */
74986462
JH
528 return read_one(&buffer, &size);
529}
6bd20358
JH
530
531struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
532{
533 while (*path) {
534 const char *slash;
535 struct cache_tree_sub *sub;
536
537 slash = strchr(path, '/');
538 if (!slash)
539 slash = path + strlen(path);
540 /* between path and slash is the name of the
541 * subtree to look for.
542 */
543 sub = find_subtree(it, path, slash - path, 0);
544 if (!sub)
545 return NULL;
546 it = sub->cache_tree;
547 if (slash)
548 while (*slash && *slash == '/')
549 slash++;
550 if (!slash || !*slash)
551 return it; /* prefix ended with slashes */
552 path = slash;
553 }
554 return it;
555}