1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "tree-walk.h"
4 #include "xdiff-interface.h"
5 #include "object-store.h"
6 #include "repository.h"
7 #include "blob.h"
8 #include "exec-cmd.h"
9 #include "merge-blobs.h"
10 
11 static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
12 
13 struct merge_list {
14 	struct merge_list *next;
15 	struct merge_list *link;	/* other stages for this object */
16 
17 	unsigned int stage : 2;
18 	unsigned int mode;
19 	const char *path;
20 	struct blob *blob;
21 };
22 
23 static struct merge_list *merge_result, **merge_result_end = &merge_result;
24 
add_merge_entry(struct merge_list * entry)25 static void add_merge_entry(struct merge_list *entry)
26 {
27 	*merge_result_end = entry;
28 	merge_result_end = &entry->next;
29 }
30 
31 static void merge_trees(struct tree_desc t[3], const char *base);
32 
explanation(struct merge_list * entry)33 static const char *explanation(struct merge_list *entry)
34 {
35 	switch (entry->stage) {
36 	case 0:
37 		return "merged";
38 	case 3:
39 		return "added in remote";
40 	case 2:
41 		if (entry->link)
42 			return "added in both";
43 		return "added in local";
44 	}
45 
46 	/* Existed in base */
47 	entry = entry->link;
48 	if (!entry)
49 		return "removed in both";
50 
51 	if (entry->link)
52 		return "changed in both";
53 
54 	if (entry->stage == 3)
55 		return "removed in local";
56 	return "removed in remote";
57 }
58 
result(struct merge_list * entry,unsigned long * size)59 static void *result(struct merge_list *entry, unsigned long *size)
60 {
61 	enum object_type type;
62 	struct blob *base, *our, *their;
63 	const char *path = entry->path;
64 
65 	if (!entry->stage)
66 		return read_object_file(&entry->blob->object.oid, &type, size);
67 	base = NULL;
68 	if (entry->stage == 1) {
69 		base = entry->blob;
70 		entry = entry->link;
71 	}
72 	our = NULL;
73 	if (entry && entry->stage == 2) {
74 		our = entry->blob;
75 		entry = entry->link;
76 	}
77 	their = NULL;
78 	if (entry)
79 		their = entry->blob;
80 	return merge_blobs(the_repository->index, path,
81 			   base, our, their, size);
82 }
83 
origin(struct merge_list * entry,unsigned long * size)84 static void *origin(struct merge_list *entry, unsigned long *size)
85 {
86 	enum object_type type;
87 	while (entry) {
88 		if (entry->stage == 2)
89 			return read_object_file(&entry->blob->object.oid,
90 						&type, size);
91 		entry = entry->link;
92 	}
93 	return NULL;
94 }
95 
show_outf(void * priv_,mmbuffer_t * mb,int nbuf)96 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
97 {
98 	int i;
99 	for (i = 0; i < nbuf; i++)
100 		printf("%.*s", (int) mb[i].size, mb[i].ptr);
101 	return 0;
102 }
103 
show_diff(struct merge_list * entry)104 static void show_diff(struct merge_list *entry)
105 {
106 	unsigned long size;
107 	mmfile_t src, dst;
108 	xpparam_t xpp;
109 	xdemitconf_t xecfg;
110 	xdemitcb_t ecb = { .out_line = show_outf };
111 
112 	memset(&xpp, 0, sizeof(xpp));
113 	xpp.flags = 0;
114 	memset(&xecfg, 0, sizeof(xecfg));
115 	xecfg.ctxlen = 3;
116 
117 	src.ptr = origin(entry, &size);
118 	if (!src.ptr)
119 		size = 0;
120 	src.size = size;
121 	dst.ptr = result(entry, &size);
122 	if (!dst.ptr)
123 		size = 0;
124 	dst.size = size;
125 	if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
126 		die("unable to generate diff");
127 	free(src.ptr);
128 	free(dst.ptr);
129 }
130 
show_result_list(struct merge_list * entry)131 static void show_result_list(struct merge_list *entry)
132 {
133 	printf("%s\n", explanation(entry));
134 	do {
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);
138 		entry = link;
139 	} while (entry);
140 }
141 
show_result(void)142 static void show_result(void)
143 {
144 	struct merge_list *walk;
145 
146 	walk = merge_result;
147 	while (walk) {
148 		show_result_list(walk);
149 		show_diff(walk);
150 		walk = walk->next;
151 	}
152 }
153 
154 /* An empty entry never compares same, not even to another empty entry */
same_entry(struct name_entry * a,struct name_entry * b)155 static int same_entry(struct name_entry *a, struct name_entry *b)
156 {
157 	return	!is_null_oid(&a->oid) &&
158 		!is_null_oid(&b->oid) &&
159 		oideq(&a->oid, &b->oid) &&
160 		a->mode == b->mode;
161 }
162 
both_empty(struct name_entry * a,struct name_entry * b)163 static int both_empty(struct name_entry *a, struct name_entry *b)
164 {
165 	return is_null_oid(&a->oid) && is_null_oid(&b->oid);
166 }
167 
create_entry(unsigned stage,unsigned mode,const struct object_id * oid,const char * path)168 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
169 {
170 	struct merge_list *res = xcalloc(1, sizeof(*res));
171 
172 	res->stage = stage;
173 	res->path = path;
174 	res->mode = mode;
175 	res->blob = lookup_blob(the_repository, oid);
176 	return res;
177 }
178 
traverse_path(const struct traverse_info * info,const struct name_entry * n)179 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
180 {
181 	struct strbuf buf = STRBUF_INIT;
182 	strbuf_make_traverse_path(&buf, info, n->path, n->pathlen);
183 	return strbuf_detach(&buf, NULL);
184 }
185 
resolve(const struct traverse_info * info,struct name_entry * ours,struct name_entry * result)186 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
187 {
188 	struct merge_list *orig, *final;
189 	const char *path;
190 
191 	/* If it's already ours, don't bother showing it */
192 	if (!ours)
193 		return;
194 
195 	path = traverse_path(info, result);
196 	orig = create_entry(2, ours->mode, &ours->oid, path);
197 	final = create_entry(0, result->mode, &result->oid, path);
198 
199 	final->link = orig;
200 
201 	add_merge_entry(final);
202 }
203 
unresolved_directory(const struct traverse_info * info,struct name_entry n[3])204 static void unresolved_directory(const struct traverse_info *info,
205 				 struct name_entry n[3])
206 {
207 	struct repository *r = the_repository;
208 	char *newbase;
209 	struct name_entry *p;
210 	struct tree_desc t[3];
211 	void *buf0, *buf1, *buf2;
212 
213 	for (p = n; p < n + 3; p++) {
214 		if (p->mode && S_ISDIR(p->mode))
215 			break;
216 	}
217 	if (n + 3 <= p)
218 		return; /* there is no tree here */
219 
220 	newbase = traverse_path(info, p);
221 
222 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
223 	buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
224 	buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
225 	buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
226 #undef ENTRY_OID
227 
228 	merge_trees(t, newbase);
229 
230 	free(buf0);
231 	free(buf1);
232 	free(buf2);
233 	free(newbase);
234 }
235 
236 
link_entry(unsigned stage,const struct traverse_info * info,struct name_entry * n,struct merge_list * entry)237 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
238 {
239 	const char *path;
240 	struct merge_list *link;
241 
242 	if (!n->mode)
243 		return entry;
244 	if (entry)
245 		path = entry->path;
246 	else
247 		path = traverse_path(info, n);
248 	link = create_entry(stage, n->mode, &n->oid, path);
249 	link->link = entry;
250 	return link;
251 }
252 
unresolved(const struct traverse_info * info,struct name_entry n[3])253 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
254 {
255 	struct merge_list *entry = NULL;
256 	int i;
257 	unsigned dirmask = 0, mask = 0;
258 
259 	for (i = 0; i < 3; i++) {
260 		mask |= (1 << i);
261 		/*
262 		 * Treat missing entries as directories so that we return
263 		 * after unresolved_directory has handled this.
264 		 */
265 		if (!n[i].mode || S_ISDIR(n[i].mode))
266 			dirmask |= (1 << i);
267 	}
268 
269 	unresolved_directory(info, n);
270 
271 	if (dirmask == mask)
272 		return;
273 
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);
280 
281 	add_merge_entry(entry);
282 }
283 
284 /*
285  * Merge two trees together (t[1] and t[2]), using a common base (t[0])
286  * as the origin.
287  *
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.
292  *
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.
295  *
296  * Think of this as a three-way diff.
297  *
298  * The output will be either:
299  *  - successful merge
300  *	 "0 mode sha1 filename"
301  *    NOTE NOTE NOTE! FIXME! We really really need to walk the index
302  *    in parallel with this too!
303  *
304  *  - conflict:
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.
309  *
310  * The successful merge rules are the same as for the three-way merge
311  * in git-read-tree.
312  */
threeway_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry * entry,struct traverse_info * info)313 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
314 {
315 	/* Same in both? */
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);
319 		return mask;
320 	}
321 
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);
326 			return mask;
327 		}
328 		/*
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.
332 		 */
333 	}
334 
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);
338 		return mask;
339 	}
340 
341 	unresolved(info, entry);
342 	return mask;
343 }
344 
merge_trees(struct tree_desc t[3],const char * base)345 static void merge_trees(struct tree_desc t[3], const char *base)
346 {
347 	struct traverse_info info;
348 
349 	setup_traverse_info(&info, base);
350 	info.fn = threeway_callback;
351 	traverse_trees(&the_index, 3, t, &info);
352 }
353 
get_tree_descriptor(struct repository * r,struct tree_desc * desc,const char * rev)354 static void *get_tree_descriptor(struct repository *r,
355 				 struct tree_desc *desc,
356 				 const char *rev)
357 {
358 	struct object_id oid;
359 	void *buf;
360 
361 	if (repo_get_oid(r, rev, &oid))
362 		die("unknown rev %s", rev);
363 	buf = fill_tree_descriptor(r, desc, &oid);
364 	if (!buf)
365 		die("%s is not a tree", rev);
366 	return buf;
367 }
368 
cmd_merge_tree(int argc,const char ** argv,const char * prefix)369 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
370 {
371 	struct repository *r = the_repository;
372 	struct tree_desc t[3];
373 	void *buf1, *buf2, *buf3;
374 
375 	if (argc != 4)
376 		usage(merge_tree_usage);
377 
378 	buf1 = get_tree_descriptor(r, t+0, argv[1]);
379 	buf2 = get_tree_descriptor(r, t+1, argv[2]);
380 	buf3 = get_tree_descriptor(r, t+2, argv[3]);
381 	merge_trees(t, "");
382 	free(buf1);
383 	free(buf2);
384 	free(buf3);
385 
386 	show_result();
387 	return 0;
388 }
389