Add 'df_name_compare()' helper function
[git/git.git] / merge-tree.c
CommitLineData
492e0759 1#include "cache.h"
1b0c7174 2#include "tree-walk.h"
0c799383 3#include "xdiff-interface.h"
83788070 4#include "blob.h"
492e0759
LT
5
6static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
7static int resolve_directories = 1;
8
83788070
LT
9struct merge_list {
10 struct merge_list *next;
11 struct merge_list *link; /* other stages for this object */
12
13 unsigned int stage : 2,
14 flags : 30;
15 unsigned int mode;
16 const char *path;
17 struct blob *blob;
18};
19
20static struct merge_list *merge_result, **merge_result_end = &merge_result;
21
22static void add_merge_entry(struct merge_list *entry)
23{
24 *merge_result_end = entry;
25 merge_result_end = &entry->next;
26}
27
492e0759
LT
28static void merge_trees(struct tree_desc t[3], const char *base);
29
83788070
LT
30static const char *explanation(struct merge_list *entry)
31{
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";
41 }
42
43 /* Existed in base */
44 entry = entry->link;
45 if (!entry)
46 return "removed in both";
47
48 if (entry->link)
49 return "changed in both";
50
51 if (entry->stage == 3)
52 return "removed in local";
53 return "removed in remote";
54}
55
0c799383
LT
56extern void *merge_file(struct blob *, struct blob *, struct blob *, unsigned long *);
57
58static void *result(struct merge_list *entry, unsigned long *size)
59{
21666f1a 60 enum object_type type;
0c799383
LT
61 struct blob *base, *our, *their;
62
63 if (!entry->stage)
21666f1a 64 return read_sha1_file(entry->blob->object.sha1, &type, size);
0c799383
LT
65 base = NULL;
66 if (entry->stage == 1) {
67 base = entry->blob;
68 entry = entry->link;
69 }
70 our = NULL;
71 if (entry && entry->stage == 2) {
72 our = entry->blob;
73 entry = entry->link;
74 }
75 their = NULL;
76 if (entry)
77 their = entry->blob;
78 return merge_file(base, our, their, size);
79}
80
81static void *origin(struct merge_list *entry, unsigned long *size)
82{
21666f1a 83 enum object_type type;
0c799383
LT
84 while (entry) {
85 if (entry->stage == 2)
21666f1a 86 return read_sha1_file(entry->blob->object.sha1, &type, size);
0c799383
LT
87 entry = entry->link;
88 }
89 return NULL;
90}
91
92static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
93{
94 int i;
95 for (i = 0; i < nbuf; i++)
96 printf("%.*s", (int) mb[i].size, mb[i].ptr);
97 return 0;
98}
99
100static void show_diff(struct merge_list *entry)
101{
102 unsigned long size;
103 mmfile_t src, dst;
104 xpparam_t xpp;
105 xdemitconf_t xecfg;
106 xdemitcb_t ecb;
107
108 xpp.flags = XDF_NEED_MINIMAL;
30b25010 109 memset(&xecfg, 0, sizeof(xecfg));
0c799383 110 xecfg.ctxlen = 3;
0c799383
LT
111 ecb.outf = show_outf;
112 ecb.priv = NULL;
113
114 src.ptr = origin(entry, &size);
115 if (!src.ptr)
116 size = 0;
117 src.size = size;
118 dst.ptr = result(entry, &size);
119 if (!dst.ptr)
120 size = 0;
121 dst.size = size;
c279d7e9 122 xdi_diff(&src, &dst, &xpp, &xecfg, &ecb);
0c799383
LT
123 free(src.ptr);
124 free(dst.ptr);
125}
126
83788070
LT
127static void show_result_list(struct merge_list *entry)
128{
129 printf("%s\n", explanation(entry));
130 do {
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);
134 entry = link;
135 } while (entry);
136}
137
138static void show_result(void)
139{
140 struct merge_list *walk;
141
142 walk = merge_result;
143 while (walk) {
144 show_result_list(walk);
0c799383 145 show_diff(walk);
83788070
LT
146 walk = walk->next;
147 }
148}
149
492e0759
LT
150/* An empty entry never compares same, not even to another empty entry */
151static int same_entry(struct name_entry *a, struct name_entry *b)
152{
153 return a->sha1 &&
154 b->sha1 &&
a89fccd2 155 !hashcmp(a->sha1, b->sha1) &&
492e0759
LT
156 a->mode == b->mode;
157}
158
83788070 159static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
492e0759 160{
83788070
LT
161 struct merge_list *res = xmalloc(sizeof(*res));
162
163 memset(res, 0, sizeof(*res));
164 res->stage = stage;
165 res->path = path;
166 res->mode = mode;
167 res->blob = lookup_blob(sha1);
168 return res;
01df5297
LT
169}
170
171static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
172{
83788070
LT
173 struct merge_list *orig, *final;
174 const char *path;
175
01df5297
LT
176 /* If it's already branch1, don't bother showing it */
177 if (!branch1)
178 return;
01df5297 179
9befac47 180 path = xstrdup(mkpath("%s%s", base, result->path));
83788070
LT
181 orig = create_entry(2, branch1->mode, branch1->sha1, path);
182 final = create_entry(0, result->mode, result->sha1, path);
183
184 final->link = orig;
185
186 add_merge_entry(final);
492e0759
LT
187}
188
189static int unresolved_directory(const char *base, struct name_entry n[3])
190{
a8c40471 191 int baselen, pathlen;
492e0759
LT
192 char *newbase;
193 struct name_entry *p;
194 struct tree_desc t[3];
195 void *buf0, *buf1, *buf2;
196
197 if (!resolve_directories)
198 return 0;
199 p = n;
200 if (!p->mode) {
201 p++;
202 if (!p->mode)
203 p++;
204 }
205 if (!S_ISDIR(p->mode))
206 return 0;
207 baselen = strlen(base);
a8c40471
LT
208 pathlen = tree_entry_len(p->path, p->sha1);
209 newbase = xmalloc(baselen + pathlen + 2);
492e0759 210 memcpy(newbase, base, baselen);
a8c40471
LT
211 memcpy(newbase + baselen, p->path, pathlen);
212 memcpy(newbase + baselen + pathlen, "/", 2);
492e0759
LT
213
214 buf0 = fill_tree_descriptor(t+0, n[0].sha1);
215 buf1 = fill_tree_descriptor(t+1, n[1].sha1);
216 buf2 = fill_tree_descriptor(t+2, n[2].sha1);
217 merge_trees(t, newbase);
218
219 free(buf0);
220 free(buf1);
221 free(buf2);
222 free(newbase);
223 return 1;
224}
225
83788070
LT
226
227static struct merge_list *link_entry(unsigned stage, const char *base, struct name_entry *n, struct merge_list *entry)
228{
229 const char *path;
230 struct merge_list *link;
231
232 if (!n->mode)
233 return entry;
234 if (entry)
235 path = entry->path;
236 else
9befac47 237 path = xstrdup(mkpath("%s%s", base, n->path));
83788070
LT
238 link = create_entry(stage, n->mode, n->sha1, path);
239 link->link = entry;
240 return link;
241}
242
492e0759
LT
243static void unresolved(const char *base, struct name_entry n[3])
244{
83788070
LT
245 struct merge_list *entry = NULL;
246
492e0759
LT
247 if (unresolved_directory(base, n))
248 return;
83788070
LT
249
250 /*
251 * Do them in reverse order so that the resulting link
252 * list has the stages in order - link_entry adds new
253 * links at the front.
254 */
255 entry = link_entry(3, base, n + 2, entry);
256 entry = link_entry(2, base, n + 1, entry);
257 entry = link_entry(1, base, n + 0, entry);
258
259 add_merge_entry(entry);
492e0759
LT
260}
261
164dcb97
LT
262/*
263 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
264 * as the origin.
265 *
266 * This walks the (sorted) trees in lock-step, checking every possible
267 * name. Note that directories automatically sort differently from other
268 * files (see "base_name_compare"), so you'll never see file/directory
269 * conflicts, because they won't ever compare the same.
270 *
271 * IOW, if a directory changes to a filename, it will automatically be
272 * seen as the directory going away, and the filename being created.
273 *
274 * Think of this as a three-way diff.
275 *
276 * The output will be either:
277 * - successful merge
278 * "0 mode sha1 filename"
279 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
280 * in parallel with this too!
281 *
282 * - conflict:
283 * "1 mode sha1 filename"
284 * "2 mode sha1 filename"
285 * "3 mode sha1 filename"
286 * where not all of the 1/2/3 lines may exist, of course.
287 *
288 * The successful merge rules are the same as for the three-way merge
289 * in git-read-tree.
290 */
291static void threeway_callback(int n, unsigned long mask, struct name_entry *entry, const char *base)
292{
293 /* Same in both? */
294 if (same_entry(entry+1, entry+2)) {
295 if (entry[0].sha1) {
296 resolve(base, NULL, entry+1);
297 return;
492e0759 298 }
164dcb97 299 }
492e0759 300
164dcb97
LT
301 if (same_entry(entry+0, entry+1)) {
302 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
303 resolve(base, entry+1, entry+2);
304 return;
492e0759 305 }
164dcb97 306 }
492e0759 307
164dcb97
LT
308 if (same_entry(entry+0, entry+2)) {
309 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
310 resolve(base, NULL, entry+1);
311 return;
492e0759 312 }
492e0759 313 }
164dcb97
LT
314
315 unresolved(base, entry);
316}
317
318static void merge_trees(struct tree_desc t[3], const char *base)
319{
320 traverse_trees(3, t, base, threeway_callback);
492e0759
LT
321}
322
323static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
324{
325 unsigned char sha1[20];
326 void *buf;
327
31fff305 328 if (get_sha1(rev, sha1))
492e0759
LT
329 die("unknown rev %s", rev);
330 buf = fill_tree_descriptor(desc, sha1);
331 if (!buf)
332 die("%s is not a tree", rev);
333 return buf;
334}
335
336int main(int argc, char **argv)
337{
338 struct tree_desc t[3];
339 void *buf1, *buf2, *buf3;
340
5b6df8e4 341 if (argc != 4)
492e0759
LT
342 usage(merge_tree_usage);
343
8112894d
DL
344 setup_git_directory();
345
492e0759
LT
346 buf1 = get_tree_descriptor(t+0, argv[1]);
347 buf2 = get_tree_descriptor(t+1, argv[2]);
348 buf3 = get_tree_descriptor(t+2, argv[3]);
349 merge_trees(t, "");
350 free(buf1);
351 free(buf2);
352 free(buf3);
83788070
LT
353
354 show_result();
492e0759
LT
355 return 0;
356}