3 #include "xdiff-interface.h"
4 #include "object-store.h"
7 #include "merge-blobs.h"
9 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
12 struct merge_list *next;
13 struct merge_list *link; /* other stages for this object */
15 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 static void *result(struct merge_list *entry, unsigned long *size)
59 enum object_type type;
60 struct blob *base, *our, *their;
61 const char *path = entry->path;
64 return read_object_file(&entry->blob->object.oid, &type, size);
66 if (entry->stage == 1) {
71 if (entry && entry->stage == 2) {
78 return merge_blobs(path, base, our, their, size);
81 static void *origin(struct merge_list *entry, unsigned long *size)
83 enum object_type type;
85 if (entry->stage == 2)
86 return read_object_file(&entry->blob->object.oid,
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)
110 memset(&xecfg, 0, sizeof(xecfg));
112 ecb.outf = show_outf;
115 src.ptr = origin(entry, &size);
119 dst.ptr = result(entry, &size);
123 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
124 die("unable to generate diff");
129 static void show_result_list(struct merge_list *entry)
131 printf("%s\n", explanation(entry));
133 struct merge_list *link = entry->link;
134 static const char *desc[4] = { "result", "base", "our", "their" };
135 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
140 static void show_result(void)
142 struct merge_list *walk;
146 show_result_list(walk);
152 /* An empty entry never compares same, not even to another empty entry */
153 static int same_entry(struct name_entry *a, struct name_entry *b)
157 !oidcmp(a->oid, b->oid) &&
161 static int both_empty(struct name_entry *a, struct name_entry *b)
163 return !(a->oid || b->oid);
166 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
168 struct merge_list *res = xcalloc(1, sizeof(*res));
173 res->blob = lookup_blob(oid);
177 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
179 char *path = xmallocz(traverse_path_len(info, n));
180 return make_traverse_path(path, info, n);
183 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
185 struct merge_list *orig, *final;
188 /* If it's already ours, don't bother showing it */
192 path = traverse_path(info, result);
193 orig = create_entry(2, ours->mode, ours->oid, path);
194 final = create_entry(0, result->mode, result->oid, path);
198 add_merge_entry(final);
201 static void unresolved_directory(const struct traverse_info *info,
202 struct name_entry n[3])
205 struct name_entry *p;
206 struct tree_desc t[3];
207 void *buf0, *buf1, *buf2;
209 for (p = n; p < n + 3; p++) {
210 if (p->mode && S_ISDIR(p->mode))
214 return; /* there is no tree here */
216 newbase = traverse_path(info, p);
218 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
219 buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
220 buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
221 buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
224 merge_trees(t, newbase);
233 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
236 struct merge_list *link;
243 path = traverse_path(info, n);
244 link = create_entry(stage, n->mode, n->oid, path);
249 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
251 struct merge_list *entry = NULL;
253 unsigned dirmask = 0, mask = 0;
255 for (i = 0; i < 3; i++) {
258 * Treat missing entries as directories so that we return
259 * after unresolved_directory has handled this.
261 if (!n[i].mode || S_ISDIR(n[i].mode))
265 unresolved_directory(info, n);
270 if (n[2].mode && !S_ISDIR(n[2].mode))
271 entry = link_entry(3, info, n + 2, entry);
272 if (n[1].mode && !S_ISDIR(n[1].mode))
273 entry = link_entry(2, info, n + 1, entry);
274 if (n[0].mode && !S_ISDIR(n[0].mode))
275 entry = link_entry(1, info, n + 0, entry);
277 add_merge_entry(entry);
281 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
284 * This walks the (sorted) trees in lock-step, checking every possible
285 * name. Note that directories automatically sort differently from other
286 * files (see "base_name_compare"), so you'll never see file/directory
287 * conflicts, because they won't ever compare the same.
289 * IOW, if a directory changes to a filename, it will automatically be
290 * seen as the directory going away, and the filename being created.
292 * Think of this as a three-way diff.
294 * The output will be either:
296 * "0 mode sha1 filename"
297 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
298 * in parallel with this too!
301 * "1 mode sha1 filename"
302 * "2 mode sha1 filename"
303 * "3 mode sha1 filename"
304 * where not all of the 1/2/3 lines may exist, of course.
306 * The successful merge rules are the same as for the three-way merge
309 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
312 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
313 /* Modified, added or removed identically */
314 resolve(info, NULL, entry+1);
318 if (same_entry(entry+0, entry+1)) {
319 if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
320 /* We did not touch, they modified -- take theirs */
321 resolve(info, entry+1, entry+2);
325 * If we did not touch a directory but they made it
326 * into a file, we fall through and unresolved()
327 * recurses down. Likewise for the opposite case.
331 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
332 /* We added, modified or removed, they did not touch -- take ours */
333 resolve(info, NULL, entry+1);
337 unresolved(info, entry);
341 static void merge_trees(struct tree_desc t[3], const char *base)
343 struct traverse_info info;
345 setup_traverse_info(&info, base);
346 info.fn = threeway_callback;
347 traverse_trees(3, t, &info);
350 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
352 struct object_id oid;
355 if (get_oid(rev, &oid))
356 die("unknown rev %s", rev);
357 buf = fill_tree_descriptor(desc, &oid);
359 die("%s is not a tree", rev);
363 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
365 struct tree_desc t[3];
366 void *buf1, *buf2, *buf3;
369 usage(merge_tree_usage);
371 buf1 = get_tree_descriptor(t+0, argv[1]);
372 buf2 = get_tree_descriptor(t+1, argv[2]);
373 buf3 = get_tree_descriptor(t+2, argv[3]);