3 #include "xdiff-interface.h"
4 #include "object-store.h"
5 #include "repository.h"
8 #include "merge-blobs.h"
10 static const char merge_tree_usage
[] = "git merge-tree <base-tree> <branch1> <branch2>";
13 struct merge_list
*next
;
14 struct merge_list
*link
; /* other stages for this object */
16 unsigned int stage
: 2;
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
) {
38 return "added in remote";
41 return "added in both";
42 return "added in local";
48 return "removed in both";
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
;
65 return read_object_file(&entry
->blob
->object
.oid
, &type
, size
);
67 if (entry
->stage
== 1) {
72 if (entry
&& entry
->stage
== 2) {
79 return merge_blobs(&the_index
, path
, base
, our
, their
, size
);
82 static void *origin(struct merge_list
*entry
, unsigned long *size
)
84 enum object_type type
;
86 if (entry
->stage
== 2)
87 return read_object_file(&entry
->blob
->object
.oid
,
94 static int show_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
97 for (i
= 0; i
< nbuf
; i
++)
98 printf("%.*s", (int) mb
[i
].size
, mb
[i
].ptr
);
102 static void show_diff(struct merge_list
*entry
)
111 memset(&xecfg
, 0, sizeof(xecfg
));
114 ecb
.out_line
= show_outf
;
117 src
.ptr
= origin(entry
, &size
);
121 dst
.ptr
= result(entry
, &size
);
125 if (xdi_diff(&src
, &dst
, &xpp
, &xecfg
, &ecb
))
126 die("unable to generate diff");
131 static void show_result_list(struct merge_list
*entry
)
133 printf("%s\n", explanation(entry
));
135 struct merge_list
*link
= entry
->link
;
136 static const char *desc
[4] = { "result", "base", "our", "their" };
137 printf(" %-6s %o %s %s\n", desc
[entry
->stage
], entry
->mode
, oid_to_hex(&entry
->blob
->object
.oid
), entry
->path
);
142 static void show_result(void)
144 struct merge_list
*walk
;
148 show_result_list(walk
);
154 /* An empty entry never compares same, not even to another empty entry */
155 static int same_entry(struct name_entry
*a
, struct name_entry
*b
)
159 oideq(a
->oid
, b
->oid
) &&
163 static int both_empty(struct name_entry
*a
, struct name_entry
*b
)
165 return !(a
->oid
|| b
->oid
);
168 static struct merge_list
*create_entry(unsigned stage
, unsigned mode
, const struct object_id
*oid
, const char *path
)
170 struct merge_list
*res
= xcalloc(1, sizeof(*res
));
175 res
->blob
= lookup_blob(the_repository
, oid
);
179 static char *traverse_path(const struct traverse_info
*info
, const struct name_entry
*n
)
181 char *path
= xmallocz(traverse_path_len(info
, n
));
182 return make_traverse_path(path
, info
, n
);
185 static void resolve(const struct traverse_info
*info
, struct name_entry
*ours
, struct name_entry
*result
)
187 struct merge_list
*orig
, *final
;
190 /* If it's already ours, don't bother showing it */
194 path
= traverse_path(info
, result
);
195 orig
= create_entry(2, ours
->mode
, ours
->oid
, path
);
196 final
= create_entry(0, result
->mode
, result
->oid
, path
);
200 add_merge_entry(final
);
203 static void unresolved_directory(const struct traverse_info
*info
,
204 struct name_entry n
[3])
207 struct name_entry
*p
;
208 struct tree_desc t
[3];
209 void *buf0
, *buf1
, *buf2
;
211 for (p
= n
; p
< n
+ 3; p
++) {
212 if (p
->mode
&& S_ISDIR(p
->mode
))
216 return; /* there is no tree here */
218 newbase
= traverse_path(info
, p
);
220 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
221 buf0
= fill_tree_descriptor(t
+ 0, ENTRY_OID(n
+ 0));
222 buf1
= fill_tree_descriptor(t
+ 1, ENTRY_OID(n
+ 1));
223 buf2
= fill_tree_descriptor(t
+ 2, ENTRY_OID(n
+ 2));
226 merge_trees(t
, newbase
);
235 static struct merge_list
*link_entry(unsigned stage
, const struct traverse_info
*info
, struct name_entry
*n
, struct merge_list
*entry
)
238 struct merge_list
*link
;
245 path
= traverse_path(info
, n
);
246 link
= create_entry(stage
, n
->mode
, n
->oid
, path
);
251 static void unresolved(const struct traverse_info
*info
, struct name_entry n
[3])
253 struct merge_list
*entry
= NULL
;
255 unsigned dirmask
= 0, mask
= 0;
257 for (i
= 0; i
< 3; i
++) {
260 * Treat missing entries as directories so that we return
261 * after unresolved_directory has handled this.
263 if (!n
[i
].mode
|| S_ISDIR(n
[i
].mode
))
267 unresolved_directory(info
, n
);
272 if (n
[2].mode
&& !S_ISDIR(n
[2].mode
))
273 entry
= link_entry(3, info
, n
+ 2, entry
);
274 if (n
[1].mode
&& !S_ISDIR(n
[1].mode
))
275 entry
= link_entry(2, info
, n
+ 1, entry
);
276 if (n
[0].mode
&& !S_ISDIR(n
[0].mode
))
277 entry
= link_entry(1, info
, n
+ 0, entry
);
279 add_merge_entry(entry
);
283 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
286 * This walks the (sorted) trees in lock-step, checking every possible
287 * name. Note that directories automatically sort differently from other
288 * files (see "base_name_compare"), so you'll never see file/directory
289 * conflicts, because they won't ever compare the same.
291 * IOW, if a directory changes to a filename, it will automatically be
292 * seen as the directory going away, and the filename being created.
294 * Think of this as a three-way diff.
296 * The output will be either:
298 * "0 mode sha1 filename"
299 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
300 * in parallel with this too!
303 * "1 mode sha1 filename"
304 * "2 mode sha1 filename"
305 * "3 mode sha1 filename"
306 * where not all of the 1/2/3 lines may exist, of course.
308 * The successful merge rules are the same as for the three-way merge
311 static int threeway_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*entry
, struct traverse_info
*info
)
314 if (same_entry(entry
+1, entry
+2) || both_empty(entry
+1, entry
+2)) {
315 /* Modified, added or removed identically */
316 resolve(info
, NULL
, entry
+1);
320 if (same_entry(entry
+0, entry
+1)) {
321 if (entry
[2].oid
&& !S_ISDIR(entry
[2].mode
)) {
322 /* We did not touch, they modified -- take theirs */
323 resolve(info
, entry
+1, entry
+2);
327 * If we did not touch a directory but they made it
328 * into a file, we fall through and unresolved()
329 * recurses down. Likewise for the opposite case.
333 if (same_entry(entry
+0, entry
+2) || both_empty(entry
+0, entry
+2)) {
334 /* We added, modified or removed, they did not touch -- take ours */
335 resolve(info
, NULL
, entry
+1);
339 unresolved(info
, entry
);
343 static void merge_trees(struct tree_desc t
[3], const char *base
)
345 struct traverse_info info
;
347 setup_traverse_info(&info
, base
);
348 info
.fn
= threeway_callback
;
349 traverse_trees(3, t
, &info
);
352 static void *get_tree_descriptor(struct tree_desc
*desc
, const char *rev
)
354 struct object_id oid
;
357 if (get_oid(rev
, &oid
))
358 die("unknown rev %s", rev
);
359 buf
= fill_tree_descriptor(desc
, &oid
);
361 die("%s is not a tree", rev
);
365 int cmd_merge_tree(int argc
, const char **argv
, const char *prefix
)
367 struct tree_desc t
[3];
368 void *buf1
, *buf2
, *buf3
;
371 usage(merge_tree_usage
);
373 buf1
= get_tree_descriptor(t
+0, argv
[1]);
374 buf2
= get_tree_descriptor(t
+1, argv
[2]);
375 buf3
= get_tree_descriptor(t
+2, argv
[3]);