3 #include "xdiff-interface.h"
6 #include "merge-blobs.h"
8 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
11 struct merge_list *next;
12 struct merge_list *link; /* other stages for this object */
14 unsigned int stage : 2;
20 static struct merge_list *merge_result, **merge_result_end = &merge_result;
22 static void add_merge_entry(struct merge_list *entry)
24 *merge_result_end = entry;
25 merge_result_end = &entry->next;
28 static void merge_trees(struct tree_desc t[3], const char *base);
30 static const char *explanation(struct merge_list *entry)
32 switch (entry->stage) {
36 return "added in remote";
39 return "added in both";
40 return "added in local";
46 return "removed in both";
49 return "changed in both";
51 if (entry->stage == 3)
52 return "removed in local";
53 return "removed in remote";
56 static void *result(struct merge_list *entry, unsigned long *size)
58 enum object_type type;
59 struct blob *base, *our, *their;
60 const char *path = entry->path;
63 return read_sha1_file(entry->blob->object.oid.hash, &type, size);
65 if (entry->stage == 1) {
70 if (entry && entry->stage == 2) {
77 return merge_blobs(path, base, our, their, size);
80 static void *origin(struct merge_list *entry, unsigned long *size)
82 enum object_type type;
84 if (entry->stage == 2)
85 return read_sha1_file(entry->blob->object.oid.hash, &type, size);
91 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
94 for (i = 0; i < nbuf; i++)
95 printf("%.*s", (int) mb[i].size, mb[i].ptr);
99 static void show_diff(struct merge_list *entry)
108 memset(&xecfg, 0, sizeof(xecfg));
110 ecb.outf = show_outf;
113 src.ptr = origin(entry, &size);
117 dst.ptr = result(entry, &size);
121 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
122 die("unable to generate diff");
127 static void show_result_list(struct merge_list *entry)
129 printf("%s\n", explanation(entry));
131 struct merge_list *link = entry->link;
132 static const char *desc[4] = { "result", "base", "our", "their" };
133 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
138 static void show_result(void)
140 struct merge_list *walk;
144 show_result_list(walk);
150 /* An empty entry never compares same, not even to another empty entry */
151 static int same_entry(struct name_entry *a, struct name_entry *b)
155 !oidcmp(a->oid, b->oid) &&
159 static int both_empty(struct name_entry *a, struct name_entry *b)
161 return !(a->oid || b->oid);
164 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
166 struct merge_list *res = xcalloc(1, sizeof(*res));
171 res->blob = lookup_blob(oid);
175 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
177 char *path = xmallocz(traverse_path_len(info, n));
178 return make_traverse_path(path, info, n);
181 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
183 struct merge_list *orig, *final;
186 /* If it's already ours, don't bother showing it */
190 path = traverse_path(info, result);
191 orig = create_entry(2, ours->mode, ours->oid, path);
192 final = create_entry(0, result->mode, result->oid, path);
196 add_merge_entry(final);
199 static void unresolved_directory(const struct traverse_info *info,
200 struct name_entry n[3])
203 struct name_entry *p;
204 struct tree_desc t[3];
205 void *buf0, *buf1, *buf2;
207 for (p = n; p < n + 3; p++) {
208 if (p->mode && S_ISDIR(p->mode))
212 return; /* there is no tree here */
214 newbase = traverse_path(info, p);
216 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
217 buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
218 buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
219 buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
222 merge_trees(t, newbase);
231 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
234 struct merge_list *link;
241 path = traverse_path(info, n);
242 link = create_entry(stage, n->mode, n->oid, path);
247 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
249 struct merge_list *entry = NULL;
251 unsigned dirmask = 0, mask = 0;
253 for (i = 0; i < 3; i++) {
256 * Treat missing entries as directories so that we return
257 * after unresolved_directory has handled this.
259 if (!n[i].mode || S_ISDIR(n[i].mode))
263 unresolved_directory(info, n);
268 if (n[2].mode && !S_ISDIR(n[2].mode))
269 entry = link_entry(3, info, n + 2, entry);
270 if (n[1].mode && !S_ISDIR(n[1].mode))
271 entry = link_entry(2, info, n + 1, entry);
272 if (n[0].mode && !S_ISDIR(n[0].mode))
273 entry = link_entry(1, info, n + 0, entry);
275 add_merge_entry(entry);
279 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
282 * This walks the (sorted) trees in lock-step, checking every possible
283 * name. Note that directories automatically sort differently from other
284 * files (see "base_name_compare"), so you'll never see file/directory
285 * conflicts, because they won't ever compare the same.
287 * IOW, if a directory changes to a filename, it will automatically be
288 * seen as the directory going away, and the filename being created.
290 * Think of this as a three-way diff.
292 * The output will be either:
294 * "0 mode sha1 filename"
295 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
296 * in parallel with this too!
299 * "1 mode sha1 filename"
300 * "2 mode sha1 filename"
301 * "3 mode sha1 filename"
302 * where not all of the 1/2/3 lines may exist, of course.
304 * The successful merge rules are the same as for the three-way merge
307 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
310 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
311 /* Modified, added or removed identically */
312 resolve(info, NULL, entry+1);
316 if (same_entry(entry+0, entry+1)) {
317 if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
318 /* We did not touch, they modified -- take theirs */
319 resolve(info, entry+1, entry+2);
323 * If we did not touch a directory but they made it
324 * into a file, we fall through and unresolved()
325 * recurses down. Likewise for the opposite case.
329 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
330 /* We added, modified or removed, they did not touch -- take ours */
331 resolve(info, NULL, entry+1);
335 unresolved(info, entry);
339 static void merge_trees(struct tree_desc t[3], const char *base)
341 struct traverse_info info;
343 setup_traverse_info(&info, base);
344 info.fn = threeway_callback;
345 traverse_trees(3, t, &info);
348 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
350 struct object_id oid;
353 if (get_oid(rev, &oid))
354 die("unknown rev %s", rev);
355 buf = fill_tree_descriptor(desc, &oid);
357 die("%s is not a tree", rev);
361 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
363 struct tree_desc t[3];
364 void *buf1, *buf2, *buf3;
367 usage(merge_tree_usage);
369 buf1 = get_tree_descriptor(t+0, argv[1]);
370 buf2 = get_tree_descriptor(t+1, argv[2]);
371 buf3 = get_tree_descriptor(t+2, argv[3]);