Fix merge-one-file for our-side-added/our-side-removed cases
[git/jrn.git] / cache-tree.c
blob077f03436941e9c0bf31d3bb2002c1e36b8817b9
1 #include "cache.h"
2 #include "tree.h"
3 #include "cache-tree.h"
5 #ifndef DEBUG
6 #define DEBUG 0
7 #endif
9 struct cache_tree *cache_tree(void)
11 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
12 it->entry_count = -1;
13 return it;
16 void cache_tree_free(struct cache_tree **it_p)
18 int i;
19 struct cache_tree *it = *it_p;
21 if (!it)
22 return;
23 for (i = 0; i < it->subtree_nr; i++)
24 if (it->down[i])
25 cache_tree_free(&it->down[i]->cache_tree);
26 free(it->down);
27 free(it);
28 *it_p = NULL;
31 static int subtree_name_cmp(const char *one, int onelen,
32 const char *two, int twolen)
34 if (onelen < twolen)
35 return -1;
36 if (twolen < onelen)
37 return 1;
38 return memcmp(one, two, onelen);
41 static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
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;
59 return -lo-1;
62 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
63 const char *path,
64 int pathlen,
65 int create)
67 struct cache_tree_sub *down;
68 int pos = subtree_pos(it, path, pathlen);
69 if (0 <= pos)
70 return it->down[pos];
71 if (!create)
72 return NULL;
74 pos = -pos-1;
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));
80 it->subtree_nr++;
82 down = xmalloc(sizeof(*down) + pathlen + 1);
83 down->cache_tree = NULL;
84 down->namelen = pathlen;
85 memcpy(down->name, path, pathlen);
86 down->name[pathlen] = 0;
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;
93 return down;
96 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
98 int pathlen = strlen(path);
99 return find_subtree(it, path, pathlen, 1);
102 void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
104 /* a/b/c
105 * ==> invalidate self
106 * ==> find "a", have it invalidate "b/c"
108 * ==> invalidate self
109 * ==> if "a" exists as a subtree, remove it.
111 const char *slash;
112 int namelen;
113 struct cache_tree_sub *down;
115 #if DEBUG
116 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
117 #endif
119 if (!it)
120 return;
121 slash = strchr(path, '/');
122 it->entry_count = -1;
123 if (!slash) {
124 int pos;
125 namelen = strlen(path);
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]);
130 /* 0 1 2 3 4 5
131 * ^ ^subtree_nr = 6
132 * pos
133 * move 4 and 5 up one place (2 entries)
134 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
136 memmove(it->down+pos, it->down+pos+1,
137 sizeof(struct cache_tree_sub *) *
138 (it->subtree_nr - pos - 1));
139 it->subtree_nr--;
141 return;
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);
149 static int verify_cache(struct cache_entry **cache,
150 int entries)
152 int i, funny;
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;
163 fprintf(stderr, "%s: unmerged (%s)\n",
164 ce->name, sha1_to_hex(ce->sha1));
167 if (funny)
168 return -1;
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.
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.
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;
190 fprintf(stderr, "You have both %s and %s\n",
191 this_name, next_name);
194 if (funny)
195 return -1;
196 return 0;
199 static void discard_unused_subtrees(struct cache_tree *it)
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 {
209 cache_tree_free(&s->cache_tree);
210 free(s);
211 it->subtree_nr--;
216 int cache_tree_fully_valid(struct cache_tree *it)
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;
227 return 1;
230 static int update_one(struct cache_tree *it,
231 struct cache_entry **cache,
232 int entries,
233 const char *base,
234 int baselen,
235 int missing_ok,
236 int dryrun)
238 unsigned long size, offset;
239 char *buffer;
240 int i;
242 if (0 <= it->entry_count && has_sha1_file(it->sha1))
243 return it->entry_count;
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.
250 for (i = 0; i < it->subtree_nr; i++)
251 it->down[i]->used = 0;
254 * Find the subtrees and update them.
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;
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 */
267 slash = strchr(path + baselen, '/');
268 if (!slash)
269 continue;
271 * a/bbb/c (base = a/, slash = /c)
272 * ==>
273 * path+baselen = bbb/c, sublen = 3
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,
283 missing_ok,
284 dryrun);
285 if (subcnt < 0)
286 return subcnt;
287 i += subcnt - 1;
288 sub->used = 1;
291 discard_unused_subtrees(it);
294 * Then write out the tree object for this level.
296 size = 8192;
297 buffer = xmalloc(size);
298 offset = 0;
300 for (i = 0; i < entries; i++) {
301 struct cache_entry *ce = cache[i];
302 struct cache_tree_sub *sub;
303 const char *path, *slash;
304 int pathlen, entlen;
305 const unsigned char *sha1;
306 unsigned mode;
308 path = ce->name;
309 pathlen = ce_namelen(ce);
310 if (pathlen <= baselen || memcmp(base, path, baselen))
311 break; /* at the end of this level */
313 slash = strchr(path + baselen, '/');
314 if (slash) {
315 entlen = slash - (path + baselen);
316 sub = find_subtree(it, path + baselen, entlen, 0);
317 if (!sub)
318 die("cache-tree.c: '%.*s' in '%s' not found",
319 entlen, path + baselen, path);
320 i += sub->cache_tree->entry_count - 1;
321 sha1 = sub->cache_tree->sha1;
322 mode = S_IFDIR;
324 else {
325 sha1 = ce->sha1;
326 mode = ntohl(ce->ce_mode);
327 entlen = pathlen - baselen;
329 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))
330 return error("invalid object %s", sha1_to_hex(sha1));
332 if (!ce->ce_mode)
333 continue; /* entry being removed */
335 if (size < offset + entlen + 100) {
336 size = alloc_nr(offset + entlen + 100);
337 buffer = xrealloc(buffer, size);
339 offset += sprintf(buffer + offset,
340 "%o %.*s", mode, entlen, path + baselen);
341 buffer[offset++] = 0;
342 hashcpy((unsigned char*)buffer + offset, sha1);
343 offset += 20;
345 #if DEBUG
346 fprintf(stderr, "cache-tree update-one %o %.*s\n",
347 mode, entlen, path + baselen);
348 #endif
351 if (dryrun)
352 hash_sha1_file(buffer, offset, tree_type, it->sha1);
353 else
354 write_sha1_file(buffer, offset, tree_type, it->sha1);
355 free(buffer);
356 it->entry_count = i;
357 #if DEBUG
358 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
359 it->entry_count, it->subtree_nr,
360 sha1_to_hex(it->sha1));
361 #endif
362 return i;
365 int cache_tree_update(struct cache_tree *it,
366 struct cache_entry **cache,
367 int entries,
368 int missing_ok,
369 int dryrun)
371 int i;
372 i = verify_cache(cache, entries);
373 if (i)
374 return i;
375 i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
376 if (i < 0)
377 return i;
378 return 0;
381 static void *write_one(struct cache_tree *it,
382 char *path,
383 int pathlen,
384 char *buffer,
385 unsigned long *size,
386 unsigned long *offset)
388 int i;
390 /* One "cache-tree" entry consists of the following:
391 * path (NUL terminated)
392 * entry_count, subtree_nr ("%d %d\n")
393 * tree-sha1 (missing if invalid)
394 * subtree_nr "cache-tree" entries for subtrees.
396 if (*size < *offset + pathlen + 100) {
397 *size = alloc_nr(*offset + pathlen + 100);
398 buffer = xrealloc(buffer, *size);
400 *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
401 pathlen, path, 0,
402 it->entry_count, it->subtree_nr);
404 #if DEBUG
405 if (0 <= it->entry_count)
406 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
407 pathlen, path, it->entry_count, it->subtree_nr,
408 sha1_to_hex(it->sha1));
409 else
410 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
411 pathlen, path, it->subtree_nr);
412 #endif
414 if (0 <= it->entry_count) {
415 hashcpy((unsigned char*)buffer + *offset, it->sha1);
416 *offset += 20;
418 for (i = 0; i < it->subtree_nr; i++) {
419 struct cache_tree_sub *down = it->down[i];
420 if (i) {
421 struct cache_tree_sub *prev = it->down[i-1];
422 if (subtree_name_cmp(down->name, down->namelen,
423 prev->name, prev->namelen) <= 0)
424 die("fatal - unsorted cache subtree");
426 buffer = write_one(down->cache_tree, down->name, down->namelen,
427 buffer, size, offset);
429 return buffer;
432 void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
434 char path[PATH_MAX];
435 unsigned long size = 8192;
436 char *buffer = xmalloc(size);
438 *size_p = 0;
439 path[0] = 0;
440 return write_one(root, path, 0, buffer, &size, size_p);
443 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
445 const char *buf = *buffer;
446 unsigned long size = *size_p;
447 const char *cp;
448 char *ep;
449 struct cache_tree *it;
450 int i, subtree_nr;
452 it = NULL;
453 /* skip name, but make sure name exists */
454 while (size && *buf) {
455 size--;
456 buf++;
458 if (!size)
459 goto free_return;
460 buf++; size--;
461 it = cache_tree();
463 cp = buf;
464 it->entry_count = strtol(cp, &ep, 10);
465 if (cp == ep)
466 goto free_return;
467 cp = ep;
468 subtree_nr = strtol(cp, &ep, 10);
469 if (cp == ep)
470 goto free_return;
471 while (size && *buf && *buf != '\n') {
472 size--;
473 buf++;
475 if (!size)
476 goto free_return;
477 buf++; size--;
478 if (0 <= it->entry_count) {
479 if (size < 20)
480 goto free_return;
481 hashcpy(it->sha1, (const unsigned char*)buf);
482 buf += 20;
483 size -= 20;
486 #if DEBUG
487 if (0 <= it->entry_count)
488 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
489 *buffer, it->entry_count, subtree_nr,
490 sha1_to_hex(it->sha1));
491 else
492 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
493 *buffer, subtree_nr);
494 #endif
497 * Just a heuristic -- we do not add directories that often but
498 * we do not want to have to extend it immediately when we do,
499 * hence +2.
501 it->subtree_alloc = subtree_nr + 2;
502 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
503 for (i = 0; i < subtree_nr; i++) {
504 /* read each subtree */
505 struct cache_tree *sub;
506 struct cache_tree_sub *subtree;
507 const char *name = buf;
509 sub = read_one(&buf, &size);
510 if (!sub)
511 goto free_return;
512 subtree = cache_tree_sub(it, name);
513 subtree->cache_tree = sub;
515 if (subtree_nr != it->subtree_nr)
516 die("cache-tree: internal error");
517 *buffer = buf;
518 *size_p = size;
519 return it;
521 free_return:
522 cache_tree_free(&it);
523 return NULL;
526 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
528 if (buffer[0])
529 return NULL; /* not the whole tree */
530 return read_one(&buffer, &size);
533 struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
535 while (*path) {
536 const char *slash;
537 struct cache_tree_sub *sub;
539 slash = strchr(path, '/');
540 if (!slash)
541 slash = path + strlen(path);
542 /* between path and slash is the name of the
543 * subtree to look for.
545 sub = find_subtree(it, path, slash - path, 0);
546 if (!sub)
547 return NULL;
548 it = sub->cache_tree;
549 if (slash)
550 while (*slash && *slash == '/')
551 slash++;
552 if (!slash || !*slash)
553 return it; /* prefix ended with slashes */
554 path = slash;
556 return it;