Merge branch 'js/mingw-o-append'
[git/raj.git] / builtin / merge-tree.c
blobf8023bae1e2eceaa8ea086f5d990d8bc4c0e6121
1 #include "builtin.h"
2 #include "tree-walk.h"
3 #include "xdiff-interface.h"
4 #include "object-store.h"
5 #include "repository.h"
6 #include "blob.h"
7 #include "exec-cmd.h"
8 #include "merge-blobs.h"
10 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
12 struct merge_list {
13 struct merge_list *next;
14 struct merge_list *link; /* other stages for this object */
16 unsigned int stage : 2;
17 unsigned int mode;
18 const char *path;
19 struct blob *blob;
22 static struct merge_list *merge_result, **merge_result_end = &merge_result;
24 static void add_merge_entry(struct merge_list *entry)
26 *merge_result_end = entry;
27 merge_result_end = &entry->next;
30 static void merge_trees(struct tree_desc t[3], const char *base);
32 static const char *explanation(struct merge_list *entry)
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";
45 /* Existed in base */
46 entry = entry->link;
47 if (!entry)
48 return "removed in both";
50 if (entry->link)
51 return "changed in both";
53 if (entry->stage == 3)
54 return "removed in local";
55 return "removed in remote";
58 static void *result(struct merge_list *entry, unsigned long *size)
60 enum object_type type;
61 struct blob *base, *our, *their;
62 const char *path = entry->path;
64 if (!entry->stage)
65 return read_object_file(&entry->blob->object.oid, &type, size);
66 base = NULL;
67 if (entry->stage == 1) {
68 base = entry->blob;
69 entry = entry->link;
71 our = NULL;
72 if (entry && entry->stage == 2) {
73 our = entry->blob;
74 entry = entry->link;
76 their = NULL;
77 if (entry)
78 their = entry->blob;
79 return merge_blobs(path, base, our, their, size);
82 static void *origin(struct merge_list *entry, unsigned long *size)
84 enum object_type type;
85 while (entry) {
86 if (entry->stage == 2)
87 return read_object_file(&entry->blob->object.oid,
88 &type, size);
89 entry = entry->link;
91 return NULL;
94 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
96 int i;
97 for (i = 0; i < nbuf; i++)
98 printf("%.*s", (int) mb[i].size, mb[i].ptr);
99 return 0;
102 static void show_diff(struct merge_list *entry)
104 unsigned long size;
105 mmfile_t src, dst;
106 xpparam_t xpp;
107 xdemitconf_t xecfg;
108 xdemitcb_t ecb;
110 xpp.flags = 0;
111 memset(&xecfg, 0, sizeof(xecfg));
112 xecfg.ctxlen = 3;
113 ecb.outf = show_outf;
114 ecb.priv = NULL;
116 src.ptr = origin(entry, &size);
117 if (!src.ptr)
118 size = 0;
119 src.size = size;
120 dst.ptr = result(entry, &size);
121 if (!dst.ptr)
122 size = 0;
123 dst.size = size;
124 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
125 die("unable to generate diff");
126 free(src.ptr);
127 free(dst.ptr);
130 static void show_result_list(struct merge_list *entry)
132 printf("%s\n", explanation(entry));
133 do {
134 struct merge_list *link = entry->link;
135 static const char *desc[4] = { "result", "base", "our", "their" };
136 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
137 entry = link;
138 } while (entry);
141 static void show_result(void)
143 struct merge_list *walk;
145 walk = merge_result;
146 while (walk) {
147 show_result_list(walk);
148 show_diff(walk);
149 walk = walk->next;
153 /* An empty entry never compares same, not even to another empty entry */
154 static int same_entry(struct name_entry *a, struct name_entry *b)
156 return a->oid &&
157 b->oid &&
158 !oidcmp(a->oid, b->oid) &&
159 a->mode == b->mode;
162 static int both_empty(struct name_entry *a, struct name_entry *b)
164 return !(a->oid || b->oid);
167 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
169 struct merge_list *res = xcalloc(1, sizeof(*res));
171 res->stage = stage;
172 res->path = path;
173 res->mode = mode;
174 res->blob = lookup_blob(the_repository, oid);
175 return res;
178 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
180 char *path = xmallocz(traverse_path_len(info, n));
181 return make_traverse_path(path, info, n);
184 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
186 struct merge_list *orig, *final;
187 const char *path;
189 /* If it's already ours, don't bother showing it */
190 if (!ours)
191 return;
193 path = traverse_path(info, result);
194 orig = create_entry(2, ours->mode, ours->oid, path);
195 final = create_entry(0, result->mode, result->oid, path);
197 final->link = orig;
199 add_merge_entry(final);
202 static void unresolved_directory(const struct traverse_info *info,
203 struct name_entry n[3])
205 char *newbase;
206 struct name_entry *p;
207 struct tree_desc t[3];
208 void *buf0, *buf1, *buf2;
210 for (p = n; p < n + 3; p++) {
211 if (p->mode && S_ISDIR(p->mode))
212 break;
214 if (n + 3 <= p)
215 return; /* there is no tree here */
217 newbase = traverse_path(info, p);
219 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
220 buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
221 buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
222 buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
223 #undef ENTRY_OID
225 merge_trees(t, newbase);
227 free(buf0);
228 free(buf1);
229 free(buf2);
230 free(newbase);
234 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
236 const char *path;
237 struct merge_list *link;
239 if (!n->mode)
240 return entry;
241 if (entry)
242 path = entry->path;
243 else
244 path = traverse_path(info, n);
245 link = create_entry(stage, n->mode, n->oid, path);
246 link->link = entry;
247 return link;
250 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
252 struct merge_list *entry = NULL;
253 int i;
254 unsigned dirmask = 0, mask = 0;
256 for (i = 0; i < 3; i++) {
257 mask |= (1 << i);
259 * Treat missing entries as directories so that we return
260 * after unresolved_directory has handled this.
262 if (!n[i].mode || S_ISDIR(n[i].mode))
263 dirmask |= (1 << i);
266 unresolved_directory(info, n);
268 if (dirmask == mask)
269 return;
271 if (n[2].mode && !S_ISDIR(n[2].mode))
272 entry = link_entry(3, info, n + 2, entry);
273 if (n[1].mode && !S_ISDIR(n[1].mode))
274 entry = link_entry(2, info, n + 1, entry);
275 if (n[0].mode && !S_ISDIR(n[0].mode))
276 entry = link_entry(1, info, n + 0, entry);
278 add_merge_entry(entry);
282 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
283 * as the origin.
285 * This walks the (sorted) trees in lock-step, checking every possible
286 * name. Note that directories automatically sort differently from other
287 * files (see "base_name_compare"), so you'll never see file/directory
288 * conflicts, because they won't ever compare the same.
290 * IOW, if a directory changes to a filename, it will automatically be
291 * seen as the directory going away, and the filename being created.
293 * Think of this as a three-way diff.
295 * The output will be either:
296 * - successful merge
297 * "0 mode sha1 filename"
298 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
299 * in parallel with this too!
301 * - conflict:
302 * "1 mode sha1 filename"
303 * "2 mode sha1 filename"
304 * "3 mode sha1 filename"
305 * where not all of the 1/2/3 lines may exist, of course.
307 * The successful merge rules are the same as for the three-way merge
308 * in git-read-tree.
310 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
312 /* Same in both? */
313 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
314 /* Modified, added or removed identically */
315 resolve(info, NULL, entry+1);
316 return mask;
319 if (same_entry(entry+0, entry+1)) {
320 if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
321 /* We did not touch, they modified -- take theirs */
322 resolve(info, entry+1, entry+2);
323 return mask;
326 * If we did not touch a directory but they made it
327 * into a file, we fall through and unresolved()
328 * recurses down. Likewise for the opposite case.
332 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
333 /* We added, modified or removed, they did not touch -- take ours */
334 resolve(info, NULL, entry+1);
335 return mask;
338 unresolved(info, entry);
339 return mask;
342 static void merge_trees(struct tree_desc t[3], const char *base)
344 struct traverse_info info;
346 setup_traverse_info(&info, base);
347 info.fn = threeway_callback;
348 traverse_trees(3, t, &info);
351 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
353 struct object_id oid;
354 void *buf;
356 if (get_oid(rev, &oid))
357 die("unknown rev %s", rev);
358 buf = fill_tree_descriptor(desc, &oid);
359 if (!buf)
360 die("%s is not a tree", rev);
361 return buf;
364 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
366 struct tree_desc t[3];
367 void *buf1, *buf2, *buf3;
369 if (argc != 4)
370 usage(merge_tree_usage);
372 buf1 = get_tree_descriptor(t+0, argv[1]);
373 buf2 = get_tree_descriptor(t+1, argv[2]);
374 buf3 = get_tree_descriptor(t+2, argv[3]);
375 merge_trees(t, "");
376 free(buf1);
377 free(buf2);
378 free(buf3);
380 show_result();
381 return 0;