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