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"
26 #define DEFAULT_TWOHEAD (1<<0)
27 #define DEFAULT_OCTOPUS (1<<1)
28 #define NO_FAST_FORWARD (1<<2)
29 #define NO_TRIVIAL (1<<3)
36 static const char * const builtin_merge_usage
[] = {
37 "git-merge [options] <remote>...",
38 "git-merge [options] <msg> HEAD <remote>",
42 static int show_diffstat
= 1, option_log
, squash
;
43 static int option_commit
= 1, allow_fast_forward
= 1;
44 static int allow_trivial
= 1, have_message
;
45 static struct strbuf merge_msg
;
46 static struct commit_list
*remoteheads
;
47 static unsigned char head
[20], stash
[20];
48 static struct strategy
**use_strategies
;
49 static size_t use_strategies_nr
, use_strategies_alloc
;
50 static const char *branch
;
52 static struct strategy all_strategy
[] = {
53 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
54 { "octopus", DEFAULT_OCTOPUS
},
56 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
57 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
60 static const char *pull_twohead
, *pull_octopus
;
62 static int option_parse_message(const struct option
*opt
,
63 const char *arg
, int unset
)
65 struct strbuf
*buf
= opt
->value
;
68 strbuf_setlen(buf
, 0);
70 strbuf_addf(buf
, "%s\n\n", arg
);
73 return error("switch `m' requires a value");
77 static struct strategy
*get_strategy(const char *name
)
84 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
85 if (!strcmp(name
, all_strategy
[i
].name
))
86 return &all_strategy
[i
];
90 static void append_strategy(struct strategy
*s
)
92 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
93 use_strategies
[use_strategies_nr
++] = s
;
96 static int option_parse_strategy(const struct option
*opt
,
97 const char *name
, int unset
)
105 s
= get_strategy(name
);
111 strbuf_init(&err
, 0);
112 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
113 strbuf_addf(&err
, " %s", all_strategy
[i
].name
);
114 fprintf(stderr
, "Could not find merge strategy '%s'.\n", name
);
115 fprintf(stderr
, "Available strategies are:%s.\n", err
.buf
);
121 static int option_parse_n(const struct option
*opt
,
122 const char *arg
, int unset
)
124 show_diffstat
= unset
;
128 static struct option builtin_merge_options
[] = {
129 { OPTION_CALLBACK
, 'n', NULL
, NULL
, NULL
,
130 "do not show a diffstat at the end of the merge",
131 PARSE_OPT_NOARG
, option_parse_n
},
132 OPT_BOOLEAN(0, "stat", &show_diffstat
,
133 "show a diffstat at the end of the merge"),
134 OPT_BOOLEAN(0, "summary", &show_diffstat
, "(synonym to --stat)"),
135 OPT_BOOLEAN(0, "log", &option_log
,
136 "add list of one-line log to merge commit message"),
137 OPT_BOOLEAN(0, "squash", &squash
,
138 "create a single commit instead of doing a merge"),
139 OPT_BOOLEAN(0, "commit", &option_commit
,
140 "perform a commit if the merge succeeds (default)"),
141 OPT_BOOLEAN(0, "ff", &allow_fast_forward
,
142 "allow fast forward (default)"),
143 OPT_CALLBACK('s', "strategy", &use_strategies
, "strategy",
144 "merge strategy to use", option_parse_strategy
),
145 OPT_CALLBACK('m', "message", &merge_msg
, "message",
146 "message to be used for the merge commit (if any)",
147 option_parse_message
),
151 /* Cleans up metadata that is uninteresting after a succeeded merge. */
152 static void drop_save(void)
154 unlink(git_path("MERGE_HEAD"));
155 unlink(git_path("MERGE_MSG"));
158 static void save_state(void)
161 struct child_process cp
;
162 struct strbuf buffer
= STRBUF_INIT
;
163 const char *argv
[] = {"stash", "create", NULL
};
165 memset(&cp
, 0, sizeof(cp
));
170 if (start_command(&cp
))
171 die("could not run stash.");
172 len
= strbuf_read(&buffer
, cp
.out
, 1024);
175 if (finish_command(&cp
) || len
< 0)
179 strbuf_setlen(&buffer
, buffer
.len
-1);
180 if (get_sha1(buffer
.buf
, stash
))
181 die("not a valid object: %s", buffer
.buf
);
184 static void reset_hard(unsigned const char *sha1
, int verbose
)
189 args
[i
++] = "read-tree";
192 args
[i
++] = "--reset";
194 args
[i
++] = sha1_to_hex(sha1
);
197 if (run_command_v_opt(args
, RUN_GIT_CMD
))
198 die("read-tree failed");
201 static void restore_state(void)
204 const char *args
[] = { "stash", "apply", NULL
, NULL
};
206 if (is_null_sha1(stash
))
212 args
[2] = sha1_to_hex(stash
);
215 * It is OK to ignore error here, for example when there was
216 * nothing to restore.
218 run_command_v_opt(args
, RUN_GIT_CMD
);
221 refresh_cache(REFRESH_QUIET
);
224 /* This is called when no merge was necessary. */
225 static void finish_up_to_date(const char *msg
)
227 printf("%s%s\n", squash
? " (nothing to squash)" : "", msg
);
231 static void squash_message(void)
234 struct commit
*commit
;
236 struct commit_list
*j
;
239 printf("Squash commit -- not updating HEAD\n");
240 fd
= open(git_path("SQUASH_MSG"), O_WRONLY
| O_CREAT
, 0666);
242 die("Could not write to %s", git_path("SQUASH_MSG"));
244 init_revisions(&rev
, NULL
);
245 rev
.ignore_merges
= 1;
246 rev
.commit_format
= CMIT_FMT_MEDIUM
;
248 commit
= lookup_commit(head
);
249 commit
->object
.flags
|= UNINTERESTING
;
250 add_pending_object(&rev
, &commit
->object
, NULL
);
252 for (j
= remoteheads
; j
; j
= j
->next
)
253 add_pending_object(&rev
, &j
->item
->object
, NULL
);
255 setup_revisions(0, NULL
, &rev
, NULL
);
256 if (prepare_revision_walk(&rev
))
257 die("revision walk setup failed");
259 strbuf_init(&out
, 0);
260 strbuf_addstr(&out
, "Squashed commit of the following:\n");
261 while ((commit
= get_revision(&rev
)) != NULL
) {
262 strbuf_addch(&out
, '\n');
263 strbuf_addf(&out
, "commit %s\n",
264 sha1_to_hex(commit
->object
.sha1
));
265 pretty_print_commit(rev
.commit_format
, commit
, &out
, rev
.abbrev
,
266 NULL
, NULL
, rev
.date_mode
, 0);
268 write(fd
, out
.buf
, out
.len
);
270 strbuf_release(&out
);
273 static int run_hook(const char *name
)
275 struct child_process hook
;
276 const char *argv
[3], *env
[2];
277 char index
[PATH_MAX
];
279 argv
[0] = git_path("hooks/%s", name
);
280 if (access(argv
[0], X_OK
) < 0)
283 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", get_index_file());
293 memset(&hook
, 0, sizeof(hook
));
296 hook
.stdout_to_stderr
= 1;
299 return run_command(&hook
);
302 static void finish(const unsigned char *new_head
, const char *msg
)
304 struct strbuf reflog_message
;
306 strbuf_init(&reflog_message
, 0);
308 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
311 strbuf_addf(&reflog_message
, "%s: %s",
312 getenv("GIT_REFLOG_ACTION"), msg
);
318 printf("No merge message -- not updating HEAD\n");
320 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
321 update_ref(reflog_message
.buf
, "HEAD",
325 * We ignore errors in 'gc --auto', since the
326 * user should see them.
328 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
331 if (new_head
&& show_diffstat
) {
332 struct diff_options opts
;
334 opts
.output_format
|=
335 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
336 opts
.detect_rename
= DIFF_DETECT_RENAME
;
337 if (diff_use_color_default
> 0)
338 DIFF_OPT_SET(&opts
, COLOR_DIFF
);
339 if (diff_setup_done(&opts
) < 0)
340 die("diff_setup_done failed");
341 diff_tree_sha1(head
, new_head
, "", &opts
);
346 /* Run a post-merge hook */
347 run_hook("post-merge");
349 strbuf_release(&reflog_message
);
352 /* Get the name for the merge commit's message. */
353 static void merge_name(const char *remote
, struct strbuf
*msg
)
355 struct object
*remote_head
;
356 unsigned char branch_head
[20], buf_sha
[20];
361 memset(branch_head
, 0, sizeof(branch_head
));
362 remote_head
= peel_to_type(remote
, 0, NULL
, OBJ_COMMIT
);
364 die("'%s' does not point to a commit", remote
);
366 strbuf_init(&buf
, 0);
367 strbuf_addstr(&buf
, "refs/heads/");
368 strbuf_addstr(&buf
, remote
);
369 resolve_ref(buf
.buf
, branch_head
, 0, 0);
371 if (!hashcmp(remote_head
->sha1
, branch_head
)) {
372 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
373 sha1_to_hex(branch_head
), remote
);
377 /* See if remote matches <name>^^^.. or <name>~<number> */
378 for (len
= 0, ptr
= remote
+ strlen(remote
);
379 remote
< ptr
&& ptr
[-1] == '^';
386 ptr
= strrchr(remote
, '~');
388 int seen_nonzero
= 0;
391 while (*++ptr
&& isdigit(*ptr
)) {
392 seen_nonzero
|= (*ptr
!= '0');
396 len
= 0; /* not ...~<number> */
397 else if (seen_nonzero
)
400 early
= 1; /* "name~" is "name~1"! */
404 struct strbuf truname
= STRBUF_INIT
;
405 strbuf_addstr(&truname
, "refs/heads/");
406 strbuf_addstr(&truname
, remote
);
407 strbuf_setlen(&truname
, len
+11);
408 if (resolve_ref(truname
.buf
, buf_sha
, 0, 0)) {
410 "%s\t\tbranch '%s'%s of .\n",
411 sha1_to_hex(remote_head
->sha1
),
413 (early
? " (early part)" : ""));
418 if (!strcmp(remote
, "FETCH_HEAD") &&
419 !access(git_path("FETCH_HEAD"), R_OK
)) {
424 strbuf_init(&line
, 0);
425 fp
= fopen(git_path("FETCH_HEAD"), "r");
427 die("could not open %s for reading: %s",
428 git_path("FETCH_HEAD"), strerror(errno
));
429 strbuf_getline(&line
, fp
, '\n');
431 ptr
= strstr(line
.buf
, "\tnot-for-merge\t");
433 strbuf_remove(&line
, ptr
-line
.buf
+1, 13);
434 strbuf_addbuf(msg
, &line
);
435 strbuf_release(&line
);
438 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
439 sha1_to_hex(remote_head
->sha1
), remote
);
442 int git_merge_config(const char *k
, const char *v
, void *cb
)
444 if (branch
&& !prefixcmp(k
, "branch.") &&
445 !prefixcmp(k
+ 7, branch
) &&
446 !strcmp(k
+ 7 + strlen(branch
), ".mergeoptions")) {
452 argc
= split_cmdline(buf
, &argv
);
453 argv
= xrealloc(argv
, sizeof(*argv
) * (argc
+ 2));
454 memmove(argv
+ 1, argv
, sizeof(*argv
) * (argc
+ 1));
456 parse_options(argc
, argv
, builtin_merge_options
,
457 builtin_merge_usage
, 0);
461 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
462 show_diffstat
= git_config_bool(k
, v
);
463 else if (!strcmp(k
, "pull.twohead"))
464 return git_config_string(&pull_twohead
, k
, v
);
465 else if (!strcmp(k
, "pull.octopus"))
466 return git_config_string(&pull_octopus
, k
, v
);
467 else if (!strcmp(k
, "merge.log") || !strcmp(k
, "merge.summary"))
468 option_log
= git_config_bool(k
, v
);
469 return git_diff_ui_config(k
, v
, cb
);
472 static int read_tree_trivial(unsigned char *common
, unsigned char *head
,
476 struct tree
*trees
[MAX_UNPACK_TREES
];
477 struct tree_desc t
[MAX_UNPACK_TREES
];
478 struct unpack_trees_options opts
;
480 memset(&opts
, 0, sizeof(opts
));
482 opts
.src_index
= &the_index
;
483 opts
.dst_index
= &the_index
;
485 opts
.verbose_update
= 1;
486 opts
.trivial_merges_only
= 1;
488 trees
[nr_trees
] = parse_tree_indirect(common
);
489 if (!trees
[nr_trees
++])
491 trees
[nr_trees
] = parse_tree_indirect(head
);
492 if (!trees
[nr_trees
++])
494 trees
[nr_trees
] = parse_tree_indirect(one
);
495 if (!trees
[nr_trees
++])
497 opts
.fn
= threeway_merge
;
498 cache_tree_free(&active_cache_tree
);
499 for (i
= 0; i
< nr_trees
; i
++) {
500 parse_tree(trees
[i
]);
501 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
503 if (unpack_trees(nr_trees
, t
, &opts
))
508 static void write_tree_trivial(unsigned char *sha1
)
510 if (write_cache_as_tree(sha1
, 0, NULL
))
511 die("git write-tree failed to write a tree");
514 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
515 const char *head_arg
)
519 struct commit_list
*j
;
522 args
= xmalloc((4 + commit_list_count(common
) +
523 commit_list_count(remoteheads
)) * sizeof(char *));
524 strbuf_init(&buf
, 0);
525 strbuf_addf(&buf
, "merge-%s", strategy
);
527 for (j
= common
; j
; j
= j
->next
)
528 args
[i
++] = xstrdup(sha1_to_hex(j
->item
->object
.sha1
));
530 args
[i
++] = head_arg
;
531 for (j
= remoteheads
; j
; j
= j
->next
)
532 args
[i
++] = xstrdup(sha1_to_hex(j
->item
->object
.sha1
));
534 ret
= run_command_v_opt(args
, RUN_GIT_CMD
);
535 strbuf_release(&buf
);
537 for (j
= common
; j
; j
= j
->next
)
538 free((void *)args
[i
++]);
540 for (j
= remoteheads
; j
; j
= j
->next
)
541 free((void *)args
[i
++]);
546 static void count_diff_files(struct diff_queue_struct
*q
,
547 struct diff_options
*opt
, void *data
)
554 static int count_unmerged_entries(void)
556 const struct index_state
*state
= &the_index
;
559 for (i
= 0; i
< state
->cache_nr
; i
++)
560 if (ce_stage(state
->cache
[i
]))
566 static int checkout_fast_forward(unsigned char *head
, unsigned char *remote
)
568 struct tree
*trees
[MAX_UNPACK_TREES
];
569 struct unpack_trees_options opts
;
570 struct tree_desc t
[MAX_UNPACK_TREES
];
571 int i
, fd
, nr_trees
= 0;
572 struct dir_struct dir
;
573 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
575 if (read_cache_unmerged())
576 die("you need to resolve your current index first");
578 fd
= hold_locked_index(lock_file
, 1);
580 memset(&trees
, 0, sizeof(trees
));
581 memset(&opts
, 0, sizeof(opts
));
582 memset(&t
, 0, sizeof(t
));
583 dir
.show_ignored
= 1;
584 dir
.exclude_per_dir
= ".gitignore";
588 opts
.src_index
= &the_index
;
589 opts
.dst_index
= &the_index
;
591 opts
.verbose_update
= 1;
593 opts
.fn
= twoway_merge
;
595 trees
[nr_trees
] = parse_tree_indirect(head
);
596 if (!trees
[nr_trees
++])
598 trees
[nr_trees
] = parse_tree_indirect(remote
);
599 if (!trees
[nr_trees
++])
601 for (i
= 0; i
< nr_trees
; i
++) {
602 parse_tree(trees
[i
]);
603 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
605 if (unpack_trees(nr_trees
, t
, &opts
))
607 if (write_cache(fd
, active_cache
, active_nr
) ||
608 commit_locked_index(lock_file
))
609 die("unable to write new index file");
613 static void split_merge_strategies(const char *string
, struct strategy
**list
,
621 buf
= xstrdup(string
);
626 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
627 (*list
)[(*nr
)++].name
= xstrdup(q
);
632 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
633 (*list
)[(*nr
)++].name
= xstrdup(q
);
639 static void add_strategies(const char *string
, unsigned attr
)
641 struct strategy
*list
= NULL
;
642 int list_alloc
= 0, list_nr
= 0, i
;
644 memset(&list
, 0, sizeof(list
));
645 split_merge_strategies(string
, &list
, &list_nr
, &list_alloc
);
647 for (i
= 0; i
< list_nr
; i
++) {
650 s
= get_strategy(list
[i
].name
);
656 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
657 if (all_strategy
[i
].attr
& attr
)
658 append_strategy(&all_strategy
[i
]);
662 static int merge_trivial(void)
664 unsigned char result_tree
[20], result_commit
[20];
665 struct commit_list parent
;
667 write_tree_trivial(result_tree
);
668 printf("Wonderful.\n");
669 parent
.item
= remoteheads
->item
;
671 commit_tree(merge_msg
.buf
, result_tree
, &parent
, result_commit
);
672 finish(result_commit
, "In-index merge");
677 static int finish_automerge(struct commit_list
*common
,
678 unsigned char *result_tree
,
679 const char *wt_strategy
)
681 struct commit_list
*parents
= NULL
, *j
;
682 struct strbuf buf
= STRBUF_INIT
;
683 unsigned char result_commit
[20];
685 free_commit_list(common
);
686 if (allow_fast_forward
) {
687 parents
= remoteheads
;
688 commit_list_insert(lookup_commit(head
), &parents
);
689 parents
= reduce_heads(parents
);
691 struct commit_list
**pptr
= &parents
;
693 pptr
= &commit_list_insert(lookup_commit(head
),
695 for (j
= remoteheads
; j
; j
= j
->next
)
696 pptr
= &commit_list_insert(j
->item
, pptr
)->next
;
698 free_commit_list(remoteheads
);
699 strbuf_addch(&merge_msg
, '\n');
700 commit_tree(merge_msg
.buf
, result_tree
, parents
, result_commit
);
701 strbuf_addf(&buf
, "Merge made by %s.", wt_strategy
);
702 finish(result_commit
, buf
.buf
);
703 strbuf_release(&buf
);
708 static int suggest_conflicts(void)
713 fp
= fopen(git_path("MERGE_MSG"), "a");
715 die("Could open %s for writing", git_path("MERGE_MSG"));
716 fprintf(fp
, "\nConflicts:\n");
717 for (pos
= 0; pos
< active_nr
; pos
++) {
718 struct cache_entry
*ce
= active_cache
[pos
];
721 fprintf(fp
, "\t%s\n", ce
->name
);
722 while (pos
+ 1 < active_nr
&&
724 active_cache
[pos
+ 1]->name
))
730 printf("Automatic merge failed; "
731 "fix conflicts and then commit the result.\n");
735 static struct commit
*is_old_style_invocation(int argc
, const char **argv
)
737 struct commit
*second_token
= NULL
;
739 unsigned char second_sha1
[20];
741 if (get_sha1(argv
[1], second_sha1
))
743 second_token
= lookup_commit_reference_gently(second_sha1
, 0);
745 die("'%s' is not a commit", argv
[1]);
746 if (hashcmp(second_token
->object
.sha1
, head
))
752 static int evaluate_result(void)
757 if (read_cache() < 0)
758 die("failed to read the cache");
760 /* Check how many files differ. */
761 init_revisions(&rev
, "");
762 setup_revisions(0, NULL
, &rev
, NULL
);
763 rev
.diffopt
.output_format
|=
764 DIFF_FORMAT_CALLBACK
;
765 rev
.diffopt
.format_callback
= count_diff_files
;
766 rev
.diffopt
.format_callback_data
= &cnt
;
767 run_diff_files(&rev
, 0);
770 * Check how many unmerged entries are
773 cnt
+= count_unmerged_entries();
778 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
780 unsigned char result_tree
[20];
782 const char *head_arg
;
783 int flag
, head_invalid
= 0, i
;
784 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
785 struct commit_list
*common
= NULL
;
786 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
787 struct commit_list
**remotes
= &remoteheads
;
790 if (unmerged_cache())
791 die("You are in the middle of a conflicted merge.");
794 * Check if we are _not_ on a detached HEAD, i.e. if there is a
797 branch
= resolve_ref("HEAD", head
, 0, &flag
);
798 if (branch
&& !prefixcmp(branch
, "refs/heads/"))
800 if (is_null_sha1(head
))
803 git_config(git_merge_config
, NULL
);
806 if (diff_use_color_default
== -1)
807 diff_use_color_default
= git_use_color_default
;
809 argc
= parse_options(argc
, argv
, builtin_merge_options
,
810 builtin_merge_usage
, 0);
813 if (!allow_fast_forward
)
814 die("You cannot combine --squash with --no-ff.");
819 usage_with_options(builtin_merge_usage
,
820 builtin_merge_options
);
823 * This could be traditional "merge <msg> HEAD <commit>..." and
824 * the way we can tell it is to see if the second token is HEAD,
825 * but some people might have misused the interface and used a
826 * committish that is the same as HEAD there instead.
827 * Traditional format never would have "-m" so it is an
828 * additional safety measure to check for it.
830 strbuf_init(&buf
, 0);
832 if (!have_message
&& is_old_style_invocation(argc
, argv
)) {
833 strbuf_addstr(&merge_msg
, argv
[0]);
837 } else if (head_invalid
) {
838 struct object
*remote_head
;
840 * If the merged head is a valid one there is no reason
841 * to forbid "git merge" into a branch yet to be born.
842 * We do the same for "git pull".
845 die("Can merge only exactly one commit into "
847 remote_head
= peel_to_type(argv
[0], 0, NULL
, OBJ_COMMIT
);
849 die("%s - not something we can merge", argv
[0]);
850 update_ref("initial pull", "HEAD", remote_head
->sha1
, NULL
, 0,
852 reset_hard(remote_head
->sha1
, 0);
857 /* We are invoked directly as the first-class UI. */
861 * All the rest are the commits being merged;
862 * prepare the standard merge summary message to
863 * be appended to the given message. If remote
864 * is invalid we will die later in the common
865 * codepath so we discard the error in this
868 strbuf_init(&msg
, 0);
869 for (i
= 0; i
< argc
; i
++)
870 merge_name(argv
[i
], &msg
);
871 fmt_merge_msg(option_log
, &msg
, &merge_msg
);
873 strbuf_setlen(&merge_msg
, merge_msg
.len
-1);
876 if (head_invalid
|| !argc
)
877 usage_with_options(builtin_merge_usage
,
878 builtin_merge_options
);
880 strbuf_addstr(&buf
, "merge");
881 for (i
= 0; i
< argc
; i
++)
882 strbuf_addf(&buf
, " %s", argv
[i
]);
883 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
886 for (i
= 0; i
< argc
; i
++) {
889 o
= peel_to_type(argv
[i
], 0, NULL
, OBJ_COMMIT
);
891 die("%s - not something we can merge", argv
[i
]);
892 remotes
= &commit_list_insert(lookup_commit(o
->sha1
),
895 strbuf_addf(&buf
, "GITHEAD_%s", sha1_to_hex(o
->sha1
));
896 setenv(buf
.buf
, argv
[i
], 1);
900 if (!use_strategies
) {
901 if (!remoteheads
->next
)
902 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
904 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
907 for (i
= 0; i
< use_strategies_nr
; i
++) {
908 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
909 allow_fast_forward
= 0;
910 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
914 if (!remoteheads
->next
)
915 common
= get_merge_bases(lookup_commit(head
),
916 remoteheads
->item
, 1);
918 struct commit_list
*list
= remoteheads
;
919 commit_list_insert(lookup_commit(head
), &list
);
920 common
= get_octopus_merge_bases(list
);
924 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head
, NULL
, 0,
928 ; /* No common ancestors found. We need a real merge. */
929 else if (!remoteheads
->next
&& !common
->next
&&
930 common
->item
== remoteheads
->item
) {
932 * If head can reach all the merge then we are up to date.
933 * but first the most common case of merging one remote.
935 finish_up_to_date("Already up-to-date.");
937 } else if (allow_fast_forward
&& !remoteheads
->next
&&
939 !hashcmp(common
->item
->object
.sha1
, head
)) {
940 /* Again the most common case of merging one remote. */
945 strcpy(hex
, find_unique_abbrev(head
, DEFAULT_ABBREV
));
947 printf("Updating %s..%s\n",
949 find_unique_abbrev(remoteheads
->item
->object
.sha1
,
951 refresh_cache(REFRESH_QUIET
);
952 strbuf_init(&msg
, 0);
953 strbuf_addstr(&msg
, "Fast forward");
956 " (no commit created; -m option ignored)");
957 o
= peel_to_type(sha1_to_hex(remoteheads
->item
->object
.sha1
),
958 0, NULL
, OBJ_COMMIT
);
962 if (checkout_fast_forward(head
, remoteheads
->item
->object
.sha1
))
965 finish(o
->sha1
, msg
.buf
);
968 } else if (!remoteheads
->next
&& common
->next
)
971 * We are not doing octopus and not fast forward. Need
974 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
976 * We are not doing octopus, not fast forward, and have
979 refresh_cache(REFRESH_QUIET
);
981 /* See if it is really trivial. */
982 git_committer_info(IDENT_ERROR_ON_NO_NAME
);
983 printf("Trying really trivial in-index merge...\n");
984 if (!read_tree_trivial(common
->item
->object
.sha1
,
985 head
, remoteheads
->item
->object
.sha1
))
986 return merge_trivial();
991 * An octopus. If we can reach all the remote we are up
995 struct commit_list
*j
;
997 for (j
= remoteheads
; j
; j
= j
->next
) {
998 struct commit_list
*common_one
;
1001 * Here we *have* to calculate the individual
1002 * merge_bases again, otherwise "git merge HEAD^
1003 * HEAD^^" would be missed.
1005 common_one
= get_merge_bases(lookup_commit(head
),
1007 if (hashcmp(common_one
->item
->object
.sha1
,
1008 j
->item
->object
.sha1
)) {
1014 finish_up_to_date("Already up-to-date. Yeeah!");
1019 /* We are going to make a new commit. */
1020 git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1023 * At this point, we need a real merge. No matter what strategy
1024 * we use, it would operate on the index, possibly affecting the
1025 * working tree, and when resolved cleanly, have the desired
1026 * tree in the index -- this means that the index must be in
1027 * sync with the head commit. The strategies are responsible
1030 if (use_strategies_nr
!= 1) {
1032 * Stash away the local changes so that we can try more
1037 memcpy(stash
, null_sha1
, 20);
1040 for (i
= 0; i
< use_strategies_nr
; i
++) {
1043 printf("Rewinding the tree to pristine...\n");
1046 if (use_strategies_nr
!= 1)
1047 printf("Trying merge strategy %s...\n",
1048 use_strategies
[i
]->name
);
1050 * Remember which strategy left the state in the working
1053 wt_strategy
= use_strategies
[i
]->name
;
1055 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1057 if (!option_commit
&& !ret
) {
1060 * This is necessary here just to avoid writing
1061 * the tree, but later we will *not* exit with
1062 * status code 1 because merge_was_ok is set.
1069 * The backend exits with 1 when conflicts are
1070 * left to be resolved, with 2 when it does not
1071 * handle the given merge at all.
1074 int cnt
= evaluate_result();
1076 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1077 best_strategy
= use_strategies
[i
]->name
;
1087 /* Automerge succeeded. */
1088 write_tree_trivial(result_tree
);
1089 automerge_was_ok
= 1;
1094 * If we have a resulting tree, that means the strategy module
1095 * auto resolved the merge cleanly.
1097 if (automerge_was_ok
)
1098 return finish_automerge(common
, result_tree
, wt_strategy
);
1101 * Pick the result from the best strategy and have the user fix
1104 if (!best_strategy
) {
1106 if (use_strategies_nr
> 1)
1108 "No merge strategy handled the merge.\n");
1110 fprintf(stderr
, "Merge with strategy %s failed.\n",
1111 use_strategies
[0]->name
);
1113 } else if (best_strategy
== wt_strategy
)
1114 ; /* We already have its result in the working tree. */
1116 printf("Rewinding the tree to pristine...\n");
1118 printf("Using the %s to prepare resolving by hand.\n",
1120 try_merge_strategy(best_strategy
, common
, head_arg
);
1127 struct commit_list
*j
;
1129 for (j
= remoteheads
; j
; j
= j
->next
)
1130 strbuf_addf(&buf
, "%s\n",
1131 sha1_to_hex(j
->item
->object
.sha1
));
1132 fd
= open(git_path("MERGE_HEAD"), O_WRONLY
| O_CREAT
, 0666);
1134 die("Could open %s for writing",
1135 git_path("MERGE_HEAD"));
1136 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
)
1137 die("Could not write to %s", git_path("MERGE_HEAD"));
1139 strbuf_addch(&merge_msg
, '\n');
1140 fd
= open(git_path("MERGE_MSG"), O_WRONLY
| O_CREAT
, 0666);
1142 die("Could open %s for writing", git_path("MERGE_MSG"));
1143 if (write_in_full(fd
, merge_msg
.buf
, merge_msg
.len
) !=
1145 die("Could not write to %s", git_path("MERGE_MSG"));
1150 fprintf(stderr
, "Automatic merge went well; "
1151 "stopped before committing as requested\n");
1154 return suggest_conflicts();