3 #include "xdiff-interface.h"
6 static const char merge_tree_usage
[] = "git-merge-tree <base-tree> <branch1> <branch2>";
7 static int resolve_directories
= 1;
10 struct merge_list
*next
;
11 struct merge_list
*link
; /* other stages for this object */
13 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 extern void *merge_file(struct blob
*, struct blob
*, struct blob
*, unsigned long *);
58 static void *result(struct merge_list
*entry
, unsigned long *size
)
60 enum object_type type
;
61 struct blob
*base
, *our
, *their
;
64 return read_sha1_file(entry
->blob
->object
.sha1
, &type
, size
);
66 if (entry
->stage
== 1) {
71 if (entry
&& entry
->stage
== 2) {
78 return merge_file(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_sha1_file(entry
->blob
->object
.sha1
, &type
, size
);
92 static int show_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
95 for (i
= 0; i
< nbuf
; i
++)
96 printf("%.*s", (int) mb
[i
].size
, mb
[i
].ptr
);
100 static void show_diff(struct merge_list
*entry
)
108 xpp
.flags
= XDF_NEED_MINIMAL
;
109 memset(&xecfg
, 0, sizeof(xecfg
));
111 ecb
.outf
= show_outf
;
114 src
.ptr
= origin(entry
, &size
);
118 dst
.ptr
= result(entry
, &size
);
122 xdi_diff(&src
, &dst
, &xpp
, &xecfg
, &ecb
);
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
, sha1_to_hex(entry
->blob
->object
.sha1
), 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 !hashcmp(a
->sha1
, b
->sha1
) &&
159 static struct merge_list
*create_entry(unsigned stage
, unsigned mode
, const unsigned char *sha1
, const char *path
)
161 struct merge_list
*res
= xcalloc(1, sizeof(*res
));
166 res
->blob
= lookup_blob(sha1
);
170 static char *traverse_path(const struct traverse_info
*info
, const struct name_entry
*n
)
172 char *path
= xmalloc(traverse_path_len(info
, n
) + 1);
173 return make_traverse_path(path
, info
, n
);
176 static void resolve(const struct traverse_info
*info
, struct name_entry
*branch1
, struct name_entry
*result
)
178 struct merge_list
*orig
, *final
;
181 /* If it's already branch1, don't bother showing it */
185 path
= traverse_path(info
, result
);
186 orig
= create_entry(2, branch1
->mode
, branch1
->sha1
, path
);
187 final
= create_entry(0, result
->mode
, result
->sha1
, path
);
191 add_merge_entry(final
);
194 static int unresolved_directory(const struct traverse_info
*info
, struct name_entry n
[3])
197 struct name_entry
*p
;
198 struct tree_desc t
[3];
199 void *buf0
, *buf1
, *buf2
;
201 if (!resolve_directories
)
209 if (!S_ISDIR(p
->mode
))
211 newbase
= traverse_path(info
, p
);
212 buf0
= fill_tree_descriptor(t
+0, n
[0].sha1
);
213 buf1
= fill_tree_descriptor(t
+1, n
[1].sha1
);
214 buf2
= fill_tree_descriptor(t
+2, n
[2].sha1
);
215 merge_trees(t
, newbase
);
225 static struct merge_list
*link_entry(unsigned stage
, const struct traverse_info
*info
, struct name_entry
*n
, struct merge_list
*entry
)
228 struct merge_list
*link
;
235 path
= traverse_path(info
, n
);
236 link
= create_entry(stage
, n
->mode
, n
->sha1
, path
);
241 static void unresolved(const struct traverse_info
*info
, struct name_entry n
[3])
243 struct merge_list
*entry
= NULL
;
245 if (unresolved_directory(info
, n
))
249 * Do them in reverse order so that the resulting link
250 * list has the stages in order - link_entry adds new
251 * links at the front.
253 entry
= link_entry(3, info
, n
+ 2, entry
);
254 entry
= link_entry(2, info
, n
+ 1, entry
);
255 entry
= link_entry(1, info
, n
+ 0, entry
);
257 add_merge_entry(entry
);
261 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
264 * This walks the (sorted) trees in lock-step, checking every possible
265 * name. Note that directories automatically sort differently from other
266 * files (see "base_name_compare"), so you'll never see file/directory
267 * conflicts, because they won't ever compare the same.
269 * IOW, if a directory changes to a filename, it will automatically be
270 * seen as the directory going away, and the filename being created.
272 * Think of this as a three-way diff.
274 * The output will be either:
276 * "0 mode sha1 filename"
277 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
278 * in parallel with this too!
281 * "1 mode sha1 filename"
282 * "2 mode sha1 filename"
283 * "3 mode sha1 filename"
284 * where not all of the 1/2/3 lines may exist, of course.
286 * The successful merge rules are the same as for the three-way merge
289 static int threeway_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*entry
, struct traverse_info
*info
)
292 if (same_entry(entry
+1, entry
+2)) {
294 resolve(info
, NULL
, entry
+1);
299 if (same_entry(entry
+0, entry
+1)) {
300 if (entry
[2].sha1
&& !S_ISDIR(entry
[2].mode
)) {
301 resolve(info
, entry
+1, entry
+2);
306 if (same_entry(entry
+0, entry
+2)) {
307 if (entry
[1].sha1
&& !S_ISDIR(entry
[1].mode
)) {
308 resolve(info
, NULL
, entry
+1);
313 unresolved(info
, entry
);
317 static void merge_trees(struct tree_desc t
[3], const char *base
)
319 struct traverse_info info
;
321 setup_traverse_info(&info
, base
);
322 info
.fn
= threeway_callback
;
323 traverse_trees(3, t
, &info
);
326 static void *get_tree_descriptor(struct tree_desc
*desc
, const char *rev
)
328 unsigned char sha1
[20];
331 if (get_sha1(rev
, sha1
))
332 die("unknown rev %s", rev
);
333 buf
= fill_tree_descriptor(desc
, sha1
);
335 die("%s is not a tree", rev
);
339 int main(int argc
, char **argv
)
341 struct tree_desc t
[3];
342 void *buf1
, *buf2
, *buf3
;
345 usage(merge_tree_usage
);
347 setup_git_directory();
349 buf1
= get_tree_descriptor(t
+0, argv
[1]);
350 buf2
= get_tree_descriptor(t
+1, argv
[2]);
351 buf3
= get_tree_descriptor(t
+2, argv
[3]);