match-trees: use hashcpy to splice trees
[git/git.git] / builtin / merge-tree.c
CommitLineData
c2e86add 1#include "builtin.h"
1b0c7174 2#include "tree-walk.h"
0c799383 3#include "xdiff-interface.h"
cbd53a21 4#include "object-store.h"
da14a7ff 5#include "repository.h"
83788070 6#include "blob.h"
d807c4a0 7#include "exec-cmd.h"
fa2364ec 8#include "merge-blobs.h"
492e0759 9
34263de0 10static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
492e0759 11
83788070
LT
12struct merge_list {
13 struct merge_list *next;
14 struct merge_list *link; /* other stages for this object */
15
b13112fa 16 unsigned int stage : 2;
83788070
LT
17 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
57065289 30static void merge_trees(struct tree_desc t[3], const char *base);
492e0759 31
83788070
LT
32static 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
0c799383
LT
58static void *result(struct merge_list *entry, unsigned long *size)
59{
21666f1a 60 enum object_type type;
0c799383 61 struct blob *base, *our, *their;
21baa6e0 62 const char *path = entry->path;
0c799383
LT
63
64 if (!entry->stage)
b4f5aca4 65 return read_object_file(&entry->blob->object.oid, &type, size);
0c799383
LT
66 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;
f4a55b27 79 return merge_blobs(&the_index, path, base, our, their, size);
0c799383
LT
80}
81
82static void *origin(struct merge_list *entry, unsigned long *size)
83{
21666f1a 84 enum object_type type;
0c799383
LT
85 while (entry) {
86 if (entry->stage == 2)
b4f5aca4 87 return read_object_file(&entry->blob->object.oid,
88 &type, size);
0c799383
LT
89 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
582aa00b 110 xpp.flags = 0;
30b25010 111 memset(&xecfg, 0, sizeof(xecfg));
0c799383 112 xecfg.ctxlen = 3;
611e42a5
JK
113 ecb.out_hunk = NULL;
114 ecb.out_line = show_outf;
0c799383
LT
115 ecb.priv = NULL;
116
117 src.ptr = origin(entry, &size);
118 if (!src.ptr)
119 size = 0;
120 src.size = size;
121 dst.ptr = result(entry, &size);
122 if (!dst.ptr)
123 size = 0;
124 dst.size = size;
3efb9880
JK
125 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
126 die("unable to generate diff");
0c799383
LT
127 free(src.ptr);
128 free(dst.ptr);
129}
130
83788070
LT
131static void show_result_list(struct merge_list *entry)
132{
133 printf("%s\n", explanation(entry));
134 do {
135 struct merge_list *link = entry->link;
136 static const char *desc[4] = { "result", "base", "our", "their" };
f2fd0760 137 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
83788070
LT
138 entry = link;
139 } while (entry);
140}
141
142static void show_result(void)
143{
144 struct merge_list *walk;
145
146 walk = merge_result;
147 while (walk) {
148 show_result_list(walk);
0c799383 149 show_diff(walk);
83788070
LT
150 walk = walk->next;
151 }
152}
153
492e0759
LT
154/* An empty entry never compares same, not even to another empty entry */
155static int same_entry(struct name_entry *a, struct name_entry *b)
156{
7d924c91 157 return a->oid &&
158 b->oid &&
4a7e27e9 159 oideq(a->oid, b->oid) &&
492e0759
LT
160 a->mode == b->mode;
161}
162
aacecc3b
JK
163static int both_empty(struct name_entry *a, struct name_entry *b)
164{
7d924c91 165 return !(a->oid || b->oid);
aacecc3b
JK
166}
167
3e930981 168static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
492e0759 169{
19d4b416 170 struct merge_list *res = xcalloc(1, sizeof(*res));
83788070 171
83788070
LT
172 res->stage = stage;
173 res->path = path;
174 res->mode = mode;
da14a7ff 175 res->blob = lookup_blob(the_repository, oid);
83788070 176 return res;
01df5297
LT
177}
178
40d934df
LT
179static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
180{
3733e694 181 char *path = xmallocz(traverse_path_len(info, n));
40d934df
LT
182 return make_traverse_path(path, info, n);
183}
184
8dd15c6a 185static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
01df5297 186{
83788070
LT
187 struct merge_list *orig, *final;
188 const char *path;
189
8dd15c6a
JH
190 /* If it's already ours, don't bother showing it */
191 if (!ours)
01df5297 192 return;
01df5297 193
40d934df 194 path = traverse_path(info, result);
3e930981 195 orig = create_entry(2, ours->mode, ours->oid, path);
196 final = create_entry(0, result->mode, result->oid, path);
83788070
LT
197
198 final->link = orig;
199
200 add_merge_entry(final);
492e0759
LT
201}
202
57065289
RS
203static void unresolved_directory(const struct traverse_info *info,
204 struct name_entry n[3])
492e0759 205{
492e0759
LT
206 char *newbase;
207 struct name_entry *p;
208 struct tree_desc t[3];
209 void *buf0, *buf1, *buf2;
210
35ffe758
JH
211 for (p = n; p < n + 3; p++) {
212 if (p->mode && S_ISDIR(p->mode))
213 break;
492e0759 214 }
35ffe758
JH
215 if (n + 3 <= p)
216 return; /* there is no tree here */
8dd15c6a 217
40d934df 218 newbase = traverse_path(info, p);
35ffe758 219
5c377d3d
RS
220#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
221 buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
222 buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
223 buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
224#undef ENTRY_OID
35ffe758 225
57065289 226 merge_trees(t, newbase);
492e0759
LT
227
228 free(buf0);
229 free(buf1);
230 free(buf2);
231 free(newbase);
492e0759
LT
232}
233
83788070 234
40d934df 235static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
83788070
LT
236{
237 const char *path;
238 struct merge_list *link;
239
240 if (!n->mode)
241 return entry;
242 if (entry)
243 path = entry->path;
244 else
40d934df 245 path = traverse_path(info, n);
3e930981 246 link = create_entry(stage, n->mode, n->oid, path);
83788070
LT
247 link->link = entry;
248 return link;
249}
250
40d934df 251static void unresolved(const struct traverse_info *info, struct name_entry n[3])
492e0759 252{
83788070 253 struct merge_list *entry = NULL;
35ffe758
JH
254 int i;
255 unsigned dirmask = 0, mask = 0;
256
257 for (i = 0; i < 3; i++) {
187c00c6 258 mask |= (1 << i);
94883b43
JK
259 /*
260 * Treat missing entries as directories so that we return
261 * after unresolved_directory has handled this.
262 */
263 if (!n[i].mode || S_ISDIR(n[i].mode))
35ffe758
JH
264 dirmask |= (1 << i);
265 }
266
57065289 267 unresolved_directory(info, n);
83788070 268
35ffe758 269 if (dirmask == mask)
492e0759 270 return;
83788070 271
35ffe758
JH
272 if (n[2].mode && !S_ISDIR(n[2].mode))
273 entry = link_entry(3, info, n + 2, entry);
274 if (n[1].mode && !S_ISDIR(n[1].mode))
275 entry = link_entry(2, info, n + 1, entry);
276 if (n[0].mode && !S_ISDIR(n[0].mode))
277 entry = link_entry(1, info, n + 0, entry);
83788070
LT
278
279 add_merge_entry(entry);
492e0759
LT
280}
281
164dcb97
LT
282/*
283 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
284 * as the origin.
285 *
286 * This walks the (sorted) trees in lock-step, checking every possible
287 * name. Note that directories automatically sort differently from other
288 * files (see "base_name_compare"), so you'll never see file/directory
289 * conflicts, because they won't ever compare the same.
290 *
291 * IOW, if a directory changes to a filename, it will automatically be
292 * seen as the directory going away, and the filename being created.
293 *
294 * Think of this as a three-way diff.
295 *
296 * The output will be either:
297 * - successful merge
298 * "0 mode sha1 filename"
299 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
300 * in parallel with this too!
301 *
302 * - conflict:
303 * "1 mode sha1 filename"
304 * "2 mode sha1 filename"
305 * "3 mode sha1 filename"
306 * where not all of the 1/2/3 lines may exist, of course.
307 *
308 * The successful merge rules are the same as for the three-way merge
309 * in git-read-tree.
310 */
91e4f036 311static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
164dcb97
LT
312{
313 /* Same in both? */
ab5f4242 314 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
aacecc3b
JK
315 /* Modified, added or removed identically */
316 resolve(info, NULL, entry+1);
317 return mask;
164dcb97 318 }
492e0759 319
164dcb97 320 if (same_entry(entry+0, entry+1)) {
7d924c91 321 if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
8dd15c6a 322 /* We did not touch, they modified -- take theirs */
40d934df 323 resolve(info, entry+1, entry+2);
5803c6f8 324 return mask;
492e0759 325 }
8dd15c6a
JH
326 /*
327 * If we did not touch a directory but they made it
328 * into a file, we fall through and unresolved()
329 * recurses down. Likewise for the opposite case.
330 */
164dcb97 331 }
492e0759 332
aacecc3b
JK
333 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
334 /* We added, modified or removed, they did not touch -- take ours */
335 resolve(info, NULL, entry+1);
336 return mask;
492e0759 337 }
164dcb97 338
40d934df 339 unresolved(info, entry);
5803c6f8 340 return mask;
164dcb97
LT
341}
342
57065289 343static void merge_trees(struct tree_desc t[3], const char *base)
164dcb97 344{
40d934df
LT
345 struct traverse_info info;
346
347 setup_traverse_info(&info, base);
348 info.fn = threeway_callback;
349 traverse_trees(3, t, &info);
492e0759
LT
350}
351
352static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
353{
d1a35e5c 354 struct object_id oid;
492e0759
LT
355 void *buf;
356
d1a35e5c 357 if (get_oid(rev, &oid))
492e0759 358 die("unknown rev %s", rev);
5c377d3d 359 buf = fill_tree_descriptor(desc, &oid);
492e0759
LT
360 if (!buf)
361 die("%s is not a tree", rev);
362 return buf;
363}
364
907a7cb5 365int cmd_merge_tree(int argc, const char **argv, const char *prefix)
492e0759
LT
366{
367 struct tree_desc t[3];
368 void *buf1, *buf2, *buf3;
369
5b6df8e4 370 if (argc != 4)
492e0759
LT
371 usage(merge_tree_usage);
372
373 buf1 = get_tree_descriptor(t+0, argv[1]);
374 buf2 = get_tree_descriptor(t+1, argv[2]);
375 buf3 = get_tree_descriptor(t+2, argv[3]);
376 merge_trees(t, "");
377 free(buf1);
378 free(buf2);
379 free(buf3);
83788070
LT
380
381 show_result();
492e0759
LT
382 return 0;
383}