1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
4 #include "xdiff-interface.h"
5 #include "object-store.h"
6 #include "repository.h"
9 #include "merge-blobs.h"
11 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
14 struct merge_list *next;
15 struct merge_list *link; /* other stages for this object */
17 unsigned int stage : 2;
23 static struct merge_list *merge_result, **merge_result_end = &merge_result;
25 static void add_merge_entry(struct merge_list *entry)
27 *merge_result_end = entry;
28 merge_result_end = &entry->next;
31 static void merge_trees(struct tree_desc t[3], const char *base);
33 static const char *explanation(struct merge_list *entry)
35 switch (entry->stage) {
39 return "added in remote";
42 return "added in both";
43 return "added in local";
49 return "removed in both";
52 return "changed in both";
54 if (entry->stage == 3)
55 return "removed in local";
56 return "removed in remote";
59 static void *result(struct merge_list *entry, unsigned long *size)
61 enum object_type type;
62 struct blob *base, *our, *their;
63 const char *path = entry->path;
66 return read_object_file(&entry->blob->object.oid, &type, size);
68 if (entry->stage == 1) {
73 if (entry && entry->stage == 2) {
80 return merge_blobs(the_repository->index, path,
81 base, our, their, size);
84 static void *origin(struct merge_list *entry, unsigned long *size)
86 enum object_type type;
88 if (entry->stage == 2)
89 return read_object_file(&entry->blob->object.oid,
96 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
99 for (i = 0; i < nbuf; i++)
100 printf("%.*s", (int) mb[i].size, mb[i].ptr);
104 static void show_diff(struct merge_list *entry)
113 memset(&xecfg, 0, sizeof(xecfg));
116 ecb.out_line = show_outf;
119 src.ptr = origin(entry, &size);
123 dst.ptr = result(entry, &size);
127 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
128 die("unable to generate diff");
133 static void show_result_list(struct merge_list *entry)
135 printf("%s\n", explanation(entry));
137 struct merge_list *link = entry->link;
138 static const char *desc[4] = { "result", "base", "our", "their" };
139 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
144 static void show_result(void)
146 struct merge_list *walk;
150 show_result_list(walk);
156 /* An empty entry never compares same, not even to another empty entry */
157 static int same_entry(struct name_entry *a, struct name_entry *b)
159 return !is_null_oid(&a->oid) &&
160 !is_null_oid(&b->oid) &&
161 oideq(&a->oid, &b->oid) &&
165 static int both_empty(struct name_entry *a, struct name_entry *b)
167 return is_null_oid(&a->oid) && is_null_oid(&b->oid);
170 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
172 struct merge_list *res = xcalloc(1, sizeof(*res));
177 res->blob = lookup_blob(the_repository, oid);
181 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
183 char *path = xmallocz(traverse_path_len(info, n) + the_hash_algo->rawsz);
184 return make_traverse_path(path, info, n);
187 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
189 struct merge_list *orig, *final;
192 /* If it's already ours, don't bother showing it */
196 path = traverse_path(info, result);
197 orig = create_entry(2, ours->mode, &ours->oid, path);
198 final = create_entry(0, result->mode, &result->oid, path);
202 add_merge_entry(final);
205 static void unresolved_directory(const struct traverse_info *info,
206 struct name_entry n[3])
209 struct name_entry *p;
210 struct tree_desc t[3];
211 void *buf0, *buf1, *buf2;
213 for (p = n; p < n + 3; p++) {
214 if (p->mode && S_ISDIR(p->mode))
218 return; /* there is no tree here */
220 newbase = traverse_path(info, p);
222 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
223 buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
224 buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
225 buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
228 merge_trees(t, newbase);
237 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
240 struct merge_list *link;
247 path = traverse_path(info, n);
248 link = create_entry(stage, n->mode, &n->oid, path);
253 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
255 struct merge_list *entry = NULL;
257 unsigned dirmask = 0, mask = 0;
259 for (i = 0; i < 3; i++) {
262 * Treat missing entries as directories so that we return
263 * after unresolved_directory has handled this.
265 if (!n[i].mode || S_ISDIR(n[i].mode))
269 unresolved_directory(info, n);
274 if (n[2].mode && !S_ISDIR(n[2].mode))
275 entry = link_entry(3, info, n + 2, entry);
276 if (n[1].mode && !S_ISDIR(n[1].mode))
277 entry = link_entry(2, info, n + 1, entry);
278 if (n[0].mode && !S_ISDIR(n[0].mode))
279 entry = link_entry(1, info, n + 0, entry);
281 add_merge_entry(entry);
285 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
288 * This walks the (sorted) trees in lock-step, checking every possible
289 * name. Note that directories automatically sort differently from other
290 * files (see "base_name_compare"), so you'll never see file/directory
291 * conflicts, because they won't ever compare the same.
293 * IOW, if a directory changes to a filename, it will automatically be
294 * seen as the directory going away, and the filename being created.
296 * Think of this as a three-way diff.
298 * The output will be either:
300 * "0 mode sha1 filename"
301 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
302 * in parallel with this too!
305 * "1 mode sha1 filename"
306 * "2 mode sha1 filename"
307 * "3 mode sha1 filename"
308 * where not all of the 1/2/3 lines may exist, of course.
310 * The successful merge rules are the same as for the three-way merge
313 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
316 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
317 /* Modified, added or removed identically */
318 resolve(info, NULL, entry+1);
322 if (same_entry(entry+0, entry+1)) {
323 if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
324 /* We did not touch, they modified -- take theirs */
325 resolve(info, entry+1, entry+2);
329 * If we did not touch a directory but they made it
330 * into a file, we fall through and unresolved()
331 * recurses down. Likewise for the opposite case.
335 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
336 /* We added, modified or removed, they did not touch -- take ours */
337 resolve(info, NULL, entry+1);
341 unresolved(info, entry);
345 static void merge_trees(struct tree_desc t[3], const char *base)
347 struct traverse_info info;
349 setup_traverse_info(&info, base);
350 info.fn = threeway_callback;
351 traverse_trees(&the_index, 3, t, &info);
354 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
356 struct object_id oid;
359 if (get_oid(rev, &oid))
360 die("unknown rev %s", rev);
361 buf = fill_tree_descriptor(desc, &oid);
363 die("%s is not a tree", rev);
367 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
369 struct tree_desc t[3];
370 void *buf1, *buf2, *buf3;
373 usage(merge_tree_usage);
375 buf1 = get_tree_descriptor(t+0, argv[1]);
376 buf2 = get_tree_descriptor(t+1, argv[2]);
377 buf3 = get_tree_descriptor(t+2, argv[3]);