1#include "cache.h"
2#include "tree-walk.h"
3
4static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
5static int resolve_directories = 1;
6
7static void merge_trees(struct tree_desc t[3], const char *base);
8
9/* An empty entry never compares same, not even to another empty entry */
10static int same_entry(struct name_entry *a, struct name_entry *b)
11{
12 return a->sha1 &&
13 b->sha1 &&
14 !memcmp(a->sha1, b->sha1, 20) &&
15 a->mode == b->mode;
16}
17
18static const char *sha1_to_hex_zero(const unsigned char *sha1)
19{
20 if (sha1)
21 return sha1_to_hex(sha1);
22 return "0000000000000000000000000000000000000000";
23}
24
25static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
26{
27 /* If it's already branch1, don't bother showing it */
28 if (!branch1)
29 return;
30
31 printf("0 %06o->%06o %s->%s %s%s\n",
32 branch1->mode, result->mode,
33 sha1_to_hex_zero(branch1->sha1),
34 sha1_to_hex_zero(result->sha1),
35 base, result->path);
36}
37
38static int unresolved_directory(const char *base, struct name_entry n[3])
39{
40 int baselen;
41 char *newbase;
42 struct name_entry *p;
43 struct tree_desc t[3];
44 void *buf0, *buf1, *buf2;
45
46 if (!resolve_directories)
47 return 0;
48 p = n;
49 if (!p->mode) {
50 p++;
51 if (!p->mode)
52 p++;
53 }
54 if (!S_ISDIR(p->mode))
55 return 0;
56 baselen = strlen(base);
57 newbase = xmalloc(baselen + p->pathlen + 2);
58 memcpy(newbase, base, baselen);
59 memcpy(newbase + baselen, p->path, p->pathlen);
60 memcpy(newbase + baselen + p->pathlen, "/", 2);
61
62 buf0 = fill_tree_descriptor(t+0, n[0].sha1);
63 buf1 = fill_tree_descriptor(t+1, n[1].sha1);
64 buf2 = fill_tree_descriptor(t+2, n[2].sha1);
65 merge_trees(t, newbase);
66
67 free(buf0);
68 free(buf1);
69 free(buf2);
70 free(newbase);
71 return 1;
72}
73
74static void unresolved(const char *base, struct name_entry n[3])
75{
76 if (unresolved_directory(base, n))
77 return;
78 if (n[0].sha1)
79 printf("1 %06o %s %s%s\n", n[0].mode, sha1_to_hex(n[0].sha1), base, n[0].path);
80 if (n[1].sha1)
81 printf("2 %06o %s %s%s\n", n[1].mode, sha1_to_hex(n[1].sha1), base, n[1].path);
82 if (n[2].sha1)
83 printf("3 %06o %s %s%s\n", n[2].mode, sha1_to_hex(n[2].sha1), base, n[2].path);
84}
85
86/*
87 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
88 * as the origin.
89 *
90 * This walks the (sorted) trees in lock-step, checking every possible
91 * name. Note that directories automatically sort differently from other
92 * files (see "base_name_compare"), so you'll never see file/directory
93 * conflicts, because they won't ever compare the same.
94 *
95 * IOW, if a directory changes to a filename, it will automatically be
96 * seen as the directory going away, and the filename being created.
97 *
98 * Think of this as a three-way diff.
99 *
100 * The output will be either:
101 * - successful merge
102 * "0 mode sha1 filename"
103 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
104 * in parallel with this too!
105 *
106 * - conflict:
107 * "1 mode sha1 filename"
108 * "2 mode sha1 filename"
109 * "3 mode sha1 filename"
110 * where not all of the 1/2/3 lines may exist, of course.
111 *
112 * The successful merge rules are the same as for the three-way merge
113 * in git-read-tree.
114 */
115static void threeway_callback(int n, unsigned long mask, struct name_entry *entry, const char *base)
116{
117 /* Same in both? */
118 if (same_entry(entry+1, entry+2)) {
119 if (entry[0].sha1) {
120 resolve(base, NULL, entry+1);
121 return;
122 }
123 }
124
125 if (same_entry(entry+0, entry+1)) {
126 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
127 resolve(base, entry+1, entry+2);
128 return;
129 }
130 }
131
132 if (same_entry(entry+0, entry+2)) {
133 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
134 resolve(base, NULL, entry+1);
135 return;
136 }
137 }
138
139 unresolved(base, entry);
140}
141
142static void merge_trees(struct tree_desc t[3], const char *base)
143{
144 traverse_trees(3, t, base, threeway_callback);
145}
146
147static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
148{
149 unsigned char sha1[20];
150 void *buf;
151
152 if (get_sha1(rev, sha1))
153 die("unknown rev %s", rev);
154 buf = fill_tree_descriptor(desc, sha1);
155 if (!buf)
156 die("%s is not a tree", rev);
157 return buf;
158}
159
160int main(int argc, char **argv)
161{
162 struct tree_desc t[3];
163 void *buf1, *buf2, *buf3;
164
165 if (argc < 4)
166 usage(merge_tree_usage);
167
168 buf1 = get_tree_descriptor(t+0, argv[1]);
169 buf2 = get_tree_descriptor(t+1, argv[2]);
170 buf3 = get_tree_descriptor(t+2, argv[3]);
171 merge_trees(t, "");
172 free(buf1);
173 free(buf2);
174 free(buf3);
175 return 0;
176}