1 #define USE_THE_INDEX_VARIABLE
4 #include "xdiff-interface.h"
7 #include "commit-reach.h"
9 #include "object-store.h"
10 #include "parse-options.h"
11 #include "repository.h"
14 #include "merge-blobs.h"
17 static int line_termination
= '\n';
20 struct merge_list
*next
;
21 struct merge_list
*link
; /* other stages for this object */
23 unsigned int stage
: 2;
29 static struct merge_list
*merge_result
, **merge_result_end
= &merge_result
;
31 static void add_merge_entry(struct merge_list
*entry
)
33 *merge_result_end
= entry
;
34 merge_result_end
= &entry
->next
;
37 static void trivial_merge_trees(struct tree_desc t
[3], const char *base
);
39 static const char *explanation(struct merge_list
*entry
)
41 switch (entry
->stage
) {
45 return "added in remote";
48 return "added in both";
49 return "added in local";
55 return "removed in both";
58 return "changed in both";
60 if (entry
->stage
== 3)
61 return "removed in local";
62 return "removed in remote";
65 static void *result(struct merge_list
*entry
, unsigned long *size
)
67 enum object_type type
;
68 struct blob
*base
, *our
, *their
;
69 const char *path
= entry
->path
;
72 return read_object_file(&entry
->blob
->object
.oid
, &type
, size
);
74 if (entry
->stage
== 1) {
79 if (entry
&& entry
->stage
== 2) {
86 return merge_blobs(the_repository
->index
, path
,
87 base
, our
, their
, size
);
90 static void *origin(struct merge_list
*entry
, unsigned long *size
)
92 enum object_type type
;
94 if (entry
->stage
== 2)
95 return read_object_file(&entry
->blob
->object
.oid
,
102 static int show_outf(void *priv UNUSED
, mmbuffer_t
*mb
, int nbuf
)
105 for (i
= 0; i
< nbuf
; i
++)
106 printf("%.*s", (int) mb
[i
].size
, mb
[i
].ptr
);
110 static void show_diff(struct merge_list
*entry
)
116 xdemitcb_t ecb
= { .out_line
= show_outf
};
118 memset(&xpp
, 0, sizeof(xpp
));
120 memset(&xecfg
, 0, sizeof(xecfg
));
123 src
.ptr
= origin(entry
, &size
);
127 dst
.ptr
= result(entry
, &size
);
131 if (xdi_diff(&src
, &dst
, &xpp
, &xecfg
, &ecb
))
132 die("unable to generate diff");
137 static void show_result_list(struct merge_list
*entry
)
139 printf("%s\n", explanation(entry
));
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
);
148 static void show_result(void)
150 struct merge_list
*walk
;
154 show_result_list(walk
);
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
)
163 return !is_null_oid(&a
->oid
) &&
164 !is_null_oid(&b
->oid
) &&
165 oideq(&a
->oid
, &b
->oid
) &&
169 static int both_empty(struct name_entry
*a
, struct name_entry
*b
)
171 return is_null_oid(&a
->oid
) && is_null_oid(&b
->oid
);
174 static struct merge_list
*create_entry(unsigned stage
, unsigned mode
, const struct object_id
*oid
, const char *path
)
176 struct merge_list
*res
= xcalloc(1, sizeof(*res
));
181 res
->blob
= lookup_blob(the_repository
, oid
);
185 static char *traverse_path(const struct traverse_info
*info
, const struct name_entry
*n
)
187 struct strbuf buf
= STRBUF_INIT
;
188 strbuf_make_traverse_path(&buf
, info
, n
->path
, n
->pathlen
);
189 return strbuf_detach(&buf
, NULL
);
192 static void resolve(const struct traverse_info
*info
, struct name_entry
*ours
, struct name_entry
*result
)
194 struct merge_list
*orig
, *final
;
197 /* If it's already ours, don't bother showing it */
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
);
207 add_merge_entry(final
);
210 static void unresolved_directory(const struct traverse_info
*info
,
211 struct name_entry n
[3])
213 struct repository
*r
= the_repository
;
215 struct name_entry
*p
;
216 struct tree_desc t
[3];
217 void *buf0
, *buf1
, *buf2
;
219 for (p
= n
; p
< n
+ 3; p
++) {
220 if (p
->mode
&& S_ISDIR(p
->mode
))
224 return; /* there is no tree here */
226 newbase
= traverse_path(info
, p
);
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));
234 trivial_merge_trees(t
, newbase
);
243 static struct merge_list
*link_entry(unsigned stage
, const struct traverse_info
*info
, struct name_entry
*n
, struct merge_list
*entry
)
246 struct merge_list
*link
;
253 path
= traverse_path(info
, n
);
254 link
= create_entry(stage
, n
->mode
, &n
->oid
, path
);
259 static void unresolved(const struct traverse_info
*info
, struct name_entry n
[3])
261 struct merge_list
*entry
= NULL
;
263 unsigned dirmask
= 0, mask
= 0;
265 for (i
= 0; i
< 3; i
++) {
268 * Treat missing entries as directories so that we return
269 * after unresolved_directory has handled this.
271 if (!n
[i
].mode
|| S_ISDIR(n
[i
].mode
))
275 unresolved_directory(info
, n
);
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
);
287 add_merge_entry(entry
);
291 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
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.
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.
302 * Think of this as a three-way diff.
304 * The output will be either:
306 * "0 mode sha1 filename"
307 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
308 * in parallel with this too!
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.
316 * The successful merge rules are the same as for the three-way merge
319 static int threeway_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*entry
, struct traverse_info
*info
)
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);
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);
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.
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);
347 unresolved(info
, entry
);
351 static void trivial_merge_trees(struct tree_desc t
[3], const char *base
)
353 struct traverse_info info
;
355 setup_traverse_info(&info
, base
);
356 info
.fn
= threeway_callback
;
357 traverse_trees(&the_index
, 3, t
, &info
);
360 static void *get_tree_descriptor(struct repository
*r
,
361 struct tree_desc
*desc
,
364 struct object_id oid
;
367 if (repo_get_oid(r
, rev
, &oid
))
368 die("unknown rev %s", rev
);
369 buf
= fill_tree_descriptor(r
, desc
, &oid
);
371 die("%s is not a tree", rev
);
375 static int trivial_merge(const char *base
,
379 struct repository
*r
= the_repository
;
380 struct tree_desc t
[3];
381 void *buf1
, *buf2
, *buf3
;
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
, "");
401 struct merge_tree_options
{
403 int allow_unrelated_histories
;
409 static int real_merge(struct merge_tree_options
*o
,
410 const char *merge_base
,
411 const char *branch1
, const char *branch2
,
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
;
420 parent1
= get_merge_parent(branch1
);
422 help_unknown_ref(branch1
, "merge-tree",
423 _("not something we can merge"));
425 parent2
= get_merge_parent(branch2
);
427 help_unknown_ref(branch2
, "merge-tree",
428 _("not something we can merge"));
430 init_merge_options(&opt
, the_repository
);
432 opt
.show_rename_progress
= 0;
434 opt
.branch1
= branch1
;
435 opt
.branch2
= branch2
;
438 struct commit
*base_commit
;
439 struct tree
*base_tree
, *parent1_tree
, *parent2_tree
;
441 base_commit
= lookup_commit_reference_by_name(merge_base
);
443 die(_("could not lookup commit %s"), merge_base
);
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
);
452 * Get the merge bases, in reverse order; see comment above
453 * merge_incore_recursive in merge-ort.h
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
);
462 if (result
.clean
< 0)
463 die(_("failure to merge"));
465 if (show_messages
== -1)
466 show_messages
= !result
.clean
;
469 printf("%d%c", result
.clean
, line_termination
);
470 printf("%s%c", oid_to_hex(&result
.tree
->object
.oid
), line_termination
);
472 struct string_list conflicted_files
= STRING_LIST_INIT_NODUP
;
473 const char *last
= NULL
;
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
;
481 printf("%06o %s %d\t",
482 c
->mode
, oid_to_hex(&c
->oid
), c
->stage
);
483 else if (last
&& !strcmp(last
, name
))
485 write_name_quoted_relative(
486 name
, prefix
, stdout
, line_termination
);
489 string_list_clear(&conflicted_files
, 1);
492 putchar(line_termination
);
493 merge_display_update_messages(&opt
, line_termination
== '\0',
497 putchar(line_termination
);
498 merge_finalize(&opt
, &result
);
499 return !result
.clean
; /* result.clean < 0 handled above */
502 int cmd_merge_tree(int argc
, const char **argv
, const char *prefix
)
504 struct merge_tree_options o
= { .show_messages
= -1 };
505 int expected_remaining_argc
;
507 const char *merge_base
= NULL
;
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>"),
514 struct option mt_options
[] = {
515 OPT_CMDMODE(0, "write-tree", &o
.mode
,
516 N_("do a real merge instead of a trivial merge"),
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",
526 N_("list filenames without modes/oids/stages"),
528 OPT_BOOL_F(0, "allow-unrelated-histories",
529 &o
.allow_unrelated_histories
,
530 N_("allow merging unrelated histories"),
532 OPT_BOOL_F(0, "stdin",
534 N_("perform multiple merges, one per line of input"),
536 OPT_STRING(0, "merge-base",
539 N_("specify a merge-base for the merge")),
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
);
550 struct strbuf buf
= STRBUF_INIT
;
552 if (o
.mode
== MODE_TRIVIAL
)
553 die(_("--trivial-merge is incompatible with all other options"));
555 die(_("--merge-base is incompatible with --stdin"));
556 line_termination
= '\0';
557 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
558 struct strbuf
**split
;
560 const char *input_merge_base
= NULL
;
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]);
568 /* parse the merge-base */
569 if (!strcmp(split
[1]->buf
, "--")) {
570 input_merge_base
= split
[0]->buf
;
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
);
580 die(_("malformed input line: '%s'."), buf
.buf
);
584 die(_("merging cannot continue; got unclean result of %d"), result
);
585 strbuf_list_free(split
);
587 strbuf_release(&buf
);
591 /* Figure out which mode to use */
594 BUG("unexpected command mode %d", o
.mode
);
598 usage_with_options(merge_tree_usage
, mt_options
);
603 o
.mode
= MODE_TRIVIAL
;
606 expected_remaining_argc
= argc
;
609 expected_remaining_argc
= 2;
612 expected_remaining_argc
= 3;
613 /* Removal of `--trivial-merge` is expected */
617 if (o
.mode
== MODE_TRIVIAL
&& argc
< original_argc
)
618 die(_("--trivial-merge is incompatible with all other options"));
620 if (argc
!= expected_remaining_argc
)
621 usage_with_options(merge_tree_usage
, mt_options
);
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
);
627 return trivial_merge(argv
[0], argv
[1], argv
[2]);