3 #include "xdiff-interface.h"
7 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
8 static int resolve_directories = 1;
11 struct merge_list *next;
12 struct merge_list *link; /* other stages for this object */
14 unsigned int stage : 2,
21 static struct merge_list *merge_result, **merge_result_end = &merge_result;
23 static void add_merge_entry(struct merge_list *entry)
25 *merge_result_end = entry;
26 merge_result_end = &entry->next;
29 static void merge_trees(struct tree_desc t[3], const char *base);
31 static const char *explanation(struct merge_list *entry)
33 switch (entry->stage) {
37 return "added in remote";
40 return "added in both";
41 return "added in local";
47 return "removed in both";
50 return "changed in both";
52 if (entry->stage == 3)
53 return "removed in local";
54 return "removed in remote";
57 extern void *merge_file(const char *, struct blob *, struct blob *, struct blob *, unsigned long *);
59 static void *result(struct merge_list *entry, unsigned long *size)
61 enum object_type type;
62 struct blob *base, *our, *their;
65 return read_sha1_file(entry->blob->object.sha1, &type, size);
67 if (entry->stage == 1) {
72 if (entry && entry->stage == 2) {
79 return merge_file(entry->path, base, our, their, size);
82 static void *origin(struct merge_list *entry, unsigned long *size)
84 enum object_type type;
86 if (entry->stage == 2)
87 return read_sha1_file(entry->blob->object.sha1, &type, size);
93 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
96 for (i = 0; i < nbuf; i++)
97 printf("%.*s", (int) mb[i].size, mb[i].ptr);
101 static void show_diff(struct merge_list *entry)
109 xpp.flags = XDF_NEED_MINIMAL;
110 memset(&xecfg, 0, sizeof(xecfg));
112 ecb.outf = show_outf;
115 src.ptr = origin(entry, &size);
119 dst.ptr = result(entry, &size);
123 xdi_diff(&src, &dst, &xpp, &xecfg, &ecb);
128 static void show_result_list(struct merge_list *entry)
130 printf("%s\n", explanation(entry));
132 struct merge_list *link = entry->link;
133 static const char *desc[4] = { "result", "base", "our", "their" };
134 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path);
139 static void show_result(void)
141 struct merge_list *walk;
145 show_result_list(walk);
151 /* An empty entry never compares same, not even to another empty entry */
152 static int same_entry(struct name_entry *a, struct name_entry *b)
156 !hashcmp(a->sha1, b->sha1) &&
160 static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
162 struct merge_list *res = xcalloc(1, sizeof(*res));
167 res->blob = lookup_blob(sha1);
171 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
173 char *path = xmalloc(traverse_path_len(info, n) + 1);
174 return make_traverse_path(path, info, n);
177 static void resolve(const struct traverse_info *info, struct name_entry *branch1, struct name_entry *result)
179 struct merge_list *orig, *final;
182 /* If it's already branch1, don't bother showing it */
186 path = traverse_path(info, result);
187 orig = create_entry(2, branch1->mode, branch1->sha1, path);
188 final = create_entry(0, result->mode, result->sha1, path);
192 add_merge_entry(final);
195 static int unresolved_directory(const struct traverse_info *info, struct name_entry n[3])
198 struct name_entry *p;
199 struct tree_desc t[3];
200 void *buf0, *buf1, *buf2;
202 if (!resolve_directories)
210 if (!S_ISDIR(p->mode))
212 newbase = traverse_path(info, p);
213 buf0 = fill_tree_descriptor(t+0, n[0].sha1);
214 buf1 = fill_tree_descriptor(t+1, n[1].sha1);
215 buf2 = fill_tree_descriptor(t+2, n[2].sha1);
216 merge_trees(t, newbase);
226 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
229 struct merge_list *link;
236 path = traverse_path(info, n);
237 link = create_entry(stage, n->mode, n->sha1, path);
242 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
244 struct merge_list *entry = NULL;
246 if (unresolved_directory(info, n))
250 * Do them in reverse order so that the resulting link
251 * list has the stages in order - link_entry adds new
252 * links at the front.
254 entry = link_entry(3, info, n + 2, entry);
255 entry = link_entry(2, info, n + 1, entry);
256 entry = link_entry(1, info, n + 0, entry);
258 add_merge_entry(entry);
262 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
265 * This walks the (sorted) trees in lock-step, checking every possible
266 * name. Note that directories automatically sort differently from other
267 * files (see "base_name_compare"), so you'll never see file/directory
268 * conflicts, because they won't ever compare the same.
270 * IOW, if a directory changes to a filename, it will automatically be
271 * seen as the directory going away, and the filename being created.
273 * Think of this as a three-way diff.
275 * The output will be either:
277 * "0 mode sha1 filename"
278 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
279 * in parallel with this too!
282 * "1 mode sha1 filename"
283 * "2 mode sha1 filename"
284 * "3 mode sha1 filename"
285 * where not all of the 1/2/3 lines may exist, of course.
287 * The successful merge rules are the same as for the three-way merge
290 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
293 if (same_entry(entry+1, entry+2)) {
295 resolve(info, NULL, entry+1);
300 if (same_entry(entry+0, entry+1)) {
301 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
302 resolve(info, entry+1, entry+2);
307 if (same_entry(entry+0, entry+2)) {
308 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
309 resolve(info, NULL, entry+1);
314 unresolved(info, entry);
318 static void merge_trees(struct tree_desc t[3], const char *base)
320 struct traverse_info info;
322 setup_traverse_info(&info, base);
323 info.fn = threeway_callback;
324 traverse_trees(3, t, &info);
327 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
329 unsigned char sha1[20];
332 if (get_sha1(rev, sha1))
333 die("unknown rev %s", rev);
334 buf = fill_tree_descriptor(desc, sha1);
336 die("%s is not a tree", rev);
340 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
342 struct tree_desc t[3];
343 void *buf1, *buf2, *buf3;
346 usage(merge_tree_usage);
348 buf1 = get_tree_descriptor(t+0, argv[1]);
349 buf2 = get_tree_descriptor(t+1, argv[2]);
350 buf3 = get_tree_descriptor(t+2, argv[3]);