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