00ae4e8d3208e09f2cf7a38202a126f728cadb48
   1#include "cache.h"
   2
   3struct tree_entry {
   4        unsigned mode;
   5        unsigned char *sha1;
   6        char *path;
   7        struct tree_entry *next;
   8};
   9
  10static struct tree_entry *read_tree(unsigned char *sha1)
  11{
  12        char type[20];
  13        unsigned long size;
  14        void *buf = read_sha1_file(sha1, type, &size);
  15        struct tree_entry *ret = NULL, **tp = &ret;
  16
  17        if (!buf || strcmp(type, "tree"))
  18                die("unable to read 'tree' object %s", sha1_to_hex(sha1));
  19        while (size) {
  20                int len = strlen(buf)+1;
  21                struct tree_entry * entry = malloc(sizeof(struct tree_entry));
  22                if (size < len + 20 || sscanf(buf, "%o", &entry->mode) != 1)
  23                        die("corrupt 'tree' object %s", sha1_to_hex(sha1));
  24                entry->path = strchr(buf, ' ')+1;
  25                entry->sha1 = buf + len;
  26                entry->next = NULL;
  27                *tp = entry;
  28                tp = &entry->next;
  29                len += 20;
  30                buf += len;
  31                size -= len;
  32        }
  33        return ret;
  34}
  35
  36static void show(const struct tree_entry *a, const char *path)
  37{
  38        printf("select %o %s %s%c", a->mode, sha1_to_hex(a->sha1), path, 0);
  39}
  40
  41static void merge(const struct tree_entry *a, const struct tree_entry *b, const struct tree_entry *c, const char *path)
  42{
  43        char hex_a[60], hex_b[60], hex_c[60];
  44        strcpy(hex_a, sha1_to_hex(a->sha1));
  45        strcpy(hex_b, sha1_to_hex(b->sha1));
  46        strcpy(hex_c, sha1_to_hex(c->sha1));
  47        printf("merge %o->%o,%o %s->%s,%s %s%c",
  48                a->mode, b->mode, c->mode,
  49                hex_a, hex_b, hex_c, path, 0);
  50}
  51
  52static int same(const struct tree_entry *a, const struct tree_entry *b)
  53{
  54        return a->mode == b->mode && !memcmp(a->sha1, b->sha1, 20);
  55}
  56
  57static void merge_entry(const struct tree_entry *src, const struct tree_entry *dst1, const struct tree_entry *dst2)
  58{
  59        static unsigned char nullsha1[20];
  60        static const struct tree_entry none = { 0, nullsha1, "", NULL };
  61        const char *path = NULL;
  62        const struct tree_entry *a, *b, *c;
  63
  64        a = &none;
  65        b = &none;
  66        c = &none;
  67        if (src) { a = src; path = src->path; }
  68        if (dst1) { b = dst1; path = dst1->path; }
  69        if (dst2) { c = dst2; path = dst2->path; }
  70        if (same(b, c)) {
  71                show(b, path);
  72                return;
  73        }
  74        if (same(a, b)) {
  75                show(c, path);
  76                return;
  77        }
  78        if (same(a, c)) {
  79                show(b, path);
  80                return;
  81        }
  82        merge(a, b, c, path);
  83}
  84
  85/* For two entries, select the smaller one, clear the bigger one */
  86static void smaller(struct tree_entry **ap, struct tree_entry **bp)
  87{
  88        struct tree_entry *a = *ap, *b = *bp;
  89        if (a && b) {
  90                int cmp = cache_name_compare(a->path, strlen(a->path), b->path, strlen(b->path));
  91                if (cmp) {
  92                        if (cmp < 0)
  93                                *bp = NULL;
  94                        else
  95                                *ap = NULL;
  96                }
  97        }
  98}
  99
 100static void merge_tree(struct tree_entry *src, struct tree_entry *dst1, struct tree_entry *dst2)
 101{
 102        while (src || dst1 || dst2) {
 103                struct tree_entry *a, *b, *c;
 104                a = src;
 105                b = dst1;
 106                c = dst2;
 107                smaller(&a,&b);
 108                smaller(&a,&c);
 109                smaller(&b,&c);
 110                if (a) src = a->next;
 111                if (b) dst1 = b->next;
 112                if (c) dst2 = c->next;
 113                merge_entry(a,b,c);
 114        }
 115}
 116
 117int main(int argc, char **argv)
 118{
 119        unsigned char src[20], dst1[20], dst2[20];
 120
 121        if (argc != 4 ||
 122            get_sha1_hex(argv[1], src) ||
 123            get_sha1_hex(argv[2], dst1) ||
 124            get_sha1_hex(argv[3], dst2))
 125                usage("merge-tree <src> <dst1> <dst2>");
 126        merge_tree(read_tree(src), read_tree(dst1), read_tree(dst2));
 127        return 0;
 128}