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