7 struct tree_entry *next;
10 static struct tree_entry *read_tree(unsigned char *sha1)
14 void *buf = read_sha1_file(sha1, type, &size);
15 struct tree_entry *ret = NULL, **tp = &ret;
17 if (!buf || strcmp(type, "tree"))
18 die("unable to read 'tree' object %s", sha1_to_hex(sha1));
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;
36 static void show(const struct tree_entry *a, const char *path)
38 printf("select %o %s %s%c", a->mode, sha1_to_hex(a->sha1), path, 0);
41 static void merge(const struct tree_entry *a, const struct tree_entry *b, const struct tree_entry *c, const char *path)
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);
52 static int same(const struct tree_entry *a, const struct tree_entry *b)
54 return a->mode == b->mode && !memcmp(a->sha1, b->sha1, 20);
57 static void merge_entry(const struct tree_entry *src, const struct tree_entry *dst1, const struct tree_entry *dst2)
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;
67 if (src) { a = src; path = src->path; }
68 if (dst1) { b = dst1; path = dst1->path; }
69 if (dst2) { c = dst2; path = dst2->path; }
85 /* For two entries, select the smaller one, clear the bigger one */
86 static void smaller(struct tree_entry **ap, struct tree_entry **bp)
88 struct tree_entry *a = *ap, *b = *bp;
90 int cmp = cache_name_compare(a->path, strlen(a->path), b->path, strlen(b->path));
100 static void merge_tree(struct tree_entry *src, struct tree_entry *dst1, struct tree_entry *dst2)
102 while (src || dst1 || dst2) {
103 struct tree_entry *a, *b, *c;
110 if (a) src = a->next;
111 if (b) dst1 = b->next;
112 if (c) dst2 = c->next;
117 int main(int argc, char **argv)
119 unsigned char src[20], dst1[20], dst2[20];
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));