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