]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/merge-tree.c
Merge branch 'ps/reftable-binsearch-updates'
[thirdparty/git.git] / builtin / merge-tree.c
CommitLineData
666f53eb 1#define USE_THE_INDEX_VARIABLE
c2e86add 2#include "builtin.h"
1b0c7174 3#include "tree-walk.h"
0c799383 4#include "xdiff-interface.h"
1f0c3a29 5#include "help.h"
f394e093 6#include "gettext.h"
41771fa4 7#include "hex.h"
66265a69 8#include "commit.h"
1f0c3a29
EN
9#include "commit-reach.h"
10#include "merge-ort.h"
dabab1d6 11#include "object-name.h"
a034e910 12#include "object-store-ll.h"
6ec755a0 13#include "parse-options.h"
da14a7ff 14#include "repository.h"
83788070 15#include "blob.h"
fa2364ec 16#include "merge-blobs.h"
7fa33388 17#include "quote.h"
d4a4f929 18#include "tree.h"
b6551fea 19#include "config.h"
6a4c9e7b 20#include "strvec.h"
7fa33388
EN
21
22static int line_termination = '\n';
492e0759 23
83788070
LT
24struct merge_list {
25 struct merge_list *next;
26 struct merge_list *link; /* other stages for this object */
27
b13112fa 28 unsigned int stage : 2;
83788070
LT
29 unsigned int mode;
30 const char *path;
31 struct blob *blob;
32};
33
34static struct merge_list *merge_result, **merge_result_end = &merge_result;
35
36static void add_merge_entry(struct merge_list *entry)
37{
38 *merge_result_end = entry;
39 merge_result_end = &entry->next;
40}
41
70176b70 42static void trivial_merge_trees(struct tree_desc t[3], const char *base);
492e0759 43
83788070
LT
44static const char *explanation(struct merge_list *entry)
45{
46 switch (entry->stage) {
47 case 0:
48 return "merged";
49 case 3:
50 return "added in remote";
51 case 2:
52 if (entry->link)
53 return "added in both";
54 return "added in local";
55 }
56
57 /* Existed in base */
58 entry = entry->link;
59 if (!entry)
60 return "removed in both";
61
62 if (entry->link)
63 return "changed in both";
64
65 if (entry->stage == 3)
66 return "removed in local";
67 return "removed in remote";
68}
69
0c799383
LT
70static void *result(struct merge_list *entry, unsigned long *size)
71{
21666f1a 72 enum object_type type;
0c799383 73 struct blob *base, *our, *their;
21baa6e0 74 const char *path = entry->path;
0c799383
LT
75
76 if (!entry->stage)
bc726bd0
ÆAB
77 return repo_read_object_file(the_repository,
78 &entry->blob->object.oid, &type,
79 size);
0c799383
LT
80 base = NULL;
81 if (entry->stage == 1) {
82 base = entry->blob;
83 entry = entry->link;
84 }
85 our = NULL;
86 if (entry && entry->stage == 2) {
87 our = entry->blob;
88 entry = entry->link;
89 }
90 their = NULL;
91 if (entry)
92 their = entry->blob;
f8adbec9
NTND
93 return merge_blobs(the_repository->index, path,
94 base, our, their, size);
0c799383
LT
95}
96
97static void *origin(struct merge_list *entry, unsigned long *size)
98{
21666f1a 99 enum object_type type;
0c799383
LT
100 while (entry) {
101 if (entry->stage == 2)
bc726bd0
ÆAB
102 return repo_read_object_file(the_repository,
103 &entry->blob->object.oid,
104 &type, size);
0c799383
LT
105 entry = entry->link;
106 }
107 return NULL;
108}
109
61bdc7c5 110static int show_outf(void *priv UNUSED, mmbuffer_t *mb, int nbuf)
0c799383
LT
111{
112 int i;
113 for (i = 0; i < nbuf; i++)
114 printf("%.*s", (int) mb[i].size, mb[i].ptr);
115 return 0;
116}
117
118static void show_diff(struct merge_list *entry)
119{
120 unsigned long size;
121 mmfile_t src, dst;
122 xpparam_t xpp;
123 xdemitconf_t xecfg;
22233d43 124 xdemitcb_t ecb = { .out_line = show_outf };
0c799383 125
ec7967cf 126 memset(&xpp, 0, sizeof(xpp));
582aa00b 127 xpp.flags = 0;
30b25010 128 memset(&xecfg, 0, sizeof(xecfg));
0c799383 129 xecfg.ctxlen = 3;
0c799383
LT
130
131 src.ptr = origin(entry, &size);
132 if (!src.ptr)
133 size = 0;
134 src.size = size;
135 dst.ptr = result(entry, &size);
136 if (!dst.ptr)
137 size = 0;
138 dst.size = size;
3efb9880
JK
139 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
140 die("unable to generate diff");
0c799383
LT
141 free(src.ptr);
142 free(dst.ptr);
143}
144
83788070
LT
145static void show_result_list(struct merge_list *entry)
146{
147 printf("%s\n", explanation(entry));
148 do {
149 struct merge_list *link = entry->link;
150 static const char *desc[4] = { "result", "base", "our", "their" };
f2fd0760 151 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
83788070
LT
152 entry = link;
153 } while (entry);
154}
155
156static void show_result(void)
157{
158 struct merge_list *walk;
159
160 walk = merge_result;
161 while (walk) {
162 show_result_list(walk);
0c799383 163 show_diff(walk);
83788070
LT
164 walk = walk->next;
165 }
166}
167
492e0759
LT
168/* An empty entry never compares same, not even to another empty entry */
169static int same_entry(struct name_entry *a, struct name_entry *b)
170{
ea82b2a0 171 return !is_null_oid(&a->oid) &&
172 !is_null_oid(&b->oid) &&
173 oideq(&a->oid, &b->oid) &&
492e0759
LT
174 a->mode == b->mode;
175}
176
aacecc3b
JK
177static int both_empty(struct name_entry *a, struct name_entry *b)
178{
ea82b2a0 179 return is_null_oid(&a->oid) && is_null_oid(&b->oid);
aacecc3b
JK
180}
181
3e930981 182static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
492e0759 183{
19d4b416 184 struct merge_list *res = xcalloc(1, sizeof(*res));
83788070 185
83788070
LT
186 res->stage = stage;
187 res->path = path;
188 res->mode = mode;
da14a7ff 189 res->blob = lookup_blob(the_repository, oid);
83788070 190 return res;
01df5297
LT
191}
192
40d934df
LT
193static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
194{
c43ab062
JK
195 struct strbuf buf = STRBUF_INIT;
196 strbuf_make_traverse_path(&buf, info, n->path, n->pathlen);
197 return strbuf_detach(&buf, NULL);
40d934df
LT
198}
199
8dd15c6a 200static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
01df5297 201{
83788070
LT
202 struct merge_list *orig, *final;
203 const char *path;
204
8dd15c6a
JH
205 /* If it's already ours, don't bother showing it */
206 if (!ours)
01df5297 207 return;
01df5297 208
40d934df 209 path = traverse_path(info, result);
ea82b2a0 210 orig = create_entry(2, ours->mode, &ours->oid, path);
211 final = create_entry(0, result->mode, &result->oid, path);
83788070
LT
212
213 final->link = orig;
214
215 add_merge_entry(final);
492e0759
LT
216}
217
57065289
RS
218static void unresolved_directory(const struct traverse_info *info,
219 struct name_entry n[3])
492e0759 220{
5e575807 221 struct repository *r = the_repository;
492e0759
LT
222 char *newbase;
223 struct name_entry *p;
224 struct tree_desc t[3];
225 void *buf0, *buf1, *buf2;
226
35ffe758
JH
227 for (p = n; p < n + 3; p++) {
228 if (p->mode && S_ISDIR(p->mode))
229 break;
492e0759 230 }
35ffe758
JH
231 if (n + 3 <= p)
232 return; /* there is no tree here */
8dd15c6a 233
40d934df 234 newbase = traverse_path(info, p);
35ffe758 235
ea82b2a0 236#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
5e575807
NTND
237 buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
238 buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
239 buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
5c377d3d 240#undef ENTRY_OID
35ffe758 241
70176b70 242 trivial_merge_trees(t, newbase);
492e0759
LT
243
244 free(buf0);
245 free(buf1);
246 free(buf2);
247 free(newbase);
492e0759
LT
248}
249
83788070 250
40d934df 251static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
83788070
LT
252{
253 const char *path;
254 struct merge_list *link;
255
256 if (!n->mode)
257 return entry;
258 if (entry)
259 path = entry->path;
260 else
40d934df 261 path = traverse_path(info, n);
ea82b2a0 262 link = create_entry(stage, n->mode, &n->oid, path);
83788070
LT
263 link->link = entry;
264 return link;
265}
266
40d934df 267static void unresolved(const struct traverse_info *info, struct name_entry n[3])
492e0759 268{
83788070 269 struct merge_list *entry = NULL;
35ffe758
JH
270 int i;
271 unsigned dirmask = 0, mask = 0;
272
273 for (i = 0; i < 3; i++) {
187c00c6 274 mask |= (1 << i);
94883b43
JK
275 /*
276 * Treat missing entries as directories so that we return
277 * after unresolved_directory has handled this.
278 */
279 if (!n[i].mode || S_ISDIR(n[i].mode))
35ffe758
JH
280 dirmask |= (1 << i);
281 }
282
57065289 283 unresolved_directory(info, n);
83788070 284
35ffe758 285 if (dirmask == mask)
492e0759 286 return;
83788070 287
35ffe758
JH
288 if (n[2].mode && !S_ISDIR(n[2].mode))
289 entry = link_entry(3, info, n + 2, entry);
290 if (n[1].mode && !S_ISDIR(n[1].mode))
291 entry = link_entry(2, info, n + 1, entry);
292 if (n[0].mode && !S_ISDIR(n[0].mode))
293 entry = link_entry(1, info, n + 0, entry);
83788070
LT
294
295 add_merge_entry(entry);
492e0759
LT
296}
297
164dcb97
LT
298/*
299 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
300 * as the origin.
301 *
302 * This walks the (sorted) trees in lock-step, checking every possible
303 * name. Note that directories automatically sort differently from other
304 * files (see "base_name_compare"), so you'll never see file/directory
305 * conflicts, because they won't ever compare the same.
306 *
307 * IOW, if a directory changes to a filename, it will automatically be
308 * seen as the directory going away, and the filename being created.
309 *
310 * Think of this as a three-way diff.
311 *
312 * The output will be either:
313 * - successful merge
314 * "0 mode sha1 filename"
315 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
316 * in parallel with this too!
317 *
318 * - conflict:
319 * "1 mode sha1 filename"
320 * "2 mode sha1 filename"
321 * "3 mode sha1 filename"
322 * where not all of the 1/2/3 lines may exist, of course.
323 *
324 * The successful merge rules are the same as for the three-way merge
325 * in git-read-tree.
326 */
ee550abc
JK
327static int threeway_callback(int n UNUSED, unsigned long mask,
328 unsigned long dirmask UNUSED,
329 struct name_entry *entry, struct traverse_info *info)
164dcb97
LT
330{
331 /* Same in both? */
ab5f4242 332 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
aacecc3b
JK
333 /* Modified, added or removed identically */
334 resolve(info, NULL, entry+1);
335 return mask;
164dcb97 336 }
492e0759 337
164dcb97 338 if (same_entry(entry+0, entry+1)) {
ea82b2a0 339 if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
8dd15c6a 340 /* We did not touch, they modified -- take theirs */
40d934df 341 resolve(info, entry+1, entry+2);
5803c6f8 342 return mask;
492e0759 343 }
8dd15c6a
JH
344 /*
345 * If we did not touch a directory but they made it
346 * into a file, we fall through and unresolved()
347 * recurses down. Likewise for the opposite case.
348 */
164dcb97 349 }
492e0759 350
aacecc3b
JK
351 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
352 /* We added, modified or removed, they did not touch -- take ours */
353 resolve(info, NULL, entry+1);
354 return mask;
492e0759 355 }
164dcb97 356
40d934df 357 unresolved(info, entry);
5803c6f8 358 return mask;
164dcb97
LT
359}
360
70176b70 361static void trivial_merge_trees(struct tree_desc t[3], const char *base)
164dcb97 362{
40d934df
LT
363 struct traverse_info info;
364
365 setup_traverse_info(&info, base);
366 info.fn = threeway_callback;
67022e02 367 traverse_trees(&the_index, 3, t, &info);
492e0759
LT
368}
369
5e575807
NTND
370static void *get_tree_descriptor(struct repository *r,
371 struct tree_desc *desc,
372 const char *rev)
492e0759 373{
d1a35e5c 374 struct object_id oid;
492e0759
LT
375 void *buf;
376
5e575807 377 if (repo_get_oid(r, rev, &oid))
492e0759 378 die("unknown rev %s", rev);
5e575807 379 buf = fill_tree_descriptor(r, desc, &oid);
492e0759
LT
380 if (!buf)
381 die("%s is not a tree", rev);
382 return buf;
383}
384
6ec755a0
EN
385static int trivial_merge(const char *base,
386 const char *branch1,
387 const char *branch2)
492e0759 388{
5e575807 389 struct repository *r = the_repository;
492e0759
LT
390 struct tree_desc t[3];
391 void *buf1, *buf2, *buf3;
392
6ec755a0
EN
393 buf1 = get_tree_descriptor(r, t+0, base);
394 buf2 = get_tree_descriptor(r, t+1, branch1);
395 buf3 = get_tree_descriptor(r, t+2, branch2);
70176b70 396 trivial_merge_trees(t, "");
492e0759
LT
397 free(buf1);
398 free(buf2);
399 free(buf3);
83788070
LT
400
401 show_result();
492e0759
LT
402 return 0;
403}
55e48f6b 404
6ec755a0
EN
405enum mode {
406 MODE_UNKNOWN,
407 MODE_TRIVIAL,
408 MODE_REAL,
409};
410
411struct merge_tree_options {
412 int mode;
7976721d 413 int allow_unrelated_histories;
a1a78119 414 int show_messages;
b520bc6c 415 int name_only;
ec1edbcb 416 int use_stdin;
6a4c9e7b 417 struct merge_options merge_options;
6ec755a0
EN
418};
419
420static int real_merge(struct merge_tree_options *o,
66265a69 421 const char *merge_base,
7fa33388
EN
422 const char *branch1, const char *branch2,
423 const char *prefix)
6ec755a0 424{
1f0c3a29
EN
425 struct commit *parent1, *parent2;
426 struct commit_list *merge_bases = NULL;
1f0c3a29 427 struct merge_result result = { 0 };
ec1edbcb 428 int show_messages = o->show_messages;
b182658e 429 struct merge_options opt;
1f0c3a29 430
b182658e 431 copy_merge_options(&opt, &o->merge_options);
1f0c3a29
EN
432 opt.show_rename_progress = 0;
433
434 opt.branch1 = branch1;
435 opt.branch2 = branch2;
436
66265a69 437 if (merge_base) {
66265a69
KZ
438 struct tree *base_tree, *parent1_tree, *parent2_tree;
439
5f43cf5b
JS
440 /*
441 * We actually only need the trees because we already
442 * have a merge base.
443 */
444 struct object_id base_oid, head_oid, merge_oid;
445
446 if (repo_get_oid_treeish(the_repository, merge_base, &base_oid))
447 die(_("could not parse as tree '%s'"), merge_base);
448 base_tree = parse_tree_indirect(&base_oid);
aa9f6189
JS
449 if (!base_tree)
450 die(_("unable to read tree (%s)"), oid_to_hex(&base_oid));
5f43cf5b
JS
451 if (repo_get_oid_treeish(the_repository, branch1, &head_oid))
452 die(_("could not parse as tree '%s'"), branch1);
453 parent1_tree = parse_tree_indirect(&head_oid);
aa9f6189
JS
454 if (!parent1_tree)
455 die(_("unable to read tree (%s)"), oid_to_hex(&head_oid));
5f43cf5b
JS
456 if (repo_get_oid_treeish(the_repository, branch2, &merge_oid))
457 die(_("could not parse as tree '%s'"), branch2);
458 parent2_tree = parse_tree_indirect(&merge_oid);
aa9f6189
JS
459 if (!parent2_tree)
460 die(_("unable to read tree (%s)"), oid_to_hex(&merge_oid));
66265a69
KZ
461
462 opt.ancestor = merge_base;
66265a69
KZ
463 merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
464 } else {
5f43cf5b
JS
465 parent1 = get_merge_parent(branch1);
466 if (!parent1)
467 help_unknown_ref(branch1, "merge-tree",
468 _("not something we can merge"));
469
470 parent2 = get_merge_parent(branch2);
471 if (!parent2)
472 help_unknown_ref(branch2, "merge-tree",
473 _("not something we can merge"));
474
66265a69
KZ
475 /*
476 * Get the merge bases, in reverse order; see comment above
477 * merge_incore_recursive in merge-ort.h
478 */
76e2a099
JS
479 if (repo_get_merge_bases(the_repository, parent1,
480 parent2, &merge_bases) < 0)
481 exit(128);
66265a69
KZ
482 if (!merge_bases && !o->allow_unrelated_histories)
483 die(_("refusing to merge unrelated histories"));
484 merge_bases = reverse_commit_list(merge_bases);
485 merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
486 }
1f0c3a29 487
1f0c3a29
EN
488 if (result.clean < 0)
489 die(_("failure to merge"));
a1a78119 490
ec1edbcb
EN
491 if (show_messages == -1)
492 show_messages = !result.clean;
a1a78119 493
ec1edbcb
EN
494 if (o->use_stdin)
495 printf("%d%c", result.clean, line_termination);
7c48b278 496 printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination);
7fa33388
EN
497 if (!result.clean) {
498 struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
499 const char *last = NULL;
500 int i;
501
502 merge_get_conflicted_files(&result, &conflicted_files);
503 for (i = 0; i < conflicted_files.nr; i++) {
504 const char *name = conflicted_files.items[i].string;
b520bc6c
EN
505 struct stage_info *c = conflicted_files.items[i].util;
506 if (!o->name_only)
507 printf("%06o %s %d\t",
508 c->mode, oid_to_hex(&c->oid), c->stage);
509 else if (last && !strcmp(last, name))
7fa33388
EN
510 continue;
511 write_name_quoted_relative(
512 name, prefix, stdout, line_termination);
513 last = name;
514 }
515 string_list_clear(&conflicted_files, 1);
516 }
ec1edbcb 517 if (show_messages) {
7fa33388 518 putchar(line_termination);
de905811
EN
519 merge_display_update_messages(&opt, line_termination == '\0',
520 &result);
a1a78119 521 }
ec1edbcb
EN
522 if (o->use_stdin)
523 putchar(line_termination);
1f0c3a29 524 merge_finalize(&opt, &result);
b182658e 525 clear_merge_options(&opt);
1f0c3a29 526 return !result.clean; /* result.clean < 0 handled above */
6ec755a0
EN
527}
528
55e48f6b
EN
529int cmd_merge_tree(int argc, const char **argv, const char *prefix)
530{
a1a78119 531 struct merge_tree_options o = { .show_messages = -1 };
6a4c9e7b 532 struct strvec xopts = STRVEC_INIT;
6ec755a0 533 int expected_remaining_argc;
a1a78119 534 int original_argc;
66265a69 535 const char *merge_base = NULL;
6ec755a0
EN
536
537 const char * const merge_tree_usage[] = {
a1a78119 538 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
6ec755a0
EN
539 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
540 NULL
541 };
542 struct option mt_options[] = {
543 OPT_CMDMODE(0, "write-tree", &o.mode,
544 N_("do a real merge instead of a trivial merge"),
545 MODE_REAL),
546 OPT_CMDMODE(0, "trivial-merge", &o.mode,
547 N_("do a trivial merge only"), MODE_TRIVIAL),
a1a78119
EN
548 OPT_BOOL(0, "messages", &o.show_messages,
549 N_("also show informational/conflict messages")),
7c48b278
EN
550 OPT_SET_INT('z', NULL, &line_termination,
551 N_("separate paths with the NUL character"), '\0'),
b520bc6c
EN
552 OPT_BOOL_F(0, "name-only",
553 &o.name_only,
554 N_("list filenames without modes/oids/stages"),
555 PARSE_OPT_NONEG),
7976721d
EN
556 OPT_BOOL_F(0, "allow-unrelated-histories",
557 &o.allow_unrelated_histories,
558 N_("allow merging unrelated histories"),
559 PARSE_OPT_NONEG),
ec1edbcb
EN
560 OPT_BOOL_F(0, "stdin",
561 &o.use_stdin,
562 N_("perform multiple merges, one per line of input"),
563 PARSE_OPT_NONEG),
66265a69
KZ
564 OPT_STRING(0, "merge-base",
565 &merge_base,
0c6ee971 566 N_("tree-ish"),
66265a69 567 N_("specify a merge-base for the merge")),
6a4c9e7b
TY
568 OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"),
569 N_("option for selected merge strategy")),
6ec755a0
EN
570 OPT_END()
571 };
572
6a4c9e7b
TY
573 /* Init merge options */
574 init_merge_options(&o.merge_options, the_repository);
575
6ec755a0 576 /* Parse arguments */
a1a78119 577 original_argc = argc - 1; /* ignoring argv[0] */
6ec755a0
EN
578 argc = parse_options(argc, argv, prefix, mt_options,
579 merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
ec1edbcb 580
6a4c9e7b
TY
581 if (xopts.nr && o.mode == MODE_TRIVIAL)
582 die(_("--trivial-merge is incompatible with all other options"));
583 for (int x = 0; x < xopts.nr; x++)
584 if (parse_merge_opt(&o.merge_options, xopts.v[x]))
585 die(_("unknown strategy option: -X%s"), xopts.v[x]);
586
ec1edbcb
EN
587 /* Handle --stdin */
588 if (o.use_stdin) {
589 struct strbuf buf = STRBUF_INIT;
590
591 if (o.mode == MODE_TRIVIAL)
592 die(_("--trivial-merge is incompatible with all other options"));
66265a69 593 if (merge_base)
7854bf49
RS
594 die(_("options '%s' and '%s' cannot be used together"),
595 "--merge-base", "--stdin");
ec1edbcb
EN
596 line_termination = '\0';
597 while (strbuf_getline_lf(&buf, stdin) != EOF) {
598 struct strbuf **split;
599 int result;
501e3bab 600 const char *input_merge_base = NULL;
ec1edbcb
EN
601
602 split = strbuf_split(&buf, ' ');
501e3bab 603 if (!split[0] || !split[1])
ec1edbcb
EN
604 die(_("malformed input line: '%s'."), buf.buf);
605 strbuf_rtrim(split[0]);
501e3bab
KZ
606 strbuf_rtrim(split[1]);
607
608 /* parse the merge-base */
609 if (!strcmp(split[1]->buf, "--")) {
610 input_merge_base = split[0]->buf;
611 }
612
613 if (input_merge_base && split[2] && split[3] && !split[4]) {
614 strbuf_rtrim(split[2]);
615 strbuf_rtrim(split[3]);
616 result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
617 } else if (!input_merge_base && !split[2]) {
618 result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
619 } else {
620 die(_("malformed input line: '%s'."), buf.buf);
621 }
622
ec1edbcb
EN
623 if (result < 0)
624 die(_("merging cannot continue; got unclean result of %d"), result);
625 strbuf_list_free(split);
626 }
627 strbuf_release(&buf);
628 return 0;
629 }
630
631 /* Figure out which mode to use */
6ec755a0
EN
632 switch (o.mode) {
633 default:
634 BUG("unexpected command mode %d", o.mode);
635 case MODE_UNKNOWN:
636 switch (argc) {
637 default:
638 usage_with_options(merge_tree_usage, mt_options);
639 case 2:
640 o.mode = MODE_REAL;
641 break;
642 case 3:
643 o.mode = MODE_TRIVIAL;
644 break;
645 }
646 expected_remaining_argc = argc;
647 break;
648 case MODE_REAL:
649 expected_remaining_argc = 2;
650 break;
651 case MODE_TRIVIAL:
652 expected_remaining_argc = 3;
a1a78119
EN
653 /* Removal of `--trivial-merge` is expected */
654 original_argc--;
6ec755a0
EN
655 break;
656 }
a1a78119
EN
657 if (o.mode == MODE_TRIVIAL && argc < original_argc)
658 die(_("--trivial-merge is incompatible with all other options"));
6ec755a0
EN
659
660 if (argc != expected_remaining_argc)
661 usage_with_options(merge_tree_usage, mt_options);
662
b6551fea
DS
663 git_config(git_default_config, NULL);
664
6ec755a0
EN
665 /* Do the relevant type of merge */
666 if (o.mode == MODE_REAL)
66265a69 667 return real_merge(&o, merge_base, argv[0], argv[1], prefix);
6ec755a0
EN
668 else
669 return trivial_merge(argv[0], argv[1], argv[2]);
55e48f6b 670}