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