Merge branch 'ah/filter-branch-setup'
[git] / cache-tree.c
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "tree.h"
4 #include "tree-walk.h"
5 #include "cache-tree.h"
6
7 #ifndef DEBUG
8 #define DEBUG 0
9 #endif
10
11 struct 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
18 void cache_tree_free(struct cache_tree **it_p)
19 {
20         int i;
21         struct cache_tree *it = *it_p;
22
23         if (!it)
24                 return;
25         for (i = 0; i < it->subtree_nr; i++)
26                 if (it->down[i]) {
27                         cache_tree_free(&it->down[i]->cache_tree);
28                         free(it->down[i]);
29                 }
30         free(it->down);
31         free(it);
32         *it_p = NULL;
33 }
34
35 static 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
45 static 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
66 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
67                                            const char *path,
68                                            int pathlen,
69                                            int create)
70 {
71         struct cache_tree_sub *down;
72         int pos = subtree_pos(it, path, pathlen);
73         if (0 <= pos)
74                 return it->down[pos];
75         if (!create)
76                 return NULL;
77
78         pos = -pos-1;
79         ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
80         it->subtree_nr++;
81
82         FLEX_ALLOC_MEM(down, name, path, pathlen);
83         down->cache_tree = NULL;
84         down->namelen = pathlen;
85
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;
91         return down;
92 }
93
94 struct 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
100 static int do_invalidate_path(struct cache_tree *it, const char *path)
101 {
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
113 #if DEBUG
114         fprintf(stderr, "cache-tree invalidate <%s>\n", path);
115 #endif
116
117         if (!it)
118                 return 0;
119         slash = strchrnul(path, '/');
120         namelen = slash - path;
121         it->entry_count = -1;
122         if (!*slash) {
123                 int pos;
124                 pos = subtree_pos(it, path, namelen);
125                 if (0 <= pos) {
126                         cache_tree_free(&it->down[pos]->cache_tree);
127                         free(it->down[pos]);
128                         /* 0 1 2 3 4 5
129                          *       ^     ^subtree_nr = 6
130                          *       pos
131                          * move 4 and 5 up one place (2 entries)
132                          * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
133                          */
134                         memmove(it->down+pos, it->down+pos+1,
135                                 sizeof(struct cache_tree_sub *) *
136                                 (it->subtree_nr - pos - 1));
137                         it->subtree_nr--;
138                 }
139                 return 1;
140         }
141         down = find_subtree(it, path, namelen, 0);
142         if (down)
143                 do_invalidate_path(down->cache_tree, slash + 1);
144         return 1;
145 }
146
147 void 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;
151 }
152
153 static int verify_cache(struct cache_entry **cache,
154                         int entries, int flags)
155 {
156         int i, funny;
157         int silent = flags & WRITE_TREE_SILENT;
158
159         /* Verify that the tree is merged */
160         funny = 0;
161         for (i = 0; i < entries; i++) {
162                 const struct cache_entry *ce = cache[i];
163                 if (ce_stage(ce)) {
164                         if (silent)
165                                 return -1;
166                         if (10 < ++funny) {
167                                 fprintf(stderr, "...\n");
168                                 break;
169                         }
170                         fprintf(stderr, "%s: unmerged (%s)\n",
171                                 ce->name, oid_to_hex(&ce->oid));
172                 }
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
206 static 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 {
216                         cache_tree_free(&s->cache_tree);
217                         free(s);
218                         it->subtree_nr--;
219                 }
220         }
221 }
222
223 int 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->oid.hash))
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
237 static int update_one(struct cache_tree *it,
238                       struct cache_entry **cache,
239                       int entries,
240                       const char *base,
241                       int baselen,
242                       int *skip_count,
243                       int flags)
244 {
245         struct strbuf buffer;
246         int missing_ok = flags & WRITE_TREE_MISSING_OK;
247         int dryrun = flags & WRITE_TREE_DRY_RUN;
248         int repair = flags & WRITE_TREE_REPAIR;
249         int to_invalidate = 0;
250         int i;
251
252         assert(!(dryrun && repair));
253
254         *skip_count = 0;
255
256         if (0 <= it->entry_count && has_sha1_file(it->oid.hash))
257                 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          */
270         i = 0;
271         while (i < entries) {
272                 const struct cache_entry *ce = cache[i];
273                 struct cache_tree_sub *sub;
274                 const char *path, *slash;
275                 int pathlen, sublen, subcnt, subskip;
276
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, '/');
283                 if (!slash) {
284                         i++;
285                         continue;
286                 }
287                 /*
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,
300                                     &subskip,
301                                     flags);
302                 if (subcnt < 0)
303                         return subcnt;
304                 if (!subcnt)
305                         die("index cache-tree records empty sub-tree");
306                 i += subcnt;
307                 sub->count = subcnt; /* to be used in the next loop */
308                 *skip_count += subskip;
309                 sub->used = 1;
310         }
311
312         discard_unused_subtrees(it);
313
314         /*
315          * Then write out the tree object for this level.
316          */
317         strbuf_init(&buffer, 8192);
318
319         i = 0;
320         while (i < entries) {
321                 const struct cache_entry *ce = cache[i];
322                 struct cache_tree_sub *sub = NULL;
323                 const char *path, *slash;
324                 int pathlen, entlen;
325                 const unsigned char *sha1;
326                 unsigned mode;
327                 int expected_missing = 0;
328                 int contains_ita = 0;
329
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);
342                         i += sub->count;
343                         sha1 = sub->cache_tree->oid.hash;
344                         mode = S_IFDIR;
345                         contains_ita = sub->cache_tree->entry_count < 0;
346                         if (contains_ita) {
347                                 to_invalidate = 1;
348                                 expected_missing = 1;
349                         }
350                 }
351                 else {
352                         sha1 = ce->oid.hash;
353                         mode = ce->ce_mode;
354                         entlen = pathlen - baselen;
355                         i++;
356                 }
357
358                 if (is_null_sha1(sha1) ||
359                     (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))) {
360                         strbuf_release(&buffer);
361                         if (expected_missing)
362                                 return -1;
363                         return error("invalid object %06o %s for '%.*s'",
364                                 mode, sha1_to_hex(sha1), entlen+baselen, path);
365                 }
366
367                 /*
368                  * CE_REMOVE entries are removed before the index is
369                  * written to disk. Skip them to remain consistent
370                  * with the future on-disk index.
371                  */
372                 if (ce->ce_flags & CE_REMOVE) {
373                         *skip_count = *skip_count + 1;
374                         continue;
375                 }
376
377                 /*
378                  * CE_INTENT_TO_ADD entries exist on on-disk index but
379                  * they are not part of generated trees. Invalidate up
380                  * to root to force cache-tree users to read elsewhere.
381                  */
382                 if (!sub && ce_intent_to_add(ce)) {
383                         to_invalidate = 1;
384                         continue;
385                 }
386
387                 /*
388                  * "sub" can be an empty tree if all subentries are i-t-a.
389                  */
390                 if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
391                         continue;
392
393                 strbuf_grow(&buffer, entlen + 100);
394                 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
395                 strbuf_add(&buffer, sha1, 20);
396
397 #if DEBUG
398                 fprintf(stderr, "cache-tree update-one %o %.*s\n",
399                         mode, entlen, path + baselen);
400 #endif
401         }
402
403         if (repair) {
404                 unsigned char sha1[20];
405                 hash_sha1_file(buffer.buf, buffer.len, tree_type, sha1);
406                 if (has_sha1_file(sha1))
407                         hashcpy(it->oid.hash, sha1);
408                 else
409                         to_invalidate = 1;
410         } else if (dryrun)
411                 hash_sha1_file(buffer.buf, buffer.len, tree_type,
412                                it->oid.hash);
413         else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->oid.hash)) {
414                 strbuf_release(&buffer);
415                 return -1;
416         }
417
418         strbuf_release(&buffer);
419         it->entry_count = to_invalidate ? -1 : i - *skip_count;
420 #if DEBUG
421         fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
422                 it->entry_count, it->subtree_nr,
423                 oid_to_hex(&it->oid));
424 #endif
425         return i;
426 }
427
428 int cache_tree_update(struct index_state *istate, int flags)
429 {
430         struct cache_tree *it = istate->cache_tree;
431         struct cache_entry **cache = istate->cache;
432         int entries = istate->cache_nr;
433         int skip, i = verify_cache(cache, entries, flags);
434
435         if (i)
436                 return i;
437         i = update_one(it, cache, entries, "", 0, &skip, flags);
438         if (i < 0)
439                 return i;
440         istate->cache_changed |= CACHE_TREE_CHANGED;
441         return 0;
442 }
443
444 static void write_one(struct strbuf *buffer, struct cache_tree *it,
445                       const char *path, int pathlen)
446 {
447         int i;
448
449         /* One "cache-tree" entry consists of the following:
450          * path (NUL terminated)
451          * entry_count, subtree_nr ("%d %d\n")
452          * tree-sha1 (missing if invalid)
453          * subtree_nr "cache-tree" entries for subtrees.
454          */
455         strbuf_grow(buffer, pathlen + 100);
456         strbuf_add(buffer, path, pathlen);
457         strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
458
459 #if DEBUG
460         if (0 <= it->entry_count)
461                 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
462                         pathlen, path, it->entry_count, it->subtree_nr,
463                         oid_to_hex(&it->oid));
464         else
465                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
466                         pathlen, path, it->subtree_nr);
467 #endif
468
469         if (0 <= it->entry_count) {
470                 strbuf_add(buffer, it->oid.hash, 20);
471         }
472         for (i = 0; i < it->subtree_nr; i++) {
473                 struct cache_tree_sub *down = it->down[i];
474                 if (i) {
475                         struct cache_tree_sub *prev = it->down[i-1];
476                         if (subtree_name_cmp(down->name, down->namelen,
477                                              prev->name, prev->namelen) <= 0)
478                                 die("fatal - unsorted cache subtree");
479                 }
480                 write_one(buffer, down->cache_tree, down->name, down->namelen);
481         }
482 }
483
484 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
485 {
486         write_one(sb, root, "", 0);
487 }
488
489 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
490 {
491         const char *buf = *buffer;
492         unsigned long size = *size_p;
493         const char *cp;
494         char *ep;
495         struct cache_tree *it;
496         int i, subtree_nr;
497
498         it = NULL;
499         /* skip name, but make sure name exists */
500         while (size && *buf) {
501                 size--;
502                 buf++;
503         }
504         if (!size)
505                 goto free_return;
506         buf++; size--;
507         it = cache_tree();
508
509         cp = buf;
510         it->entry_count = strtol(cp, &ep, 10);
511         if (cp == ep)
512                 goto free_return;
513         cp = ep;
514         subtree_nr = strtol(cp, &ep, 10);
515         if (cp == ep)
516                 goto free_return;
517         while (size && *buf && *buf != '\n') {
518                 size--;
519                 buf++;
520         }
521         if (!size)
522                 goto free_return;
523         buf++; size--;
524         if (0 <= it->entry_count) {
525                 if (size < 20)
526                         goto free_return;
527                 hashcpy(it->oid.hash, (const unsigned char*)buf);
528                 buf += 20;
529                 size -= 20;
530         }
531
532 #if DEBUG
533         if (0 <= it->entry_count)
534                 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
535                         *buffer, it->entry_count, subtree_nr,
536                         oid_to_hex(&it->oid));
537         else
538                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
539                         *buffer, subtree_nr);
540 #endif
541
542         /*
543          * Just a heuristic -- we do not add directories that often but
544          * we do not want to have to extend it immediately when we do,
545          * hence +2.
546          */
547         it->subtree_alloc = subtree_nr + 2;
548         it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
549         for (i = 0; i < subtree_nr; i++) {
550                 /* read each subtree */
551                 struct cache_tree *sub;
552                 struct cache_tree_sub *subtree;
553                 const char *name = buf;
554
555                 sub = read_one(&buf, &size);
556                 if (!sub)
557                         goto free_return;
558                 subtree = cache_tree_sub(it, name);
559                 subtree->cache_tree = sub;
560         }
561         if (subtree_nr != it->subtree_nr)
562                 die("cache-tree: internal error");
563         *buffer = buf;
564         *size_p = size;
565         return it;
566
567  free_return:
568         cache_tree_free(&it);
569         return NULL;
570 }
571
572 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
573 {
574         if (buffer[0])
575                 return NULL; /* not the whole tree */
576         return read_one(&buffer, &size);
577 }
578
579 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
580 {
581         if (!it)
582                 return NULL;
583         while (*path) {
584                 const char *slash;
585                 struct cache_tree_sub *sub;
586
587                 slash = strchrnul(path, '/');
588                 /*
589                  * Between path and slash is the name of the subtree
590                  * to look for.
591                  */
592                 sub = find_subtree(it, path, slash - path, 0);
593                 if (!sub)
594                         return NULL;
595                 it = sub->cache_tree;
596
597                 path = slash;
598                 while (*path == '/')
599                         path++;
600         }
601         return it;
602 }
603
604 int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
605 {
606         int entries, was_valid, newfd;
607         struct lock_file *lock_file;
608
609         /*
610          * We can't free this memory, it becomes part of a linked list
611          * parsed atexit()
612          */
613         lock_file = xcalloc(1, sizeof(struct lock_file));
614
615         newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);
616
617         entries = read_index_from(index_state, index_path);
618         if (entries < 0)
619                 return WRITE_TREE_UNREADABLE_INDEX;
620         if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
621                 cache_tree_free(&index_state->cache_tree);
622
623         if (!index_state->cache_tree)
624                 index_state->cache_tree = cache_tree();
625
626         was_valid = cache_tree_fully_valid(index_state->cache_tree);
627         if (!was_valid) {
628                 if (cache_tree_update(index_state, flags) < 0)
629                         return WRITE_TREE_UNMERGED_INDEX;
630                 if (0 <= newfd) {
631                         if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
632                                 newfd = -1;
633                 }
634                 /* Not being able to write is fine -- we are only interested
635                  * in updating the cache-tree part, and if the next caller
636                  * ends up using the old index with unupdated cache-tree part
637                  * it misses the work we did here, but that is just a
638                  * performance penalty and not a big deal.
639                  */
640         }
641
642         if (prefix) {
643                 struct cache_tree *subtree;
644                 subtree = cache_tree_find(index_state->cache_tree, prefix);
645                 if (!subtree)
646                         return WRITE_TREE_PREFIX_ERROR;
647                 hashcpy(sha1, subtree->oid.hash);
648         }
649         else
650                 hashcpy(sha1, index_state->cache_tree->oid.hash);
651
652         if (0 <= newfd)
653                 rollback_lock_file(lock_file);
654
655         return 0;
656 }
657
658 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
659 {
660         return write_index_as_tree(sha1, &the_index, get_index_file(), flags, prefix);
661 }
662
663 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
664 {
665         struct tree_desc desc;
666         struct name_entry entry;
667         int cnt;
668
669         oidcpy(&it->oid, &tree->object.oid);
670         init_tree_desc(&desc, tree->buffer, tree->size);
671         cnt = 0;
672         while (tree_entry(&desc, &entry)) {
673                 if (!S_ISDIR(entry.mode))
674                         cnt++;
675                 else {
676                         struct cache_tree_sub *sub;
677                         struct tree *subtree = lookup_tree(entry.oid);
678                         if (!subtree->object.parsed)
679                                 parse_tree(subtree);
680                         sub = cache_tree_sub(it, entry.path);
681                         sub->cache_tree = cache_tree();
682                         prime_cache_tree_rec(sub->cache_tree, subtree);
683                         cnt += sub->cache_tree->entry_count;
684                 }
685         }
686         it->entry_count = cnt;
687 }
688
689 void prime_cache_tree(struct index_state *istate, struct tree *tree)
690 {
691         cache_tree_free(&istate->cache_tree);
692         istate->cache_tree = cache_tree();
693         prime_cache_tree_rec(istate->cache_tree, tree);
694         istate->cache_changed |= CACHE_TREE_CHANGED;
695 }
696
697 /*
698  * find the cache_tree that corresponds to the current level without
699  * exploding the full path into textual form.  The root of the
700  * cache tree is given as "root", and our current level is "info".
701  * (1) When at root level, info->prev is NULL, so it is "root" itself.
702  * (2) Otherwise, find the cache_tree that corresponds to one level
703  *     above us, and find ourselves in there.
704  */
705 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
706                                                          struct traverse_info *info)
707 {
708         struct cache_tree *our_parent;
709
710         if (!info->prev)
711                 return root;
712         our_parent = find_cache_tree_from_traversal(root, info->prev);
713         return cache_tree_find(our_parent, info->name.path);
714 }
715
716 int cache_tree_matches_traversal(struct cache_tree *root,
717                                  struct name_entry *ent,
718                                  struct traverse_info *info)
719 {
720         struct cache_tree *it;
721
722         it = find_cache_tree_from_traversal(root, info);
723         it = cache_tree_find(it, ent->path);
724         if (it && it->entry_count > 0 && !oidcmp(ent->oid, &it->oid))
725                 return it->entry_count;
726         return 0;
727 }
728
729 int update_main_cache_tree(int flags)
730 {
731         if (!the_index.cache_tree)
732                 the_index.cache_tree = cache_tree();
733         return cache_tree_update(&the_index, flags);
734 }