1#include "builtin.h"
2#include "tree-walk.h"
3#include "xdiff-interface.h"
4#include "blob.h"
5#include "exec_cmd.h"
6#include "merge-blobs.h"
7
8static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
9
10struct merge_list {
11 struct merge_list *next;
12 struct merge_list *link; /* other stages for this object */
13
14 unsigned int stage : 2;
15 unsigned int mode;
16 const char *path;
17 struct blob *blob;
18};
19
20static struct merge_list *merge_result, **merge_result_end = &merge_result;
21
22static void add_merge_entry(struct merge_list *entry)
23{
24 *merge_result_end = entry;
25 merge_result_end = &entry->next;
26}
27
28static void merge_trees_recursive(struct tree_desc t[3], const char *base, int df_conflict);
29
30static const char *explanation(struct merge_list *entry)
31{
32 switch (entry->stage) {
33 case 0:
34 return "merged";
35 case 3:
36 return "added in remote";
37 case 2:
38 if (entry->link)
39 return "added in both";
40 return "added in local";
41 }
42
43 /* Existed in base */
44 entry = entry->link;
45 if (!entry)
46 return "removed in both";
47
48 if (entry->link)
49 return "changed in both";
50
51 if (entry->stage == 3)
52 return "removed in local";
53 return "removed in remote";
54}
55
56static void *result(struct merge_list *entry, unsigned long *size)
57{
58 enum object_type type;
59 struct blob *base, *our, *their;
60 const char *path = entry->path;
61
62 if (!entry->stage)
63 return read_sha1_file(entry->blob->object.sha1, &type, size);
64 base = NULL;
65 if (entry->stage == 1) {
66 base = entry->blob;
67 entry = entry->link;
68 }
69 our = NULL;
70 if (entry && entry->stage == 2) {
71 our = entry->blob;
72 entry = entry->link;
73 }
74 their = NULL;
75 if (entry)
76 their = entry->blob;
77 return merge_blobs(path, base, our, their, size);
78}
79
80static void *origin(struct merge_list *entry, unsigned long *size)
81{
82 enum object_type type;
83 while (entry) {
84 if (entry->stage == 2)
85 return read_sha1_file(entry->blob->object.sha1, &type, size);
86 entry = entry->link;
87 }
88 return NULL;
89}
90
91static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
92{
93 int i;
94 for (i = 0; i < nbuf; i++)
95 printf("%.*s", (int) mb[i].size, mb[i].ptr);
96 return 0;
97}
98
99static void show_diff(struct merge_list *entry)
100{
101 unsigned long size;
102 mmfile_t src, dst;
103 xpparam_t xpp;
104 xdemitconf_t xecfg;
105 xdemitcb_t ecb;
106
107 xpp.flags = 0;
108 memset(&xecfg, 0, sizeof(xecfg));
109 xecfg.ctxlen = 3;
110 ecb.outf = show_outf;
111 ecb.priv = NULL;
112
113 src.ptr = origin(entry, &size);
114 if (!src.ptr)
115 size = 0;
116 src.size = size;
117 dst.ptr = result(entry, &size);
118 if (!dst.ptr)
119 size = 0;
120 dst.size = size;
121 xdi_diff(&src, &dst, &xpp, &xecfg, &ecb);
122 free(src.ptr);
123 free(dst.ptr);
124}
125
126static void show_result_list(struct merge_list *entry)
127{
128 printf("%s\n", explanation(entry));
129 do {
130 struct merge_list *link = entry->link;
131 static const char *desc[4] = { "result", "base", "our", "their" };
132 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path);
133 entry = link;
134 } while (entry);
135}
136
137static void show_result(void)
138{
139 struct merge_list *walk;
140
141 walk = merge_result;
142 while (walk) {
143 show_result_list(walk);
144 show_diff(walk);
145 walk = walk->next;
146 }
147}
148
149/* An empty entry never compares same, not even to another empty entry */
150static int same_entry(struct name_entry *a, struct name_entry *b)
151{
152 return a->sha1 &&
153 b->sha1 &&
154 !hashcmp(a->sha1, b->sha1) &&
155 a->mode == b->mode;
156}
157
158static int both_empty(struct name_entry *a, struct name_entry *b)
159{
160 return !(a->sha1 || b->sha1);
161}
162
163static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
164{
165 struct merge_list *res = xcalloc(1, sizeof(*res));
166
167 res->stage = stage;
168 res->path = path;
169 res->mode = mode;
170 res->blob = lookup_blob(sha1);
171 return res;
172}
173
174static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
175{
176 char *path = xmalloc(traverse_path_len(info, n) + 1);
177 return make_traverse_path(path, info, n);
178}
179
180static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
181{
182 struct merge_list *orig, *final;
183 const char *path;
184
185 /* If it's already ours, don't bother showing it */
186 if (!ours)
187 return;
188
189 path = traverse_path(info, result);
190 orig = create_entry(2, ours->mode, ours->sha1, path);
191 final = create_entry(0, result->mode, result->sha1, path);
192
193 final->link = orig;
194
195 add_merge_entry(final);
196}
197
198static void unresolved_directory(const struct traverse_info *info, struct name_entry n[3],
199 int df_conflict)
200{
201 char *newbase;
202 struct name_entry *p;
203 struct tree_desc t[3];
204 void *buf0, *buf1, *buf2;
205
206 for (p = n; p < n + 3; p++) {
207 if (p->mode && S_ISDIR(p->mode))
208 break;
209 }
210 if (n + 3 <= p)
211 return; /* there is no tree here */
212
213 newbase = traverse_path(info, p);
214
215#define ENTRY_SHA1(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->sha1 : NULL)
216 buf0 = fill_tree_descriptor(t+0, ENTRY_SHA1(n + 0));
217 buf1 = fill_tree_descriptor(t+1, ENTRY_SHA1(n + 1));
218 buf2 = fill_tree_descriptor(t+2, ENTRY_SHA1(n + 2));
219#undef ENTRY_SHA1
220
221 merge_trees_recursive(t, newbase, df_conflict);
222
223 free(buf0);
224 free(buf1);
225 free(buf2);
226 free(newbase);
227}
228
229
230static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
231{
232 const char *path;
233 struct merge_list *link;
234
235 if (!n->mode)
236 return entry;
237 if (entry)
238 path = entry->path;
239 else
240 path = traverse_path(info, n);
241 link = create_entry(stage, n->mode, n->sha1, path);
242 link->link = entry;
243 return link;
244}
245
246static void unresolved(const struct traverse_info *info, struct name_entry n[3])
247{
248 struct merge_list *entry = NULL;
249 int i;
250 unsigned dirmask = 0, mask = 0;
251
252 for (i = 0; i < 3; i++) {
253 mask |= (1 << i);
254 if (n[i].mode && S_ISDIR(n[i].mode))
255 dirmask |= (1 << i);
256 }
257
258 unresolved_directory(info, n, dirmask && (dirmask != mask));
259
260 if (dirmask == mask)
261 return;
262
263 if (n[2].mode && !S_ISDIR(n[2].mode))
264 entry = link_entry(3, info, n + 2, entry);
265 if (n[1].mode && !S_ISDIR(n[1].mode))
266 entry = link_entry(2, info, n + 1, entry);
267 if (n[0].mode && !S_ISDIR(n[0].mode))
268 entry = link_entry(1, info, n + 0, entry);
269
270 add_merge_entry(entry);
271}
272
273/*
274 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
275 * as the origin.
276 *
277 * This walks the (sorted) trees in lock-step, checking every possible
278 * name. Note that directories automatically sort differently from other
279 * files (see "base_name_compare"), so you'll never see file/directory
280 * conflicts, because they won't ever compare the same.
281 *
282 * IOW, if a directory changes to a filename, it will automatically be
283 * seen as the directory going away, and the filename being created.
284 *
285 * Think of this as a three-way diff.
286 *
287 * The output will be either:
288 * - successful merge
289 * "0 mode sha1 filename"
290 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
291 * in parallel with this too!
292 *
293 * - conflict:
294 * "1 mode sha1 filename"
295 * "2 mode sha1 filename"
296 * "3 mode sha1 filename"
297 * where not all of the 1/2/3 lines may exist, of course.
298 *
299 * The successful merge rules are the same as for the three-way merge
300 * in git-read-tree.
301 */
302static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
303{
304 /* Same in both? */
305 if (same_entry(entry+1, entry+2) || both_empty(entry+0, entry+2)) {
306 /* Modified, added or removed identically */
307 resolve(info, NULL, entry+1);
308 return mask;
309 }
310
311 if (same_entry(entry+0, entry+1)) {
312 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
313 /* We did not touch, they modified -- take theirs */
314 resolve(info, entry+1, entry+2);
315 return mask;
316 }
317 /*
318 * If we did not touch a directory but they made it
319 * into a file, we fall through and unresolved()
320 * recurses down. Likewise for the opposite case.
321 */
322 }
323
324 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
325 /* We added, modified or removed, they did not touch -- take ours */
326 resolve(info, NULL, entry+1);
327 return mask;
328 }
329
330 unresolved(info, entry);
331 return mask;
332}
333
334static void merge_trees_recursive(struct tree_desc t[3], const char *base, int df_conflict)
335{
336 struct traverse_info info;
337
338 setup_traverse_info(&info, base);
339 info.data = &df_conflict;
340 info.fn = threeway_callback;
341 traverse_trees(3, t, &info);
342}
343
344static void merge_trees(struct tree_desc t[3], const char *base)
345{
346 merge_trees_recursive(t, base, 0);
347}
348
349static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
350{
351 unsigned char sha1[20];
352 void *buf;
353
354 if (get_sha1(rev, sha1))
355 die("unknown rev %s", rev);
356 buf = fill_tree_descriptor(desc, sha1);
357 if (!buf)
358 die("%s is not a tree", rev);
359 return buf;
360}
361
362int cmd_merge_tree(int argc, const char **argv, const char *prefix)
363{
364 struct tree_desc t[3];
365 void *buf1, *buf2, *buf3;
366
367 if (argc != 4)
368 usage(merge_tree_usage);
369
370 buf1 = get_tree_descriptor(t+0, argv[1]);
371 buf2 = get_tree_descriptor(t+1, argv[2]);
372 buf3 = get_tree_descriptor(t+2, argv[3]);
373 merge_trees(t, "");
374 free(buf1);
375 free(buf2);
376 free(buf3);
377
378 show_result();
379 return 0;
380}