1 #define USE_THE_INDEX_VARIABLE
4 #include "xdiff-interface.h"
9 #include "commit-reach.h"
10 #include "merge-ort.h"
11 #include "object-store.h"
12 #include "parse-options.h"
13 #include "repository.h"
16 #include "merge-blobs.h"
19 static int line_termination
= '\n';
22 struct merge_list
*next
;
23 struct merge_list
*link
; /* other stages for this object */
25 unsigned int stage
: 2;
31 static struct merge_list
*merge_result
, **merge_result_end
= &merge_result
;
33 static void add_merge_entry(struct merge_list
*entry
)
35 *merge_result_end
= entry
;
36 merge_result_end
= &entry
->next
;
39 static void trivial_merge_trees(struct tree_desc t
[3], const char *base
);
41 static const char *explanation(struct merge_list
*entry
)
43 switch (entry
->stage
) {
47 return "added in remote";
50 return "added in both";
51 return "added in local";
57 return "removed in both";
60 return "changed in both";
62 if (entry
->stage
== 3)
63 return "removed in local";
64 return "removed in remote";
67 static void *result(struct merge_list
*entry
, unsigned long *size
)
69 enum object_type type
;
70 struct blob
*base
, *our
, *their
;
71 const char *path
= entry
->path
;
74 return repo_read_object_file(the_repository
,
75 &entry
->blob
->object
.oid
, &type
,
78 if (entry
->stage
== 1) {
83 if (entry
&& entry
->stage
== 2) {
90 return merge_blobs(the_repository
->index
, path
,
91 base
, our
, their
, size
);
94 static void *origin(struct merge_list
*entry
, unsigned long *size
)
96 enum object_type type
;
98 if (entry
->stage
== 2)
99 return repo_read_object_file(the_repository
,
100 &entry
->blob
->object
.oid
,
107 static int show_outf(void *priv UNUSED
, mmbuffer_t
*mb
, int nbuf
)
110 for (i
= 0; i
< nbuf
; i
++)
111 printf("%.*s", (int) mb
[i
].size
, mb
[i
].ptr
);
115 static void show_diff(struct merge_list
*entry
)
121 xdemitcb_t ecb
= { .out_line
= show_outf
};
123 memset(&xpp
, 0, sizeof(xpp
));
125 memset(&xecfg
, 0, sizeof(xecfg
));
128 src
.ptr
= origin(entry
, &size
);
132 dst
.ptr
= result(entry
, &size
);
136 if (xdi_diff(&src
, &dst
, &xpp
, &xecfg
, &ecb
))
137 die("unable to generate diff");
142 static void show_result_list(struct merge_list
*entry
)
144 printf("%s\n", explanation(entry
));
146 struct merge_list
*link
= entry
->link
;
147 static const char *desc
[4] = { "result", "base", "our", "their" };
148 printf(" %-6s %o %s %s\n", desc
[entry
->stage
], entry
->mode
, oid_to_hex(&entry
->blob
->object
.oid
), entry
->path
);
153 static void show_result(void)
155 struct merge_list
*walk
;
159 show_result_list(walk
);
165 /* An empty entry never compares same, not even to another empty entry */
166 static int same_entry(struct name_entry
*a
, struct name_entry
*b
)
168 return !is_null_oid(&a
->oid
) &&
169 !is_null_oid(&b
->oid
) &&
170 oideq(&a
->oid
, &b
->oid
) &&
174 static int both_empty(struct name_entry
*a
, struct name_entry
*b
)
176 return is_null_oid(&a
->oid
) && is_null_oid(&b
->oid
);
179 static struct merge_list
*create_entry(unsigned stage
, unsigned mode
, const struct object_id
*oid
, const char *path
)
181 struct merge_list
*res
= xcalloc(1, sizeof(*res
));
186 res
->blob
= lookup_blob(the_repository
, oid
);
190 static char *traverse_path(const struct traverse_info
*info
, const struct name_entry
*n
)
192 struct strbuf buf
= STRBUF_INIT
;
193 strbuf_make_traverse_path(&buf
, info
, n
->path
, n
->pathlen
);
194 return strbuf_detach(&buf
, NULL
);
197 static void resolve(const struct traverse_info
*info
, struct name_entry
*ours
, struct name_entry
*result
)
199 struct merge_list
*orig
, *final
;
202 /* If it's already ours, don't bother showing it */
206 path
= traverse_path(info
, result
);
207 orig
= create_entry(2, ours
->mode
, &ours
->oid
, path
);
208 final
= create_entry(0, result
->mode
, &result
->oid
, path
);
212 add_merge_entry(final
);
215 static void unresolved_directory(const struct traverse_info
*info
,
216 struct name_entry n
[3])
218 struct repository
*r
= the_repository
;
220 struct name_entry
*p
;
221 struct tree_desc t
[3];
222 void *buf0
, *buf1
, *buf2
;
224 for (p
= n
; p
< n
+ 3; p
++) {
225 if (p
->mode
&& S_ISDIR(p
->mode
))
229 return; /* there is no tree here */
231 newbase
= traverse_path(info
, p
);
233 #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
234 buf0
= fill_tree_descriptor(r
, t
+ 0, ENTRY_OID(n
+ 0));
235 buf1
= fill_tree_descriptor(r
, t
+ 1, ENTRY_OID(n
+ 1));
236 buf2
= fill_tree_descriptor(r
, t
+ 2, ENTRY_OID(n
+ 2));
239 trivial_merge_trees(t
, newbase
);
248 static struct merge_list
*link_entry(unsigned stage
, const struct traverse_info
*info
, struct name_entry
*n
, struct merge_list
*entry
)
251 struct merge_list
*link
;
258 path
= traverse_path(info
, n
);
259 link
= create_entry(stage
, n
->mode
, &n
->oid
, path
);
264 static void unresolved(const struct traverse_info
*info
, struct name_entry n
[3])
266 struct merge_list
*entry
= NULL
;
268 unsigned dirmask
= 0, mask
= 0;
270 for (i
= 0; i
< 3; i
++) {
273 * Treat missing entries as directories so that we return
274 * after unresolved_directory has handled this.
276 if (!n
[i
].mode
|| S_ISDIR(n
[i
].mode
))
280 unresolved_directory(info
, n
);
285 if (n
[2].mode
&& !S_ISDIR(n
[2].mode
))
286 entry
= link_entry(3, info
, n
+ 2, entry
);
287 if (n
[1].mode
&& !S_ISDIR(n
[1].mode
))
288 entry
= link_entry(2, info
, n
+ 1, entry
);
289 if (n
[0].mode
&& !S_ISDIR(n
[0].mode
))
290 entry
= link_entry(1, info
, n
+ 0, entry
);
292 add_merge_entry(entry
);
296 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
299 * This walks the (sorted) trees in lock-step, checking every possible
300 * name. Note that directories automatically sort differently from other
301 * files (see "base_name_compare"), so you'll never see file/directory
302 * conflicts, because they won't ever compare the same.
304 * IOW, if a directory changes to a filename, it will automatically be
305 * seen as the directory going away, and the filename being created.
307 * Think of this as a three-way diff.
309 * The output will be either:
311 * "0 mode sha1 filename"
312 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
313 * in parallel with this too!
316 * "1 mode sha1 filename"
317 * "2 mode sha1 filename"
318 * "3 mode sha1 filename"
319 * where not all of the 1/2/3 lines may exist, of course.
321 * The successful merge rules are the same as for the three-way merge
324 static int threeway_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*entry
, struct traverse_info
*info
)
327 if (same_entry(entry
+1, entry
+2) || both_empty(entry
+1, entry
+2)) {
328 /* Modified, added or removed identically */
329 resolve(info
, NULL
, entry
+1);
333 if (same_entry(entry
+0, entry
+1)) {
334 if (!is_null_oid(&entry
[2].oid
) && !S_ISDIR(entry
[2].mode
)) {
335 /* We did not touch, they modified -- take theirs */
336 resolve(info
, entry
+1, entry
+2);
340 * If we did not touch a directory but they made it
341 * into a file, we fall through and unresolved()
342 * recurses down. Likewise for the opposite case.
346 if (same_entry(entry
+0, entry
+2) || both_empty(entry
+0, entry
+2)) {
347 /* We added, modified or removed, they did not touch -- take ours */
348 resolve(info
, NULL
, entry
+1);
352 unresolved(info
, entry
);
356 static void trivial_merge_trees(struct tree_desc t
[3], const char *base
)
358 struct traverse_info info
;
360 setup_traverse_info(&info
, base
);
361 info
.fn
= threeway_callback
;
362 traverse_trees(&the_index
, 3, t
, &info
);
365 static void *get_tree_descriptor(struct repository
*r
,
366 struct tree_desc
*desc
,
369 struct object_id oid
;
372 if (repo_get_oid(r
, rev
, &oid
))
373 die("unknown rev %s", rev
);
374 buf
= fill_tree_descriptor(r
, desc
, &oid
);
376 die("%s is not a tree", rev
);
380 static int trivial_merge(const char *base
,
384 struct repository
*r
= the_repository
;
385 struct tree_desc t
[3];
386 void *buf1
, *buf2
, *buf3
;
388 buf1
= get_tree_descriptor(r
, t
+0, base
);
389 buf2
= get_tree_descriptor(r
, t
+1, branch1
);
390 buf3
= get_tree_descriptor(r
, t
+2, branch2
);
391 trivial_merge_trees(t
, "");
406 struct merge_tree_options
{
408 int allow_unrelated_histories
;
414 static int real_merge(struct merge_tree_options
*o
,
415 const char *merge_base
,
416 const char *branch1
, const char *branch2
,
419 struct commit
*parent1
, *parent2
;
420 struct commit_list
*merge_bases
= NULL
;
421 struct merge_options opt
;
422 struct merge_result result
= { 0 };
423 int show_messages
= o
->show_messages
;
425 parent1
= get_merge_parent(branch1
);
427 help_unknown_ref(branch1
, "merge-tree",
428 _("not something we can merge"));
430 parent2
= get_merge_parent(branch2
);
432 help_unknown_ref(branch2
, "merge-tree",
433 _("not something we can merge"));
435 init_merge_options(&opt
, the_repository
);
437 opt
.show_rename_progress
= 0;
439 opt
.branch1
= branch1
;
440 opt
.branch2
= branch2
;
443 struct commit
*base_commit
;
444 struct tree
*base_tree
, *parent1_tree
, *parent2_tree
;
446 base_commit
= lookup_commit_reference_by_name(merge_base
);
448 die(_("could not lookup commit %s"), merge_base
);
450 opt
.ancestor
= merge_base
;
451 base_tree
= repo_get_commit_tree(the_repository
, base_commit
);
452 parent1_tree
= repo_get_commit_tree(the_repository
, parent1
);
453 parent2_tree
= repo_get_commit_tree(the_repository
, parent2
);
454 merge_incore_nonrecursive(&opt
, base_tree
, parent1_tree
, parent2_tree
, &result
);
457 * Get the merge bases, in reverse order; see comment above
458 * merge_incore_recursive in merge-ort.h
460 merge_bases
= repo_get_merge_bases(the_repository
, parent1
,
462 if (!merge_bases
&& !o
->allow_unrelated_histories
)
463 die(_("refusing to merge unrelated histories"));
464 merge_bases
= reverse_commit_list(merge_bases
);
465 merge_incore_recursive(&opt
, merge_bases
, parent1
, parent2
, &result
);
468 if (result
.clean
< 0)
469 die(_("failure to merge"));
471 if (show_messages
== -1)
472 show_messages
= !result
.clean
;
475 printf("%d%c", result
.clean
, line_termination
);
476 printf("%s%c", oid_to_hex(&result
.tree
->object
.oid
), line_termination
);
478 struct string_list conflicted_files
= STRING_LIST_INIT_NODUP
;
479 const char *last
= NULL
;
482 merge_get_conflicted_files(&result
, &conflicted_files
);
483 for (i
= 0; i
< conflicted_files
.nr
; i
++) {
484 const char *name
= conflicted_files
.items
[i
].string
;
485 struct stage_info
*c
= conflicted_files
.items
[i
].util
;
487 printf("%06o %s %d\t",
488 c
->mode
, oid_to_hex(&c
->oid
), c
->stage
);
489 else if (last
&& !strcmp(last
, name
))
491 write_name_quoted_relative(
492 name
, prefix
, stdout
, line_termination
);
495 string_list_clear(&conflicted_files
, 1);
498 putchar(line_termination
);
499 merge_display_update_messages(&opt
, line_termination
== '\0',
503 putchar(line_termination
);
504 merge_finalize(&opt
, &result
);
505 return !result
.clean
; /* result.clean < 0 handled above */
508 int cmd_merge_tree(int argc
, const char **argv
, const char *prefix
)
510 struct merge_tree_options o
= { .show_messages
= -1 };
511 int expected_remaining_argc
;
513 const char *merge_base
= NULL
;
515 const char * const merge_tree_usage
[] = {
516 N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
517 N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
520 struct option mt_options
[] = {
521 OPT_CMDMODE(0, "write-tree", &o
.mode
,
522 N_("do a real merge instead of a trivial merge"),
524 OPT_CMDMODE(0, "trivial-merge", &o
.mode
,
525 N_("do a trivial merge only"), MODE_TRIVIAL
),
526 OPT_BOOL(0, "messages", &o
.show_messages
,
527 N_("also show informational/conflict messages")),
528 OPT_SET_INT('z', NULL
, &line_termination
,
529 N_("separate paths with the NUL character"), '\0'),
530 OPT_BOOL_F(0, "name-only",
532 N_("list filenames without modes/oids/stages"),
534 OPT_BOOL_F(0, "allow-unrelated-histories",
535 &o
.allow_unrelated_histories
,
536 N_("allow merging unrelated histories"),
538 OPT_BOOL_F(0, "stdin",
540 N_("perform multiple merges, one per line of input"),
542 OPT_STRING(0, "merge-base",
545 N_("specify a merge-base for the merge")),
549 /* Parse arguments */
550 original_argc
= argc
- 1; /* ignoring argv[0] */
551 argc
= parse_options(argc
, argv
, prefix
, mt_options
,
552 merge_tree_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
556 struct strbuf buf
= STRBUF_INIT
;
558 if (o
.mode
== MODE_TRIVIAL
)
559 die(_("--trivial-merge is incompatible with all other options"));
561 die(_("--merge-base is incompatible with --stdin"));
562 line_termination
= '\0';
563 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
564 struct strbuf
**split
;
566 const char *input_merge_base
= NULL
;
568 split
= strbuf_split(&buf
, ' ');
569 if (!split
[0] || !split
[1])
570 die(_("malformed input line: '%s'."), buf
.buf
);
571 strbuf_rtrim(split
[0]);
572 strbuf_rtrim(split
[1]);
574 /* parse the merge-base */
575 if (!strcmp(split
[1]->buf
, "--")) {
576 input_merge_base
= split
[0]->buf
;
579 if (input_merge_base
&& split
[2] && split
[3] && !split
[4]) {
580 strbuf_rtrim(split
[2]);
581 strbuf_rtrim(split
[3]);
582 result
= real_merge(&o
, input_merge_base
, split
[2]->buf
, split
[3]->buf
, prefix
);
583 } else if (!input_merge_base
&& !split
[2]) {
584 result
= real_merge(&o
, NULL
, split
[0]->buf
, split
[1]->buf
, prefix
);
586 die(_("malformed input line: '%s'."), buf
.buf
);
590 die(_("merging cannot continue; got unclean result of %d"), result
);
591 strbuf_list_free(split
);
593 strbuf_release(&buf
);
597 /* Figure out which mode to use */
600 BUG("unexpected command mode %d", o
.mode
);
604 usage_with_options(merge_tree_usage
, mt_options
);
609 o
.mode
= MODE_TRIVIAL
;
612 expected_remaining_argc
= argc
;
615 expected_remaining_argc
= 2;
618 expected_remaining_argc
= 3;
619 /* Removal of `--trivial-merge` is expected */
623 if (o
.mode
== MODE_TRIVIAL
&& argc
< original_argc
)
624 die(_("--trivial-merge is incompatible with all other options"));
626 if (argc
!= expected_remaining_argc
)
627 usage_with_options(merge_tree_usage
, mt_options
);
629 /* Do the relevant type of merge */
630 if (o
.mode
== MODE_REAL
)
631 return real_merge(&o
, merge_base
, argv
[0], argv
[1], prefix
);
633 return trivial_merge(argv
[0], argv
[1], argv
[2]);