1 #define USE_THE_INDEX_VARIABLE
4 #include "xdiff-interface.h"
9 #include "commit-reach.h"
10 #include "merge-ort.h"
11 #include "object-name.h"
12 #include "object-store-ll.h"
13 #include "parse-options.h"
14 #include "repository.h"
17 #include "merge-blobs.h"
23 static int line_termination
= '\n';
26 struct merge_list
*next
;
27 struct merge_list
*link
; /* other stages for this object */
29 unsigned int stage
: 2;
35 static struct merge_list
*merge_result
, **merge_result_end
= &merge_result
;
37 static void add_merge_entry(struct merge_list
*entry
)
39 *merge_result_end
= entry
;
40 merge_result_end
= &entry
->next
;
43 static void trivial_merge_trees(struct tree_desc t
[3], const char *base
);
45 static const char *explanation(struct merge_list
*entry
)
47 switch (entry
->stage
) {
51 return "added in remote";
54 return "added in both";
55 return "added in local";
61 return "removed in both";
64 return "changed in both";
66 if (entry
->stage
== 3)
67 return "removed in local";
68 return "removed in remote";
71 static void *result(struct merge_list
*entry
, unsigned long *size
)
73 enum object_type type
;
74 struct blob
*base
, *our
, *their
;
75 const char *path
= entry
->path
;
78 return repo_read_object_file(the_repository
,
79 &entry
->blob
->object
.oid
, &type
,
82 if (entry
->stage
== 1) {
87 if (entry
&& entry
->stage
== 2) {
94 return merge_blobs(the_repository
->index
, path
,
95 base
, our
, their
, size
);
98 static void *origin(struct merge_list
*entry
, unsigned long *size
)
100 enum object_type type
;
102 if (entry
->stage
== 2)
103 return repo_read_object_file(the_repository
,
104 &entry
->blob
->object
.oid
,
111 static int show_outf(void *priv UNUSED
, mmbuffer_t
*mb
, int nbuf
)
114 for (i
= 0; i
< nbuf
; i
++)
115 printf("%.*s", (int) mb
[i
].size
, mb
[i
].ptr
);
119 static void show_diff(struct merge_list
*entry
)
125 xdemitcb_t ecb
= { .out_line
= show_outf
};
127 memset(&xpp
, 0, sizeof(xpp
));
129 memset(&xecfg
, 0, sizeof(xecfg
));
132 src
.ptr
= origin(entry
, &size
);
136 dst
.ptr
= result(entry
, &size
);
140 if (xdi_diff(&src
, &dst
, &xpp
, &xecfg
, &ecb
))
141 die("unable to generate diff");
146 static void show_result_list(struct merge_list
*entry
)
148 printf("%s\n", explanation(entry
));
150 struct merge_list
*link
= entry
->link
;
151 static const char *desc
[4] = { "result", "base", "our", "their" };
152 printf(" %-6s %o %s %s\n", desc
[entry
->stage
], entry
->mode
, oid_to_hex(&entry
->blob
->object
.oid
), entry
->path
);
157 static void show_result(void)
159 struct merge_list
*walk
;
163 show_result_list(walk
);
169 /* An empty entry never compares same, not even to another empty entry */
170 static int same_entry(struct name_entry
*a
, struct name_entry
*b
)
172 return !is_null_oid(&a
->oid
) &&
173 !is_null_oid(&b
->oid
) &&
174 oideq(&a
->oid
, &b
->oid
) &&
178 static int both_empty(struct name_entry
*a
, struct name_entry
*b
)
180 return is_null_oid(&a
->oid
) && is_null_oid(&b
->oid
);
183 static struct merge_list
*create_entry(unsigned stage
, unsigned mode
, const struct object_id
*oid
, const char *path
)
185 struct merge_list
*res
= xcalloc(1, sizeof(*res
));
190 res
->blob
= lookup_blob(the_repository
, oid
);
194 static char *traverse_path(const struct traverse_info
*info
, const struct name_entry
*n
)
196 struct strbuf buf
= STRBUF_INIT
;
197 strbuf_make_traverse_path(&buf
, info
, n
->path
, n
->pathlen
);
198 return strbuf_detach(&buf
, NULL
);
201 static void resolve(const struct traverse_info
*info
, struct name_entry
*ours
, struct name_entry
*result
)
203 struct merge_list
*orig
, *final
;
206 /* If it's already ours, don't bother showing it */
210 path
= traverse_path(info
, result
);
211 orig
= create_entry(2, ours
->mode
, &ours
->oid
, path
);
212 final
= create_entry(0, result
->mode
, &result
->oid
, path
);
216 add_merge_entry(final
);
219 static void unresolved_directory(const struct traverse_info
*info
,
220 struct name_entry n
[3])
222 struct repository
*r
= the_repository
;
224 struct name_entry
*p
;
225 struct tree_desc t
[3];
226 void *buf0
, *buf1
, *buf2
;
228 for (p
= n
; p
< n
+ 3; p
++) {
229 if (p
->mode
&& S_ISDIR(p
->mode
))
233 return; /* there is no tree here */
235 newbase
= traverse_path(info
, p
);
237 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
238 buf0
= fill_tree_descriptor(r
, t
+ 0, ENTRY_OID(n
+ 0));
239 buf1
= fill_tree_descriptor(r
, t
+ 1, ENTRY_OID(n
+ 1));
240 buf2
= fill_tree_descriptor(r
, t
+ 2, ENTRY_OID(n
+ 2));
243 trivial_merge_trees(t
, newbase
);
252 static struct merge_list
*link_entry(unsigned stage
, const struct traverse_info
*info
, struct name_entry
*n
, struct merge_list
*entry
)
255 struct merge_list
*link
;
262 path
= traverse_path(info
, n
);
263 link
= create_entry(stage
, n
->mode
, &n
->oid
, path
);
268 static void unresolved(const struct traverse_info
*info
, struct name_entry n
[3])
270 struct merge_list
*entry
= NULL
;
272 unsigned dirmask
= 0, mask
= 0;
274 for (i
= 0; i
< 3; i
++) {
277 * Treat missing entries as directories so that we return
278 * after unresolved_directory has handled this.
280 if (!n
[i
].mode
|| S_ISDIR(n
[i
].mode
))
284 unresolved_directory(info
, n
);
289 if (n
[2].mode
&& !S_ISDIR(n
[2].mode
))
290 entry
= link_entry(3, info
, n
+ 2, entry
);
291 if (n
[1].mode
&& !S_ISDIR(n
[1].mode
))
292 entry
= link_entry(2, info
, n
+ 1, entry
);
293 if (n
[0].mode
&& !S_ISDIR(n
[0].mode
))
294 entry
= link_entry(1, info
, n
+ 0, entry
);
296 add_merge_entry(entry
);
300 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
303 * This walks the (sorted) trees in lock-step, checking every possible
304 * name. Note that directories automatically sort differently from other
305 * files (see "base_name_compare"), so you'll never see file/directory
306 * conflicts, because they won't ever compare the same.
308 * IOW, if a directory changes to a filename, it will automatically be
309 * seen as the directory going away, and the filename being created.
311 * Think of this as a three-way diff.
313 * The output will be either:
315 * "0 mode sha1 filename"
316 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
317 * in parallel with this too!
320 * "1 mode sha1 filename"
321 * "2 mode sha1 filename"
322 * "3 mode sha1 filename"
323 * where not all of the 1/2/3 lines may exist, of course.
325 * The successful merge rules are the same as for the three-way merge
328 static int threeway_callback(int n UNUSED
, unsigned long mask
,
329 unsigned long dirmask UNUSED
,
330 struct name_entry
*entry
, struct traverse_info
*info
)
333 if (same_entry(entry
+1, entry
+2) || both_empty(entry
+1, entry
+2)) {
334 /* Modified, added or removed identically */
335 resolve(info
, NULL
, entry
+1);
339 if (same_entry(entry
+0, entry
+1)) {
340 if (!is_null_oid(&entry
[2].oid
) && !S_ISDIR(entry
[2].mode
)) {
341 /* We did not touch, they modified -- take theirs */
342 resolve(info
, entry
+1, entry
+2);
346 * If we did not touch a directory but they made it
347 * into a file, we fall through and unresolved()
348 * recurses down. Likewise for the opposite case.
352 if (same_entry(entry
+0, entry
+2) || both_empty(entry
+0, entry
+2)) {
353 /* We added, modified or removed, they did not touch -- take ours */
354 resolve(info
, NULL
, entry
+1);
358 unresolved(info
, entry
);
362 static void trivial_merge_trees(struct tree_desc t
[3], const char *base
)
364 struct traverse_info info
;
366 setup_traverse_info(&info
, base
);
367 info
.fn
= threeway_callback
;
368 traverse_trees(&the_index
, 3, t
, &info
);
371 static void *get_tree_descriptor(struct repository
*r
,
372 struct tree_desc
*desc
,
375 struct object_id oid
;
378 if (repo_get_oid(r
, rev
, &oid
))
379 die("unknown rev %s", rev
);
380 buf
= fill_tree_descriptor(r
, desc
, &oid
);
382 die("%s is not a tree", rev
);
386 static int trivial_merge(const char *base
,
390 struct repository
*r
= the_repository
;
391 struct tree_desc t
[3];
392 void *buf1
, *buf2
, *buf3
;
394 buf1
= get_tree_descriptor(r
, t
+0, base
);
395 buf2
= get_tree_descriptor(r
, t
+1, branch1
);
396 buf3
= get_tree_descriptor(r
, t
+2, branch2
);
397 trivial_merge_trees(t
, "");
412 struct merge_tree_options
{
414 int allow_unrelated_histories
;
418 struct merge_options merge_options
;
421 static int real_merge(struct merge_tree_options
*o
,
422 const char *merge_base
,
423 const char *branch1
, const char *branch2
,
426 struct commit
*parent1
, *parent2
;
427 struct commit_list
*merge_bases
= NULL
;
428 struct merge_result result
= { 0 };
429 int show_messages
= o
->show_messages
;
430 struct merge_options opt
;
432 copy_merge_options(&opt
, &o
->merge_options
);
433 opt
.show_rename_progress
= 0;
435 opt
.branch1
= branch1
;
436 opt
.branch2
= branch2
;
439 struct tree
*base_tree
, *parent1_tree
, *parent2_tree
;
442 * We actually only need the trees because we already
445 struct object_id base_oid
, head_oid
, merge_oid
;
447 if (repo_get_oid_treeish(the_repository
, merge_base
, &base_oid
))
448 die(_("could not parse as tree '%s'"), merge_base
);
449 base_tree
= parse_tree_indirect(&base_oid
);
451 die(_("unable to read tree (%s)"), oid_to_hex(&base_oid
));
452 if (repo_get_oid_treeish(the_repository
, branch1
, &head_oid
))
453 die(_("could not parse as tree '%s'"), branch1
);
454 parent1_tree
= parse_tree_indirect(&head_oid
);
456 die(_("unable to read tree (%s)"), oid_to_hex(&head_oid
));
457 if (repo_get_oid_treeish(the_repository
, branch2
, &merge_oid
))
458 die(_("could not parse as tree '%s'"), branch2
);
459 parent2_tree
= parse_tree_indirect(&merge_oid
);
461 die(_("unable to read tree (%s)"), oid_to_hex(&merge_oid
));
463 opt
.ancestor
= merge_base
;
464 merge_incore_nonrecursive(&opt
, base_tree
, parent1_tree
, parent2_tree
, &result
);
466 parent1
= get_merge_parent(branch1
);
468 help_unknown_ref(branch1
, "merge-tree",
469 _("not something we can merge"));
471 parent2
= get_merge_parent(branch2
);
473 help_unknown_ref(branch2
, "merge-tree",
474 _("not something we can merge"));
477 * Get the merge bases, in reverse order; see comment above
478 * merge_incore_recursive in merge-ort.h
480 merge_bases
= repo_get_merge_bases(the_repository
, parent1
,
482 if (!merge_bases
&& !o
->allow_unrelated_histories
)
483 die(_("refusing to merge unrelated histories"));
484 merge_bases
= reverse_commit_list(merge_bases
);
485 merge_incore_recursive(&opt
, merge_bases
, parent1
, parent2
, &result
);
488 if (result
.clean
< 0)
489 die(_("failure to merge"));
491 if (show_messages
== -1)
492 show_messages
= !result
.clean
;
495 printf("%d%c", result
.clean
, line_termination
);
496 printf("%s%c", oid_to_hex(&result
.tree
->object
.oid
), line_termination
);
498 struct string_list conflicted_files
= STRING_LIST_INIT_NODUP
;
499 const char *last
= NULL
;
502 merge_get_conflicted_files(&result
, &conflicted_files
);
503 for (i
= 0; i
< conflicted_files
.nr
; i
++) {
504 const char *name
= conflicted_files
.items
[i
].string
;
505 struct stage_info
*c
= conflicted_files
.items
[i
].util
;
507 printf("%06o %s %d\t",
508 c
->mode
, oid_to_hex(&c
->oid
), c
->stage
);
509 else if (last
&& !strcmp(last
, name
))
511 write_name_quoted_relative(
512 name
, prefix
, stdout
, line_termination
);
515 string_list_clear(&conflicted_files
, 1);
518 putchar(line_termination
);
519 merge_display_update_messages(&opt
, line_termination
== '\0',
523 putchar(line_termination
);
524 merge_finalize(&opt
, &result
);
525 clear_merge_options(&opt
);
526 return !result
.clean
; /* result.clean < 0 handled above */
529 int cmd_merge_tree(int argc
, const char **argv
, const char *prefix
)
531 struct merge_tree_options o
= { .show_messages
= -1 };
532 struct strvec xopts
= STRVEC_INIT
;
533 int expected_remaining_argc
;
535 const char *merge_base
= NULL
;
537 const char * const merge_tree_usage
[] = {
538 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
539 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
542 struct option mt_options
[] = {
543 OPT_CMDMODE(0, "write-tree", &o
.mode
,
544 N_("do a real merge instead of a trivial merge"),
546 OPT_CMDMODE(0, "trivial-merge", &o
.mode
,
547 N_("do a trivial merge only"), MODE_TRIVIAL
),
548 OPT_BOOL(0, "messages", &o
.show_messages
,
549 N_("also show informational/conflict messages")),
550 OPT_SET_INT('z', NULL
, &line_termination
,
551 N_("separate paths with the NUL character"), '\0'),
552 OPT_BOOL_F(0, "name-only",
554 N_("list filenames without modes/oids/stages"),
556 OPT_BOOL_F(0, "allow-unrelated-histories",
557 &o
.allow_unrelated_histories
,
558 N_("allow merging unrelated histories"),
560 OPT_BOOL_F(0, "stdin",
562 N_("perform multiple merges, one per line of input"),
564 OPT_STRING(0, "merge-base",
567 N_("specify a merge-base for the merge")),
568 OPT_STRVEC('X', "strategy-option", &xopts
, N_("option=value"),
569 N_("option for selected merge strategy")),
573 /* Init merge options */
574 init_merge_options(&o
.merge_options
, the_repository
);
576 /* Parse arguments */
577 original_argc
= argc
- 1; /* ignoring argv[0] */
578 argc
= parse_options(argc
, argv
, prefix
, mt_options
,
579 merge_tree_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
581 if (xopts
.nr
&& o
.mode
== MODE_TRIVIAL
)
582 die(_("--trivial-merge is incompatible with all other options"));
583 for (int x
= 0; x
< xopts
.nr
; x
++)
584 if (parse_merge_opt(&o
.merge_options
, xopts
.v
[x
]))
585 die(_("unknown strategy option: -X%s"), xopts
.v
[x
]);
589 struct strbuf buf
= STRBUF_INIT
;
591 if (o
.mode
== MODE_TRIVIAL
)
592 die(_("--trivial-merge is incompatible with all other options"));
594 die(_("--merge-base is incompatible with --stdin"));
595 line_termination
= '\0';
596 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
597 struct strbuf
**split
;
599 const char *input_merge_base
= NULL
;
601 split
= strbuf_split(&buf
, ' ');
602 if (!split
[0] || !split
[1])
603 die(_("malformed input line: '%s'."), buf
.buf
);
604 strbuf_rtrim(split
[0]);
605 strbuf_rtrim(split
[1]);
607 /* parse the merge-base */
608 if (!strcmp(split
[1]->buf
, "--")) {
609 input_merge_base
= split
[0]->buf
;
612 if (input_merge_base
&& split
[2] && split
[3] && !split
[4]) {
613 strbuf_rtrim(split
[2]);
614 strbuf_rtrim(split
[3]);
615 result
= real_merge(&o
, input_merge_base
, split
[2]->buf
, split
[3]->buf
, prefix
);
616 } else if (!input_merge_base
&& !split
[2]) {
617 result
= real_merge(&o
, NULL
, split
[0]->buf
, split
[1]->buf
, prefix
);
619 die(_("malformed input line: '%s'."), buf
.buf
);
623 die(_("merging cannot continue; got unclean result of %d"), result
);
624 strbuf_list_free(split
);
626 strbuf_release(&buf
);
630 /* Figure out which mode to use */
633 BUG("unexpected command mode %d", o
.mode
);
637 usage_with_options(merge_tree_usage
, mt_options
);
642 o
.mode
= MODE_TRIVIAL
;
645 expected_remaining_argc
= argc
;
648 expected_remaining_argc
= 2;
651 expected_remaining_argc
= 3;
652 /* Removal of `--trivial-merge` is expected */
656 if (o
.mode
== MODE_TRIVIAL
&& argc
< original_argc
)
657 die(_("--trivial-merge is incompatible with all other options"));
659 if (argc
!= expected_remaining_argc
)
660 usage_with_options(merge_tree_usage
, mt_options
);
662 git_config(git_default_config
, NULL
);
664 /* Do the relevant type of merge */
665 if (o
.mode
== MODE_REAL
)
666 return real_merge(&o
, merge_base
, argv
[0], argv
[1], prefix
);
668 return trivial_merge(argv
[0], argv
[1], argv
[2]);