cache-tree: replace "for" loops in update_one with "while" loops
[git.git] / cache-tree.c
1 #include "cache.h"
2 #include "tree.h"
3 #include "tree-walk.h"
4 #include "cache-tree.h"
5
6 #ifndef DEBUG
7 #define DEBUG 0
8 #endif
9
10 struct cache_tree *cache_tree(void)
11 {
12         struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
13         it->entry_count = -1;
14         return it;
15 }
16
17 void cache_tree_free(struct cache_tree **it_p)
18 {
19         int i;
20         struct cache_tree *it = *it_p;
21
22         if (!it)
23                 return;
24         for (i = 0; i < it->subtree_nr; i++)
25                 if (it->down[i]) {
26                         cache_tree_free(&it->down[i]->cache_tree);
27                         free(it->down[i]);
28                 }
29         free(it->down);
30         free(it);
31         *it_p = NULL;
32 }
33
34 static int subtree_name_cmp(const char *one, int onelen,
35                             const char *two, int twolen)
36 {
37         if (onelen < twolen)
38                 return -1;
39         if (twolen < onelen)
40                 return 1;
41         return memcmp(one, two, onelen);
42 }
43
44 static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
45 {
46         struct cache_tree_sub **down = it->down;
47         int lo, hi;
48         lo = 0;
49         hi = it->subtree_nr;
50         while (lo < hi) {
51                 int mi = (lo + hi) / 2;
52                 struct cache_tree_sub *mdl = down[mi];
53                 int cmp = subtree_name_cmp(path, pathlen,
54                                            mdl->name, mdl->namelen);
55                 if (!cmp)
56                         return mi;
57                 if (cmp < 0)
58                         hi = mi;
59                 else
60                         lo = mi + 1;
61         }
62         return -lo-1;
63 }
64
65 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
66                                            const char *path,
67                                            int pathlen,
68                                            int create)
69 {
70         struct cache_tree_sub *down;
71         int pos = subtree_pos(it, path, pathlen);
72         if (0 <= pos)
73                 return it->down[pos];
74         if (!create)
75                 return NULL;
76
77         pos = -pos-1;
78         if (it->subtree_alloc <= it->subtree_nr) {
79                 it->subtree_alloc = alloc_nr(it->subtree_alloc);
80                 it->down = xrealloc(it->down, it->subtree_alloc *
81                                     sizeof(*it->down));
82         }
83         it->subtree_nr++;
84
85         down = xmalloc(sizeof(*down) + pathlen + 1);
86         down->cache_tree = NULL;
87         down->namelen = pathlen;
88         memcpy(down->name, path, pathlen);
89         down->name[pathlen] = 0;
90
91         if (pos < it->subtree_nr)
92                 memmove(it->down + pos + 1,
93                         it->down + pos,
94                         sizeof(down) * (it->subtree_nr - pos - 1));
95         it->down[pos] = down;
96         return down;
97 }
98
99 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
100 {
101         int pathlen = strlen(path);
102         return find_subtree(it, path, pathlen, 1);
103 }
104
105 void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
106 {
107         /* a/b/c
108          * ==> invalidate self
109          * ==> find "a", have it invalidate "b/c"
110          * a
111          * ==> invalidate self
112          * ==> if "a" exists as a subtree, remove it.
113          */
114         const char *slash;
115         int namelen;
116         struct cache_tree_sub *down;
117
118 #if DEBUG
119         fprintf(stderr, "cache-tree invalidate <%s>\n", path);
120 #endif
121
122         if (!it)
123                 return;
124         slash = strchr(path, '/');
125         it->entry_count = -1;
126         if (!slash) {
127                 int pos;
128                 namelen = strlen(path);
129                 pos = subtree_pos(it, path, namelen);
130                 if (0 <= pos) {
131                         cache_tree_free(&it->down[pos]->cache_tree);
132                         free(it->down[pos]);
133                         /* 0 1 2 3 4 5
134                          *       ^     ^subtree_nr = 6
135                          *       pos
136                          * move 4 and 5 up one place (2 entries)
137                          * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
138                          */
139                         memmove(it->down+pos, it->down+pos+1,
140                                 sizeof(struct cache_tree_sub *) *
141                                 (it->subtree_nr - pos - 1));
142                         it->subtree_nr--;
143                 }
144                 return;
145         }
146         namelen = slash - path;
147         down = find_subtree(it, path, namelen, 0);
148         if (down)
149                 cache_tree_invalidate_path(down->cache_tree, slash + 1);
150 }
151
152 static int verify_cache(struct cache_entry **cache,
153                         int entries, int flags)
154 {
155         int i, funny;
156         int silent = flags & WRITE_TREE_SILENT;
157
158         /* Verify that the tree is merged */
159         funny = 0;
160         for (i = 0; i < entries; i++) {
161                 struct cache_entry *ce = cache[i];
162                 if (ce_stage(ce)) {
163                         if (silent)
164                                 return -1;
165                         if (10 < ++funny) {
166                                 fprintf(stderr, "...\n");
167                                 break;
168                         }
169                         fprintf(stderr, "%s: unmerged (%s)\n",
170                                 ce->name, sha1_to_hex(ce->sha1));
171                 }
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
205 static 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 {
215                         cache_tree_free(&s->cache_tree);
216                         free(s);
217                         it->subtree_nr--;
218                 }
219         }
220 }
221
222 int cache_tree_fully_valid(struct cache_tree *it)
223 {
224         int i;
225         if (!it)
226                 return 0;
227         if (it->entry_count < 0 || !has_sha1_file(it->sha1))
228                 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
236 static int update_one(struct cache_tree *it,
237                       struct cache_entry **cache,
238                       int entries,
239                       const char *base,
240                       int baselen,
241                       int flags)
242 {
243         struct strbuf buffer;
244         int missing_ok = flags & WRITE_TREE_MISSING_OK;
245         int dryrun = flags & WRITE_TREE_DRY_RUN;
246         int i;
247
248         if (0 <= it->entry_count && has_sha1_file(it->sha1))
249                 return it->entry_count;
250
251         /*
252          * We first scan for subtrees and update them; we start by
253          * marking existing subtrees -- the ones that are unmarked
254          * should not be in the result.
255          */
256         for (i = 0; i < it->subtree_nr; i++)
257                 it->down[i]->used = 0;
258
259         /*
260          * Find the subtrees and update them.
261          */
262         i = 0;
263         while (i < entries) {
264                 struct cache_entry *ce = cache[i];
265                 struct cache_tree_sub *sub;
266                 const char *path, *slash;
267                 int pathlen, sublen, subcnt;
268
269                 path = ce->name;
270                 pathlen = ce_namelen(ce);
271                 if (pathlen <= baselen || memcmp(base, path, baselen))
272                         break; /* at the end of this level */
273
274                 slash = strchr(path + baselen, '/');
275                 if (!slash) {
276                         i++;
277                         continue;
278                 }
279                 /*
280                  * a/bbb/c (base = a/, slash = /c)
281                  * ==>
282                  * path+baselen = bbb/c, sublen = 3
283                  */
284                 sublen = slash - (path + baselen);
285                 sub = find_subtree(it, path + baselen, sublen, 1);
286                 if (!sub->cache_tree)
287                         sub->cache_tree = cache_tree();
288                 subcnt = update_one(sub->cache_tree,
289                                     cache + i, entries - i,
290                                     path,
291                                     baselen + sublen + 1,
292                                     flags);
293                 if (subcnt < 0)
294                         return subcnt;
295                 i += subcnt;
296                 sub->used = 1;
297         }
298
299         discard_unused_subtrees(it);
300
301         /*
302          * Then write out the tree object for this level.
303          */
304         strbuf_init(&buffer, 8192);
305
306         i = 0;
307         while (i < entries) {
308                 struct cache_entry *ce = cache[i];
309                 struct cache_tree_sub *sub;
310                 const char *path, *slash;
311                 int pathlen, entlen;
312                 const unsigned char *sha1;
313                 unsigned mode;
314
315                 path = ce->name;
316                 pathlen = ce_namelen(ce);
317                 if (pathlen <= baselen || memcmp(base, path, baselen))
318                         break; /* at the end of this level */
319
320                 slash = strchr(path + baselen, '/');
321                 if (slash) {
322                         entlen = slash - (path + baselen);
323                         sub = find_subtree(it, path + baselen, entlen, 0);
324                         if (!sub)
325                                 die("cache-tree.c: '%.*s' in '%s' not found",
326                                     entlen, path + baselen, path);
327                         i += sub->cache_tree->entry_count;
328                         sha1 = sub->cache_tree->sha1;
329                         mode = S_IFDIR;
330                 }
331                 else {
332                         sha1 = ce->sha1;
333                         mode = ce->ce_mode;
334                         entlen = pathlen - baselen;
335                         i++;
336                 }
337                 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
338                         strbuf_release(&buffer);
339                         return error("invalid object %06o %s for '%.*s'",
340                                 mode, sha1_to_hex(sha1), entlen+baselen, path);
341                 }
342
343                 if (ce->ce_flags & (CE_REMOVE | CE_INTENT_TO_ADD))
344                         continue; /* entry being removed or placeholder */
345
346                 strbuf_grow(&buffer, entlen + 100);
347                 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
348                 strbuf_add(&buffer, sha1, 20);
349
350 #if DEBUG
351                 fprintf(stderr, "cache-tree update-one %o %.*s\n",
352                         mode, entlen, path + baselen);
353 #endif
354         }
355
356         if (dryrun)
357                 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
358         else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
359                 strbuf_release(&buffer);
360                 return -1;
361         }
362
363         strbuf_release(&buffer);
364         it->entry_count = i;
365 #if DEBUG
366         fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
367                 it->entry_count, it->subtree_nr,
368                 sha1_to_hex(it->sha1));
369 #endif
370         return i;
371 }
372
373 int cache_tree_update(struct cache_tree *it,
374                       struct cache_entry **cache,
375                       int entries,
376                       int flags)
377 {
378         int i;
379         i = verify_cache(cache, entries, flags);
380         if (i)
381                 return i;
382         i = update_one(it, cache, entries, "", 0, flags);
383         if (i < 0)
384                 return i;
385         return 0;
386 }
387
388 static void write_one(struct strbuf *buffer, struct cache_tree *it,
389                       const char *path, int pathlen)
390 {
391         int i;
392
393         /* One "cache-tree" entry consists of the following:
394          * path (NUL terminated)
395          * entry_count, subtree_nr ("%d %d\n")
396          * tree-sha1 (missing if invalid)
397          * subtree_nr "cache-tree" entries for subtrees.
398          */
399         strbuf_grow(buffer, pathlen + 100);
400         strbuf_add(buffer, path, pathlen);
401         strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
402
403 #if DEBUG
404         if (0 <= it->entry_count)
405                 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
406                         pathlen, path, it->entry_count, it->subtree_nr,
407                         sha1_to_hex(it->sha1));
408         else
409                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
410                         pathlen, path, it->subtree_nr);
411 #endif
412
413         if (0 <= it->entry_count) {
414                 strbuf_add(buffer, it->sha1, 20);
415         }
416         for (i = 0; i < it->subtree_nr; i++) {
417                 struct cache_tree_sub *down = it->down[i];
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                 }
424                 write_one(buffer, down->cache_tree, down->name, down->namelen);
425         }
426 }
427
428 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
429 {
430         write_one(sb, root, "", 0);
431 }
432
433 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
434 {
435         const char *buf = *buffer;
436         unsigned long size = *size_p;
437         const char *cp;
438         char *ep;
439         struct cache_tree *it;
440         int i, subtree_nr;
441
442         it = NULL;
443         /* skip name, but make sure name exists */
444         while (size && *buf) {
445                 size--;
446                 buf++;
447         }
448         if (!size)
449                 goto free_return;
450         buf++; size--;
451         it = cache_tree();
452
453         cp = buf;
454         it->entry_count = strtol(cp, &ep, 10);
455         if (cp == ep)
456                 goto free_return;
457         cp = ep;
458         subtree_nr = strtol(cp, &ep, 10);
459         if (cp == ep)
460                 goto free_return;
461         while (size && *buf && *buf != '\n') {
462                 size--;
463                 buf++;
464         }
465         if (!size)
466                 goto free_return;
467         buf++; size--;
468         if (0 <= it->entry_count) {
469                 if (size < 20)
470                         goto free_return;
471                 hashcpy(it->sha1, (const unsigned char*)buf);
472                 buf += 20;
473                 size -= 20;
474         }
475
476 #if DEBUG
477         if (0 <= it->entry_count)
478                 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
479                         *buffer, it->entry_count, subtree_nr,
480                         sha1_to_hex(it->sha1));
481         else
482                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
483                         *buffer, subtree_nr);
484 #endif
485
486         /*
487          * Just a heuristic -- we do not add directories that often but
488          * we do not want to have to extend it immediately when we do,
489          * hence +2.
490          */
491         it->subtree_alloc = subtree_nr + 2;
492         it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
493         for (i = 0; i < subtree_nr; i++) {
494                 /* read each subtree */
495                 struct cache_tree *sub;
496                 struct cache_tree_sub *subtree;
497                 const char *name = buf;
498
499                 sub = read_one(&buf, &size);
500                 if (!sub)
501                         goto free_return;
502                 subtree = cache_tree_sub(it, name);
503                 subtree->cache_tree = sub;
504         }
505         if (subtree_nr != it->subtree_nr)
506                 die("cache-tree: internal error");
507         *buffer = buf;
508         *size_p = size;
509         return it;
510
511  free_return:
512         cache_tree_free(&it);
513         return NULL;
514 }
515
516 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
517 {
518         if (buffer[0])
519                 return NULL; /* not the whole tree */
520         return read_one(&buffer, &size);
521 }
522
523 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
524 {
525         if (!it)
526                 return NULL;
527         while (*path) {
528                 const char *slash;
529                 struct cache_tree_sub *sub;
530
531                 slash = strchr(path, '/');
532                 if (!slash)
533                         slash = path + strlen(path);
534                 /* between path and slash is the name of the
535                  * subtree to look for.
536                  */
537                 sub = find_subtree(it, path, slash - path, 0);
538                 if (!sub)
539                         return NULL;
540                 it = sub->cache_tree;
541                 if (slash)
542                         while (*slash && *slash == '/')
543                                 slash++;
544                 if (!slash || !*slash)
545                         return it; /* prefix ended with slashes */
546                 path = slash;
547         }
548         return it;
549 }
550
551 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
552 {
553         int entries, was_valid, newfd;
554         struct lock_file *lock_file;
555
556         /*
557          * We can't free this memory, it becomes part of a linked list
558          * parsed atexit()
559          */
560         lock_file = xcalloc(1, sizeof(struct lock_file));
561
562         newfd = hold_locked_index(lock_file, 1);
563
564         entries = read_cache();
565         if (entries < 0)
566                 return WRITE_TREE_UNREADABLE_INDEX;
567         if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
568                 cache_tree_free(&(active_cache_tree));
569
570         if (!active_cache_tree)
571                 active_cache_tree = cache_tree();
572
573         was_valid = cache_tree_fully_valid(active_cache_tree);
574         if (!was_valid) {
575                 if (cache_tree_update(active_cache_tree,
576                                       active_cache, active_nr,
577                                       flags) < 0)
578                         return WRITE_TREE_UNMERGED_INDEX;
579                 if (0 <= newfd) {
580                         if (!write_cache(newfd, active_cache, active_nr) &&
581                             !commit_lock_file(lock_file))
582                                 newfd = -1;
583                 }
584                 /* Not being able to write is fine -- we are only interested
585                  * in updating the cache-tree part, and if the next caller
586                  * ends up using the old index with unupdated cache-tree part
587                  * it misses the work we did here, but that is just a
588                  * performance penalty and not a big deal.
589                  */
590         }
591
592         if (prefix) {
593                 struct cache_tree *subtree =
594                         cache_tree_find(active_cache_tree, prefix);
595                 if (!subtree)
596                         return WRITE_TREE_PREFIX_ERROR;
597                 hashcpy(sha1, subtree->sha1);
598         }
599         else
600                 hashcpy(sha1, active_cache_tree->sha1);
601
602         if (0 <= newfd)
603                 rollback_lock_file(lock_file);
604
605         return 0;
606 }
607
608 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
609 {
610         struct tree_desc desc;
611         struct name_entry entry;
612         int cnt;
613
614         hashcpy(it->sha1, tree->object.sha1);
615         init_tree_desc(&desc, tree->buffer, tree->size);
616         cnt = 0;
617         while (tree_entry(&desc, &entry)) {
618                 if (!S_ISDIR(entry.mode))
619                         cnt++;
620                 else {
621                         struct cache_tree_sub *sub;
622                         struct tree *subtree = lookup_tree(entry.sha1);
623                         if (!subtree->object.parsed)
624                                 parse_tree(subtree);
625                         sub = cache_tree_sub(it, entry.path);
626                         sub->cache_tree = cache_tree();
627                         prime_cache_tree_rec(sub->cache_tree, subtree);
628                         cnt += sub->cache_tree->entry_count;
629                 }
630         }
631         it->entry_count = cnt;
632 }
633
634 void prime_cache_tree(struct cache_tree **it, struct tree *tree)
635 {
636         cache_tree_free(it);
637         *it = cache_tree();
638         prime_cache_tree_rec(*it, tree);
639 }
640
641 /*
642  * find the cache_tree that corresponds to the current level without
643  * exploding the full path into textual form.  The root of the
644  * cache tree is given as "root", and our current level is "info".
645  * (1) When at root level, info->prev is NULL, so it is "root" itself.
646  * (2) Otherwise, find the cache_tree that corresponds to one level
647  *     above us, and find ourselves in there.
648  */
649 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
650                                                          struct traverse_info *info)
651 {
652         struct cache_tree *our_parent;
653
654         if (!info->prev)
655                 return root;
656         our_parent = find_cache_tree_from_traversal(root, info->prev);
657         return cache_tree_find(our_parent, info->name.path);
658 }
659
660 int cache_tree_matches_traversal(struct cache_tree *root,
661                                  struct name_entry *ent,
662                                  struct traverse_info *info)
663 {
664         struct cache_tree *it;
665
666         it = find_cache_tree_from_traversal(root, info);
667         it = cache_tree_find(it, ent->path);
668         if (it && it->entry_count > 0 && !hashcmp(ent->sha1, it->sha1))
669                 return it->entry_count;
670         return 0;
671 }
672
673 int update_main_cache_tree(int flags)
674 {
675         if (!the_index.cache_tree)
676                 the_index.cache_tree = cache_tree();
677         return cache_tree_update(the_index.cache_tree,
678                                  the_index.cache, the_index.cache_nr, flags);
679 }