t7006: use test_config helpers
[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)
154 {
155         int i, funny;
156
157         /* Verify that the tree is merged */
158         funny = 0;
159         for (i = 0; i < entries; i++) {
160                 struct cache_entry *ce = cache[i];
161                 if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
162                         if (10 < ++funny) {
163                                 fprintf(stderr, "...\n");
164                                 break;
165                         }
166                         if (ce_stage(ce))
167                                 fprintf(stderr, "%s: unmerged (%s)\n",
168                                         ce->name, sha1_to_hex(ce->sha1));
169                         else
170                                 fprintf(stderr, "%s: not added yet\n",
171                                         ce->name);
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->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
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 missing_ok,
243                       int dryrun)
244 {
245         struct strbuf buffer;
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         for (i = 0; i < entries; i++) {
263                 struct cache_entry *ce = cache[i];
264                 struct cache_tree_sub *sub;
265                 const char *path, *slash;
266                 int pathlen, sublen, subcnt;
267
268                 path = ce->name;
269                 pathlen = ce_namelen(ce);
270                 if (pathlen <= baselen || memcmp(base, path, baselen))
271                         break; /* at the end of this level */
272
273                 slash = strchr(path + baselen, '/');
274                 if (!slash)
275                         continue;
276                 /*
277                  * a/bbb/c (base = a/, slash = /c)
278                  * ==>
279                  * path+baselen = bbb/c, sublen = 3
280                  */
281                 sublen = slash - (path + baselen);
282                 sub = find_subtree(it, path + baselen, sublen, 1);
283                 if (!sub->cache_tree)
284                         sub->cache_tree = cache_tree();
285                 subcnt = update_one(sub->cache_tree,
286                                     cache + i, entries - i,
287                                     path,
288                                     baselen + sublen + 1,
289                                     missing_ok,
290                                     dryrun);
291                 if (subcnt < 0)
292                         return subcnt;
293                 i += subcnt - 1;
294                 sub->used = 1;
295         }
296
297         discard_unused_subtrees(it);
298
299         /*
300          * Then write out the tree object for this level.
301          */
302         strbuf_init(&buffer, 8192);
303
304         for (i = 0; i < entries; i++) {
305                 struct cache_entry *ce = cache[i];
306                 struct cache_tree_sub *sub;
307                 const char *path, *slash;
308                 int pathlen, entlen;
309                 const unsigned char *sha1;
310                 unsigned mode;
311
312                 path = ce->name;
313                 pathlen = ce_namelen(ce);
314                 if (pathlen <= baselen || memcmp(base, path, baselen))
315                         break; /* at the end of this level */
316
317                 slash = strchr(path + baselen, '/');
318                 if (slash) {
319                         entlen = slash - (path + baselen);
320                         sub = find_subtree(it, path + baselen, entlen, 0);
321                         if (!sub)
322                                 die("cache-tree.c: '%.*s' in '%s' not found",
323                                     entlen, path + baselen, path);
324                         i += sub->cache_tree->entry_count - 1;
325                         sha1 = sub->cache_tree->sha1;
326                         mode = S_IFDIR;
327                 }
328                 else {
329                         sha1 = ce->sha1;
330                         mode = ce->ce_mode;
331                         entlen = pathlen - baselen;
332                 }
333                 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
334                         strbuf_release(&buffer);
335                         return error("invalid object %06o %s for '%.*s'",
336                                 mode, sha1_to_hex(sha1), entlen+baselen, path);
337                 }
338
339                 if (ce->ce_flags & CE_REMOVE)
340                         continue; /* entry being removed */
341
342                 strbuf_grow(&buffer, entlen + 100);
343                 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
344                 strbuf_add(&buffer, sha1, 20);
345
346 #if DEBUG
347                 fprintf(stderr, "cache-tree update-one %o %.*s\n",
348                         mode, entlen, path + baselen);
349 #endif
350         }
351
352         if (dryrun)
353                 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
354         else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
355                 strbuf_release(&buffer);
356                 return -1;
357         }
358
359         strbuf_release(&buffer);
360         it->entry_count = i;
361 #if DEBUG
362         fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
363                 it->entry_count, it->subtree_nr,
364                 sha1_to_hex(it->sha1));
365 #endif
366         return i;
367 }
368
369 int cache_tree_update(struct cache_tree *it,
370                       struct cache_entry **cache,
371                       int entries,
372                       int missing_ok,
373                       int dryrun)
374 {
375         int i;
376         i = verify_cache(cache, entries);
377         if (i)
378                 return i;
379         i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
380         if (i < 0)
381                 return i;
382         return 0;
383 }
384
385 static void write_one(struct strbuf *buffer, struct cache_tree *it,
386                       const char *path, int pathlen)
387 {
388         int i;
389
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.
395          */
396         strbuf_grow(buffer, pathlen + 100);
397         strbuf_add(buffer, path, pathlen);
398         strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
399
400 #if DEBUG
401         if (0 <= it->entry_count)
402                 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
403                         pathlen, path, it->entry_count, it->subtree_nr,
404                         sha1_to_hex(it->sha1));
405         else
406                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
407                         pathlen, path, it->subtree_nr);
408 #endif
409
410         if (0 <= it->entry_count) {
411                 strbuf_add(buffer, it->sha1, 20);
412         }
413         for (i = 0; i < it->subtree_nr; i++) {
414                 struct cache_tree_sub *down = it->down[i];
415                 if (i) {
416                         struct cache_tree_sub *prev = it->down[i-1];
417                         if (subtree_name_cmp(down->name, down->namelen,
418                                              prev->name, prev->namelen) <= 0)
419                                 die("fatal - unsorted cache subtree");
420                 }
421                 write_one(buffer, down->cache_tree, down->name, down->namelen);
422         }
423 }
424
425 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
426 {
427         write_one(sb, root, "", 0);
428 }
429
430 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
431 {
432         const char *buf = *buffer;
433         unsigned long size = *size_p;
434         const char *cp;
435         char *ep;
436         struct cache_tree *it;
437         int i, subtree_nr;
438
439         it = NULL;
440         /* skip name, but make sure name exists */
441         while (size && *buf) {
442                 size--;
443                 buf++;
444         }
445         if (!size)
446                 goto free_return;
447         buf++; size--;
448         it = cache_tree();
449
450         cp = buf;
451         it->entry_count = strtol(cp, &ep, 10);
452         if (cp == ep)
453                 goto free_return;
454         cp = ep;
455         subtree_nr = strtol(cp, &ep, 10);
456         if (cp == ep)
457                 goto free_return;
458         while (size && *buf && *buf != '\n') {
459                 size--;
460                 buf++;
461         }
462         if (!size)
463                 goto free_return;
464         buf++; size--;
465         if (0 <= it->entry_count) {
466                 if (size < 20)
467                         goto free_return;
468                 hashcpy(it->sha1, (const unsigned char*)buf);
469                 buf += 20;
470                 size -= 20;
471         }
472
473 #if DEBUG
474         if (0 <= it->entry_count)
475                 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
476                         *buffer, it->entry_count, subtree_nr,
477                         sha1_to_hex(it->sha1));
478         else
479                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
480                         *buffer, subtree_nr);
481 #endif
482
483         /*
484          * Just a heuristic -- we do not add directories that often but
485          * we do not want to have to extend it immediately when we do,
486          * hence +2.
487          */
488         it->subtree_alloc = subtree_nr + 2;
489         it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
490         for (i = 0; i < subtree_nr; i++) {
491                 /* read each subtree */
492                 struct cache_tree *sub;
493                 struct cache_tree_sub *subtree;
494                 const char *name = buf;
495
496                 sub = read_one(&buf, &size);
497                 if (!sub)
498                         goto free_return;
499                 subtree = cache_tree_sub(it, name);
500                 subtree->cache_tree = sub;
501         }
502         if (subtree_nr != it->subtree_nr)
503                 die("cache-tree: internal error");
504         *buffer = buf;
505         *size_p = size;
506         return it;
507
508  free_return:
509         cache_tree_free(&it);
510         return NULL;
511 }
512
513 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
514 {
515         if (buffer[0])
516                 return NULL; /* not the whole tree */
517         return read_one(&buffer, &size);
518 }
519
520 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
521 {
522         if (!it)
523                 return NULL;
524         while (*path) {
525                 const char *slash;
526                 struct cache_tree_sub *sub;
527
528                 slash = strchr(path, '/');
529                 if (!slash)
530                         slash = path + strlen(path);
531                 /* between path and slash is the name of the
532                  * subtree to look for.
533                  */
534                 sub = find_subtree(it, path, slash - path, 0);
535                 if (!sub)
536                         return NULL;
537                 it = sub->cache_tree;
538                 if (slash)
539                         while (*slash && *slash == '/')
540                                 slash++;
541                 if (!slash || !*slash)
542                         return it; /* prefix ended with slashes */
543                 path = slash;
544         }
545         return it;
546 }
547
548 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
549 {
550         int entries, was_valid, newfd;
551         struct lock_file *lock_file;
552
553         /*
554          * We can't free this memory, it becomes part of a linked list
555          * parsed atexit()
556          */
557         lock_file = xcalloc(1, sizeof(struct lock_file));
558
559         newfd = hold_locked_index(lock_file, 1);
560
561         entries = read_cache();
562         if (entries < 0)
563                 return WRITE_TREE_UNREADABLE_INDEX;
564         if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
565                 cache_tree_free(&(active_cache_tree));
566
567         if (!active_cache_tree)
568                 active_cache_tree = cache_tree();
569
570         was_valid = cache_tree_fully_valid(active_cache_tree);
571         if (!was_valid) {
572                 int missing_ok = flags & WRITE_TREE_MISSING_OK;
573
574                 if (cache_tree_update(active_cache_tree,
575                                       active_cache, active_nr,
576                                       missing_ok, 0) < 0)
577                         return WRITE_TREE_UNMERGED_INDEX;
578                 if (0 <= newfd) {
579                         if (!write_cache(newfd, active_cache, active_nr) &&
580                             !commit_lock_file(lock_file))
581                                 newfd = -1;
582                 }
583                 /* Not being able to write is fine -- we are only interested
584                  * in updating the cache-tree part, and if the next caller
585                  * ends up using the old index with unupdated cache-tree part
586                  * it misses the work we did here, but that is just a
587                  * performance penalty and not a big deal.
588                  */
589         }
590
591         if (prefix) {
592                 struct cache_tree *subtree =
593                         cache_tree_find(active_cache_tree, prefix);
594                 if (!subtree)
595                         return WRITE_TREE_PREFIX_ERROR;
596                 hashcpy(sha1, subtree->sha1);
597         }
598         else
599                 hashcpy(sha1, active_cache_tree->sha1);
600
601         if (0 <= newfd)
602                 rollback_lock_file(lock_file);
603
604         return 0;
605 }
606
607 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
608 {
609         struct tree_desc desc;
610         struct name_entry entry;
611         int cnt;
612
613         hashcpy(it->sha1, tree->object.sha1);
614         init_tree_desc(&desc, tree->buffer, tree->size);
615         cnt = 0;
616         while (tree_entry(&desc, &entry)) {
617                 if (!S_ISDIR(entry.mode))
618                         cnt++;
619                 else {
620                         struct cache_tree_sub *sub;
621                         struct tree *subtree = lookup_tree(entry.sha1);
622                         if (!subtree->object.parsed)
623                                 parse_tree(subtree);
624                         sub = cache_tree_sub(it, entry.path);
625                         sub->cache_tree = cache_tree();
626                         prime_cache_tree_rec(sub->cache_tree, subtree);
627                         cnt += sub->cache_tree->entry_count;
628                 }
629         }
630         it->entry_count = cnt;
631 }
632
633 void prime_cache_tree(struct cache_tree **it, struct tree *tree)
634 {
635         cache_tree_free(it);
636         *it = cache_tree();
637         prime_cache_tree_rec(*it, tree);
638 }
639
640 /*
641  * find the cache_tree that corresponds to the current level without
642  * exploding the full path into textual form.  The root of the
643  * cache tree is given as "root", and our current level is "info".
644  * (1) When at root level, info->prev is NULL, so it is "root" itself.
645  * (2) Otherwise, find the cache_tree that corresponds to one level
646  *     above us, and find ourselves in there.
647  */
648 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
649                                                          struct traverse_info *info)
650 {
651         struct cache_tree *our_parent;
652
653         if (!info->prev)
654                 return root;
655         our_parent = find_cache_tree_from_traversal(root, info->prev);
656         return cache_tree_find(our_parent, info->name.path);
657 }
658
659 int cache_tree_matches_traversal(struct cache_tree *root,
660                                  struct name_entry *ent,
661                                  struct traverse_info *info)
662 {
663         struct cache_tree *it;
664
665         it = find_cache_tree_from_traversal(root, info);
666         it = cache_tree_find(it, ent->path);
667         if (it && it->entry_count > 0 && !hashcmp(ent->sha1, it->sha1))
668                 return it->entry_count;
669         return 0;
670 }