blob: f8023bae1e2eceaa8ea086f5d990d8bc4c0e6121 [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Junio C Hamano1b0c7172006-03-29 22:55:43 -08002#include "tree-walk.h"
Linus Torvalds0c799382006-06-28 22:06:36 -07003#include "xdiff-interface.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Stefan Bellerda14a7f2018-06-28 18:21:55 -07005#include "repository.h"
Linus Torvalds83788072006-06-28 11:18:27 -07006#include "blob.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07007#include "exec-cmd.h"
Junio C Hamanofa2364e2012-12-06 15:08:01 -08008#include "merge-blobs.h"
Linus Torvalds492e0752006-02-14 18:05:30 -08009
Alexander Potashev34263de2009-01-04 21:39:27 +030010static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
Linus Torvalds492e0752006-02-14 18:05:30 -080011
Linus Torvalds83788072006-06-28 11:18:27 -070012struct merge_list {
13 struct merge_list *next;
14 struct merge_list *link; /* other stages for this object */
15
Junio C Hamanob13112f2012-12-06 15:41:04 -080016 unsigned int stage : 2;
Linus Torvalds83788072006-06-28 11:18:27 -070017 unsigned int mode;
18 const char *path;
19 struct blob *blob;
20};
21
22static struct merge_list *merge_result, **merge_result_end = &merge_result;
23
24static void add_merge_entry(struct merge_list *entry)
25{
26 *merge_result_end = entry;
27 merge_result_end = &entry->next;
28}
29
René Scharfe57065282014-08-30 23:40:34 +020030static void merge_trees(struct tree_desc t[3], const char *base);
Linus Torvalds492e0752006-02-14 18:05:30 -080031
Linus Torvalds83788072006-06-28 11:18:27 -070032static const char *explanation(struct merge_list *entry)
33{
34 switch (entry->stage) {
35 case 0:
36 return "merged";
37 case 3:
38 return "added in remote";
39 case 2:
40 if (entry->link)
41 return "added in both";
42 return "added in local";
43 }
44
45 /* Existed in base */
46 entry = entry->link;
47 if (!entry)
48 return "removed in both";
49
50 if (entry->link)
51 return "changed in both";
52
53 if (entry->stage == 3)
54 return "removed in local";
55 return "removed in remote";
56}
57
Linus Torvalds0c799382006-06-28 22:06:36 -070058static void *result(struct merge_list *entry, unsigned long *size)
59{
Nicolas Pitre21666f12007-02-26 14:55:59 -050060 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070061 struct blob *base, *our, *their;
Will Palmer21baa6e2010-07-14 18:04:07 +010062 const char *path = entry->path;
Linus Torvalds0c799382006-06-28 22:06:36 -070063
64 if (!entry->stage)
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000065 return read_object_file(&entry->blob->object.oid, &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070066 base = NULL;
67 if (entry->stage == 1) {
68 base = entry->blob;
69 entry = entry->link;
70 }
71 our = NULL;
72 if (entry && entry->stage == 2) {
73 our = entry->blob;
74 entry = entry->link;
75 }
76 their = NULL;
77 if (entry)
78 their = entry->blob;
Junio C Hamanofa2364e2012-12-06 15:08:01 -080079 return merge_blobs(path, base, our, their, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070080}
81
82static void *origin(struct merge_list *entry, unsigned long *size)
83{
Nicolas Pitre21666f12007-02-26 14:55:59 -050084 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070085 while (entry) {
86 if (entry->stage == 2)
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000087 return read_object_file(&entry->blob->object.oid,
88 &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070089 entry = entry->link;
90 }
91 return NULL;
92}
93
94static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
95{
96 int i;
97 for (i = 0; i < nbuf; i++)
98 printf("%.*s", (int) mb[i].size, mb[i].ptr);
99 return 0;
100}
101
102static void show_diff(struct merge_list *entry)
103{
104 unsigned long size;
105 mmfile_t src, dst;
106 xpparam_t xpp;
107 xdemitconf_t xecfg;
108 xdemitcb_t ecb;
109
René Scharfe582aa002010-05-02 15:04:41 +0200110 xpp.flags = 0;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100111 memset(&xecfg, 0, sizeof(xecfg));
Linus Torvalds0c799382006-06-28 22:06:36 -0700112 xecfg.ctxlen = 3;
Linus Torvalds0c799382006-06-28 22:06:36 -0700113 ecb.outf = show_outf;
114 ecb.priv = NULL;
115
116 src.ptr = origin(entry, &size);
117 if (!src.ptr)
118 size = 0;
119 src.size = size;
120 dst.ptr = result(entry, &size);
121 if (!dst.ptr)
122 size = 0;
123 dst.size = size;
Jeff King3efb9882015-09-24 19:12:23 -0400124 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
125 die("unable to generate diff");
Linus Torvalds0c799382006-06-28 22:06:36 -0700126 free(src.ptr);
127 free(dst.ptr);
128}
129
Linus Torvalds83788072006-06-28 11:18:27 -0700130static void show_result_list(struct merge_list *entry)
131{
132 printf("%s\n", explanation(entry));
133 do {
134 struct merge_list *link = entry->link;
135 static const char *desc[4] = { "result", "base", "our", "their" };
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000136 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
Linus Torvalds83788072006-06-28 11:18:27 -0700137 entry = link;
138 } while (entry);
139}
140
141static void show_result(void)
142{
143 struct merge_list *walk;
144
145 walk = merge_result;
146 while (walk) {
147 show_result_list(walk);
Linus Torvalds0c799382006-06-28 22:06:36 -0700148 show_diff(walk);
Linus Torvalds83788072006-06-28 11:18:27 -0700149 walk = walk->next;
150 }
151}
152
Linus Torvalds492e0752006-02-14 18:05:30 -0800153/* An empty entry never compares same, not even to another empty entry */
154static int same_entry(struct name_entry *a, struct name_entry *b)
155{
brian m. carlson7d924c92016-04-17 23:10:39 +0000156 return a->oid &&
157 b->oid &&
158 !oidcmp(a->oid, b->oid) &&
Linus Torvalds492e0752006-02-14 18:05:30 -0800159 a->mode == b->mode;
160}
161
John Keepingaacecc32013-04-07 22:07:51 +0100162static int both_empty(struct name_entry *a, struct name_entry *b)
163{
brian m. carlson7d924c92016-04-17 23:10:39 +0000164 return !(a->oid || b->oid);
John Keepingaacecc32013-04-07 22:07:51 +0100165}
166
brian m. carlson3e930982017-05-06 22:10:13 +0000167static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
Linus Torvalds492e0752006-02-14 18:05:30 -0800168{
Brandon Casey19d4b412008-10-06 18:39:10 -0500169 struct merge_list *res = xcalloc(1, sizeof(*res));
Linus Torvalds83788072006-06-28 11:18:27 -0700170
Linus Torvalds83788072006-06-28 11:18:27 -0700171 res->stage = stage;
172 res->path = path;
173 res->mode = mode;
Stefan Bellerda14a7f2018-06-28 18:21:55 -0700174 res->blob = lookup_blob(the_repository, oid);
Linus Torvalds83788072006-06-28 11:18:27 -0700175 return res;
Linus Torvalds01df5292006-02-14 18:33:02 -0800176}
177
Linus Torvalds40d934d2008-03-05 18:59:29 -0800178static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
179{
Jeff King3733e692016-02-22 17:44:28 -0500180 char *path = xmallocz(traverse_path_len(info, n));
Linus Torvalds40d934d2008-03-05 18:59:29 -0800181 return make_traverse_path(path, info, n);
182}
183
Junio C Hamano8dd15c62012-12-06 16:15:45 -0800184static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
Linus Torvalds01df5292006-02-14 18:33:02 -0800185{
Linus Torvalds83788072006-06-28 11:18:27 -0700186 struct merge_list *orig, *final;
187 const char *path;
188
Junio C Hamano8dd15c62012-12-06 16:15:45 -0800189 /* If it's already ours, don't bother showing it */
190 if (!ours)
Linus Torvalds01df5292006-02-14 18:33:02 -0800191 return;
Linus Torvalds01df5292006-02-14 18:33:02 -0800192
Linus Torvalds40d934d2008-03-05 18:59:29 -0800193 path = traverse_path(info, result);
brian m. carlson3e930982017-05-06 22:10:13 +0000194 orig = create_entry(2, ours->mode, ours->oid, path);
195 final = create_entry(0, result->mode, result->oid, path);
Linus Torvalds83788072006-06-28 11:18:27 -0700196
197 final->link = orig;
198
199 add_merge_entry(final);
Linus Torvalds492e0752006-02-14 18:05:30 -0800200}
201
René Scharfe57065282014-08-30 23:40:34 +0200202static void unresolved_directory(const struct traverse_info *info,
203 struct name_entry n[3])
Linus Torvalds492e0752006-02-14 18:05:30 -0800204{
Linus Torvalds492e0752006-02-14 18:05:30 -0800205 char *newbase;
206 struct name_entry *p;
207 struct tree_desc t[3];
208 void *buf0, *buf1, *buf2;
209
Junio C Hamano35ffe752012-12-13 15:51:29 -0800210 for (p = n; p < n + 3; p++) {
211 if (p->mode && S_ISDIR(p->mode))
212 break;
Linus Torvalds492e0752006-02-14 18:05:30 -0800213 }
Junio C Hamano35ffe752012-12-13 15:51:29 -0800214 if (n + 3 <= p)
215 return; /* there is no tree here */
Junio C Hamano8dd15c62012-12-06 16:15:45 -0800216
Linus Torvalds40d934d2008-03-05 18:59:29 -0800217 newbase = traverse_path(info, p);
Junio C Hamano35ffe752012-12-13 15:51:29 -0800218
René Scharfe5c377d32017-08-12 10:32:59 +0200219#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
220 buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
221 buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
222 buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
223#undef ENTRY_OID
Junio C Hamano35ffe752012-12-13 15:51:29 -0800224
René Scharfe57065282014-08-30 23:40:34 +0200225 merge_trees(t, newbase);
Linus Torvalds492e0752006-02-14 18:05:30 -0800226
227 free(buf0);
228 free(buf1);
229 free(buf2);
230 free(newbase);
Linus Torvalds492e0752006-02-14 18:05:30 -0800231}
232
Linus Torvalds83788072006-06-28 11:18:27 -0700233
Linus Torvalds40d934d2008-03-05 18:59:29 -0800234static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
Linus Torvalds83788072006-06-28 11:18:27 -0700235{
236 const char *path;
237 struct merge_list *link;
238
239 if (!n->mode)
240 return entry;
241 if (entry)
242 path = entry->path;
243 else
Linus Torvalds40d934d2008-03-05 18:59:29 -0800244 path = traverse_path(info, n);
brian m. carlson3e930982017-05-06 22:10:13 +0000245 link = create_entry(stage, n->mode, n->oid, path);
Linus Torvalds83788072006-06-28 11:18:27 -0700246 link->link = entry;
247 return link;
248}
249
Linus Torvalds40d934d2008-03-05 18:59:29 -0800250static void unresolved(const struct traverse_info *info, struct name_entry n[3])
Linus Torvalds492e0752006-02-14 18:05:30 -0800251{
Linus Torvalds83788072006-06-28 11:18:27 -0700252 struct merge_list *entry = NULL;
Junio C Hamano35ffe752012-12-13 15:51:29 -0800253 int i;
254 unsigned dirmask = 0, mask = 0;
Linus Torvalds83788072006-06-28 11:18:27 -0700255
Junio C Hamano35ffe752012-12-13 15:51:29 -0800256 for (i = 0; i < 3; i++) {
John Keeping187c00c2013-03-27 15:58:50 +0000257 mask |= (1 << i);
John Keeping94883b42013-05-06 16:20:54 +0100258 /*
259 * Treat missing entries as directories so that we return
260 * after unresolved_directory has handled this.
261 */
262 if (!n[i].mode || S_ISDIR(n[i].mode))
Junio C Hamano35ffe752012-12-13 15:51:29 -0800263 dirmask |= (1 << i);
264 }
265
René Scharfe57065282014-08-30 23:40:34 +0200266 unresolved_directory(info, n);
Junio C Hamano35ffe752012-12-13 15:51:29 -0800267
268 if (dirmask == mask)
Linus Torvalds492e0752006-02-14 18:05:30 -0800269 return;
Linus Torvalds83788072006-06-28 11:18:27 -0700270
Junio C Hamano35ffe752012-12-13 15:51:29 -0800271 if (n[2].mode && !S_ISDIR(n[2].mode))
272 entry = link_entry(3, info, n + 2, entry);
273 if (n[1].mode && !S_ISDIR(n[1].mode))
274 entry = link_entry(2, info, n + 1, entry);
275 if (n[0].mode && !S_ISDIR(n[0].mode))
276 entry = link_entry(1, info, n + 0, entry);
Linus Torvalds83788072006-06-28 11:18:27 -0700277
278 add_merge_entry(entry);
Linus Torvalds492e0752006-02-14 18:05:30 -0800279}
280
Linus Torvalds164dcb92006-02-15 19:25:32 -0800281/*
282 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
283 * as the origin.
284 *
285 * This walks the (sorted) trees in lock-step, checking every possible
286 * name. Note that directories automatically sort differently from other
287 * files (see "base_name_compare"), so you'll never see file/directory
288 * conflicts, because they won't ever compare the same.
289 *
290 * IOW, if a directory changes to a filename, it will automatically be
291 * seen as the directory going away, and the filename being created.
292 *
293 * Think of this as a three-way diff.
294 *
295 * The output will be either:
296 * - successful merge
297 * "0 mode sha1 filename"
298 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
299 * in parallel with this too!
300 *
301 * - conflict:
302 * "1 mode sha1 filename"
303 * "2 mode sha1 filename"
304 * "3 mode sha1 filename"
305 * where not all of the 1/2/3 lines may exist, of course.
306 *
307 * The successful merge rules are the same as for the three-way merge
308 * in git-read-tree.
309 */
Linus Torvalds91e4f032008-03-05 20:06:18 -0800310static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
Linus Torvalds164dcb92006-02-15 19:25:32 -0800311{
312 /* Same in both? */
John Keepingab5f4242013-04-27 14:40:33 +0100313 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
John Keepingaacecc32013-04-07 22:07:51 +0100314 /* Modified, added or removed identically */
315 resolve(info, NULL, entry+1);
316 return mask;
Linus Torvalds164dcb92006-02-15 19:25:32 -0800317 }
318
319 if (same_entry(entry+0, entry+1)) {
brian m. carlson7d924c92016-04-17 23:10:39 +0000320 if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
Junio C Hamano8dd15c62012-12-06 16:15:45 -0800321 /* We did not touch, they modified -- take theirs */
Linus Torvalds40d934d2008-03-05 18:59:29 -0800322 resolve(info, entry+1, entry+2);
Linus Torvalds5803c6f2008-03-05 19:44:06 -0800323 return mask;
Linus Torvalds164dcb92006-02-15 19:25:32 -0800324 }
Junio C Hamano8dd15c62012-12-06 16:15:45 -0800325 /*
326 * If we did not touch a directory but they made it
327 * into a file, we fall through and unresolved()
328 * recurses down. Likewise for the opposite case.
329 */
Linus Torvalds164dcb92006-02-15 19:25:32 -0800330 }
331
John Keepingaacecc32013-04-07 22:07:51 +0100332 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
333 /* We added, modified or removed, they did not touch -- take ours */
334 resolve(info, NULL, entry+1);
335 return mask;
Linus Torvalds164dcb92006-02-15 19:25:32 -0800336 }
337
Linus Torvalds40d934d2008-03-05 18:59:29 -0800338 unresolved(info, entry);
Linus Torvalds5803c6f2008-03-05 19:44:06 -0800339 return mask;
Linus Torvalds164dcb92006-02-15 19:25:32 -0800340}
341
René Scharfe57065282014-08-30 23:40:34 +0200342static void merge_trees(struct tree_desc t[3], const char *base)
Linus Torvalds164dcb92006-02-15 19:25:32 -0800343{
Linus Torvalds40d934d2008-03-05 18:59:29 -0800344 struct traverse_info info;
345
346 setup_traverse_info(&info, base);
347 info.fn = threeway_callback;
348 traverse_trees(3, t, &info);
Linus Torvalds492e0752006-02-14 18:05:30 -0800349}
350
351static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
352{
brian m. carlsond1a35e52017-07-13 23:49:19 +0000353 struct object_id oid;
Linus Torvalds492e0752006-02-14 18:05:30 -0800354 void *buf;
355
brian m. carlsond1a35e52017-07-13 23:49:19 +0000356 if (get_oid(rev, &oid))
Linus Torvalds492e0752006-02-14 18:05:30 -0800357 die("unknown rev %s", rev);
René Scharfe5c377d32017-08-12 10:32:59 +0200358 buf = fill_tree_descriptor(desc, &oid);
Linus Torvalds492e0752006-02-14 18:05:30 -0800359 if (!buf)
360 die("%s is not a tree", rev);
361 return buf;
362}
363
Linus Torvalds907a7cb2010-01-21 18:25:20 -0800364int cmd_merge_tree(int argc, const char **argv, const char *prefix)
Linus Torvalds492e0752006-02-14 18:05:30 -0800365{
366 struct tree_desc t[3];
367 void *buf1, *buf2, *buf3;
368
Dmitry V. Levin5b6df8e2006-09-14 05:04:09 +0400369 if (argc != 4)
Linus Torvalds492e0752006-02-14 18:05:30 -0800370 usage(merge_tree_usage);
371
372 buf1 = get_tree_descriptor(t+0, argv[1]);
373 buf2 = get_tree_descriptor(t+1, argv[2]);
374 buf3 = get_tree_descriptor(t+2, argv[3]);
375 merge_trees(t, "");
376 free(buf1);
377 free(buf2);
378 free(buf3);
Linus Torvalds83788072006-06-28 11:18:27 -0700379
380 show_result();
Linus Torvalds492e0752006-02-14 18:05:30 -0800381 return 0;
382}