git-merge-tree.txt: replace spurious HTML entity
[git.git] / builtin / merge-tree.c
blobae2c0116817381ed791c795eace3036b94277c0d
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 "commit-reach.h"
7 #include "merge-ort.h"
8 #include "object-store.h"
9 #include "parse-options.h"
10 #include "repository.h"
11 #include "blob.h"
12 #include "exec-cmd.h"
13 #include "merge-blobs.h"
14 #include "quote.h"
16 static int line_termination = '\n';
18 struct merge_list {
19 struct merge_list *next;
20 struct merge_list *link; /* other stages for this object */
22 unsigned int stage : 2;
23 unsigned int mode;
24 const char *path;
25 struct blob *blob;
28 static struct merge_list *merge_result, **merge_result_end = &merge_result;
30 static void add_merge_entry(struct merge_list *entry)
32 *merge_result_end = entry;
33 merge_result_end = &entry->next;
36 static void trivial_merge_trees(struct tree_desc t[3], const char *base);
38 static const char *explanation(struct merge_list *entry)
40 switch (entry->stage) {
41 case 0:
42 return "merged";
43 case 3:
44 return "added in remote";
45 case 2:
46 if (entry->link)
47 return "added in both";
48 return "added in local";
51 /* Existed in base */
52 entry = entry->link;
53 if (!entry)
54 return "removed in both";
56 if (entry->link)
57 return "changed in both";
59 if (entry->stage == 3)
60 return "removed in local";
61 return "removed in remote";
64 static void *result(struct merge_list *entry, unsigned long *size)
66 enum object_type type;
67 struct blob *base, *our, *their;
68 const char *path = entry->path;
70 if (!entry->stage)
71 return read_object_file(&entry->blob->object.oid, &type, size);
72 base = NULL;
73 if (entry->stage == 1) {
74 base = entry->blob;
75 entry = entry->link;
77 our = NULL;
78 if (entry && entry->stage == 2) {
79 our = entry->blob;
80 entry = entry->link;
82 their = NULL;
83 if (entry)
84 their = entry->blob;
85 return merge_blobs(the_repository->index, path,
86 base, our, their, size);
89 static void *origin(struct merge_list *entry, unsigned long *size)
91 enum object_type type;
92 while (entry) {
93 if (entry->stage == 2)
94 return read_object_file(&entry->blob->object.oid,
95 &type, size);
96 entry = entry->link;
98 return NULL;
101 static int show_outf(void *priv UNUSED, mmbuffer_t *mb, int nbuf)
103 int i;
104 for (i = 0; i < nbuf; i++)
105 printf("%.*s", (int) mb[i].size, mb[i].ptr);
106 return 0;
109 static void show_diff(struct merge_list *entry)
111 unsigned long size;
112 mmfile_t src, dst;
113 xpparam_t xpp;
114 xdemitconf_t xecfg;
115 xdemitcb_t ecb = { .out_line = show_outf };
117 memset(&xpp, 0, sizeof(xpp));
118 xpp.flags = 0;
119 memset(&xecfg, 0, sizeof(xecfg));
120 xecfg.ctxlen = 3;
122 src.ptr = origin(entry, &size);
123 if (!src.ptr)
124 size = 0;
125 src.size = size;
126 dst.ptr = result(entry, &size);
127 if (!dst.ptr)
128 size = 0;
129 dst.size = size;
130 if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb))
131 die("unable to generate diff");
132 free(src.ptr);
133 free(dst.ptr);
136 static void show_result_list(struct merge_list *entry)
138 printf("%s\n", explanation(entry));
139 do {
140 struct merge_list *link = entry->link;
141 static const char *desc[4] = { "result", "base", "our", "their" };
142 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path);
143 entry = link;
144 } while (entry);
147 static void show_result(void)
149 struct merge_list *walk;
151 walk = merge_result;
152 while (walk) {
153 show_result_list(walk);
154 show_diff(walk);
155 walk = walk->next;
159 /* An empty entry never compares same, not even to another empty entry */
160 static int same_entry(struct name_entry *a, struct name_entry *b)
162 return !is_null_oid(&a->oid) &&
163 !is_null_oid(&b->oid) &&
164 oideq(&a->oid, &b->oid) &&
165 a->mode == b->mode;
168 static int both_empty(struct name_entry *a, struct name_entry *b)
170 return is_null_oid(&a->oid) && is_null_oid(&b->oid);
173 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
175 struct merge_list *res = xcalloc(1, sizeof(*res));
177 res->stage = stage;
178 res->path = path;
179 res->mode = mode;
180 res->blob = lookup_blob(the_repository, oid);
181 return res;
184 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
186 struct strbuf buf = STRBUF_INIT;
187 strbuf_make_traverse_path(&buf, info, n->path, n->pathlen);
188 return strbuf_detach(&buf, NULL);
191 static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result)
193 struct merge_list *orig, *final;
194 const char *path;
196 /* If it's already ours, don't bother showing it */
197 if (!ours)
198 return;
200 path = traverse_path(info, result);
201 orig = create_entry(2, ours->mode, &ours->oid, path);
202 final = create_entry(0, result->mode, &result->oid, path);
204 final->link = orig;
206 add_merge_entry(final);
209 static void unresolved_directory(const struct traverse_info *info,
210 struct name_entry n[3])
212 struct repository *r = the_repository;
213 char *newbase;
214 struct name_entry *p;
215 struct tree_desc t[3];
216 void *buf0, *buf1, *buf2;
218 for (p = n; p < n + 3; p++) {
219 if (p->mode && S_ISDIR(p->mode))
220 break;
222 if (n + 3 <= p)
223 return; /* there is no tree here */
225 newbase = traverse_path(info, p);
227 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
228 buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
229 buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
230 buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
231 #undef ENTRY_OID
233 trivial_merge_trees(t, newbase);
235 free(buf0);
236 free(buf1);
237 free(buf2);
238 free(newbase);
242 static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
244 const char *path;
245 struct merge_list *link;
247 if (!n->mode)
248 return entry;
249 if (entry)
250 path = entry->path;
251 else
252 path = traverse_path(info, n);
253 link = create_entry(stage, n->mode, &n->oid, path);
254 link->link = entry;
255 return link;
258 static void unresolved(const struct traverse_info *info, struct name_entry n[3])
260 struct merge_list *entry = NULL;
261 int i;
262 unsigned dirmask = 0, mask = 0;
264 for (i = 0; i < 3; i++) {
265 mask |= (1 << i);
267 * Treat missing entries as directories so that we return
268 * after unresolved_directory has handled this.
270 if (!n[i].mode || S_ISDIR(n[i].mode))
271 dirmask |= (1 << i);
274 unresolved_directory(info, n);
276 if (dirmask == mask)
277 return;
279 if (n[2].mode && !S_ISDIR(n[2].mode))
280 entry = link_entry(3, info, n + 2, entry);
281 if (n[1].mode && !S_ISDIR(n[1].mode))
282 entry = link_entry(2, info, n + 1, entry);
283 if (n[0].mode && !S_ISDIR(n[0].mode))
284 entry = link_entry(1, info, n + 0, entry);
286 add_merge_entry(entry);
290 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
291 * as the origin.
293 * This walks the (sorted) trees in lock-step, checking every possible
294 * name. Note that directories automatically sort differently from other
295 * files (see "base_name_compare"), so you'll never see file/directory
296 * conflicts, because they won't ever compare the same.
298 * IOW, if a directory changes to a filename, it will automatically be
299 * seen as the directory going away, and the filename being created.
301 * Think of this as a three-way diff.
303 * The output will be either:
304 * - successful merge
305 * "0 mode sha1 filename"
306 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
307 * in parallel with this too!
309 * - conflict:
310 * "1 mode sha1 filename"
311 * "2 mode sha1 filename"
312 * "3 mode sha1 filename"
313 * where not all of the 1/2/3 lines may exist, of course.
315 * The successful merge rules are the same as for the three-way merge
316 * in git-read-tree.
318 static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
320 /* Same in both? */
321 if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) {
322 /* Modified, added or removed identically */
323 resolve(info, NULL, entry+1);
324 return mask;
327 if (same_entry(entry+0, entry+1)) {
328 if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
329 /* We did not touch, they modified -- take theirs */
330 resolve(info, entry+1, entry+2);
331 return mask;
334 * If we did not touch a directory but they made it
335 * into a file, we fall through and unresolved()
336 * recurses down. Likewise for the opposite case.
340 if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) {
341 /* We added, modified or removed, they did not touch -- take ours */
342 resolve(info, NULL, entry+1);
343 return mask;
346 unresolved(info, entry);
347 return mask;
350 static void trivial_merge_trees(struct tree_desc t[3], const char *base)
352 struct traverse_info info;
354 setup_traverse_info(&info, base);
355 info.fn = threeway_callback;
356 traverse_trees(&the_index, 3, t, &info);
359 static void *get_tree_descriptor(struct repository *r,
360 struct tree_desc *desc,
361 const char *rev)
363 struct object_id oid;
364 void *buf;
366 if (repo_get_oid(r, rev, &oid))
367 die("unknown rev %s", rev);
368 buf = fill_tree_descriptor(r, desc, &oid);
369 if (!buf)
370 die("%s is not a tree", rev);
371 return buf;
374 static int trivial_merge(const char *base,
375 const char *branch1,
376 const char *branch2)
378 struct repository *r = the_repository;
379 struct tree_desc t[3];
380 void *buf1, *buf2, *buf3;
382 buf1 = get_tree_descriptor(r, t+0, base);
383 buf2 = get_tree_descriptor(r, t+1, branch1);
384 buf3 = get_tree_descriptor(r, t+2, branch2);
385 trivial_merge_trees(t, "");
386 free(buf1);
387 free(buf2);
388 free(buf3);
390 show_result();
391 return 0;
394 enum mode {
395 MODE_UNKNOWN,
396 MODE_TRIVIAL,
397 MODE_REAL,
400 struct merge_tree_options {
401 int mode;
402 int allow_unrelated_histories;
403 int show_messages;
404 int name_only;
405 int use_stdin;
408 static int real_merge(struct merge_tree_options *o,
409 const char *branch1, const char *branch2,
410 const char *prefix)
412 struct commit *parent1, *parent2;
413 struct commit_list *merge_bases = NULL;
414 struct merge_options opt;
415 struct merge_result result = { 0 };
416 int show_messages = o->show_messages;
418 parent1 = get_merge_parent(branch1);
419 if (!parent1)
420 help_unknown_ref(branch1, "merge-tree",
421 _("not something we can merge"));
423 parent2 = get_merge_parent(branch2);
424 if (!parent2)
425 help_unknown_ref(branch2, "merge-tree",
426 _("not something we can merge"));
428 init_merge_options(&opt, the_repository);
430 opt.show_rename_progress = 0;
432 opt.branch1 = branch1;
433 opt.branch2 = branch2;
436 * Get the merge bases, in reverse order; see comment above
437 * merge_incore_recursive in merge-ort.h
439 merge_bases = get_merge_bases(parent1, parent2);
440 if (!merge_bases && !o->allow_unrelated_histories)
441 die(_("refusing to merge unrelated histories"));
442 merge_bases = reverse_commit_list(merge_bases);
444 merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
445 if (result.clean < 0)
446 die(_("failure to merge"));
448 if (show_messages == -1)
449 show_messages = !result.clean;
451 if (o->use_stdin)
452 printf("%d%c", result.clean, line_termination);
453 printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination);
454 if (!result.clean) {
455 struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
456 const char *last = NULL;
457 int i;
459 merge_get_conflicted_files(&result, &conflicted_files);
460 for (i = 0; i < conflicted_files.nr; i++) {
461 const char *name = conflicted_files.items[i].string;
462 struct stage_info *c = conflicted_files.items[i].util;
463 if (!o->name_only)
464 printf("%06o %s %d\t",
465 c->mode, oid_to_hex(&c->oid), c->stage);
466 else if (last && !strcmp(last, name))
467 continue;
468 write_name_quoted_relative(
469 name, prefix, stdout, line_termination);
470 last = name;
472 string_list_clear(&conflicted_files, 1);
474 if (show_messages) {
475 putchar(line_termination);
476 merge_display_update_messages(&opt, line_termination == '\0',
477 &result);
479 if (o->use_stdin)
480 putchar(line_termination);
481 merge_finalize(&opt, &result);
482 return !result.clean; /* result.clean < 0 handled above */
485 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
487 struct merge_tree_options o = { .show_messages = -1 };
488 int expected_remaining_argc;
489 int original_argc;
491 const char * const merge_tree_usage[] = {
492 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
493 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
494 NULL
496 struct option mt_options[] = {
497 OPT_CMDMODE(0, "write-tree", &o.mode,
498 N_("do a real merge instead of a trivial merge"),
499 MODE_REAL),
500 OPT_CMDMODE(0, "trivial-merge", &o.mode,
501 N_("do a trivial merge only"), MODE_TRIVIAL),
502 OPT_BOOL(0, "messages", &o.show_messages,
503 N_("also show informational/conflict messages")),
504 OPT_SET_INT('z', NULL, &line_termination,
505 N_("separate paths with the NUL character"), '\0'),
506 OPT_BOOL_F(0, "name-only",
507 &o.name_only,
508 N_("list filenames without modes/oids/stages"),
509 PARSE_OPT_NONEG),
510 OPT_BOOL_F(0, "allow-unrelated-histories",
511 &o.allow_unrelated_histories,
512 N_("allow merging unrelated histories"),
513 PARSE_OPT_NONEG),
514 OPT_BOOL_F(0, "stdin",
515 &o.use_stdin,
516 N_("perform multiple merges, one per line of input"),
517 PARSE_OPT_NONEG),
518 OPT_END()
521 /* Parse arguments */
522 original_argc = argc - 1; /* ignoring argv[0] */
523 argc = parse_options(argc, argv, prefix, mt_options,
524 merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
526 /* Handle --stdin */
527 if (o.use_stdin) {
528 struct strbuf buf = STRBUF_INIT;
530 if (o.mode == MODE_TRIVIAL)
531 die(_("--trivial-merge is incompatible with all other options"));
532 line_termination = '\0';
533 while (strbuf_getline_lf(&buf, stdin) != EOF) {
534 struct strbuf **split;
535 int result;
537 split = strbuf_split(&buf, ' ');
538 if (!split[0] || !split[1] || split[2])
539 die(_("malformed input line: '%s'."), buf.buf);
540 strbuf_rtrim(split[0]);
541 result = real_merge(&o, split[0]->buf, split[1]->buf, prefix);
542 if (result < 0)
543 die(_("merging cannot continue; got unclean result of %d"), result);
544 strbuf_list_free(split);
546 strbuf_release(&buf);
547 return 0;
550 /* Figure out which mode to use */
551 switch (o.mode) {
552 default:
553 BUG("unexpected command mode %d", o.mode);
554 case MODE_UNKNOWN:
555 switch (argc) {
556 default:
557 usage_with_options(merge_tree_usage, mt_options);
558 case 2:
559 o.mode = MODE_REAL;
560 break;
561 case 3:
562 o.mode = MODE_TRIVIAL;
563 break;
565 expected_remaining_argc = argc;
566 break;
567 case MODE_REAL:
568 expected_remaining_argc = 2;
569 break;
570 case MODE_TRIVIAL:
571 expected_remaining_argc = 3;
572 /* Removal of `--trivial-merge` is expected */
573 original_argc--;
574 break;
576 if (o.mode == MODE_TRIVIAL && argc < original_argc)
577 die(_("--trivial-merge is incompatible with all other options"));
579 if (argc != expected_remaining_argc)
580 usage_with_options(merge_tree_usage, mt_options);
582 /* Do the relevant type of merge */
583 if (o.mode == MODE_REAL)
584 return real_merge(&o, argv[0], argv[1], prefix);
585 else
586 return trivial_merge(argv[0], argv[1], argv[2]);