4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
10 #include "parse-options.h"
12 #include "run-command.h"
18 #include "unpack-trees.h"
19 #include "cache-tree.h"
27 #define DEFAULT_TWOHEAD (1<<0)
28 #define DEFAULT_OCTOPUS (1<<1)
29 #define NO_FAST_FORWARD (1<<2)
30 #define NO_TRIVIAL (1<<3)
37 static const char * const builtin_merge_usage
[] = {
38 "git-merge [options] <remote>...",
39 "git-merge [options] <msg> HEAD <remote>",
43 static int show_diffstat
= 1, option_log
, squash
;
44 static int option_commit
= 1, allow_fast_forward
= 1;
45 static int allow_trivial
= 1, have_message
;
46 static struct strbuf merge_msg
;
47 static struct commit_list
*remoteheads
;
48 static unsigned char head
[20], stash
[20];
49 static struct strategy
**use_strategies
;
50 static size_t use_strategies_nr
, use_strategies_alloc
;
51 static const char *branch
;
53 static struct strategy all_strategy
[] = {
54 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
55 { "octopus", DEFAULT_OCTOPUS
},
57 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
58 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
61 static const char *pull_twohead
, *pull_octopus
;
63 static int option_parse_message(const struct option
*opt
,
64 const char *arg
, int unset
)
66 struct strbuf
*buf
= opt
->value
;
69 strbuf_setlen(buf
, 0);
71 strbuf_addf(buf
, "%s\n\n", arg
);
74 return error("switch `m' requires a value");
78 static struct strategy
*get_strategy(const char *name
)
82 static struct cmdnames main_cmds
, other_cmds
;
88 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
89 if (!strcmp(name
, all_strategy
[i
].name
))
90 return &all_strategy
[i
];
93 struct cmdnames not_strategies
;
95 memset(&main_cmds
, 0, sizeof(struct cmdnames
));
96 memset(&other_cmds
, 0, sizeof(struct cmdnames
));
97 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
98 longest
= load_command_list("git-merge-", &main_cmds
,
100 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
102 struct cmdname
*ent
= main_cmds
.names
[i
];
103 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
104 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
105 && !all_strategy
[j
].name
[ent
->len
])
108 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
109 exclude_cmds(&main_cmds
, ¬_strategies
);
112 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
114 fprintf(stderr
, "Could not find merge strategy '%s'.\n\n", name
);
115 list_commands("strategies", longest
, &main_cmds
, &other_cmds
);
119 ret
= xmalloc(sizeof(struct strategy
));
120 memset(ret
, 0, sizeof(struct strategy
));
121 ret
->name
= xstrdup(name
);
125 static void append_strategy(struct strategy
*s
)
127 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
128 use_strategies
[use_strategies_nr
++] = s
;
131 static int option_parse_strategy(const struct option
*opt
,
132 const char *name
, int unset
)
137 append_strategy(get_strategy(name
));
141 static int option_parse_n(const struct option
*opt
,
142 const char *arg
, int unset
)
144 show_diffstat
= unset
;
148 static struct option builtin_merge_options
[] = {
149 { OPTION_CALLBACK
, 'n', NULL
, NULL
, NULL
,
150 "do not show a diffstat at the end of the merge",
151 PARSE_OPT_NOARG
, option_parse_n
},
152 OPT_BOOLEAN(0, "stat", &show_diffstat
,
153 "show a diffstat at the end of the merge"),
154 OPT_BOOLEAN(0, "summary", &show_diffstat
, "(synonym to --stat)"),
155 OPT_BOOLEAN(0, "log", &option_log
,
156 "add list of one-line log to merge commit message"),
157 OPT_BOOLEAN(0, "squash", &squash
,
158 "create a single commit instead of doing a merge"),
159 OPT_BOOLEAN(0, "commit", &option_commit
,
160 "perform a commit if the merge succeeds (default)"),
161 OPT_BOOLEAN(0, "ff", &allow_fast_forward
,
162 "allow fast forward (default)"),
163 OPT_CALLBACK('s', "strategy", &use_strategies
, "strategy",
164 "merge strategy to use", option_parse_strategy
),
165 OPT_CALLBACK('m', "message", &merge_msg
, "message",
166 "message to be used for the merge commit (if any)",
167 option_parse_message
),
171 /* Cleans up metadata that is uninteresting after a succeeded merge. */
172 static void drop_save(void)
174 unlink(git_path("MERGE_HEAD"));
175 unlink(git_path("MERGE_MSG"));
178 static void save_state(void)
181 struct child_process cp
;
182 struct strbuf buffer
= STRBUF_INIT
;
183 const char *argv
[] = {"stash", "create", NULL
};
185 memset(&cp
, 0, sizeof(cp
));
190 if (start_command(&cp
))
191 die("could not run stash.");
192 len
= strbuf_read(&buffer
, cp
.out
, 1024);
195 if (finish_command(&cp
) || len
< 0)
199 strbuf_setlen(&buffer
, buffer
.len
-1);
200 if (get_sha1(buffer
.buf
, stash
))
201 die("not a valid object: %s", buffer
.buf
);
204 static void reset_hard(unsigned const char *sha1
, int verbose
)
209 args
[i
++] = "read-tree";
212 args
[i
++] = "--reset";
214 args
[i
++] = sha1_to_hex(sha1
);
217 if (run_command_v_opt(args
, RUN_GIT_CMD
))
218 die("read-tree failed");
221 static void restore_state(void)
224 const char *args
[] = { "stash", "apply", NULL
, NULL
};
226 if (is_null_sha1(stash
))
232 args
[2] = sha1_to_hex(stash
);
235 * It is OK to ignore error here, for example when there was
236 * nothing to restore.
238 run_command_v_opt(args
, RUN_GIT_CMD
);
241 refresh_cache(REFRESH_QUIET
);
244 /* This is called when no merge was necessary. */
245 static void finish_up_to_date(const char *msg
)
247 printf("%s%s\n", squash
? " (nothing to squash)" : "", msg
);
251 static void squash_message(void)
254 struct commit
*commit
;
256 struct commit_list
*j
;
259 printf("Squash commit -- not updating HEAD\n");
260 fd
= open(git_path("SQUASH_MSG"), O_WRONLY
| O_CREAT
, 0666);
262 die("Could not write to %s", git_path("SQUASH_MSG"));
264 init_revisions(&rev
, NULL
);
265 rev
.ignore_merges
= 1;
266 rev
.commit_format
= CMIT_FMT_MEDIUM
;
268 commit
= lookup_commit(head
);
269 commit
->object
.flags
|= UNINTERESTING
;
270 add_pending_object(&rev
, &commit
->object
, NULL
);
272 for (j
= remoteheads
; j
; j
= j
->next
)
273 add_pending_object(&rev
, &j
->item
->object
, NULL
);
275 setup_revisions(0, NULL
, &rev
, NULL
);
276 if (prepare_revision_walk(&rev
))
277 die("revision walk setup failed");
279 strbuf_init(&out
, 0);
280 strbuf_addstr(&out
, "Squashed commit of the following:\n");
281 while ((commit
= get_revision(&rev
)) != NULL
) {
282 strbuf_addch(&out
, '\n');
283 strbuf_addf(&out
, "commit %s\n",
284 sha1_to_hex(commit
->object
.sha1
));
285 pretty_print_commit(rev
.commit_format
, commit
, &out
, rev
.abbrev
,
286 NULL
, NULL
, rev
.date_mode
, 0);
288 write(fd
, out
.buf
, out
.len
);
290 strbuf_release(&out
);
293 static int run_hook(const char *name
)
295 struct child_process hook
;
296 const char *argv
[3], *env
[2];
297 char index
[PATH_MAX
];
299 argv
[0] = git_path("hooks/%s", name
);
300 if (access(argv
[0], X_OK
) < 0)
303 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", get_index_file());
313 memset(&hook
, 0, sizeof(hook
));
316 hook
.stdout_to_stderr
= 1;
319 return run_command(&hook
);
322 static void finish(const unsigned char *new_head
, const char *msg
)
324 struct strbuf reflog_message
;
326 strbuf_init(&reflog_message
, 0);
328 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
331 strbuf_addf(&reflog_message
, "%s: %s",
332 getenv("GIT_REFLOG_ACTION"), msg
);
338 printf("No merge message -- not updating HEAD\n");
340 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
341 update_ref(reflog_message
.buf
, "HEAD",
345 * We ignore errors in 'gc --auto', since the
346 * user should see them.
348 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
351 if (new_head
&& show_diffstat
) {
352 struct diff_options opts
;
354 opts
.output_format
|=
355 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
356 opts
.detect_rename
= DIFF_DETECT_RENAME
;
357 if (diff_use_color_default
> 0)
358 DIFF_OPT_SET(&opts
, COLOR_DIFF
);
359 if (diff_setup_done(&opts
) < 0)
360 die("diff_setup_done failed");
361 diff_tree_sha1(head
, new_head
, "", &opts
);
366 /* Run a post-merge hook */
367 run_hook("post-merge");
369 strbuf_release(&reflog_message
);
372 /* Get the name for the merge commit's message. */
373 static void merge_name(const char *remote
, struct strbuf
*msg
)
375 struct object
*remote_head
;
376 unsigned char branch_head
[20], buf_sha
[20];
381 memset(branch_head
, 0, sizeof(branch_head
));
382 remote_head
= peel_to_type(remote
, 0, NULL
, OBJ_COMMIT
);
384 die("'%s' does not point to a commit", remote
);
386 strbuf_init(&buf
, 0);
387 strbuf_addstr(&buf
, "refs/heads/");
388 strbuf_addstr(&buf
, remote
);
389 resolve_ref(buf
.buf
, branch_head
, 0, 0);
391 if (!hashcmp(remote_head
->sha1
, branch_head
)) {
392 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
393 sha1_to_hex(branch_head
), remote
);
397 /* See if remote matches <name>^^^.. or <name>~<number> */
398 for (len
= 0, ptr
= remote
+ strlen(remote
);
399 remote
< ptr
&& ptr
[-1] == '^';
406 ptr
= strrchr(remote
, '~');
408 int seen_nonzero
= 0;
411 while (*++ptr
&& isdigit(*ptr
)) {
412 seen_nonzero
|= (*ptr
!= '0');
416 len
= 0; /* not ...~<number> */
417 else if (seen_nonzero
)
420 early
= 1; /* "name~" is "name~1"! */
424 struct strbuf truname
= STRBUF_INIT
;
425 strbuf_addstr(&truname
, "refs/heads/");
426 strbuf_addstr(&truname
, remote
);
427 strbuf_setlen(&truname
, len
+11);
428 if (resolve_ref(truname
.buf
, buf_sha
, 0, 0)) {
430 "%s\t\tbranch '%s'%s of .\n",
431 sha1_to_hex(remote_head
->sha1
),
433 (early
? " (early part)" : ""));
438 if (!strcmp(remote
, "FETCH_HEAD") &&
439 !access(git_path("FETCH_HEAD"), R_OK
)) {
444 strbuf_init(&line
, 0);
445 fp
= fopen(git_path("FETCH_HEAD"), "r");
447 die("could not open %s for reading: %s",
448 git_path("FETCH_HEAD"), strerror(errno
));
449 strbuf_getline(&line
, fp
, '\n');
451 ptr
= strstr(line
.buf
, "\tnot-for-merge\t");
453 strbuf_remove(&line
, ptr
-line
.buf
+1, 13);
454 strbuf_addbuf(msg
, &line
);
455 strbuf_release(&line
);
458 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
459 sha1_to_hex(remote_head
->sha1
), remote
);
462 static int git_merge_config(const char *k
, const char *v
, void *cb
)
464 if (branch
&& !prefixcmp(k
, "branch.") &&
465 !prefixcmp(k
+ 7, branch
) &&
466 !strcmp(k
+ 7 + strlen(branch
), ".mergeoptions")) {
472 argc
= split_cmdline(buf
, &argv
);
473 argv
= xrealloc(argv
, sizeof(*argv
) * (argc
+ 2));
474 memmove(argv
+ 1, argv
, sizeof(*argv
) * (argc
+ 1));
476 parse_options(argc
, argv
, builtin_merge_options
,
477 builtin_merge_usage
, 0);
481 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
482 show_diffstat
= git_config_bool(k
, v
);
483 else if (!strcmp(k
, "pull.twohead"))
484 return git_config_string(&pull_twohead
, k
, v
);
485 else if (!strcmp(k
, "pull.octopus"))
486 return git_config_string(&pull_octopus
, k
, v
);
487 else if (!strcmp(k
, "merge.log") || !strcmp(k
, "merge.summary"))
488 option_log
= git_config_bool(k
, v
);
489 return git_diff_ui_config(k
, v
, cb
);
492 static int read_tree_trivial(unsigned char *common
, unsigned char *head
,
496 struct tree
*trees
[MAX_UNPACK_TREES
];
497 struct tree_desc t
[MAX_UNPACK_TREES
];
498 struct unpack_trees_options opts
;
500 memset(&opts
, 0, sizeof(opts
));
502 opts
.src_index
= &the_index
;
503 opts
.dst_index
= &the_index
;
505 opts
.verbose_update
= 1;
506 opts
.trivial_merges_only
= 1;
508 trees
[nr_trees
] = parse_tree_indirect(common
);
509 if (!trees
[nr_trees
++])
511 trees
[nr_trees
] = parse_tree_indirect(head
);
512 if (!trees
[nr_trees
++])
514 trees
[nr_trees
] = parse_tree_indirect(one
);
515 if (!trees
[nr_trees
++])
517 opts
.fn
= threeway_merge
;
518 cache_tree_free(&active_cache_tree
);
519 for (i
= 0; i
< nr_trees
; i
++) {
520 parse_tree(trees
[i
]);
521 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
523 if (unpack_trees(nr_trees
, t
, &opts
))
528 static void write_tree_trivial(unsigned char *sha1
)
530 if (write_cache_as_tree(sha1
, 0, NULL
))
531 die("git write-tree failed to write a tree");
534 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
535 const char *head_arg
)
539 struct commit_list
*j
;
542 args
= xmalloc((4 + commit_list_count(common
) +
543 commit_list_count(remoteheads
)) * sizeof(char *));
544 strbuf_init(&buf
, 0);
545 strbuf_addf(&buf
, "merge-%s", strategy
);
547 for (j
= common
; j
; j
= j
->next
)
548 args
[i
++] = xstrdup(sha1_to_hex(j
->item
->object
.sha1
));
550 args
[i
++] = head_arg
;
551 for (j
= remoteheads
; j
; j
= j
->next
)
552 args
[i
++] = xstrdup(sha1_to_hex(j
->item
->object
.sha1
));
554 ret
= run_command_v_opt(args
, RUN_GIT_CMD
);
555 strbuf_release(&buf
);
557 for (j
= common
; j
; j
= j
->next
)
558 free((void *)args
[i
++]);
560 for (j
= remoteheads
; j
; j
= j
->next
)
561 free((void *)args
[i
++]);
566 static void count_diff_files(struct diff_queue_struct
*q
,
567 struct diff_options
*opt
, void *data
)
574 static int count_unmerged_entries(void)
576 const struct index_state
*state
= &the_index
;
579 for (i
= 0; i
< state
->cache_nr
; i
++)
580 if (ce_stage(state
->cache
[i
]))
586 static int checkout_fast_forward(unsigned char *head
, unsigned char *remote
)
588 struct tree
*trees
[MAX_UNPACK_TREES
];
589 struct unpack_trees_options opts
;
590 struct tree_desc t
[MAX_UNPACK_TREES
];
591 int i
, fd
, nr_trees
= 0;
592 struct dir_struct dir
;
593 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
595 if (read_cache_unmerged())
596 die("you need to resolve your current index first");
598 fd
= hold_locked_index(lock_file
, 1);
600 memset(&trees
, 0, sizeof(trees
));
601 memset(&opts
, 0, sizeof(opts
));
602 memset(&t
, 0, sizeof(t
));
603 memset(&dir
, 0, sizeof(dir
));
604 dir
.show_ignored
= 1;
605 dir
.exclude_per_dir
= ".gitignore";
609 opts
.src_index
= &the_index
;
610 opts
.dst_index
= &the_index
;
612 opts
.verbose_update
= 1;
614 opts
.fn
= twoway_merge
;
616 trees
[nr_trees
] = parse_tree_indirect(head
);
617 if (!trees
[nr_trees
++])
619 trees
[nr_trees
] = parse_tree_indirect(remote
);
620 if (!trees
[nr_trees
++])
622 for (i
= 0; i
< nr_trees
; i
++) {
623 parse_tree(trees
[i
]);
624 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
626 if (unpack_trees(nr_trees
, t
, &opts
))
628 if (write_cache(fd
, active_cache
, active_nr
) ||
629 commit_locked_index(lock_file
))
630 die("unable to write new index file");
634 static void split_merge_strategies(const char *string
, struct strategy
**list
,
642 buf
= xstrdup(string
);
647 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
648 (*list
)[(*nr
)++].name
= xstrdup(q
);
653 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
654 (*list
)[(*nr
)++].name
= xstrdup(q
);
660 static void add_strategies(const char *string
, unsigned attr
)
662 struct strategy
*list
= NULL
;
663 int list_alloc
= 0, list_nr
= 0, i
;
665 memset(&list
, 0, sizeof(list
));
666 split_merge_strategies(string
, &list
, &list_nr
, &list_alloc
);
668 for (i
= 0; i
< list_nr
; i
++)
669 append_strategy(get_strategy(list
[i
].name
));
672 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
673 if (all_strategy
[i
].attr
& attr
)
674 append_strategy(&all_strategy
[i
]);
678 static int merge_trivial(void)
680 unsigned char result_tree
[20], result_commit
[20];
681 struct commit_list parent
;
683 write_tree_trivial(result_tree
);
684 printf("Wonderful.\n");
685 parent
.item
= remoteheads
->item
;
687 commit_tree(merge_msg
.buf
, result_tree
, &parent
, result_commit
);
688 finish(result_commit
, "In-index merge");
693 static int finish_automerge(struct commit_list
*common
,
694 unsigned char *result_tree
,
695 const char *wt_strategy
)
697 struct commit_list
*parents
= NULL
, *j
;
698 struct strbuf buf
= STRBUF_INIT
;
699 unsigned char result_commit
[20];
701 free_commit_list(common
);
702 if (allow_fast_forward
) {
703 parents
= remoteheads
;
704 commit_list_insert(lookup_commit(head
), &parents
);
705 parents
= reduce_heads(parents
);
707 struct commit_list
**pptr
= &parents
;
709 pptr
= &commit_list_insert(lookup_commit(head
),
711 for (j
= remoteheads
; j
; j
= j
->next
)
712 pptr
= &commit_list_insert(j
->item
, pptr
)->next
;
714 free_commit_list(remoteheads
);
715 strbuf_addch(&merge_msg
, '\n');
716 commit_tree(merge_msg
.buf
, result_tree
, parents
, result_commit
);
717 strbuf_addf(&buf
, "Merge made by %s.", wt_strategy
);
718 finish(result_commit
, buf
.buf
);
719 strbuf_release(&buf
);
724 static int suggest_conflicts(void)
729 fp
= fopen(git_path("MERGE_MSG"), "a");
731 die("Could open %s for writing", git_path("MERGE_MSG"));
732 fprintf(fp
, "\nConflicts:\n");
733 for (pos
= 0; pos
< active_nr
; pos
++) {
734 struct cache_entry
*ce
= active_cache
[pos
];
737 fprintf(fp
, "\t%s\n", ce
->name
);
738 while (pos
+ 1 < active_nr
&&
740 active_cache
[pos
+ 1]->name
))
746 printf("Automatic merge failed; "
747 "fix conflicts and then commit the result.\n");
751 static struct commit
*is_old_style_invocation(int argc
, const char **argv
)
753 struct commit
*second_token
= NULL
;
755 unsigned char second_sha1
[20];
757 if (get_sha1(argv
[1], second_sha1
))
759 second_token
= lookup_commit_reference_gently(second_sha1
, 0);
761 die("'%s' is not a commit", argv
[1]);
762 if (hashcmp(second_token
->object
.sha1
, head
))
768 static int evaluate_result(void)
773 if (read_cache() < 0)
774 die("failed to read the cache");
776 /* Check how many files differ. */
777 init_revisions(&rev
, "");
778 setup_revisions(0, NULL
, &rev
, NULL
);
779 rev
.diffopt
.output_format
|=
780 DIFF_FORMAT_CALLBACK
;
781 rev
.diffopt
.format_callback
= count_diff_files
;
782 rev
.diffopt
.format_callback_data
= &cnt
;
783 run_diff_files(&rev
, 0);
786 * Check how many unmerged entries are
789 cnt
+= count_unmerged_entries();
794 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
796 unsigned char result_tree
[20];
798 const char *head_arg
;
799 int flag
, head_invalid
= 0, i
;
800 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
801 struct commit_list
*common
= NULL
;
802 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
803 struct commit_list
**remotes
= &remoteheads
;
806 if (unmerged_cache())
807 die("You are in the middle of a conflicted merge.");
810 * Check if we are _not_ on a detached HEAD, i.e. if there is a
813 branch
= resolve_ref("HEAD", head
, 0, &flag
);
814 if (branch
&& !prefixcmp(branch
, "refs/heads/"))
816 if (is_null_sha1(head
))
819 git_config(git_merge_config
, NULL
);
822 if (diff_use_color_default
== -1)
823 diff_use_color_default
= git_use_color_default
;
825 argc
= parse_options(argc
, argv
, builtin_merge_options
,
826 builtin_merge_usage
, 0);
829 if (!allow_fast_forward
)
830 die("You cannot combine --squash with --no-ff.");
835 usage_with_options(builtin_merge_usage
,
836 builtin_merge_options
);
839 * This could be traditional "merge <msg> HEAD <commit>..." and
840 * the way we can tell it is to see if the second token is HEAD,
841 * but some people might have misused the interface and used a
842 * committish that is the same as HEAD there instead.
843 * Traditional format never would have "-m" so it is an
844 * additional safety measure to check for it.
846 strbuf_init(&buf
, 0);
848 if (!have_message
&& is_old_style_invocation(argc
, argv
)) {
849 strbuf_addstr(&merge_msg
, argv
[0]);
853 } else if (head_invalid
) {
854 struct object
*remote_head
;
856 * If the merged head is a valid one there is no reason
857 * to forbid "git merge" into a branch yet to be born.
858 * We do the same for "git pull".
861 die("Can merge only exactly one commit into "
863 remote_head
= peel_to_type(argv
[0], 0, NULL
, OBJ_COMMIT
);
865 die("%s - not something we can merge", argv
[0]);
866 update_ref("initial pull", "HEAD", remote_head
->sha1
, NULL
, 0,
868 reset_hard(remote_head
->sha1
, 0);
873 /* We are invoked directly as the first-class UI. */
877 * All the rest are the commits being merged;
878 * prepare the standard merge summary message to
879 * be appended to the given message. If remote
880 * is invalid we will die later in the common
881 * codepath so we discard the error in this
884 strbuf_init(&msg
, 0);
885 for (i
= 0; i
< argc
; i
++)
886 merge_name(argv
[i
], &msg
);
887 fmt_merge_msg(option_log
, &msg
, &merge_msg
);
889 strbuf_setlen(&merge_msg
, merge_msg
.len
-1);
892 if (head_invalid
|| !argc
)
893 usage_with_options(builtin_merge_usage
,
894 builtin_merge_options
);
896 strbuf_addstr(&buf
, "merge");
897 for (i
= 0; i
< argc
; i
++)
898 strbuf_addf(&buf
, " %s", argv
[i
]);
899 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
902 for (i
= 0; i
< argc
; i
++) {
905 o
= peel_to_type(argv
[i
], 0, NULL
, OBJ_COMMIT
);
907 die("%s - not something we can merge", argv
[i
]);
908 remotes
= &commit_list_insert(lookup_commit(o
->sha1
),
911 strbuf_addf(&buf
, "GITHEAD_%s", sha1_to_hex(o
->sha1
));
912 setenv(buf
.buf
, argv
[i
], 1);
916 if (!use_strategies
) {
917 if (!remoteheads
->next
)
918 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
920 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
923 for (i
= 0; i
< use_strategies_nr
; i
++) {
924 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
925 allow_fast_forward
= 0;
926 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
930 if (!remoteheads
->next
)
931 common
= get_merge_bases(lookup_commit(head
),
932 remoteheads
->item
, 1);
934 struct commit_list
*list
= remoteheads
;
935 commit_list_insert(lookup_commit(head
), &list
);
936 common
= get_octopus_merge_bases(list
);
940 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head
, NULL
, 0,
944 ; /* No common ancestors found. We need a real merge. */
945 else if (!remoteheads
->next
&& !common
->next
&&
946 common
->item
== remoteheads
->item
) {
948 * If head can reach all the merge then we are up to date.
949 * but first the most common case of merging one remote.
951 finish_up_to_date("Already up-to-date.");
953 } else if (allow_fast_forward
&& !remoteheads
->next
&&
955 !hashcmp(common
->item
->object
.sha1
, head
)) {
956 /* Again the most common case of merging one remote. */
961 strcpy(hex
, find_unique_abbrev(head
, DEFAULT_ABBREV
));
963 printf("Updating %s..%s\n",
965 find_unique_abbrev(remoteheads
->item
->object
.sha1
,
967 refresh_cache(REFRESH_QUIET
);
968 strbuf_init(&msg
, 0);
969 strbuf_addstr(&msg
, "Fast forward");
972 " (no commit created; -m option ignored)");
973 o
= peel_to_type(sha1_to_hex(remoteheads
->item
->object
.sha1
),
974 0, NULL
, OBJ_COMMIT
);
978 if (checkout_fast_forward(head
, remoteheads
->item
->object
.sha1
))
981 finish(o
->sha1
, msg
.buf
);
984 } else if (!remoteheads
->next
&& common
->next
)
987 * We are not doing octopus and not fast forward. Need
990 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
992 * We are not doing octopus, not fast forward, and have
995 refresh_cache(REFRESH_QUIET
);
997 /* See if it is really trivial. */
998 git_committer_info(IDENT_ERROR_ON_NO_NAME
);
999 printf("Trying really trivial in-index merge...\n");
1000 if (!read_tree_trivial(common
->item
->object
.sha1
,
1001 head
, remoteheads
->item
->object
.sha1
))
1002 return merge_trivial();
1007 * An octopus. If we can reach all the remote we are up
1011 struct commit_list
*j
;
1013 for (j
= remoteheads
; j
; j
= j
->next
) {
1014 struct commit_list
*common_one
;
1017 * Here we *have* to calculate the individual
1018 * merge_bases again, otherwise "git merge HEAD^
1019 * HEAD^^" would be missed.
1021 common_one
= get_merge_bases(lookup_commit(head
),
1023 if (hashcmp(common_one
->item
->object
.sha1
,
1024 j
->item
->object
.sha1
)) {
1030 finish_up_to_date("Already up-to-date. Yeeah!");
1035 /* We are going to make a new commit. */
1036 git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1039 * At this point, we need a real merge. No matter what strategy
1040 * we use, it would operate on the index, possibly affecting the
1041 * working tree, and when resolved cleanly, have the desired
1042 * tree in the index -- this means that the index must be in
1043 * sync with the head commit. The strategies are responsible
1046 if (use_strategies_nr
!= 1) {
1048 * Stash away the local changes so that we can try more
1053 memcpy(stash
, null_sha1
, 20);
1056 for (i
= 0; i
< use_strategies_nr
; i
++) {
1059 printf("Rewinding the tree to pristine...\n");
1062 if (use_strategies_nr
!= 1)
1063 printf("Trying merge strategy %s...\n",
1064 use_strategies
[i
]->name
);
1066 * Remember which strategy left the state in the working
1069 wt_strategy
= use_strategies
[i
]->name
;
1071 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1073 if (!option_commit
&& !ret
) {
1076 * This is necessary here just to avoid writing
1077 * the tree, but later we will *not* exit with
1078 * status code 1 because merge_was_ok is set.
1085 * The backend exits with 1 when conflicts are
1086 * left to be resolved, with 2 when it does not
1087 * handle the given merge at all.
1090 int cnt
= evaluate_result();
1092 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1093 best_strategy
= use_strategies
[i
]->name
;
1103 /* Automerge succeeded. */
1104 write_tree_trivial(result_tree
);
1105 automerge_was_ok
= 1;
1110 * If we have a resulting tree, that means the strategy module
1111 * auto resolved the merge cleanly.
1113 if (automerge_was_ok
)
1114 return finish_automerge(common
, result_tree
, wt_strategy
);
1117 * Pick the result from the best strategy and have the user fix
1120 if (!best_strategy
) {
1122 if (use_strategies_nr
> 1)
1124 "No merge strategy handled the merge.\n");
1126 fprintf(stderr
, "Merge with strategy %s failed.\n",
1127 use_strategies
[0]->name
);
1129 } else if (best_strategy
== wt_strategy
)
1130 ; /* We already have its result in the working tree. */
1132 printf("Rewinding the tree to pristine...\n");
1134 printf("Using the %s to prepare resolving by hand.\n",
1136 try_merge_strategy(best_strategy
, common
, head_arg
);
1143 struct commit_list
*j
;
1145 for (j
= remoteheads
; j
; j
= j
->next
)
1146 strbuf_addf(&buf
, "%s\n",
1147 sha1_to_hex(j
->item
->object
.sha1
));
1148 fd
= open(git_path("MERGE_HEAD"), O_WRONLY
| O_CREAT
, 0666);
1150 die("Could open %s for writing",
1151 git_path("MERGE_HEAD"));
1152 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
)
1153 die("Could not write to %s", git_path("MERGE_HEAD"));
1155 strbuf_addch(&merge_msg
, '\n');
1156 fd
= open(git_path("MERGE_MSG"), O_WRONLY
| O_CREAT
, 0666);
1158 die("Could open %s for writing", git_path("MERGE_MSG"));
1159 if (write_in_full(fd
, merge_msg
.buf
, merge_msg
.len
) !=
1161 die("Could not write to %s", git_path("MERGE_MSG"));
1166 fprintf(stderr
, "Automatic merge went well; "
1167 "stopped before committing as requested\n");
1170 return suggest_conflicts();