OSDN Git Service

Merge branch 'sh/use-hashcpy'
[git-core/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         ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
79         it->subtree_nr++;
80
81         down = xmalloc(sizeof(*down) + pathlen + 1);
82         down->cache_tree = NULL;
83         down->namelen = pathlen;
84         memcpy(down->name, path, pathlen);
85         down->name[pathlen] = 0;
86
87         if (pos < it->subtree_nr)
88                 memmove(it->down + pos + 1,
89                         it->down + pos,
90                         sizeof(down) * (it->subtree_nr - pos - 1));
91         it->down[pos] = down;
92         return down;
93 }
94
95 struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
96 {
97         int pathlen = strlen(path);
98         return find_subtree(it, path, pathlen, 1);
99 }
100
101 void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
102 {
103         /* a/b/c
104          * ==> invalidate self
105          * ==> find "a", have it invalidate "b/c"
106          * a
107          * ==> invalidate self
108          * ==> if "a" exists as a subtree, remove it.
109          */
110         const char *slash;
111         int namelen;
112         struct cache_tree_sub *down;
113
114 #if DEBUG
115         fprintf(stderr, "cache-tree invalidate <%s>\n", path);
116 #endif
117
118         if (!it)
119                 return;
120         slash = strchr(path, '/');
121         it->entry_count = -1;
122         if (!slash) {
123                 int pos;
124                 namelen = strlen(path);
125                 pos = subtree_pos(it, path, namelen);
126                 if (0 <= pos) {
127                         cache_tree_free(&it->down[pos]->cache_tree);
128                         free(it->down[pos]);
129                         /* 0 1 2 3 4 5
130                          *       ^     ^subtree_nr = 6
131                          *       pos
132                          * move 4 and 5 up one place (2 entries)
133                          * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
134                          */
135                         memmove(it->down+pos, it->down+pos+1,
136                                 sizeof(struct cache_tree_sub *) *
137                                 (it->subtree_nr - pos - 1));
138                         it->subtree_nr--;
139                 }
140                 return;
141         }
142         namelen = slash - path;
143         down = find_subtree(it, path, namelen, 0);
144         if (down)
145                 cache_tree_invalidate_path(down->cache_tree, slash + 1);
146 }
147
148 static int verify_cache(const struct cache_entry * const *cache,
149                         int entries, int flags)
150 {
151         int i, funny;
152         int silent = flags & WRITE_TREE_SILENT;
153
154         /* Verify that the tree is merged */
155         funny = 0;
156         for (i = 0; i < entries; i++) {
157                 const struct cache_entry *ce = cache[i];
158                 if (ce_stage(ce)) {
159                         if (silent)
160                                 return -1;
161                         if (10 < ++funny) {
162                                 fprintf(stderr, "...\n");
163                                 break;
164                         }
165                         fprintf(stderr, "%s: unmerged (%s)\n",
166                                 ce->name, sha1_to_hex(ce->sha1));
167                 }
168         }
169         if (funny)
170                 return -1;
171
172         /* Also verify that the cache does not have path and path/file
173          * at the same time.  At this point we know the cache has only
174          * stage 0 entries.
175          */
176         funny = 0;
177         for (i = 0; i < entries - 1; i++) {
178                 /* path/file always comes after path because of the way
179                  * the cache is sorted.  Also path can appear only once,
180                  * which means conflicting one would immediately follow.
181                  */
182                 const char *this_name = cache[i]->name;
183                 const char *next_name = cache[i+1]->name;
184                 int this_len = strlen(this_name);
185                 if (this_len < strlen(next_name) &&
186                     strncmp(this_name, next_name, this_len) == 0 &&
187                     next_name[this_len] == '/') {
188                         if (10 < ++funny) {
189                                 fprintf(stderr, "...\n");
190                                 break;
191                         }
192                         fprintf(stderr, "You have both %s and %s\n",
193                                 this_name, next_name);
194                 }
195         }
196         if (funny)
197                 return -1;
198         return 0;
199 }
200
201 static void discard_unused_subtrees(struct cache_tree *it)
202 {
203         struct cache_tree_sub **down = it->down;
204         int nr = it->subtree_nr;
205         int dst, src;
206         for (dst = src = 0; src < nr; src++) {
207                 struct cache_tree_sub *s = down[src];
208                 if (s->used)
209                         down[dst++] = s;
210                 else {
211                         cache_tree_free(&s->cache_tree);
212                         free(s);
213                         it->subtree_nr--;
214                 }
215         }
216 }
217
218 int cache_tree_fully_valid(struct cache_tree *it)
219 {
220         int i;
221         if (!it)
222                 return 0;
223         if (it->entry_count < 0 || !has_sha1_file(it->sha1))
224                 return 0;
225         for (i = 0; i < it->subtree_nr; i++) {
226                 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
227                         return 0;
228         }
229         return 1;
230 }
231
232 static int update_one(struct cache_tree *it,
233                       const struct cache_entry * const *cache,
234                       int entries,
235                       const char *base,
236                       int baselen,
237                       int *skip_count,
238                       int flags)
239 {
240         struct strbuf buffer;
241         int missing_ok = flags & WRITE_TREE_MISSING_OK;
242         int dryrun = flags & WRITE_TREE_DRY_RUN;
243         int to_invalidate = 0;
244         int i;
245
246         *skip_count = 0;
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                 const struct cache_entry *ce = cache[i];
265                 struct cache_tree_sub *sub;
266                 const char *path, *slash;
267                 int pathlen, sublen, subcnt, subskip;
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                                     &subskip,
293                                     flags);
294                 if (subcnt < 0)
295                         return subcnt;
296                 i += subcnt;
297                 sub->count = subcnt; /* to be used in the next loop */
298                 *skip_count += subskip;
299                 sub->used = 1;
300         }
301
302         discard_unused_subtrees(it);
303
304         /*
305          * Then write out the tree object for this level.
306          */
307         strbuf_init(&buffer, 8192);
308
309         i = 0;
310         while (i < entries) {
311                 const struct cache_entry *ce = cache[i];
312                 struct cache_tree_sub *sub;
313                 const char *path, *slash;
314                 int pathlen, entlen;
315                 const unsigned char *sha1;
316                 unsigned mode;
317
318                 path = ce->name;
319                 pathlen = ce_namelen(ce);
320                 if (pathlen <= baselen || memcmp(base, path, baselen))
321                         break; /* at the end of this level */
322
323                 slash = strchr(path + baselen, '/');
324                 if (slash) {
325                         entlen = slash - (path + baselen);
326                         sub = find_subtree(it, path + baselen, entlen, 0);
327                         if (!sub)
328                                 die("cache-tree.c: '%.*s' in '%s' not found",
329                                     entlen, path + baselen, path);
330                         i += sub->count;
331                         sha1 = sub->cache_tree->sha1;
332                         mode = S_IFDIR;
333                         if (sub->cache_tree->entry_count < 0)
334                                 to_invalidate = 1;
335                 }
336                 else {
337                         sha1 = ce->sha1;
338                         mode = ce->ce_mode;
339                         entlen = pathlen - baselen;
340                         i++;
341                 }
342                 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
343                         strbuf_release(&buffer);
344                         return error("invalid object %06o %s for '%.*s'",
345                                 mode, sha1_to_hex(sha1), entlen+baselen, path);
346                 }
347
348                 /*
349                  * CE_REMOVE entries are removed before the index is
350                  * written to disk. Skip them to remain consistent
351                  * with the future on-disk index.
352                  */
353                 if (ce->ce_flags & CE_REMOVE) {
354                         *skip_count = *skip_count + 1;
355                         continue;
356                 }
357
358                 /*
359                  * CE_INTENT_TO_ADD entries exist on on-disk index but
360                  * they are not part of generated trees. Invalidate up
361                  * to root to force cache-tree users to read elsewhere.
362                  */
363                 if (ce->ce_flags & CE_INTENT_TO_ADD) {
364                         to_invalidate = 1;
365                         continue;
366                 }
367
368                 strbuf_grow(&buffer, entlen + 100);
369                 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
370                 strbuf_add(&buffer, sha1, 20);
371
372 #if DEBUG
373                 fprintf(stderr, "cache-tree update-one %o %.*s\n",
374                         mode, entlen, path + baselen);
375 #endif
376         }
377
378         if (dryrun)
379                 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
380         else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
381                 strbuf_release(&buffer);
382                 return -1;
383         }
384
385         strbuf_release(&buffer);
386         it->entry_count = to_invalidate ? -1 : i - *skip_count;
387 #if DEBUG
388         fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
389                 it->entry_count, it->subtree_nr,
390                 sha1_to_hex(it->sha1));
391 #endif
392         return i;
393 }
394
395 int cache_tree_update(struct cache_tree *it,
396                       const struct cache_entry * const *cache,
397                       int entries,
398                       int flags)
399 {
400         int i, skip;
401         i = verify_cache(cache, entries, flags);
402         if (i)
403                 return i;
404         i = update_one(it, cache, entries, "", 0, &skip, flags);
405         if (i < 0)
406                 return i;
407         return 0;
408 }
409
410 static void write_one(struct strbuf *buffer, struct cache_tree *it,
411                       const char *path, int pathlen)
412 {
413         int i;
414
415         /* One "cache-tree" entry consists of the following:
416          * path (NUL terminated)
417          * entry_count, subtree_nr ("%d %d\n")
418          * tree-sha1 (missing if invalid)
419          * subtree_nr "cache-tree" entries for subtrees.
420          */
421         strbuf_grow(buffer, pathlen + 100);
422         strbuf_add(buffer, path, pathlen);
423         strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
424
425 #if DEBUG
426         if (0 <= it->entry_count)
427                 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
428                         pathlen, path, it->entry_count, it->subtree_nr,
429                         sha1_to_hex(it->sha1));
430         else
431                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
432                         pathlen, path, it->subtree_nr);
433 #endif
434
435         if (0 <= it->entry_count) {
436                 strbuf_add(buffer, it->sha1, 20);
437         }
438         for (i = 0; i < it->subtree_nr; i++) {
439                 struct cache_tree_sub *down = it->down[i];
440                 if (i) {
441                         struct cache_tree_sub *prev = it->down[i-1];
442                         if (subtree_name_cmp(down->name, down->namelen,
443                                              prev->name, prev->namelen) <= 0)
444                                 die("fatal - unsorted cache subtree");
445                 }
446                 write_one(buffer, down->cache_tree, down->name, down->namelen);
447         }
448 }
449
450 void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
451 {
452         write_one(sb, root, "", 0);
453 }
454
455 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
456 {
457         const char *buf = *buffer;
458         unsigned long size = *size_p;
459         const char *cp;
460         char *ep;
461         struct cache_tree *it;
462         int i, subtree_nr;
463
464         it = NULL;
465         /* skip name, but make sure name exists */
466         while (size && *buf) {
467                 size--;
468                 buf++;
469         }
470         if (!size)
471                 goto free_return;
472         buf++; size--;
473         it = cache_tree();
474
475         cp = buf;
476         it->entry_count = strtol(cp, &ep, 10);
477         if (cp == ep)
478                 goto free_return;
479         cp = ep;
480         subtree_nr = strtol(cp, &ep, 10);
481         if (cp == ep)
482                 goto free_return;
483         while (size && *buf && *buf != '\n') {
484                 size--;
485                 buf++;
486         }
487         if (!size)
488                 goto free_return;
489         buf++; size--;
490         if (0 <= it->entry_count) {
491                 if (size < 20)
492                         goto free_return;
493                 hashcpy(it->sha1, (const unsigned char*)buf);
494                 buf += 20;
495                 size -= 20;
496         }
497
498 #if DEBUG
499         if (0 <= it->entry_count)
500                 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
501                         *buffer, it->entry_count, subtree_nr,
502                         sha1_to_hex(it->sha1));
503         else
504                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
505                         *buffer, subtree_nr);
506 #endif
507
508         /*
509          * Just a heuristic -- we do not add directories that often but
510          * we do not want to have to extend it immediately when we do,
511          * hence +2.
512          */
513         it->subtree_alloc = subtree_nr + 2;
514         it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
515         for (i = 0; i < subtree_nr; i++) {
516                 /* read each subtree */
517                 struct cache_tree *sub;
518                 struct cache_tree_sub *subtree;
519                 const char *name = buf;
520
521                 sub = read_one(&buf, &size);
522                 if (!sub)
523                         goto free_return;
524                 subtree = cache_tree_sub(it, name);
525                 subtree->cache_tree = sub;
526         }
527         if (subtree_nr != it->subtree_nr)
528                 die("cache-tree: internal error");
529         *buffer = buf;
530         *size_p = size;
531         return it;
532
533  free_return:
534         cache_tree_free(&it);
535         return NULL;
536 }
537
538 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
539 {
540         if (buffer[0])
541                 return NULL; /* not the whole tree */
542         return read_one(&buffer, &size);
543 }
544
545 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
546 {
547         if (!it)
548                 return NULL;
549         while (*path) {
550                 const char *slash;
551                 struct cache_tree_sub *sub;
552
553                 slash = strchrnul(path, '/');
554                 /*
555                  * Between path and slash is the name of the subtree
556                  * to look for.
557                  */
558                 sub = find_subtree(it, path, slash - path, 0);
559                 if (!sub)
560                         return NULL;
561                 it = sub->cache_tree;
562
563                 path = slash;
564                 while (*path == '/')
565                         path++;
566         }
567         return it;
568 }
569
570 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
571 {
572         int entries, was_valid, newfd;
573         struct lock_file *lock_file;
574
575         /*
576          * We can't free this memory, it becomes part of a linked list
577          * parsed atexit()
578          */
579         lock_file = xcalloc(1, sizeof(struct lock_file));
580
581         newfd = hold_locked_index(lock_file, 1);
582
583         entries = read_cache();
584         if (entries < 0)
585                 return WRITE_TREE_UNREADABLE_INDEX;
586         if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
587                 cache_tree_free(&(active_cache_tree));
588
589         if (!active_cache_tree)
590                 active_cache_tree = cache_tree();
591
592         was_valid = cache_tree_fully_valid(active_cache_tree);
593         if (!was_valid) {
594                 if (cache_tree_update(active_cache_tree,
595                                       (const struct cache_entry * const *)active_cache,
596                                       active_nr, flags) < 0)
597                         return WRITE_TREE_UNMERGED_INDEX;
598                 if (0 <= newfd) {
599                         if (!write_cache(newfd, active_cache, active_nr) &&
600                             !commit_lock_file(lock_file))
601                                 newfd = -1;
602                 }
603                 /* Not being able to write is fine -- we are only interested
604                  * in updating the cache-tree part, and if the next caller
605                  * ends up using the old index with unupdated cache-tree part
606                  * it misses the work we did here, but that is just a
607                  * performance penalty and not a big deal.
608                  */
609         }
610
611         if (prefix) {
612                 struct cache_tree *subtree =
613                         cache_tree_find(active_cache_tree, prefix);
614                 if (!subtree)
615                         return WRITE_TREE_PREFIX_ERROR;
616                 hashcpy(sha1, subtree->sha1);
617         }
618         else
619                 hashcpy(sha1, active_cache_tree->sha1);
620
621         if (0 <= newfd)
622                 rollback_lock_file(lock_file);
623
624         return 0;
625 }
626
627 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
628 {
629         struct tree_desc desc;
630         struct name_entry entry;
631         int cnt;
632
633         hashcpy(it->sha1, tree->object.sha1);
634         init_tree_desc(&desc, tree->buffer, tree->size);
635         cnt = 0;
636         while (tree_entry(&desc, &entry)) {
637                 if (!S_ISDIR(entry.mode))
638                         cnt++;
639                 else {
640                         struct cache_tree_sub *sub;
641                         struct tree *subtree = lookup_tree(entry.sha1);
642                         if (!subtree->object.parsed)
643                                 parse_tree(subtree);
644                         sub = cache_tree_sub(it, entry.path);
645                         sub->cache_tree = cache_tree();
646                         prime_cache_tree_rec(sub->cache_tree, subtree);
647                         cnt += sub->cache_tree->entry_count;
648                 }
649         }
650         it->entry_count = cnt;
651 }
652
653 void prime_cache_tree(struct cache_tree **it, struct tree *tree)
654 {
655         cache_tree_free(it);
656         *it = cache_tree();
657         prime_cache_tree_rec(*it, tree);
658 }
659
660 /*
661  * find the cache_tree that corresponds to the current level without
662  * exploding the full path into textual form.  The root of the
663  * cache tree is given as "root", and our current level is "info".
664  * (1) When at root level, info->prev is NULL, so it is "root" itself.
665  * (2) Otherwise, find the cache_tree that corresponds to one level
666  *     above us, and find ourselves in there.
667  */
668 static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
669                                                          struct traverse_info *info)
670 {
671         struct cache_tree *our_parent;
672
673         if (!info->prev)
674                 return root;
675         our_parent = find_cache_tree_from_traversal(root, info->prev);
676         return cache_tree_find(our_parent, info->name.path);
677 }
678
679 int cache_tree_matches_traversal(struct cache_tree *root,
680                                  struct name_entry *ent,
681                                  struct traverse_info *info)
682 {
683         struct cache_tree *it;
684
685         it = find_cache_tree_from_traversal(root, info);
686         it = cache_tree_find(it, ent->path);
687         if (it && it->entry_count > 0 && !hashcmp(ent->sha1, it->sha1))
688                 return it->entry_count;
689         return 0;
690 }
691
692 int update_main_cache_tree(int flags)
693 {
694         if (!the_index.cache_tree)
695                 the_index.cache_tree = cache_tree();
696         return cache_tree_update(the_index.cache_tree,
697                                  (const struct cache_entry * const *)the_index.cache,
698                                  the_index.cache_nr, flags);
699 }