rebase -i: remove patch file after conflict resolution
[git.git] / builtin / merge-tree.c
blob803e380856edf087ca06bb42bef1813d2c6b11ba
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-store.h"
12 #include "parse-options.h"
13 #include "repository.h"
14 #include "blob.h"
15 #include "exec-cmd.h"
16 #include "merge-blobs.h"
17 #include "quote.h"
19 static int line_termination = '\n';
21 struct merge_list {
22 struct merge_list *next;
23 struct merge_list *link; /* other stages for this object */
25 unsigned int stage : 2;
26 unsigned int mode;
27 const char *path;
28 struct blob *blob;
31 static struct merge_list *merge_result, **merge_result_end = &merge_result;
33 static void add_merge_entry(struct merge_list *entry)
35 *merge_result_end = entry;
36 merge_result_end = &entry->next;
39 static void trivial_merge_trees(struct tree_desc t[3], const char *base);
41 static const char *explanation(struct merge_list *entry)
43 switch (entry->stage) {
44 case 0:
45 return "merged";
46 case 3:
47 return "added in remote";
48 case 2:
49 if (entry->link)
50 return "added in both";
51 return "added in local";
54 /* Existed in base */
55 entry = entry->link;
56 if (!entry)
57 return "removed in both";
59 if (entry->link)
60 return "changed in both";
62 if (entry->stage == 3)
63 return "removed in local";
64 return "removed in remote";
67 static void *result(struct merge_list *entry, unsigned long *size)
69 enum object_type type;
70 struct blob *base, *our, *their;
71 const char *path = entry->path;
73 if (!entry->stage)
74 return repo_read_object_file(the_repository,
75 &entry->blob->object.oid, &type,
76 size);
77 base = NULL;
78 if (entry->stage == 1) {
79 base = entry->blob;
80 entry = entry->link;
82 our = NULL;
83 if (entry && entry->stage == 2) {
84 our = entry->blob;
85 entry = entry->link;
87 their = NULL;
88 if (entry)
89 their = entry->blob;
90 return merge_blobs(the_repository->index, path,
91 base, our, their, size);
94 static void *origin(struct merge_list *entry, unsigned long *size)
96 enum object_type type;
97 while (entry) {
98 if (entry->stage == 2)
99 return repo_read_object_file(the_repository,
100 &entry->blob->object.oid,
101 &type, size);
102 entry = entry->link;
104 return NULL;
107 static int show_outf(void *priv UNUSED, mmbuffer_t *mb, int nbuf)
109 int i;
110 for (i = 0; i < nbuf; i++)
111 printf("%.*s", (int) mb[i].size, mb[i].ptr);
112 return 0;
115 static void show_diff(struct merge_list *entry)
117 unsigned long size;
118 mmfile_t src, dst;
119 xpparam_t xpp;
120 xdemitconf_t xecfg;
121 xdemitcb_t ecb = { .out_line = show_outf };
123 memset(&xpp, 0, sizeof(xpp));
124 xpp.flags = 0;
125 memset(&xecfg, 0, sizeof(xecfg));
126 xecfg.ctxlen = 3;
128 src.ptr = origin(entry, &size);
129 if (!src.ptr)
130 size = 0;
131 src.size = size;
132 dst.ptr = result(entry, &size);
133 if (!dst.ptr)
134 size = 0;
135 dst.size = size;
136 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
137 die("unable to generate diff");
138 free(src.ptr);
139 free(dst.ptr);
142 static void show_result_list(struct merge_list *entry)
144 printf("%s\n", explanation(entry));
145 do {
146 struct merge_list *link = entry->link;
147 static const char *desc[4] = { "result", "base", "our", "their" };
148 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
149 entry = link;
150 } while (entry);
153 static void show_result(void)
155 struct merge_list *walk;
157 walk = merge_result;
158 while (walk) {
159 show_result_list(walk);
160 show_diff(walk);
161 walk = walk->next;
165 /* An empty entry never compares same, not even to another empty entry */
166 static int same_entry(struct name_entry *a, struct name_entry *b)
168 return !is_null_oid(&a->oid) &&
169 !is_null_oid(&b->oid) &&
170 oideq(&a->oid, &b->oid) &&
171 a->mode == b->mode;
174 static int both_empty(struct name_entry *a, struct name_entry *b)
176 return is_null_oid(&a->oid) && is_null_oid(&b->oid);
179 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
181 struct merge_list *res = xcalloc(1, sizeof(*res));
183 res->stage = stage;
184 res->path = path;
185 res->mode = mode;
186 res->blob = lookup_blob(the_repository, oid);
187 return res;
190 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
192 struct strbuf buf = STRBUF_INIT;
193 strbuf_make_traverse_path(&buf, info, n->path, n->pathlen);
194 return strbuf_detach(&buf, NULL);
197 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
199 struct merge_list *orig, *final;
200 const char *path;
202 /* If it's already ours, don't bother showing it */
203 if (!ours)
204 return;
206 path = traverse_path(info, result);
207 orig = create_entry(2, ours->mode, &ours->oid, path);
208 final = create_entry(0, result->mode, &result->oid, path);
210 final->link = orig;
212 add_merge_entry(final);
215 static void unresolved_directory(const struct traverse_info *info,
216 struct name_entry n[3])
218 struct repository *r = the_repository;
219 char *newbase;
220 struct name_entry *p;
221 struct tree_desc t[3];
222 void *buf0, *buf1, *buf2;
224 for (p = n; p < n + 3; p++) {
225 if (p->mode && S_ISDIR(p->mode))
226 break;
228 if (n + 3 <= p)
229 return; /* there is no tree here */
231 newbase = traverse_path(info, p);
233 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
234 buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
235 buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
236 buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
237 #undef ENTRY_OID
239 trivial_merge_trees(t, newbase);
241 free(buf0);
242 free(buf1);
243 free(buf2);
244 free(newbase);
248 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
250 const char *path;
251 struct merge_list *link;
253 if (!n->mode)
254 return entry;
255 if (entry)
256 path = entry->path;
257 else
258 path = traverse_path(info, n);
259 link = create_entry(stage, n->mode, &n->oid, path);
260 link->link = entry;
261 return link;
264 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
266 struct merge_list *entry = NULL;
267 int i;
268 unsigned dirmask = 0, mask = 0;
270 for (i = 0; i < 3; i++) {
271 mask |= (1 << i);
273 * Treat missing entries as directories so that we return
274 * after unresolved_directory has handled this.
276 if (!n[i].mode || S_ISDIR(n[i].mode))
277 dirmask |= (1 << i);
280 unresolved_directory(info, n);
282 if (dirmask == mask)
283 return;
285 if (n[2].mode && !S_ISDIR(n[2].mode))
286 entry = link_entry(3, info, n + 2, entry);
287 if (n[1].mode && !S_ISDIR(n[1].mode))
288 entry = link_entry(2, info, n + 1, entry);
289 if (n[0].mode && !S_ISDIR(n[0].mode))
290 entry = link_entry(1, info, n + 0, entry);
292 add_merge_entry(entry);
296 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
297 * as the origin.
299 * This walks the (sorted) trees in lock-step, checking every possible
300 * name. Note that directories automatically sort differently from other
301 * files (see "base_name_compare"), so you'll never see file/directory
302 * conflicts, because they won't ever compare the same.
304 * IOW, if a directory changes to a filename, it will automatically be
305 * seen as the directory going away, and the filename being created.
307 * Think of this as a three-way diff.
309 * The output will be either:
310 * - successful merge
311 * "0 mode sha1 filename"
312 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
313 * in parallel with this too!
315 * - conflict:
316 * "1 mode sha1 filename"
317 * "2 mode sha1 filename"
318 * "3 mode sha1 filename"
319 * where not all of the 1/2/3 lines may exist, of course.
321 * The successful merge rules are the same as for the three-way merge
322 * in git-read-tree.
324 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
326 /* Same in both? */
327 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
328 /* Modified, added or removed identically */
329 resolve(info, NULL, entry+1);
330 return mask;
333 if (same_entry(entry+0, entry+1)) {
334 if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
335 /* We did not touch, they modified -- take theirs */
336 resolve(info, entry+1, entry+2);
337 return mask;
340 * If we did not touch a directory but they made it
341 * into a file, we fall through and unresolved()
342 * recurses down. Likewise for the opposite case.
346 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
347 /* We added, modified or removed, they did not touch -- take ours */
348 resolve(info, NULL, entry+1);
349 return mask;
352 unresolved(info, entry);
353 return mask;
356 static void trivial_merge_trees(struct tree_desc t[3], const char *base)
358 struct traverse_info info;
360 setup_traverse_info(&info, base);
361 info.fn = threeway_callback;
362 traverse_trees(&the_index, 3, t, &info);
365 static void *get_tree_descriptor(struct repository *r,
366 struct tree_desc *desc,
367 const char *rev)
369 struct object_id oid;
370 void *buf;
372 if (repo_get_oid(r, rev, &oid))
373 die("unknown rev %s", rev);
374 buf = fill_tree_descriptor(r, desc, &oid);
375 if (!buf)
376 die("%s is not a tree", rev);
377 return buf;
380 static int trivial_merge(const char *base,
381 const char *branch1,
382 const char *branch2)
384 struct repository *r = the_repository;
385 struct tree_desc t[3];
386 void *buf1, *buf2, *buf3;
388 buf1 = get_tree_descriptor(r, t+0, base);
389 buf2 = get_tree_descriptor(r, t+1, branch1);
390 buf3 = get_tree_descriptor(r, t+2, branch2);
391 trivial_merge_trees(t, "");
392 free(buf1);
393 free(buf2);
394 free(buf3);
396 show_result();
397 return 0;
400 enum mode {
401 MODE_UNKNOWN,
402 MODE_TRIVIAL,
403 MODE_REAL,
406 struct merge_tree_options {
407 int mode;
408 int allow_unrelated_histories;
409 int show_messages;
410 int name_only;
411 int use_stdin;
414 static int real_merge(struct merge_tree_options *o,
415 const char *merge_base,
416 const char *branch1, const char *branch2,
417 const char *prefix)
419 struct commit *parent1, *parent2;
420 struct commit_list *merge_bases = NULL;
421 struct merge_options opt;
422 struct merge_result result = { 0 };
423 int show_messages = o->show_messages;
425 parent1 = get_merge_parent(branch1);
426 if (!parent1)
427 help_unknown_ref(branch1, "merge-tree",
428 _("not something we can merge"));
430 parent2 = get_merge_parent(branch2);
431 if (!parent2)
432 help_unknown_ref(branch2, "merge-tree",
433 _("not something we can merge"));
435 init_merge_options(&opt, the_repository);
437 opt.show_rename_progress = 0;
439 opt.branch1 = branch1;
440 opt.branch2 = branch2;
442 if (merge_base) {
443 struct commit *base_commit;
444 struct tree *base_tree, *parent1_tree, *parent2_tree;
446 base_commit = lookup_commit_reference_by_name(merge_base);
447 if (!base_commit)
448 die(_("could not lookup commit %s"), merge_base);
450 opt.ancestor = merge_base;
451 base_tree = repo_get_commit_tree(the_repository, base_commit);
452 parent1_tree = repo_get_commit_tree(the_repository, parent1);
453 parent2_tree = repo_get_commit_tree(the_repository, parent2);
454 merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);
455 } else {
457 * Get the merge bases, in reverse order; see comment above
458 * merge_incore_recursive in merge-ort.h
460 merge_bases = repo_get_merge_bases(the_repository, parent1,
461 parent2);
462 if (!merge_bases && !o->allow_unrelated_histories)
463 die(_("refusing to merge unrelated histories"));
464 merge_bases = reverse_commit_list(merge_bases);
465 merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
468 if (result.clean < 0)
469 die(_("failure to merge"));
471 if (show_messages == -1)
472 show_messages = !result.clean;
474 if (o->use_stdin)
475 printf("%d%c", result.clean, line_termination);
476 printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination);
477 if (!result.clean) {
478 struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
479 const char *last = NULL;
480 int i;
482 merge_get_conflicted_files(&result, &conflicted_files);
483 for (i = 0; i < conflicted_files.nr; i++) {
484 const char *name = conflicted_files.items[i].string;
485 struct stage_info *c = conflicted_files.items[i].util;
486 if (!o->name_only)
487 printf("%06o %s %d\t",
488 c->mode, oid_to_hex(&c->oid), c->stage);
489 else if (last && !strcmp(last, name))
490 continue;
491 write_name_quoted_relative(
492 name, prefix, stdout, line_termination);
493 last = name;
495 string_list_clear(&conflicted_files, 1);
497 if (show_messages) {
498 putchar(line_termination);
499 merge_display_update_messages(&opt, line_termination == '\0',
500 &result);
502 if (o->use_stdin)
503 putchar(line_termination);
504 merge_finalize(&opt, &result);
505 return !result.clean; /* result.clean < 0 handled above */
508 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
510 struct merge_tree_options o = { .show_messages = -1 };
511 int expected_remaining_argc;
512 int original_argc;
513 const char *merge_base = NULL;
515 const char * const merge_tree_usage[] = {
516 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
517 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
518 NULL
520 struct option mt_options[] = {
521 OPT_CMDMODE(0, "write-tree", &o.mode,
522 N_("do a real merge instead of a trivial merge"),
523 MODE_REAL),
524 OPT_CMDMODE(0, "trivial-merge", &o.mode,
525 N_("do a trivial merge only"), MODE_TRIVIAL),
526 OPT_BOOL(0, "messages", &o.show_messages,
527 N_("also show informational/conflict messages")),
528 OPT_SET_INT('z', NULL, &line_termination,
529 N_("separate paths with the NUL character"), '\0'),
530 OPT_BOOL_F(0, "name-only",
531 &o.name_only,
532 N_("list filenames without modes/oids/stages"),
533 PARSE_OPT_NONEG),
534 OPT_BOOL_F(0, "allow-unrelated-histories",
535 &o.allow_unrelated_histories,
536 N_("allow merging unrelated histories"),
537 PARSE_OPT_NONEG),
538 OPT_BOOL_F(0, "stdin",
539 &o.use_stdin,
540 N_("perform multiple merges, one per line of input"),
541 PARSE_OPT_NONEG),
542 OPT_STRING(0, "merge-base",
543 &merge_base,
544 N_("commit"),
545 N_("specify a merge-base for the merge")),
546 OPT_END()
549 /* Parse arguments */
550 original_argc = argc - 1; /* ignoring argv[0] */
551 argc = parse_options(argc, argv, prefix, mt_options,
552 merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
554 /* Handle --stdin */
555 if (o.use_stdin) {
556 struct strbuf buf = STRBUF_INIT;
558 if (o.mode == MODE_TRIVIAL)
559 die(_("--trivial-merge is incompatible with all other options"));
560 if (merge_base)
561 die(_("--merge-base is incompatible with --stdin"));
562 line_termination = '\0';
563 while (strbuf_getline_lf(&buf, stdin) != EOF) {
564 struct strbuf **split;
565 int result;
566 const char *input_merge_base = NULL;
568 split = strbuf_split(&buf, ' ');
569 if (!split[0] || !split[1])
570 die(_("malformed input line: '%s'."), buf.buf);
571 strbuf_rtrim(split[0]);
572 strbuf_rtrim(split[1]);
574 /* parse the merge-base */
575 if (!strcmp(split[1]->buf, "--")) {
576 input_merge_base = split[0]->buf;
579 if (input_merge_base && split[2] && split[3] && !split[4]) {
580 strbuf_rtrim(split[2]);
581 strbuf_rtrim(split[3]);
582 result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
583 } else if (!input_merge_base && !split[2]) {
584 result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
585 } else {
586 die(_("malformed input line: '%s'."), buf.buf);
589 if (result < 0)
590 die(_("merging cannot continue; got unclean result of %d"), result);
591 strbuf_list_free(split);
593 strbuf_release(&buf);
594 return 0;
597 /* Figure out which mode to use */
598 switch (o.mode) {
599 default:
600 BUG("unexpected command mode %d", o.mode);
601 case MODE_UNKNOWN:
602 switch (argc) {
603 default:
604 usage_with_options(merge_tree_usage, mt_options);
605 case 2:
606 o.mode = MODE_REAL;
607 break;
608 case 3:
609 o.mode = MODE_TRIVIAL;
610 break;
612 expected_remaining_argc = argc;
613 break;
614 case MODE_REAL:
615 expected_remaining_argc = 2;
616 break;
617 case MODE_TRIVIAL:
618 expected_remaining_argc = 3;
619 /* Removal of `--trivial-merge` is expected */
620 original_argc--;
621 break;
623 if (o.mode == MODE_TRIVIAL && argc < original_argc)
624 die(_("--trivial-merge is incompatible with all other options"));
626 if (argc != expected_remaining_argc)
627 usage_with_options(merge_tree_usage, mt_options);
629 /* Do the relevant type of merge */
630 if (o.mode == MODE_REAL)
631 return real_merge(&o, merge_base, argv[0], argv[1], prefix);
632 else
633 return trivial_merge(argv[0], argv[1], argv[2]);