reftable/merged: advance subiter on subsequent iteration
[alt-git.git] / builtin / merge-tree.c
blob3bdec53fbe58bb5edef6e4d425829a58ce87bffe
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "tree-walk.h"
4 #include "xdiff-interface.h"
5 #include "help.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "commit.h"
9 #include "commit-reach.h"
10 #include "merge-ort.h"
11 #include "object-name.h"
12 #include "object-store-ll.h"
13 #include "parse-options.h"
14 #include "repository.h"
15 #include "blob.h"
16 #include "merge-blobs.h"
17 #include "quote.h"
18 #include "tree.h"
19 #include "config.h"
20 #include "strvec.h"
22 static int line_termination = '\n';
24 struct merge_list {
25 struct merge_list *next;
26 struct merge_list *link; /* other stages for this object */
28 unsigned int stage : 2;
29 unsigned int mode;
30 const char *path;
31 struct blob *blob;
34 static struct merge_list *merge_result, **merge_result_end = &merge_result;
36 static void add_merge_entry(struct merge_list *entry)
38 *merge_result_end = entry;
39 merge_result_end = &entry->next;
42 static void trivial_merge_trees(struct tree_desc t[3], const char *base);
44 static const char *explanation(struct merge_list *entry)
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";
57 /* Existed in base */
58 entry = entry->link;
59 if (!entry)
60 return "removed in both";
62 if (entry->link)
63 return "changed in both";
65 if (entry->stage == 3)
66 return "removed in local";
67 return "removed in remote";
70 static void *result(struct merge_list *entry, unsigned long *size)
72 enum object_type type;
73 struct blob *base, *our, *their;
74 const char *path = entry->path;
76 if (!entry->stage)
77 return repo_read_object_file(the_repository,
78 &entry->blob->object.oid, &type,
79 size);
80 base = NULL;
81 if (entry->stage == 1) {
82 base = entry->blob;
83 entry = entry->link;
85 our = NULL;
86 if (entry && entry->stage == 2) {
87 our = entry->blob;
88 entry = entry->link;
90 their = NULL;
91 if (entry)
92 their = entry->blob;
93 return merge_blobs(the_repository->index, path,
94 base, our, their, size);
97 static void *origin(struct merge_list *entry, unsigned long *size)
99 enum object_type type;
100 while (entry) {
101 if (entry->stage == 2)
102 return repo_read_object_file(the_repository,
103 &entry->blob->object.oid,
104 &type, size);
105 entry = entry->link;
107 return NULL;
110 static int show_outf(void *priv UNUSED, mmbuffer_t *mb, int nbuf)
112 int i;
113 for (i = 0; i < nbuf; i++)
114 printf("%.*s", (int) mb[i].size, mb[i].ptr);
115 return 0;
118 static void show_diff(struct merge_list *entry)
120 unsigned long size;
121 mmfile_t src, dst;
122 xpparam_t xpp;
123 xdemitconf_t xecfg;
124 xdemitcb_t ecb = { .out_line = show_outf };
126 memset(&xpp, 0, sizeof(xpp));
127 xpp.flags = 0;
128 memset(&xecfg, 0, sizeof(xecfg));
129 xecfg.ctxlen = 3;
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;
139 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
140 die("unable to generate diff");
141 free(src.ptr);
142 free(dst.ptr);
145 static void show_result_list(struct merge_list *entry)
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" };
151 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
152 entry = link;
153 } while (entry);
156 static void show_result(void)
158 struct merge_list *walk;
160 walk = merge_result;
161 while (walk) {
162 show_result_list(walk);
163 show_diff(walk);
164 walk = walk->next;
168 /* An empty entry never compares same, not even to another empty entry */
169 static int same_entry(struct name_entry *a, struct name_entry *b)
171 return !is_null_oid(&a->oid) &&
172 !is_null_oid(&b->oid) &&
173 oideq(&a->oid, &b->oid) &&
174 a->mode == b->mode;
177 static int both_empty(struct name_entry *a, struct name_entry *b)
179 return is_null_oid(&a->oid) && is_null_oid(&b->oid);
182 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
184 struct merge_list *res = xcalloc(1, sizeof(*res));
186 res->stage = stage;
187 res->path = path;
188 res->mode = mode;
189 res->blob = lookup_blob(the_repository, oid);
190 return res;
193 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
195 struct strbuf buf = STRBUF_INIT;
196 strbuf_make_traverse_path(&buf, info, n->path, n->pathlen);
197 return strbuf_detach(&buf, NULL);
200 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
202 struct merge_list *orig, *final;
203 const char *path;
205 /* If it's already ours, don't bother showing it */
206 if (!ours)
207 return;
209 path = traverse_path(info, result);
210 orig = create_entry(2, ours->mode, &ours->oid, path);
211 final = create_entry(0, result->mode, &result->oid, path);
213 final->link = orig;
215 add_merge_entry(final);
218 static void unresolved_directory(const struct traverse_info *info,
219 struct name_entry n[3])
221 struct repository *r = the_repository;
222 char *newbase;
223 struct name_entry *p;
224 struct tree_desc t[3];
225 void *buf0, *buf1, *buf2;
227 for (p = n; p < n + 3; p++) {
228 if (p->mode && S_ISDIR(p->mode))
229 break;
231 if (n + 3 <= p)
232 return; /* there is no tree here */
234 newbase = traverse_path(info, p);
236 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
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));
240 #undef ENTRY_OID
242 trivial_merge_trees(t, newbase);
244 free(buf0);
245 free(buf1);
246 free(buf2);
247 free(newbase);
251 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
253 const char *path;
254 struct merge_list *link;
256 if (!n->mode)
257 return entry;
258 if (entry)
259 path = entry->path;
260 else
261 path = traverse_path(info, n);
262 link = create_entry(stage, n->mode, &n->oid, path);
263 link->link = entry;
264 return link;
267 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
269 struct merge_list *entry = NULL;
270 int i;
271 unsigned dirmask = 0, mask = 0;
273 for (i = 0; i < 3; i++) {
274 mask |= (1 << i);
276 * Treat missing entries as directories so that we return
277 * after unresolved_directory has handled this.
279 if (!n[i].mode || S_ISDIR(n[i].mode))
280 dirmask |= (1 << i);
283 unresolved_directory(info, n);
285 if (dirmask == mask)
286 return;
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);
295 add_merge_entry(entry);
299 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
300 * as the origin.
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.
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.
310 * Think of this as a three-way diff.
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!
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.
324 * The successful merge rules are the same as for the three-way merge
325 * in git-read-tree.
327 static int threeway_callback(int n UNUSED, unsigned long mask,
328 unsigned long dirmask UNUSED,
329 struct name_entry *entry, struct traverse_info *info)
331 /* Same in both? */
332 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
333 /* Modified, added or removed identically */
334 resolve(info, NULL, entry+1);
335 return mask;
338 if (same_entry(entry+0, entry+1)) {
339 if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
340 /* We did not touch, they modified -- take theirs */
341 resolve(info, entry+1, entry+2);
342 return mask;
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.
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;
357 unresolved(info, entry);
358 return mask;
361 static void trivial_merge_trees(struct tree_desc t[3], const char *base)
363 struct traverse_info info;
365 setup_traverse_info(&info, base);
366 info.fn = threeway_callback;
367 traverse_trees(&the_index, 3, t, &info);
370 static void *get_tree_descriptor(struct repository *r,
371 struct tree_desc *desc,
372 const char *rev)
374 struct object_id oid;
375 void *buf;
377 if (repo_get_oid(r, rev, &oid))
378 die("unknown rev %s", rev);
379 buf = fill_tree_descriptor(r, desc, &oid);
380 if (!buf)
381 die("%s is not a tree", rev);
382 return buf;
385 static int trivial_merge(const char *base,
386 const char *branch1,
387 const char *branch2)
389 struct repository *r = the_repository;
390 struct tree_desc t[3];
391 void *buf1, *buf2, *buf3;
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);
396 trivial_merge_trees(t, "");
397 free(buf1);
398 free(buf2);
399 free(buf3);
401 show_result();
402 return 0;
405 enum mode {
406 MODE_UNKNOWN,
407 MODE_TRIVIAL,
408 MODE_REAL,
411 struct merge_tree_options {
412 int mode;
413 int allow_unrelated_histories;
414 int show_messages;
415 int name_only;
416 int use_stdin;
417 struct merge_options merge_options;
420 static int real_merge(struct merge_tree_options *o,
421 const char *merge_base,
422 const char *branch1, const char *branch2,
423 const char *prefix)
425 struct commit *parent1, *parent2;
426 struct commit_list *merge_bases = NULL;
427 struct merge_result result = { 0 };
428 int show_messages = o->show_messages;
429 struct merge_options opt;
431 copy_merge_options(&opt, &o->merge_options);
432 parent1 = get_merge_parent(branch1);
433 if (!parent1)
434 help_unknown_ref(branch1, "merge-tree",
435 _("not something we can merge"));
437 parent2 = get_merge_parent(branch2);
438 if (!parent2)
439 help_unknown_ref(branch2, "merge-tree",
440 _("not something we can merge"));
442 opt.show_rename_progress = 0;
444 opt.branch1 = branch1;
445 opt.branch2 = branch2;
447 if (merge_base) {
448 struct commit *base_commit;
449 struct tree *base_tree, *parent1_tree, *parent2_tree;
451 base_commit = lookup_commit_reference_by_name(merge_base);
452 if (!base_commit)
453 die(_("could not lookup commit '%s'"), merge_base);
455 opt.ancestor = merge_base;
456 base_tree = repo_get_commit_tree(the_repository, base_commit);
457 parent1_tree = repo_get_commit_tree(the_repository, parent1);
458 parent2_tree = repo_get_commit_tree(the_repository, parent2);
459 merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
460 } else {
462 * Get the merge bases, in reverse order; see comment above
463 * merge_incore_recursive in merge-ort.h
465 merge_bases = repo_get_merge_bases(the_repository, parent1,
466 parent2);
467 if (!merge_bases && !o->allow_unrelated_histories)
468 die(_("refusing to merge unrelated histories"));
469 merge_bases = reverse_commit_list(merge_bases);
470 merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
473 if (result.clean < 0)
474 die(_("failure to merge"));
476 if (show_messages == -1)
477 show_messages = !result.clean;
479 if (o->use_stdin)
480 printf("%d%c", result.clean, line_termination);
481 printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination);
482 if (!result.clean) {
483 struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
484 const char *last = NULL;
485 int i;
487 merge_get_conflicted_files(&result, &conflicted_files);
488 for (i = 0; i < conflicted_files.nr; i++) {
489 const char *name = conflicted_files.items[i].string;
490 struct stage_info *c = conflicted_files.items[i].util;
491 if (!o->name_only)
492 printf("%06o %s %d\t",
493 c->mode, oid_to_hex(&c->oid), c->stage);
494 else if (last && !strcmp(last, name))
495 continue;
496 write_name_quoted_relative(
497 name, prefix, stdout, line_termination);
498 last = name;
500 string_list_clear(&conflicted_files, 1);
502 if (show_messages) {
503 putchar(line_termination);
504 merge_display_update_messages(&opt, line_termination == '\0',
505 &result);
507 if (o->use_stdin)
508 putchar(line_termination);
509 merge_finalize(&opt, &result);
510 clear_merge_options(&opt);
511 return !result.clean; /* result.clean < 0 handled above */
514 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
516 struct merge_tree_options o = { .show_messages = -1 };
517 struct strvec xopts = STRVEC_INIT;
518 int expected_remaining_argc;
519 int original_argc;
520 const char *merge_base = NULL;
522 const char * const merge_tree_usage[] = {
523 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
524 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
525 NULL
527 struct option mt_options[] = {
528 OPT_CMDMODE(0, "write-tree", &o.mode,
529 N_("do a real merge instead of a trivial merge"),
530 MODE_REAL),
531 OPT_CMDMODE(0, "trivial-merge", &o.mode,
532 N_("do a trivial merge only"), MODE_TRIVIAL),
533 OPT_BOOL(0, "messages", &o.show_messages,
534 N_("also show informational/conflict messages")),
535 OPT_SET_INT('z', NULL, &line_termination,
536 N_("separate paths with the NUL character"), '\0'),
537 OPT_BOOL_F(0, "name-only",
538 &o.name_only,
539 N_("list filenames without modes/oids/stages"),
540 PARSE_OPT_NONEG),
541 OPT_BOOL_F(0, "allow-unrelated-histories",
542 &o.allow_unrelated_histories,
543 N_("allow merging unrelated histories"),
544 PARSE_OPT_NONEG),
545 OPT_BOOL_F(0, "stdin",
546 &o.use_stdin,
547 N_("perform multiple merges, one per line of input"),
548 PARSE_OPT_NONEG),
549 OPT_STRING(0, "merge-base",
550 &merge_base,
551 N_("commit"),
552 N_("specify a merge-base for the merge")),
553 OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"),
554 N_("option for selected merge strategy")),
555 OPT_END()
558 /* Init merge options */
559 init_merge_options(&o.merge_options, the_repository);
561 /* Parse arguments */
562 original_argc = argc - 1; /* ignoring argv[0] */
563 argc = parse_options(argc, argv, prefix, mt_options,
564 merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
566 if (xopts.nr && o.mode == MODE_TRIVIAL)
567 die(_("--trivial-merge is incompatible with all other options"));
568 for (int x = 0; x < xopts.nr; x++)
569 if (parse_merge_opt(&o.merge_options, xopts.v[x]))
570 die(_("unknown strategy option: -X%s"), xopts.v[x]);
572 /* Handle --stdin */
573 if (o.use_stdin) {
574 struct strbuf buf = STRBUF_INIT;
576 if (o.mode == MODE_TRIVIAL)
577 die(_("--trivial-merge is incompatible with all other options"));
578 if (merge_base)
579 die(_("options '%s' and '%s' cannot be used together"),
580 "--merge-base", "--stdin");
581 line_termination = '\0';
582 while (strbuf_getline_lf(&buf, stdin) != EOF) {
583 struct strbuf **split;
584 int result;
585 const char *input_merge_base = NULL;
587 split = strbuf_split(&buf, ' ');
588 if (!split[0] || !split[1])
589 die(_("malformed input line: '%s'."), buf.buf);
590 strbuf_rtrim(split[0]);
591 strbuf_rtrim(split[1]);
593 /* parse the merge-base */
594 if (!strcmp(split[1]->buf, "--")) {
595 input_merge_base = split[0]->buf;
598 if (input_merge_base && split[2] && split[3] && !split[4]) {
599 strbuf_rtrim(split[2]);
600 strbuf_rtrim(split[3]);
601 result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
602 } else if (!input_merge_base && !split[2]) {
603 result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
604 } else {
605 die(_("malformed input line: '%s'."), buf.buf);
608 if (result < 0)
609 die(_("merging cannot continue; got unclean result of %d"), result);
610 strbuf_list_free(split);
612 strbuf_release(&buf);
613 return 0;
616 /* Figure out which mode to use */
617 switch (o.mode) {
618 default:
619 BUG("unexpected command mode %d", o.mode);
620 case MODE_UNKNOWN:
621 switch (argc) {
622 default:
623 usage_with_options(merge_tree_usage, mt_options);
624 case 2:
625 o.mode = MODE_REAL;
626 break;
627 case 3:
628 o.mode = MODE_TRIVIAL;
629 break;
631 expected_remaining_argc = argc;
632 break;
633 case MODE_REAL:
634 expected_remaining_argc = 2;
635 break;
636 case MODE_TRIVIAL:
637 expected_remaining_argc = 3;
638 /* Removal of `--trivial-merge` is expected */
639 original_argc--;
640 break;
642 if (o.mode == MODE_TRIVIAL && argc < original_argc)
643 die(_("--trivial-merge is incompatible with all other options"));
645 if (argc != expected_remaining_argc)
646 usage_with_options(merge_tree_usage, mt_options);
648 git_config(git_default_config, NULL);
650 /* Do the relevant type of merge */
651 if (o.mode == MODE_REAL)
652 return real_merge(&o, merge_base, argv[0], argv[1], prefix);
653 else
654 return trivial_merge(argv[0], argv[1], argv[2]);