merge-tree: add comments to clarify what these functions are doing
[git.git] / builtin / merge-tree.c
blob5704d51050d3de27a5ffadde9931179ad3fa713d
1 #include "builtin.h"
2 #include "tree-walk.h"
3 #include "xdiff-interface.h"
4 #include "blob.h"
5 #include "exec_cmd.h"
6 #include "merge-blobs.h"
8 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
10 struct merge_list {
11 struct merge_list *next;
12 struct merge_list *link; /* other stages for this object */
14 unsigned int stage : 2;
15 unsigned int mode;
16 const char *path;
17 struct blob *blob;
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) {
33 case 0:
34 return "merged";
35 case 3:
36 return "added in remote";
37 case 2:
38 if (entry->link)
39 return "added in both";
40 return "added in local";
43 /* Existed in base */
44 entry = entry->link;
45 if (!entry)
46 return "removed in both";
48 if (entry->link)
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;
62 if (!entry->stage)
63 return read_sha1_file(entry->blob->object.sha1, &type, size);
64 base = NULL;
65 if (entry->stage == 1) {
66 base = entry->blob;
67 entry = entry->link;
69 our = NULL;
70 if (entry && entry->stage == 2) {
71 our = entry->blob;
72 entry = entry->link;
74 their = NULL;
75 if (entry)
76 their = entry->blob;
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;
83 while (entry) {
84 if (entry->stage == 2)
85 return read_sha1_file(entry->blob->object.sha1, &type, size);
86 entry = entry->link;
88 return NULL;
91 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
93 int i;
94 for (i = 0; i < nbuf; i++)
95 printf("%.*s", (int) mb[i].size, mb[i].ptr);
96 return 0;
99 static void show_diff(struct merge_list *entry)
101 unsigned long size;
102 mmfile_t src, dst;
103 xpparam_t xpp;
104 xdemitconf_t xecfg;
105 xdemitcb_t ecb;
107 xpp.flags = 0;
108 memset(&xecfg, 0, sizeof(xecfg));
109 xecfg.ctxlen = 3;
110 ecb.outf = show_outf;
111 ecb.priv = NULL;
113 src.ptr = origin(entry, &size);
114 if (!src.ptr)
115 size = 0;
116 src.size = size;
117 dst.ptr = result(entry, &size);
118 if (!dst.ptr)
119 size = 0;
120 dst.size = size;
121 xdi_diff(&src, &dst, &xpp, &xecfg, &ecb);
122 free(src.ptr);
123 free(dst.ptr);
126 static void show_result_list(struct merge_list *entry)
128 printf("%s\n", explanation(entry));
129 do {
130 struct merge_list *link = entry->link;
131 static const char *desc[4] = { "result", "base", "our", "their" };
132 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path);
133 entry = link;
134 } while (entry);
137 static void show_result(void)
139 struct merge_list *walk;
141 walk = merge_result;
142 while (walk) {
143 show_result_list(walk);
144 show_diff(walk);
145 walk = walk->next;
149 /* An empty entry never compares same, not even to another empty entry */
150 static int same_entry(struct name_entry *a, struct name_entry *b)
152 return a->sha1 &&
153 b->sha1 &&
154 !hashcmp(a->sha1, b->sha1) &&
155 a->mode == b->mode;
158 static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
160 struct merge_list *res = xcalloc(1, sizeof(*res));
162 res->stage = stage;
163 res->path = path;
164 res->mode = mode;
165 res->blob = lookup_blob(sha1);
166 return res;
169 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
171 char *path = xmalloc(traverse_path_len(info, n) + 1);
172 return make_traverse_path(path, info, n);
175 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
177 struct merge_list *orig, *final;
178 const char *path;
180 /* If it's already ours, don't bother showing it */
181 if (!ours)
182 return;
184 path = traverse_path(info, result);
185 orig = create_entry(2, ours->mode, ours->sha1, path);
186 final = create_entry(0, result->mode, result->sha1, path);
188 final->link = orig;
190 add_merge_entry(final);
193 static int unresolved_directory(const struct traverse_info *info, struct name_entry n[3])
195 char *newbase;
196 struct name_entry *p;
197 struct tree_desc t[3];
198 void *buf0, *buf1, *buf2;
200 p = n;
201 if (!p->mode) {
202 p++;
203 if (!p->mode)
204 p++;
206 if (!S_ISDIR(p->mode))
207 return 0;
209 * NEEDSWORK: this is broken. The path can originally be a file
210 * and then one side may have turned it into a directory, in which
211 * case we return and let the three-way merge as if the tree were
212 * a regular file. If the path that was originally a tree is
213 * now a file in either branch, fill_tree_descriptor() below will
214 * die when fed a blob sha1.
217 newbase = traverse_path(info, p);
218 buf0 = fill_tree_descriptor(t+0, n[0].sha1);
219 buf1 = fill_tree_descriptor(t+1, n[1].sha1);
220 buf2 = fill_tree_descriptor(t+2, n[2].sha1);
221 merge_trees(t, newbase);
223 free(buf0);
224 free(buf1);
225 free(buf2);
226 free(newbase);
227 return 1;
231 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
233 const char *path;
234 struct merge_list *link;
236 if (!n->mode)
237 return entry;
238 if (entry)
239 path = entry->path;
240 else
241 path = traverse_path(info, n);
242 link = create_entry(stage, n->mode, n->sha1, path);
243 link->link = entry;
244 return link;
247 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
249 struct merge_list *entry = NULL;
251 if (unresolved_directory(info, n))
252 return;
255 * Do them in reverse order so that the resulting link
256 * list has the stages in order - link_entry adds new
257 * links at the front.
259 entry = link_entry(3, info, n + 2, entry);
260 entry = link_entry(2, info, n + 1, entry);
261 entry = link_entry(1, info, n + 0, entry);
263 add_merge_entry(entry);
267 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
268 * as the origin.
270 * This walks the (sorted) trees in lock-step, checking every possible
271 * name. Note that directories automatically sort differently from other
272 * files (see "base_name_compare"), so you'll never see file/directory
273 * conflicts, because they won't ever compare the same.
275 * IOW, if a directory changes to a filename, it will automatically be
276 * seen as the directory going away, and the filename being created.
278 * Think of this as a three-way diff.
280 * The output will be either:
281 * - successful merge
282 * "0 mode sha1 filename"
283 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
284 * in parallel with this too!
286 * - conflict:
287 * "1 mode sha1 filename"
288 * "2 mode sha1 filename"
289 * "3 mode sha1 filename"
290 * where not all of the 1/2/3 lines may exist, of course.
292 * The successful merge rules are the same as for the three-way merge
293 * in git-read-tree.
295 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
297 /* Same in both? */
298 if (same_entry(entry+1, entry+2)) {
299 if (entry[0].sha1) {
300 /* Modified identically */
301 resolve(info, NULL, entry+1);
302 return mask;
304 /* "Both added the same" is left unresolved */
307 if (same_entry(entry+0, entry+1)) {
308 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
309 /* We did not touch, they modified -- take theirs */
310 resolve(info, entry+1, entry+2);
311 return mask;
314 * If we did not touch a directory but they made it
315 * into a file, we fall through and unresolved()
316 * recurses down. Likewise for the opposite case.
320 if (same_entry(entry+0, entry+2)) {
321 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
322 /* We modified, they did not touch -- take ours */
323 resolve(info, NULL, entry+1);
324 return mask;
328 unresolved(info, entry);
329 return mask;
332 static void merge_trees(struct tree_desc t[3], const char *base)
334 struct traverse_info info;
336 setup_traverse_info(&info, base);
337 info.fn = threeway_callback;
338 traverse_trees(3, t, &info);
341 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
343 unsigned char sha1[20];
344 void *buf;
346 if (get_sha1(rev, sha1))
347 die("unknown rev %s", rev);
348 buf = fill_tree_descriptor(desc, sha1);
349 if (!buf)
350 die("%s is not a tree", rev);
351 return buf;
354 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
356 struct tree_desc t[3];
357 void *buf1, *buf2, *buf3;
359 if (argc != 4)
360 usage(merge_tree_usage);
362 buf1 = get_tree_descriptor(t+0, argv[1]);
363 buf2 = get_tree_descriptor(t+1, argv[2]);
364 buf3 = get_tree_descriptor(t+2, argv[3]);
365 merge_trees(t, "");
366 free(buf1);
367 free(buf2);
368 free(buf3);
370 show_result();
371 return 0;