The sixth batch
[alt-git.git] / builtin / merge-tree.c
blob1082d919fd1ca99e91168fda923d839f73907095
1 #include "builtin.h"
2 #include "tree-walk.h"
3 #include "xdiff-interface.h"
4 #include "help.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "commit.h"
8 #include "commit-reach.h"
9 #include "merge-ort.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "parse-options.h"
13 #include "repository.h"
14 #include "blob.h"
15 #include "merge-blobs.h"
16 #include "quote.h"
17 #include "tree.h"
18 #include "config.h"
19 #include "strvec.h"
21 static int line_termination = '\n';
23 struct merge_list {
24 struct merge_list *next;
25 struct merge_list *link; /* other stages for this object */
27 unsigned int stage : 2;
28 unsigned int mode;
29 const char *path;
30 struct blob *blob;
33 static struct merge_list *merge_result, **merge_result_end = &merge_result;
35 static void add_merge_entry(struct merge_list *entry)
37 *merge_result_end = entry;
38 merge_result_end = &entry->next;
41 static void trivial_merge_trees(struct tree_desc t[3], const char *base);
43 static const char *explanation(struct merge_list *entry)
45 switch (entry->stage) {
46 case 0:
47 return "merged";
48 case 3:
49 return "added in remote";
50 case 2:
51 if (entry->link)
52 return "added in both";
53 return "added in local";
56 /* Existed in base */
57 entry = entry->link;
58 if (!entry)
59 return "removed in both";
61 if (entry->link)
62 return "changed in both";
64 if (entry->stage == 3)
65 return "removed in local";
66 return "removed in remote";
69 static void *result(struct merge_list *entry, unsigned long *size)
71 enum object_type type;
72 struct blob *base, *our, *their;
73 const char *path = entry->path;
75 if (!entry->stage)
76 return repo_read_object_file(the_repository,
77 &entry->blob->object.oid, &type,
78 size);
79 base = NULL;
80 if (entry->stage == 1) {
81 base = entry->blob;
82 entry = entry->link;
84 our = NULL;
85 if (entry && entry->stage == 2) {
86 our = entry->blob;
87 entry = entry->link;
89 their = NULL;
90 if (entry)
91 their = entry->blob;
92 return merge_blobs(the_repository->index, path,
93 base, our, their, size);
96 static void *origin(struct merge_list *entry, unsigned long *size)
98 enum object_type type;
99 while (entry) {
100 if (entry->stage == 2)
101 return repo_read_object_file(the_repository,
102 &entry->blob->object.oid,
103 &type, size);
104 entry = entry->link;
106 return NULL;
109 static int show_outf(void *priv UNUSED, mmbuffer_t *mb, int nbuf)
111 int i;
112 for (i = 0; i < nbuf; i++)
113 printf("%.*s", (int) mb[i].size, mb[i].ptr);
114 return 0;
117 static void show_diff(struct merge_list *entry)
119 unsigned long size;
120 mmfile_t src, dst;
121 xpparam_t xpp;
122 xdemitconf_t xecfg;
123 xdemitcb_t ecb = { .out_line = show_outf };
125 memset(&xpp, 0, sizeof(xpp));
126 xpp.flags = 0;
127 memset(&xecfg, 0, sizeof(xecfg));
128 xecfg.ctxlen = 3;
130 src.ptr = origin(entry, &size);
131 if (!src.ptr)
132 size = 0;
133 src.size = size;
134 dst.ptr = result(entry, &size);
135 if (!dst.ptr)
136 size = 0;
137 dst.size = size;
138 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
139 die("unable to generate diff");
140 free(src.ptr);
141 free(dst.ptr);
144 static void show_result_list(struct merge_list *entry)
146 printf("%s\n", explanation(entry));
147 do {
148 struct merge_list *link = entry->link;
149 static const char *desc[4] = { "result", "base", "our", "their" };
150 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
151 entry = link;
152 } while (entry);
155 static void show_result(void)
157 struct merge_list *walk;
159 walk = merge_result;
160 while (walk) {
161 show_result_list(walk);
162 show_diff(walk);
163 walk = walk->next;
167 /* An empty entry never compares same, not even to another empty entry */
168 static int same_entry(struct name_entry *a, struct name_entry *b)
170 return !is_null_oid(&a->oid) &&
171 !is_null_oid(&b->oid) &&
172 oideq(&a->oid, &b->oid) &&
173 a->mode == b->mode;
176 static int both_empty(struct name_entry *a, struct name_entry *b)
178 return is_null_oid(&a->oid) && is_null_oid(&b->oid);
181 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
183 struct merge_list *res = xcalloc(1, sizeof(*res));
185 res->stage = stage;
186 res->path = path;
187 res->mode = mode;
188 res->blob = lookup_blob(the_repository, oid);
189 return res;
192 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
194 struct strbuf buf = STRBUF_INIT;
195 strbuf_make_traverse_path(&buf, info, n->path, n->pathlen);
196 return strbuf_detach(&buf, NULL);
199 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
201 struct merge_list *orig, *final;
202 const char *path;
204 /* If it's already ours, don't bother showing it */
205 if (!ours)
206 return;
208 path = traverse_path(info, result);
209 orig = create_entry(2, ours->mode, &ours->oid, path);
210 final = create_entry(0, result->mode, &result->oid, path);
212 final->link = orig;
214 add_merge_entry(final);
217 static void unresolved_directory(const struct traverse_info *info,
218 struct name_entry n[3])
220 struct repository *r = the_repository;
221 char *newbase;
222 struct name_entry *p;
223 struct tree_desc t[3];
224 void *buf0, *buf1, *buf2;
226 for (p = n; p < n + 3; p++) {
227 if (p->mode && S_ISDIR(p->mode))
228 break;
230 if (n + 3 <= p)
231 return; /* there is no tree here */
233 newbase = traverse_path(info, p);
235 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
236 buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
237 buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
238 buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
239 #undef ENTRY_OID
241 trivial_merge_trees(t, newbase);
243 free(buf0);
244 free(buf1);
245 free(buf2);
246 free(newbase);
250 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
252 const char *path;
253 struct merge_list *link;
255 if (!n->mode)
256 return entry;
257 if (entry)
258 path = entry->path;
259 else
260 path = traverse_path(info, n);
261 link = create_entry(stage, n->mode, &n->oid, path);
262 link->link = entry;
263 return link;
266 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
268 struct merge_list *entry = NULL;
269 int i;
270 unsigned dirmask = 0, mask = 0;
272 for (i = 0; i < 3; i++) {
273 mask |= (1 << i);
275 * Treat missing entries as directories so that we return
276 * after unresolved_directory has handled this.
278 if (!n[i].mode || S_ISDIR(n[i].mode))
279 dirmask |= (1 << i);
282 unresolved_directory(info, n);
284 if (dirmask == mask)
285 return;
287 if (n[2].mode && !S_ISDIR(n[2].mode))
288 entry = link_entry(3, info, n + 2, entry);
289 if (n[1].mode && !S_ISDIR(n[1].mode))
290 entry = link_entry(2, info, n + 1, entry);
291 if (n[0].mode && !S_ISDIR(n[0].mode))
292 entry = link_entry(1, info, n + 0, entry);
294 add_merge_entry(entry);
298 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
299 * as the origin.
301 * This walks the (sorted) trees in lock-step, checking every possible
302 * name. Note that directories automatically sort differently from other
303 * files (see "base_name_compare"), so you'll never see file/directory
304 * conflicts, because they won't ever compare the same.
306 * IOW, if a directory changes to a filename, it will automatically be
307 * seen as the directory going away, and the filename being created.
309 * Think of this as a three-way diff.
311 * The output will be either:
312 * - successful merge
313 * "0 mode sha1 filename"
314 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
315 * in parallel with this too!
317 * - conflict:
318 * "1 mode sha1 filename"
319 * "2 mode sha1 filename"
320 * "3 mode sha1 filename"
321 * where not all of the 1/2/3 lines may exist, of course.
323 * The successful merge rules are the same as for the three-way merge
324 * in git-read-tree.
326 static int threeway_callback(int n UNUSED, unsigned long mask,
327 unsigned long dirmask UNUSED,
328 struct name_entry *entry, struct traverse_info *info)
330 /* Same in both? */
331 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
332 /* Modified, added or removed identically */
333 resolve(info, NULL, entry+1);
334 return mask;
337 if (same_entry(entry+0, entry+1)) {
338 if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
339 /* We did not touch, they modified -- take theirs */
340 resolve(info, entry+1, entry+2);
341 return mask;
344 * If we did not touch a directory but they made it
345 * into a file, we fall through and unresolved()
346 * recurses down. Likewise for the opposite case.
350 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
351 /* We added, modified or removed, they did not touch -- take ours */
352 resolve(info, NULL, entry+1);
353 return mask;
356 unresolved(info, entry);
357 return mask;
360 static void trivial_merge_trees(struct tree_desc t[3], const char *base)
362 struct traverse_info info;
364 setup_traverse_info(&info, base);
365 info.fn = threeway_callback;
366 traverse_trees(the_repository->index, 3, t, &info);
369 static void *get_tree_descriptor(struct repository *r,
370 struct tree_desc *desc,
371 const char *rev)
373 struct object_id oid;
374 void *buf;
376 if (repo_get_oid(r, rev, &oid))
377 die("unknown rev %s", rev);
378 buf = fill_tree_descriptor(r, desc, &oid);
379 if (!buf)
380 die("%s is not a tree", rev);
381 return buf;
384 static int trivial_merge(const char *base,
385 const char *branch1,
386 const char *branch2)
388 struct repository *r = the_repository;
389 struct tree_desc t[3];
390 void *buf1, *buf2, *buf3;
392 buf1 = get_tree_descriptor(r, t+0, base);
393 buf2 = get_tree_descriptor(r, t+1, branch1);
394 buf3 = get_tree_descriptor(r, t+2, branch2);
395 trivial_merge_trees(t, "");
396 free(buf1);
397 free(buf2);
398 free(buf3);
400 show_result();
401 return 0;
404 enum mode {
405 MODE_UNKNOWN,
406 MODE_TRIVIAL,
407 MODE_REAL,
410 struct merge_tree_options {
411 int mode;
412 int allow_unrelated_histories;
413 int show_messages;
414 int name_only;
415 int use_stdin;
416 struct merge_options merge_options;
419 static int real_merge(struct merge_tree_options *o,
420 const char *merge_base,
421 const char *branch1, const char *branch2,
422 const char *prefix)
424 struct commit *parent1, *parent2;
425 struct commit_list *merge_bases = NULL;
426 struct merge_result result = { 0 };
427 int show_messages = o->show_messages;
428 struct merge_options opt;
430 copy_merge_options(&opt, &o->merge_options);
431 opt.show_rename_progress = 0;
433 opt.branch1 = branch1;
434 opt.branch2 = branch2;
436 if (merge_base) {
437 struct tree *base_tree, *parent1_tree, *parent2_tree;
440 * We actually only need the trees because we already
441 * have a merge base.
443 struct object_id base_oid, head_oid, merge_oid;
445 if (repo_get_oid_treeish(the_repository, merge_base, &base_oid))
446 die(_("could not parse as tree '%s'"), merge_base);
447 base_tree = parse_tree_indirect(&base_oid);
448 if (!base_tree)
449 die(_("unable to read tree (%s)"), oid_to_hex(&base_oid));
450 if (repo_get_oid_treeish(the_repository, branch1, &head_oid))
451 die(_("could not parse as tree '%s'"), branch1);
452 parent1_tree = parse_tree_indirect(&head_oid);
453 if (!parent1_tree)
454 die(_("unable to read tree (%s)"), oid_to_hex(&head_oid));
455 if (repo_get_oid_treeish(the_repository, branch2, &merge_oid))
456 die(_("could not parse as tree '%s'"), branch2);
457 parent2_tree = parse_tree_indirect(&merge_oid);
458 if (!parent2_tree)
459 die(_("unable to read tree (%s)"), oid_to_hex(&merge_oid));
461 opt.ancestor = merge_base;
462 merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
463 } else {
464 parent1 = get_merge_parent(branch1);
465 if (!parent1)
466 help_unknown_ref(branch1, "merge-tree",
467 _("not something we can merge"));
469 parent2 = get_merge_parent(branch2);
470 if (!parent2)
471 help_unknown_ref(branch2, "merge-tree",
472 _("not something we can merge"));
475 * Get the merge bases, in reverse order; see comment above
476 * merge_incore_recursive in merge-ort.h
478 if (repo_get_merge_bases(the_repository, parent1,
479 parent2, &merge_bases) < 0)
480 exit(128);
481 if (!merge_bases && !o->allow_unrelated_histories)
482 die(_("refusing to merge unrelated histories"));
483 merge_bases = reverse_commit_list(merge_bases);
484 merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
487 if (result.clean < 0)
488 die(_("failure to merge"));
490 if (show_messages == -1)
491 show_messages = !result.clean;
493 if (o->use_stdin)
494 printf("%d%c", result.clean, line_termination);
495 printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination);
496 if (!result.clean) {
497 struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
498 const char *last = NULL;
499 int i;
501 merge_get_conflicted_files(&result, &conflicted_files);
502 for (i = 0; i < conflicted_files.nr; i++) {
503 const char *name = conflicted_files.items[i].string;
504 struct stage_info *c = conflicted_files.items[i].util;
505 if (!o->name_only)
506 printf("%06o %s %d\t",
507 c->mode, oid_to_hex(&c->oid), c->stage);
508 else if (last && !strcmp(last, name))
509 continue;
510 write_name_quoted_relative(
511 name, prefix, stdout, line_termination);
512 last = name;
514 string_list_clear(&conflicted_files, 1);
516 if (show_messages) {
517 putchar(line_termination);
518 merge_display_update_messages(&opt, line_termination == '\0',
519 &result);
521 if (o->use_stdin)
522 putchar(line_termination);
523 merge_finalize(&opt, &result);
524 clear_merge_options(&opt);
525 return !result.clean; /* result.clean < 0 handled above */
528 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
530 struct merge_tree_options o = { .show_messages = -1 };
531 struct strvec xopts = STRVEC_INIT;
532 int expected_remaining_argc;
533 int original_argc;
534 const char *merge_base = NULL;
536 const char * const merge_tree_usage[] = {
537 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
538 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
539 NULL
541 struct option mt_options[] = {
542 OPT_CMDMODE(0, "write-tree", &o.mode,
543 N_("do a real merge instead of a trivial merge"),
544 MODE_REAL),
545 OPT_CMDMODE(0, "trivial-merge", &o.mode,
546 N_("do a trivial merge only"), MODE_TRIVIAL),
547 OPT_BOOL(0, "messages", &o.show_messages,
548 N_("also show informational/conflict messages")),
549 OPT_SET_INT('z', NULL, &line_termination,
550 N_("separate paths with the NUL character"), '\0'),
551 OPT_BOOL_F(0, "name-only",
552 &o.name_only,
553 N_("list filenames without modes/oids/stages"),
554 PARSE_OPT_NONEG),
555 OPT_BOOL_F(0, "allow-unrelated-histories",
556 &o.allow_unrelated_histories,
557 N_("allow merging unrelated histories"),
558 PARSE_OPT_NONEG),
559 OPT_BOOL_F(0, "stdin",
560 &o.use_stdin,
561 N_("perform multiple merges, one per line of input"),
562 PARSE_OPT_NONEG),
563 OPT_STRING(0, "merge-base",
564 &merge_base,
565 N_("tree-ish"),
566 N_("specify a merge-base for the merge")),
567 OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"),
568 N_("option for selected merge strategy")),
569 OPT_END()
572 /* Init merge options */
573 init_merge_options(&o.merge_options, the_repository);
575 /* Parse arguments */
576 original_argc = argc - 1; /* ignoring argv[0] */
577 argc = parse_options(argc, argv, prefix, mt_options,
578 merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
580 if (xopts.nr && o.mode == MODE_TRIVIAL)
581 die(_("--trivial-merge is incompatible with all other options"));
582 for (int x = 0; x < xopts.nr; x++)
583 if (parse_merge_opt(&o.merge_options, xopts.v[x]))
584 die(_("unknown strategy option: -X%s"), xopts.v[x]);
586 /* Handle --stdin */
587 if (o.use_stdin) {
588 struct strbuf buf = STRBUF_INIT;
590 if (o.mode == MODE_TRIVIAL)
591 die(_("--trivial-merge is incompatible with all other options"));
592 if (merge_base)
593 die(_("options '%s' and '%s' cannot be used together"),
594 "--merge-base", "--stdin");
595 line_termination = '\0';
596 while (strbuf_getline_lf(&buf, stdin) != EOF) {
597 struct strbuf **split;
598 int result;
599 const char *input_merge_base = NULL;
601 split = strbuf_split(&buf, ' ');
602 if (!split[0] || !split[1])
603 die(_("malformed input line: '%s'."), buf.buf);
604 strbuf_rtrim(split[0]);
605 strbuf_rtrim(split[1]);
607 /* parse the merge-base */
608 if (!strcmp(split[1]->buf, "--")) {
609 input_merge_base = split[0]->buf;
612 if (input_merge_base && split[2] && split[3] && !split[4]) {
613 strbuf_rtrim(split[2]);
614 strbuf_rtrim(split[3]);
615 result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
616 } else if (!input_merge_base && !split[2]) {
617 result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
618 } else {
619 die(_("malformed input line: '%s'."), buf.buf);
622 if (result < 0)
623 die(_("merging cannot continue; got unclean result of %d"), result);
624 strbuf_list_free(split);
626 strbuf_release(&buf);
627 return 0;
630 /* Figure out which mode to use */
631 switch (o.mode) {
632 default:
633 BUG("unexpected command mode %d", o.mode);
634 case MODE_UNKNOWN:
635 switch (argc) {
636 default:
637 usage_with_options(merge_tree_usage, mt_options);
638 case 2:
639 o.mode = MODE_REAL;
640 break;
641 case 3:
642 o.mode = MODE_TRIVIAL;
643 break;
645 expected_remaining_argc = argc;
646 break;
647 case MODE_REAL:
648 expected_remaining_argc = 2;
649 break;
650 case MODE_TRIVIAL:
651 expected_remaining_argc = 3;
652 /* Removal of `--trivial-merge` is expected */
653 original_argc--;
654 break;
656 if (o.mode == MODE_TRIVIAL && argc < original_argc)
657 die(_("--trivial-merge is incompatible with all other options"));
659 if (argc != expected_remaining_argc)
660 usage_with_options(merge_tree_usage, mt_options);
662 git_config(git_default_config, NULL);
664 /* Do the relevant type of merge */
665 if (o.mode == MODE_REAL)
666 return real_merge(&o, merge_base, argv[0], argv[1], prefix);
667 else
668 return trivial_merge(argv[0], argv[1], argv[2]);