Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git merge" |
| 3 | * |
| 4 | * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org> |
| 5 | * |
| 6 | * Based on git-merge.sh by Junio C Hamano. |
| 7 | */ |
| 8 | |
| 9 | #include "cache.h" |
| 10 | #include "parse-options.h" |
| 11 | #include "builtin.h" |
| 12 | #include "run-command.h" |
| 13 | #include "diff.h" |
| 14 | #include "refs.h" |
| 15 | #include "commit.h" |
| 16 | #include "diffcore.h" |
| 17 | #include "revision.h" |
| 18 | #include "unpack-trees.h" |
| 19 | #include "cache-tree.h" |
| 20 | #include "dir.h" |
| 21 | #include "utf8.h" |
| 22 | #include "log-tree.h" |
| 23 | #include "color.h" |
Junio C Hamano | fcab40a | 2008-07-15 19:09:46 -0700 | [diff] [blame] | 24 | #include "rerere.h" |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 25 | #include "help.h" |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 26 | #include "merge-recursive.h" |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 27 | #include "resolve-undo.h" |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 28 | #include "remote.h" |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 29 | #include "fmt-merge-msg.h" |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 30 | |
| 31 | #define DEFAULT_TWOHEAD (1<<0) |
| 32 | #define DEFAULT_OCTOPUS (1<<1) |
| 33 | #define NO_FAST_FORWARD (1<<2) |
| 34 | #define NO_TRIVIAL (1<<3) |
| 35 | |
| 36 | struct strategy { |
| 37 | const char *name; |
| 38 | unsigned attr; |
| 39 | }; |
| 40 | |
| 41 | static const char * const builtin_merge_usage[] = { |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 42 | "git merge [options] [<commit>...]", |
Jared Hance | c395c25 | 2011-02-10 18:52:41 -0500 | [diff] [blame] | 43 | "git merge [options] <msg> HEAD <commit>", |
| 44 | "git merge --abort", |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 45 | NULL |
| 46 | }; |
| 47 | |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 48 | static int show_diffstat = 1, shortlog_len = -1, squash; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 49 | static int option_commit = 1, allow_fast_forward = 1; |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 50 | static int fast_forward_only, option_edit; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 51 | static int allow_trivial = 1, have_message; |
| 52 | static struct strbuf merge_msg; |
| 53 | static struct commit_list *remoteheads; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 54 | static struct strategy **use_strategies; |
| 55 | static size_t use_strategies_nr, use_strategies_alloc; |
Avery Pennarun | 8cc5b29 | 2009-11-25 21:23:55 -0500 | [diff] [blame] | 56 | static const char **xopts; |
| 57 | static size_t xopts_nr, xopts_alloc; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 58 | static const char *branch; |
Junio C Hamano | 0d8fc3e | 2011-05-04 17:42:51 -0700 | [diff] [blame] | 59 | static char *branch_mergeoptions; |
Jonathan Nieder | 7610fa5 | 2010-08-05 06:32:41 -0500 | [diff] [blame] | 60 | static int option_renormalize; |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 61 | static int verbosity; |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 62 | static int allow_rerere_auto; |
Johan Herland | 35d2fff | 2010-11-09 22:49:59 +0100 | [diff] [blame] | 63 | static int abort_current_merge; |
Jeff King | 99bfc66 | 2011-02-20 04:53:21 -0500 | [diff] [blame] | 64 | static int show_progress = -1; |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 65 | static int default_to_upstream; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 66 | |
| 67 | static struct strategy all_strategy[] = { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 68 | { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL }, |
| 69 | { "octopus", DEFAULT_OCTOPUS }, |
| 70 | { "resolve", 0 }, |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 71 | { "ours", NO_FAST_FORWARD | NO_TRIVIAL }, |
| 72 | { "subtree", NO_FAST_FORWARD | NO_TRIVIAL }, |
| 73 | }; |
| 74 | |
| 75 | static const char *pull_twohead, *pull_octopus; |
| 76 | |
| 77 | static int option_parse_message(const struct option *opt, |
| 78 | const char *arg, int unset) |
| 79 | { |
| 80 | struct strbuf *buf = opt->value; |
| 81 | |
| 82 | if (unset) |
| 83 | strbuf_setlen(buf, 0); |
Michele Ballabio | 74f5b7f | 2008-07-20 14:34:47 +0200 | [diff] [blame] | 84 | else if (arg) { |
Junio C Hamano | ce9d823 | 2009-12-02 10:00:58 -0800 | [diff] [blame] | 85 | strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 86 | have_message = 1; |
Michele Ballabio | 74f5b7f | 2008-07-20 14:34:47 +0200 | [diff] [blame] | 87 | } else |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 88 | return error(_("switch `m' requires a value")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static struct strategy *get_strategy(const char *name) |
| 93 | { |
| 94 | int i; |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 95 | struct strategy *ret; |
| 96 | static struct cmdnames main_cmds, other_cmds; |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 97 | static int loaded; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 98 | |
| 99 | if (!name) |
| 100 | return NULL; |
| 101 | |
| 102 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) |
| 103 | if (!strcmp(name, all_strategy[i].name)) |
| 104 | return &all_strategy[i]; |
Miklos Vajna | 1719b5e | 2008-07-21 18:10:47 +0200 | [diff] [blame] | 105 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 106 | if (!loaded) { |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 107 | struct cmdnames not_strategies; |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 108 | loaded = 1; |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 109 | |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 110 | memset(¬_strategies, 0, sizeof(struct cmdnames)); |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 111 | load_command_list("git-merge-", &main_cmds, &other_cmds); |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 112 | for (i = 0; i < main_cmds.cnt; i++) { |
| 113 | int j, found = 0; |
| 114 | struct cmdname *ent = main_cmds.names[i]; |
| 115 | for (j = 0; j < ARRAY_SIZE(all_strategy); j++) |
| 116 | if (!strncmp(ent->name, all_strategy[j].name, ent->len) |
| 117 | && !all_strategy[j].name[ent->len]) |
| 118 | found = 1; |
| 119 | if (!found) |
| 120 | add_cmdname(¬_strategies, ent->name, ent->len); |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 121 | } |
Avery Pennarun | ed87465 | 2009-11-25 21:23:54 -0500 | [diff] [blame] | 122 | exclude_cmds(&main_cmds, ¬_strategies); |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 123 | } |
| 124 | if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 125 | fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); |
| 126 | fprintf(stderr, _("Available strategies are:")); |
Junio C Hamano | 131f9a1 | 2008-08-20 22:07:55 -0700 | [diff] [blame] | 127 | for (i = 0; i < main_cmds.cnt; i++) |
| 128 | fprintf(stderr, " %s", main_cmds.names[i]->name); |
| 129 | fprintf(stderr, ".\n"); |
| 130 | if (other_cmds.cnt) { |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 131 | fprintf(stderr, _("Available custom strategies are:")); |
Junio C Hamano | 131f9a1 | 2008-08-20 22:07:55 -0700 | [diff] [blame] | 132 | for (i = 0; i < other_cmds.cnt; i++) |
| 133 | fprintf(stderr, " %s", other_cmds.names[i]->name); |
| 134 | fprintf(stderr, ".\n"); |
| 135 | } |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 136 | exit(1); |
| 137 | } |
| 138 | |
Brandon Casey | 19d4b41 | 2008-10-06 18:39:10 -0500 | [diff] [blame] | 139 | ret = xcalloc(1, sizeof(struct strategy)); |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 140 | ret->name = xstrdup(name); |
Jonathan Nieder | 52b48ef | 2010-08-15 20:11:06 -0500 | [diff] [blame] | 141 | ret->attr = NO_TRIVIAL; |
Miklos Vajna | 87091b4 | 2008-07-30 01:16:59 +0200 | [diff] [blame] | 142 | return ret; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void append_strategy(struct strategy *s) |
| 146 | { |
| 147 | ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc); |
| 148 | use_strategies[use_strategies_nr++] = s; |
| 149 | } |
| 150 | |
| 151 | static int option_parse_strategy(const struct option *opt, |
| 152 | const char *name, int unset) |
| 153 | { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 154 | if (unset) |
| 155 | return 0; |
| 156 | |
Miklos Vajna | 1719b5e | 2008-07-21 18:10:47 +0200 | [diff] [blame] | 157 | append_strategy(get_strategy(name)); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Avery Pennarun | 8cc5b29 | 2009-11-25 21:23:55 -0500 | [diff] [blame] | 161 | static int option_parse_x(const struct option *opt, |
| 162 | const char *arg, int unset) |
| 163 | { |
| 164 | if (unset) |
| 165 | return 0; |
| 166 | |
| 167 | ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc); |
| 168 | xopts[xopts_nr++] = xstrdup(arg); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 172 | static int option_parse_n(const struct option *opt, |
| 173 | const char *arg, int unset) |
| 174 | { |
| 175 | show_diffstat = unset; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static struct option builtin_merge_options[] = { |
| 180 | { OPTION_CALLBACK, 'n', NULL, NULL, NULL, |
| 181 | "do not show a diffstat at the end of the merge", |
| 182 | PARSE_OPT_NOARG, option_parse_n }, |
| 183 | OPT_BOOLEAN(0, "stat", &show_diffstat, |
| 184 | "show a diffstat at the end of the merge"), |
| 185 | OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"), |
Ramkumar Ramachandra | 96e9420 | 2010-09-08 23:29:54 +0530 | [diff] [blame] | 186 | { OPTION_INTEGER, 0, "log", &shortlog_len, "n", |
| 187 | "add (at most <n>) entries from shortlog to merge commit message", |
| 188 | PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 189 | OPT_BOOLEAN(0, "squash", &squash, |
| 190 | "create a single commit instead of doing a merge"), |
| 191 | OPT_BOOLEAN(0, "commit", &option_commit, |
| 192 | "perform a commit if the merge succeeds (default)"), |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 193 | OPT_BOOLEAN('e', "edit", &option_edit, |
| 194 | "edit message before committing"), |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 195 | OPT_BOOLEAN(0, "ff", &allow_fast_forward, |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 196 | "allow fast-forward (default)"), |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 197 | OPT_BOOLEAN(0, "ff-only", &fast_forward_only, |
Junio C Hamano | 4d8c325 | 2009-11-15 16:41:02 -0800 | [diff] [blame] | 198 | "abort if fast-forward is not possible"), |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 199 | OPT_RERERE_AUTOUPDATE(&allow_rerere_auto), |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 200 | OPT_CALLBACK('s', "strategy", &use_strategies, "strategy", |
| 201 | "merge strategy to use", option_parse_strategy), |
Avery Pennarun | 8cc5b29 | 2009-11-25 21:23:55 -0500 | [diff] [blame] | 202 | OPT_CALLBACK('X', "strategy-option", &xopts, "option=value", |
| 203 | "option for selected merge strategy", option_parse_x), |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 204 | OPT_CALLBACK('m', "message", &merge_msg, "message", |
Michael J Gruber | 3f40617 | 2011-02-15 14:09:07 +0100 | [diff] [blame] | 205 | "merge commit message (for a non-fast-forward merge)", |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 206 | option_parse_message), |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 207 | OPT__VERBOSITY(&verbosity), |
Johan Herland | 35d2fff | 2010-11-09 22:49:59 +0100 | [diff] [blame] | 208 | OPT_BOOLEAN(0, "abort", &abort_current_merge, |
| 209 | "abort the current in-progress merge"), |
Jeff King | 99bfc66 | 2011-02-20 04:53:21 -0500 | [diff] [blame] | 210 | OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1), |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 211 | OPT_END() |
| 212 | }; |
| 213 | |
| 214 | /* Cleans up metadata that is uninteresting after a succeeded merge. */ |
| 215 | static void drop_save(void) |
| 216 | { |
| 217 | unlink(git_path("MERGE_HEAD")); |
| 218 | unlink(git_path("MERGE_MSG")); |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 219 | unlink(git_path("MERGE_MODE")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Nguyễn Thái Ngọc Duy | b4fd940 | 2011-08-19 21:50:05 +0700 | [diff] [blame] | 222 | static int save_state(unsigned char *stash) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 223 | { |
| 224 | int len; |
| 225 | struct child_process cp; |
| 226 | struct strbuf buffer = STRBUF_INIT; |
| 227 | const char *argv[] = {"stash", "create", NULL}; |
| 228 | |
| 229 | memset(&cp, 0, sizeof(cp)); |
| 230 | cp.argv = argv; |
| 231 | cp.out = -1; |
| 232 | cp.git_cmd = 1; |
| 233 | |
| 234 | if (start_command(&cp)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 235 | die(_("could not run stash.")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 236 | len = strbuf_read(&buffer, cp.out, 1024); |
| 237 | close(cp.out); |
| 238 | |
| 239 | if (finish_command(&cp) || len < 0) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 240 | die(_("stash failed")); |
Nguyễn Thái Ngọc Duy | b4fd940 | 2011-08-19 21:50:05 +0700 | [diff] [blame] | 241 | else if (!len) /* no changes */ |
| 242 | return -1; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 243 | strbuf_setlen(&buffer, buffer.len-1); |
| 244 | if (get_sha1(buffer.buf, stash)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 245 | die(_("not a valid object: %s"), buffer.buf); |
Nguyễn Thái Ngọc Duy | b4fd940 | 2011-08-19 21:50:05 +0700 | [diff] [blame] | 246 | return 0; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Clemens Buchacher | 172b642 | 2010-11-14 23:07:49 +0100 | [diff] [blame] | 249 | static void read_empty(unsigned const char *sha1, int verbose) |
| 250 | { |
| 251 | int i = 0; |
| 252 | const char *args[7]; |
| 253 | |
| 254 | args[i++] = "read-tree"; |
| 255 | if (verbose) |
| 256 | args[i++] = "-v"; |
| 257 | args[i++] = "-m"; |
| 258 | args[i++] = "-u"; |
| 259 | args[i++] = EMPTY_TREE_SHA1_HEX; |
| 260 | args[i++] = sha1_to_hex(sha1); |
| 261 | args[i] = NULL; |
| 262 | |
| 263 | if (run_command_v_opt(args, RUN_GIT_CMD)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 264 | die(_("read-tree failed")); |
Clemens Buchacher | 172b642 | 2010-11-14 23:07:49 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 267 | static void reset_hard(unsigned const char *sha1, int verbose) |
| 268 | { |
| 269 | int i = 0; |
| 270 | const char *args[6]; |
| 271 | |
| 272 | args[i++] = "read-tree"; |
| 273 | if (verbose) |
| 274 | args[i++] = "-v"; |
| 275 | args[i++] = "--reset"; |
| 276 | args[i++] = "-u"; |
| 277 | args[i++] = sha1_to_hex(sha1); |
| 278 | args[i] = NULL; |
| 279 | |
| 280 | if (run_command_v_opt(args, RUN_GIT_CMD)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 281 | die(_("read-tree failed")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 284 | static void restore_state(const unsigned char *head, |
| 285 | const unsigned char *stash) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 286 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 287 | struct strbuf sb = STRBUF_INIT; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 288 | const char *args[] = { "stash", "apply", NULL, NULL }; |
| 289 | |
| 290 | if (is_null_sha1(stash)) |
| 291 | return; |
| 292 | |
| 293 | reset_hard(head, 1); |
| 294 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 295 | args[2] = sha1_to_hex(stash); |
| 296 | |
| 297 | /* |
| 298 | * It is OK to ignore error here, for example when there was |
| 299 | * nothing to restore. |
| 300 | */ |
| 301 | run_command_v_opt(args, RUN_GIT_CMD); |
| 302 | |
| 303 | strbuf_release(&sb); |
| 304 | refresh_cache(REFRESH_QUIET); |
| 305 | } |
| 306 | |
| 307 | /* This is called when no merge was necessary. */ |
| 308 | static void finish_up_to_date(const char *msg) |
| 309 | { |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 310 | if (verbosity >= 0) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 311 | printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 312 | drop_save(); |
| 313 | } |
| 314 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 315 | static void squash_message(struct commit *commit) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 316 | { |
| 317 | struct rev_info rev; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 318 | struct strbuf out = STRBUF_INIT; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 319 | struct commit_list *j; |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 320 | const char *filename; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 321 | int fd; |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 322 | struct pretty_print_context ctx = {0}; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 323 | |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 324 | printf(_("Squash commit -- not updating HEAD\n")); |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 325 | filename = git_path("SQUASH_MSG"); |
| 326 | fd = open(filename, O_WRONLY | O_CREAT, 0666); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 327 | if (fd < 0) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 328 | die_errno(_("Could not write to '%s'"), filename); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 329 | |
| 330 | init_revisions(&rev, NULL); |
| 331 | rev.ignore_merges = 1; |
| 332 | rev.commit_format = CMIT_FMT_MEDIUM; |
| 333 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 334 | commit->object.flags |= UNINTERESTING; |
| 335 | add_pending_object(&rev, &commit->object, NULL); |
| 336 | |
| 337 | for (j = remoteheads; j; j = j->next) |
| 338 | add_pending_object(&rev, &j->item->object, NULL); |
| 339 | |
| 340 | setup_revisions(0, NULL, &rev, NULL); |
| 341 | if (prepare_revision_walk(&rev)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 342 | die(_("revision walk setup failed")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 343 | |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 344 | ctx.abbrev = rev.abbrev; |
| 345 | ctx.date_mode = rev.date_mode; |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 346 | ctx.fmt = rev.commit_format; |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 347 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 348 | strbuf_addstr(&out, "Squashed commit of the following:\n"); |
| 349 | while ((commit = get_revision(&rev)) != NULL) { |
| 350 | strbuf_addch(&out, '\n'); |
| 351 | strbuf_addf(&out, "commit %s\n", |
| 352 | sha1_to_hex(commit->object.sha1)); |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 353 | pretty_print_commit(&ctx, commit, &out); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 354 | } |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 355 | if (write(fd, out.buf, out.len) < 0) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 356 | die_errno(_("Writing SQUASH_MSG")); |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 357 | if (close(fd)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 358 | die_errno(_("Finishing SQUASH_MSG")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 359 | strbuf_release(&out); |
| 360 | } |
| 361 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 362 | static void finish(struct commit *head_commit, |
| 363 | const unsigned char *new_head, const char *msg) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 364 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 365 | struct strbuf reflog_message = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 366 | const unsigned char *head = head_commit->object.sha1; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 367 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 368 | if (!msg) |
| 369 | strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION")); |
| 370 | else { |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 371 | if (verbosity >= 0) |
| 372 | printf("%s\n", msg); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 373 | strbuf_addf(&reflog_message, "%s: %s", |
| 374 | getenv("GIT_REFLOG_ACTION"), msg); |
| 375 | } |
| 376 | if (squash) { |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 377 | squash_message(head_commit); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 378 | } else { |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 379 | if (verbosity >= 0 && !merge_msg.len) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 380 | printf(_("No merge message -- not updating HEAD\n")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 381 | else { |
| 382 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; |
| 383 | update_ref(reflog_message.buf, "HEAD", |
| 384 | new_head, head, 0, |
| 385 | DIE_ON_ERR); |
| 386 | /* |
| 387 | * We ignore errors in 'gc --auto', since the |
| 388 | * user should see them. |
| 389 | */ |
| 390 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); |
| 391 | } |
| 392 | } |
| 393 | if (new_head && show_diffstat) { |
| 394 | struct diff_options opts; |
| 395 | diff_setup(&opts); |
| 396 | opts.output_format |= |
| 397 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; |
| 398 | opts.detect_rename = DIFF_DETECT_RENAME; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 399 | if (diff_setup_done(&opts) < 0) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 400 | die(_("diff_setup_done failed")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 401 | diff_tree_sha1(head, new_head, "", &opts); |
| 402 | diffcore_std(&opts); |
| 403 | diff_flush(&opts); |
| 404 | } |
| 405 | |
| 406 | /* Run a post-merge hook */ |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 407 | run_hook(NULL, "post-merge", squash ? "1" : "0", NULL); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 408 | |
| 409 | strbuf_release(&reflog_message); |
| 410 | } |
| 411 | |
| 412 | /* Get the name for the merge commit's message. */ |
| 413 | static void merge_name(const char *remote, struct strbuf *msg) |
| 414 | { |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 415 | struct commit *remote_head; |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 416 | unsigned char branch_head[20]; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 417 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | c9717ee | 2009-02-13 23:26:12 -0800 | [diff] [blame] | 418 | struct strbuf bname = STRBUF_INIT; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 419 | const char *ptr; |
Jeff King | 751c597 | 2009-08-09 06:02:24 -0400 | [diff] [blame] | 420 | char *found_ref; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 421 | int len, early; |
| 422 | |
Junio C Hamano | a552de7 | 2009-03-21 13:17:30 -0700 | [diff] [blame] | 423 | strbuf_branchname(&bname, remote); |
| 424 | remote = bname.buf; |
Junio C Hamano | c9717ee | 2009-02-13 23:26:12 -0800 | [diff] [blame] | 425 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 426 | memset(branch_head, 0, sizeof(branch_head)); |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 427 | remote_head = get_merge_parent(remote); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 428 | if (!remote_head) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 429 | die(_("'%s' does not point to a commit"), remote); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 430 | |
Jeff King | 751c597 | 2009-08-09 06:02:24 -0400 | [diff] [blame] | 431 | if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) { |
| 432 | if (!prefixcmp(found_ref, "refs/heads/")) { |
| 433 | strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", |
| 434 | sha1_to_hex(branch_head), remote); |
| 435 | goto cleanup; |
| 436 | } |
Junio C Hamano | 57b58db | 2011-11-04 21:31:28 -0700 | [diff] [blame] | 437 | if (!prefixcmp(found_ref, "refs/tags/")) { |
| 438 | strbuf_addf(msg, "%s\t\ttag '%s' of .\n", |
| 439 | sha1_to_hex(branch_head), remote); |
| 440 | goto cleanup; |
| 441 | } |
Jeff King | 69a8b7c | 2009-08-09 06:02:51 -0400 | [diff] [blame] | 442 | if (!prefixcmp(found_ref, "refs/remotes/")) { |
Matthieu Moy | 1393123 | 2010-11-02 16:31:25 +0100 | [diff] [blame] | 443 | strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n", |
Jeff King | 69a8b7c | 2009-08-09 06:02:51 -0400 | [diff] [blame] | 444 | sha1_to_hex(branch_head), remote); |
| 445 | goto cleanup; |
| 446 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* See if remote matches <name>^^^.. or <name>~<number> */ |
| 450 | for (len = 0, ptr = remote + strlen(remote); |
| 451 | remote < ptr && ptr[-1] == '^'; |
| 452 | ptr--) |
| 453 | len++; |
| 454 | if (len) |
| 455 | early = 1; |
| 456 | else { |
| 457 | early = 0; |
| 458 | ptr = strrchr(remote, '~'); |
| 459 | if (ptr) { |
| 460 | int seen_nonzero = 0; |
| 461 | |
| 462 | len++; /* count ~ */ |
| 463 | while (*++ptr && isdigit(*ptr)) { |
| 464 | seen_nonzero |= (*ptr != '0'); |
| 465 | len++; |
| 466 | } |
| 467 | if (*ptr) |
| 468 | len = 0; /* not ...~<number> */ |
| 469 | else if (seen_nonzero) |
| 470 | early = 1; |
| 471 | else if (len == 1) |
| 472 | early = 1; /* "name~" is "name~1"! */ |
| 473 | } |
| 474 | } |
| 475 | if (len) { |
| 476 | struct strbuf truname = STRBUF_INIT; |
| 477 | strbuf_addstr(&truname, "refs/heads/"); |
| 478 | strbuf_addstr(&truname, remote); |
Junio C Hamano | 9b6bf4d | 2008-07-30 01:12:19 -0700 | [diff] [blame] | 479 | strbuf_setlen(&truname, truname.len - len); |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 480 | if (ref_exists(truname.buf)) { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 481 | strbuf_addf(msg, |
| 482 | "%s\t\tbranch '%s'%s of .\n", |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 483 | sha1_to_hex(remote_head->object.sha1), |
Junio C Hamano | 9b6bf4d | 2008-07-30 01:12:19 -0700 | [diff] [blame] | 484 | truname.buf + 11, |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 485 | (early ? " (early part)" : "")); |
Junio C Hamano | c9717ee | 2009-02-13 23:26:12 -0800 | [diff] [blame] | 486 | strbuf_release(&truname); |
| 487 | goto cleanup; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | if (!strcmp(remote, "FETCH_HEAD") && |
| 492 | !access(git_path("FETCH_HEAD"), R_OK)) { |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 493 | const char *filename; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 494 | FILE *fp; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 495 | struct strbuf line = STRBUF_INIT; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 496 | char *ptr; |
| 497 | |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 498 | filename = git_path("FETCH_HEAD"); |
| 499 | fp = fopen(filename, "r"); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 500 | if (!fp) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 501 | die_errno(_("could not open '%s' for reading"), |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 502 | filename); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 503 | strbuf_getline(&line, fp, '\n'); |
| 504 | fclose(fp); |
| 505 | ptr = strstr(line.buf, "\tnot-for-merge\t"); |
| 506 | if (ptr) |
| 507 | strbuf_remove(&line, ptr-line.buf+1, 13); |
| 508 | strbuf_addbuf(msg, &line); |
| 509 | strbuf_release(&line); |
Junio C Hamano | c9717ee | 2009-02-13 23:26:12 -0800 | [diff] [blame] | 510 | goto cleanup; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 511 | } |
| 512 | strbuf_addf(msg, "%s\t\tcommit '%s'\n", |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 513 | sha1_to_hex(remote_head->object.sha1), remote); |
Junio C Hamano | c9717ee | 2009-02-13 23:26:12 -0800 | [diff] [blame] | 514 | cleanup: |
| 515 | strbuf_release(&buf); |
| 516 | strbuf_release(&bname); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Junio C Hamano | 0d8fc3e | 2011-05-04 17:42:51 -0700 | [diff] [blame] | 519 | static void parse_branch_merge_options(char *bmo) |
| 520 | { |
| 521 | const char **argv; |
| 522 | int argc; |
| 523 | |
| 524 | if (!bmo) |
| 525 | return; |
| 526 | argc = split_cmdline(bmo, &argv); |
| 527 | if (argc < 0) |
Junio C Hamano | c7fe5b6 | 2011-05-11 11:38:36 -0700 | [diff] [blame] | 528 | die(_("Bad branch.%s.mergeoptions string: %s"), branch, |
| 529 | split_cmdline_strerror(argc)); |
Junio C Hamano | 0d8fc3e | 2011-05-04 17:42:51 -0700 | [diff] [blame] | 530 | argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); |
| 531 | memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); |
| 532 | argc++; |
| 533 | argv[0] = "branch.*.mergeoptions"; |
| 534 | parse_options(argc, argv, NULL, builtin_merge_options, |
| 535 | builtin_merge_usage, 0); |
| 536 | free(argv); |
| 537 | } |
| 538 | |
Stephan Beyer | 186458b | 2008-07-24 01:09:35 +0200 | [diff] [blame] | 539 | static int git_merge_config(const char *k, const char *v, void *cb) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 540 | { |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 541 | int status; |
| 542 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 543 | if (branch && !prefixcmp(k, "branch.") && |
| 544 | !prefixcmp(k + 7, branch) && |
| 545 | !strcmp(k + 7 + strlen(branch), ".mergeoptions")) { |
Junio C Hamano | 0d8fc3e | 2011-05-04 17:42:51 -0700 | [diff] [blame] | 546 | free(branch_mergeoptions); |
| 547 | branch_mergeoptions = xstrdup(v); |
| 548 | return 0; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat")) |
| 552 | show_diffstat = git_config_bool(k, v); |
| 553 | else if (!strcmp(k, "pull.twohead")) |
| 554 | return git_config_string(&pull_twohead, k, v); |
| 555 | else if (!strcmp(k, "pull.octopus")) |
| 556 | return git_config_string(&pull_octopus, k, v); |
Jonathan Nieder | 7610fa5 | 2010-08-05 06:32:41 -0500 | [diff] [blame] | 557 | else if (!strcmp(k, "merge.renormalize")) |
| 558 | option_renormalize = git_config_bool(k, v); |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 559 | else if (!strcmp(k, "merge.ff")) { |
Junio C Hamano | f23e8de | 2011-05-06 12:27:05 -0700 | [diff] [blame] | 560 | int boolval = git_config_maybe_bool(k, v); |
| 561 | if (0 <= boolval) { |
| 562 | allow_fast_forward = boolval; |
| 563 | } else if (v && !strcmp(v, "only")) { |
| 564 | allow_fast_forward = 1; |
| 565 | fast_forward_only = 1; |
| 566 | } /* do not barf on values from future versions of git */ |
| 567 | return 0; |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 568 | } else if (!strcmp(k, "merge.defaulttoupstream")) { |
| 569 | default_to_upstream = git_config_bool(k, v); |
| 570 | return 0; |
Ramkumar Ramachandra | 96e9420 | 2010-09-08 23:29:54 +0530 | [diff] [blame] | 571 | } |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 572 | status = fmt_merge_msg_config(k, v, cb); |
| 573 | if (status) |
| 574 | return status; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 575 | return git_diff_ui_config(k, v, cb); |
| 576 | } |
| 577 | |
| 578 | static int read_tree_trivial(unsigned char *common, unsigned char *head, |
| 579 | unsigned char *one) |
| 580 | { |
| 581 | int i, nr_trees = 0; |
| 582 | struct tree *trees[MAX_UNPACK_TREES]; |
| 583 | struct tree_desc t[MAX_UNPACK_TREES]; |
| 584 | struct unpack_trees_options opts; |
| 585 | |
| 586 | memset(&opts, 0, sizeof(opts)); |
| 587 | opts.head_idx = 2; |
| 588 | opts.src_index = &the_index; |
| 589 | opts.dst_index = &the_index; |
| 590 | opts.update = 1; |
| 591 | opts.verbose_update = 1; |
| 592 | opts.trivial_merges_only = 1; |
| 593 | opts.merge = 1; |
| 594 | trees[nr_trees] = parse_tree_indirect(common); |
| 595 | if (!trees[nr_trees++]) |
| 596 | return -1; |
| 597 | trees[nr_trees] = parse_tree_indirect(head); |
| 598 | if (!trees[nr_trees++]) |
| 599 | return -1; |
| 600 | trees[nr_trees] = parse_tree_indirect(one); |
| 601 | if (!trees[nr_trees++]) |
| 602 | return -1; |
| 603 | opts.fn = threeway_merge; |
| 604 | cache_tree_free(&active_cache_tree); |
| 605 | for (i = 0; i < nr_trees; i++) { |
| 606 | parse_tree(trees[i]); |
| 607 | init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); |
| 608 | } |
| 609 | if (unpack_trees(nr_trees, t, &opts)) |
| 610 | return -1; |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static void write_tree_trivial(unsigned char *sha1) |
| 615 | { |
| 616 | if (write_cache_as_tree(sha1, 0, NULL)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 617 | die(_("git write-tree failed to write a tree")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Jeff King | 1618073 | 2011-05-12 07:09:46 -0400 | [diff] [blame] | 620 | static const char *merge_argument(struct commit *commit) |
| 621 | { |
| 622 | if (commit) |
| 623 | return sha1_to_hex(commit->object.sha1); |
| 624 | else |
| 625 | return EMPTY_TREE_SHA1_HEX; |
| 626 | } |
| 627 | |
Jonathan Nieder | 67ac1e1 | 2010-12-10 18:51:44 -0600 | [diff] [blame] | 628 | int try_merge_command(const char *strategy, size_t xopts_nr, |
| 629 | const char **xopts, struct commit_list *common, |
Christian Couder | c674d05 | 2010-03-31 21:22:07 +0200 | [diff] [blame] | 630 | const char *head_arg, struct commit_list *remotes) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 631 | { |
| 632 | const char **args; |
Avery Pennarun | 8cc5b29 | 2009-11-25 21:23:55 -0500 | [diff] [blame] | 633 | int i = 0, x = 0, ret; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 634 | struct commit_list *j; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 635 | struct strbuf buf = STRBUF_INIT; |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 636 | |
| 637 | args = xmalloc((4 + xopts_nr + commit_list_count(common) + |
| 638 | commit_list_count(remotes)) * sizeof(char *)); |
| 639 | strbuf_addf(&buf, "merge-%s", strategy); |
| 640 | args[i++] = buf.buf; |
| 641 | for (x = 0; x < xopts_nr; x++) { |
| 642 | char *s = xmalloc(strlen(xopts[x])+2+1); |
| 643 | strcpy(s, "--"); |
| 644 | strcpy(s+2, xopts[x]); |
| 645 | args[i++] = s; |
| 646 | } |
| 647 | for (j = common; j; j = j->next) |
Jeff King | 1618073 | 2011-05-12 07:09:46 -0400 | [diff] [blame] | 648 | args[i++] = xstrdup(merge_argument(j->item)); |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 649 | args[i++] = "--"; |
| 650 | args[i++] = head_arg; |
| 651 | for (j = remotes; j; j = j->next) |
Jeff King | 1618073 | 2011-05-12 07:09:46 -0400 | [diff] [blame] | 652 | args[i++] = xstrdup(merge_argument(j->item)); |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 653 | args[i] = NULL; |
| 654 | ret = run_command_v_opt(args, RUN_GIT_CMD); |
| 655 | strbuf_release(&buf); |
| 656 | i = 1; |
| 657 | for (x = 0; x < xopts_nr; x++) |
| 658 | free((void *)args[i++]); |
| 659 | for (j = common; j; j = j->next) |
| 660 | free((void *)args[i++]); |
| 661 | i += 2; |
| 662 | for (j = remotes; j; j = j->next) |
| 663 | free((void *)args[i++]); |
| 664 | free(args); |
| 665 | discard_cache(); |
| 666 | if (read_cache() < 0) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 667 | die(_("failed to read the cache")); |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 668 | resolve_undo_clear(); |
| 669 | |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | static int try_merge_strategy(const char *strategy, struct commit_list *common, |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 674 | struct commit *head, const char *head_arg) |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 675 | { |
Miklos Vajna | 668f26f | 2008-10-03 15:02:31 +0200 | [diff] [blame] | 676 | int index_fd; |
| 677 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
| 678 | |
| 679 | index_fd = hold_locked_index(lock, 1); |
| 680 | refresh_cache(REFRESH_QUIET); |
| 681 | if (active_cache_changed && |
| 682 | (write_cache(index_fd, active_cache, active_nr) || |
| 683 | commit_locked_index(lock))) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 684 | return error(_("Unable to write index.")); |
Miklos Vajna | 668f26f | 2008-10-03 15:02:31 +0200 | [diff] [blame] | 685 | rollback_lock_file(lock); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 686 | |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 687 | if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) { |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 688 | int clean, x; |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 689 | struct commit *result; |
| 690 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
| 691 | int index_fd; |
| 692 | struct commit_list *reversed = NULL; |
| 693 | struct merge_options o; |
Christian Couder | 3f9083c | 2010-03-31 21:22:06 +0200 | [diff] [blame] | 694 | struct commit_list *j; |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 695 | |
| 696 | if (remoteheads->next) { |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 697 | error(_("Not handling anything other than two heads merge.")); |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 698 | return 2; |
| 699 | } |
| 700 | |
| 701 | init_merge_options(&o); |
| 702 | if (!strcmp(strategy, "subtree")) |
Junio C Hamano | 85e51b7 | 2008-06-30 22:18:57 -0700 | [diff] [blame] | 703 | o.subtree_shift = ""; |
Avery Pennarun | 8cc5b29 | 2009-11-25 21:23:55 -0500 | [diff] [blame] | 704 | |
Jonathan Nieder | 7610fa5 | 2010-08-05 06:32:41 -0500 | [diff] [blame] | 705 | o.renormalize = option_renormalize; |
Jeff King | 99bfc66 | 2011-02-20 04:53:21 -0500 | [diff] [blame] | 706 | o.show_rename_progress = |
| 707 | show_progress == -1 ? isatty(2) : show_progress; |
Jonathan Nieder | 7610fa5 | 2010-08-05 06:32:41 -0500 | [diff] [blame] | 708 | |
Jonathan Nieder | 635a7bb | 2010-08-26 00:47:58 -0500 | [diff] [blame] | 709 | for (x = 0; x < xopts_nr; x++) |
| 710 | if (parse_merge_opt(&o, xopts[x])) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 711 | die(_("Unknown option for merge-recursive: -X%s"), xopts[x]); |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 712 | |
| 713 | o.branch1 = head_arg; |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 714 | o.branch2 = merge_remote_util(remoteheads->item)->name; |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 715 | |
| 716 | for (j = common; j; j = j->next) |
| 717 | commit_list_insert(j->item, &reversed); |
| 718 | |
| 719 | index_fd = hold_locked_index(lock, 1); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 720 | clean = merge_recursive(&o, head, |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 721 | remoteheads->item, reversed, &result); |
| 722 | if (active_cache_changed && |
| 723 | (write_cache(index_fd, active_cache, active_nr) || |
| 724 | commit_locked_index(lock))) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 725 | die (_("unable to write %s"), get_index_file()); |
Miklos Vajna | 4271666 | 2008-09-06 18:29:49 +0200 | [diff] [blame] | 726 | rollback_lock_file(lock); |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 727 | return clean ? 0 : 1; |
| 728 | } else { |
Jonathan Nieder | 67ac1e1 | 2010-12-10 18:51:44 -0600 | [diff] [blame] | 729 | return try_merge_command(strategy, xopts_nr, xopts, |
| 730 | common, head_arg, remoteheads); |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 731 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static void count_diff_files(struct diff_queue_struct *q, |
| 735 | struct diff_options *opt, void *data) |
| 736 | { |
| 737 | int *count = data; |
| 738 | |
| 739 | (*count) += q->nr; |
| 740 | } |
| 741 | |
| 742 | static int count_unmerged_entries(void) |
| 743 | { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 744 | int i, ret = 0; |
| 745 | |
Junio C Hamano | be6ff81 | 2009-12-17 22:23:54 -0800 | [diff] [blame] | 746 | for (i = 0; i < active_nr; i++) |
| 747 | if (ce_stage(active_cache[i])) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 748 | ret++; |
| 749 | |
| 750 | return ret; |
| 751 | } |
| 752 | |
Junio C Hamano | cac42b2 | 2010-03-06 21:34:41 +0100 | [diff] [blame] | 753 | int checkout_fast_forward(const unsigned char *head, const unsigned char *remote) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 754 | { |
| 755 | struct tree *trees[MAX_UNPACK_TREES]; |
| 756 | struct unpack_trees_options opts; |
| 757 | struct tree_desc t[MAX_UNPACK_TREES]; |
| 758 | int i, fd, nr_trees = 0; |
| 759 | struct dir_struct dir; |
| 760 | struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); |
| 761 | |
Junio C Hamano | 9ca8f60 | 2008-08-20 15:09:28 -0700 | [diff] [blame] | 762 | refresh_cache(REFRESH_QUIET); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 763 | |
| 764 | fd = hold_locked_index(lock_file, 1); |
| 765 | |
| 766 | memset(&trees, 0, sizeof(trees)); |
| 767 | memset(&opts, 0, sizeof(opts)); |
| 768 | memset(&t, 0, sizeof(t)); |
Pierre Habouzit | 5d2299d | 2008-07-21 22:32:53 -0700 | [diff] [blame] | 769 | memset(&dir, 0, sizeof(dir)); |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 770 | dir.flags |= DIR_SHOW_IGNORED; |
Nguyễn Thái Ngọc Duy | fc001b5 | 2011-11-27 17:15:32 +0700 | [diff] [blame] | 771 | setup_standard_excludes(&dir); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 772 | opts.dir = &dir; |
| 773 | |
| 774 | opts.head_idx = 1; |
| 775 | opts.src_index = &the_index; |
| 776 | opts.dst_index = &the_index; |
| 777 | opts.update = 1; |
| 778 | opts.verbose_update = 1; |
| 779 | opts.merge = 1; |
| 780 | opts.fn = twoway_merge; |
Matthieu Moy | e294030 | 2010-09-02 13:57:34 +0200 | [diff] [blame] | 781 | setup_unpack_trees_porcelain(&opts, "merge"); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 782 | |
| 783 | trees[nr_trees] = parse_tree_indirect(head); |
| 784 | if (!trees[nr_trees++]) |
| 785 | return -1; |
| 786 | trees[nr_trees] = parse_tree_indirect(remote); |
| 787 | if (!trees[nr_trees++]) |
| 788 | return -1; |
| 789 | for (i = 0; i < nr_trees; i++) { |
| 790 | parse_tree(trees[i]); |
| 791 | init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); |
| 792 | } |
| 793 | if (unpack_trees(nr_trees, t, &opts)) |
| 794 | return -1; |
| 795 | if (write_cache(fd, active_cache, active_nr) || |
| 796 | commit_locked_index(lock_file)) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 797 | die(_("unable to write new index file")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static void split_merge_strategies(const char *string, struct strategy **list, |
| 802 | int *nr, int *alloc) |
| 803 | { |
| 804 | char *p, *q, *buf; |
| 805 | |
| 806 | if (!string) |
| 807 | return; |
| 808 | |
| 809 | buf = xstrdup(string); |
| 810 | q = buf; |
| 811 | for (;;) { |
| 812 | p = strchr(q, ' '); |
| 813 | if (!p) { |
| 814 | ALLOC_GROW(*list, *nr + 1, *alloc); |
| 815 | (*list)[(*nr)++].name = xstrdup(q); |
| 816 | free(buf); |
| 817 | return; |
| 818 | } else { |
| 819 | *p = '\0'; |
| 820 | ALLOC_GROW(*list, *nr + 1, *alloc); |
| 821 | (*list)[(*nr)++].name = xstrdup(q); |
| 822 | q = ++p; |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static void add_strategies(const char *string, unsigned attr) |
| 828 | { |
| 829 | struct strategy *list = NULL; |
| 830 | int list_alloc = 0, list_nr = 0, i; |
| 831 | |
| 832 | memset(&list, 0, sizeof(list)); |
| 833 | split_merge_strategies(string, &list, &list_nr, &list_alloc); |
Miklos Vajna | 1719b5e | 2008-07-21 18:10:47 +0200 | [diff] [blame] | 834 | if (list) { |
| 835 | for (i = 0; i < list_nr; i++) |
| 836 | append_strategy(get_strategy(list[i].name)); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) |
| 840 | if (all_strategy[i].attr & attr) |
| 841 | append_strategy(&all_strategy[i]); |
| 842 | |
| 843 | } |
| 844 | |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 845 | static void write_merge_msg(struct strbuf *msg) |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 846 | { |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 847 | const char *filename = git_path("MERGE_MSG"); |
| 848 | int fd = open(filename, O_WRONLY | O_CREAT, 0666); |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 849 | if (fd < 0) |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 850 | die_errno(_("Could not open '%s' for writing"), |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 851 | filename); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 852 | if (write_in_full(fd, msg->buf, msg->len) != msg->len) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 853 | die_errno(_("Could not write to '%s'"), filename); |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 854 | close(fd); |
| 855 | } |
| 856 | |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 857 | static void read_merge_msg(struct strbuf *msg) |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 858 | { |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 859 | const char *filename = git_path("MERGE_MSG"); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 860 | strbuf_reset(msg); |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 861 | if (strbuf_read_file(msg, filename, 0) < 0) |
| 862 | die_errno(_("Could not read from '%s'"), filename); |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 863 | } |
| 864 | |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 865 | static void write_merge_state(void); |
| 866 | static void abort_commit(const char *err_msg) |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 867 | { |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 868 | if (err_msg) |
| 869 | error("%s", err_msg); |
| 870 | fprintf(stderr, |
| 871 | _("Not committing merge; use 'git commit' to complete the merge.\n")); |
| 872 | write_merge_state(); |
| 873 | exit(1); |
| 874 | } |
| 875 | |
| 876 | static void prepare_to_commit(void) |
| 877 | { |
| 878 | struct strbuf msg = STRBUF_INIT; |
| 879 | strbuf_addbuf(&msg, &merge_msg); |
| 880 | strbuf_addch(&msg, '\n'); |
| 881 | write_merge_msg(&msg); |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 882 | run_hook(get_index_file(), "prepare-commit-msg", |
| 883 | git_path("MERGE_MSG"), "merge", NULL, NULL); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 884 | if (option_edit) { |
| 885 | if (launch_editor(git_path("MERGE_MSG"), NULL, NULL)) |
| 886 | abort_commit(NULL); |
| 887 | } |
| 888 | read_merge_msg(&msg); |
| 889 | stripspace(&msg, option_edit); |
| 890 | if (!msg.len) |
| 891 | abort_commit(_("Empty commit message.")); |
| 892 | strbuf_release(&merge_msg); |
| 893 | strbuf_addbuf(&merge_msg, &msg); |
| 894 | strbuf_release(&msg); |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 895 | } |
| 896 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 897 | static int merge_trivial(struct commit *head) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 898 | { |
| 899 | unsigned char result_tree[20], result_commit[20]; |
Brandon Casey | 36e4053 | 2008-10-08 19:07:54 -0500 | [diff] [blame] | 900 | struct commit_list *parent = xmalloc(sizeof(*parent)); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 901 | |
| 902 | write_tree_trivial(result_tree); |
Ævar Arnfjörð Bjarmason | 157efde | 2011-02-22 23:42:02 +0000 | [diff] [blame] | 903 | printf(_("Wonderful.\n")); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 904 | parent->item = head; |
Brandon Casey | 36e4053 | 2008-10-08 19:07:54 -0500 | [diff] [blame] | 905 | parent->next = xmalloc(sizeof(*parent->next)); |
Junio C Hamano | 446247d | 2008-08-23 12:56:57 -0700 | [diff] [blame] | 906 | parent->next->item = remoteheads->item; |
| 907 | parent->next->next = NULL; |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 908 | prepare_to_commit(); |
Miklos Vajna | ee20a12 | 2008-09-10 22:10:31 +0200 | [diff] [blame] | 909 | commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 910 | finish(head, result_commit, "In-index merge"); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 911 | drop_save(); |
| 912 | return 0; |
| 913 | } |
| 914 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 915 | static int finish_automerge(struct commit *head, |
| 916 | struct commit_list *common, |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 917 | unsigned char *result_tree, |
| 918 | const char *wt_strategy) |
| 919 | { |
| 920 | struct commit_list *parents = NULL, *j; |
| 921 | struct strbuf buf = STRBUF_INIT; |
| 922 | unsigned char result_commit[20]; |
| 923 | |
| 924 | free_commit_list(common); |
| 925 | if (allow_fast_forward) { |
| 926 | parents = remoteheads; |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 927 | commit_list_insert(head, &parents); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 928 | parents = reduce_heads(parents); |
| 929 | } else { |
| 930 | struct commit_list **pptr = &parents; |
| 931 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 932 | pptr = &commit_list_insert(head, |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 933 | pptr)->next; |
| 934 | for (j = remoteheads; j; j = j->next) |
| 935 | pptr = &commit_list_insert(j->item, pptr)->next; |
| 936 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 937 | strbuf_addch(&merge_msg, '\n'); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 938 | prepare_to_commit(); |
| 939 | free_commit_list(remoteheads); |
Miklos Vajna | ee20a12 | 2008-09-10 22:10:31 +0200 | [diff] [blame] | 940 | commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL); |
Junio C Hamano | f23101b | 2011-05-25 12:43:59 -0700 | [diff] [blame] | 941 | strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 942 | finish(head, result_commit, buf.buf); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 943 | strbuf_release(&buf); |
| 944 | drop_save(); |
| 945 | return 0; |
| 946 | } |
| 947 | |
Jonathan Nieder | 7610fa5 | 2010-08-05 06:32:41 -0500 | [diff] [blame] | 948 | static int suggest_conflicts(int renormalizing) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 949 | { |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 950 | const char *filename; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 951 | FILE *fp; |
| 952 | int pos; |
| 953 | |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 954 | filename = git_path("MERGE_MSG"); |
| 955 | fp = fopen(filename, "a"); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 956 | if (!fp) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 957 | die_errno(_("Could not open '%s' for writing"), filename); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 958 | fprintf(fp, "\nConflicts:\n"); |
| 959 | for (pos = 0; pos < active_nr; pos++) { |
| 960 | struct cache_entry *ce = active_cache[pos]; |
| 961 | |
| 962 | if (ce_stage(ce)) { |
| 963 | fprintf(fp, "\t%s\n", ce->name); |
| 964 | while (pos + 1 < active_nr && |
| 965 | !strcmp(ce->name, |
| 966 | active_cache[pos + 1]->name)) |
| 967 | pos++; |
| 968 | } |
| 969 | } |
| 970 | fclose(fp); |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 971 | rerere(allow_rerere_auto); |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 972 | printf(_("Automatic merge failed; " |
| 973 | "fix conflicts and then commit the result.\n")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 974 | return 1; |
| 975 | } |
| 976 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 977 | static struct commit *is_old_style_invocation(int argc, const char **argv, |
| 978 | const unsigned char *head) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 979 | { |
| 980 | struct commit *second_token = NULL; |
Junio C Hamano | 76bf488 | 2009-12-02 09:59:35 -0800 | [diff] [blame] | 981 | if (argc > 2) { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 982 | unsigned char second_sha1[20]; |
| 983 | |
| 984 | if (get_sha1(argv[1], second_sha1)) |
| 985 | return NULL; |
| 986 | second_token = lookup_commit_reference_gently(second_sha1, 0); |
| 987 | if (!second_token) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 988 | die(_("'%s' is not a commit"), argv[1]); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 989 | if (hashcmp(second_token->object.sha1, head)) |
| 990 | return NULL; |
| 991 | } |
| 992 | return second_token; |
| 993 | } |
| 994 | |
| 995 | static int evaluate_result(void) |
| 996 | { |
| 997 | int cnt = 0; |
| 998 | struct rev_info rev; |
| 999 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1000 | /* Check how many files differ. */ |
| 1001 | init_revisions(&rev, ""); |
| 1002 | setup_revisions(0, NULL, &rev, NULL); |
| 1003 | rev.diffopt.output_format |= |
| 1004 | DIFF_FORMAT_CALLBACK; |
| 1005 | rev.diffopt.format_callback = count_diff_files; |
| 1006 | rev.diffopt.format_callback_data = &cnt; |
| 1007 | run_diff_files(&rev, 0); |
| 1008 | |
| 1009 | /* |
| 1010 | * Check how many unmerged entries are |
| 1011 | * there. |
| 1012 | */ |
| 1013 | cnt += count_unmerged_entries(); |
| 1014 | |
| 1015 | return cnt; |
| 1016 | } |
| 1017 | |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * Pretend as if the user told us to merge with the tracking |
| 1020 | * branch we have for the upstream of the current branch |
| 1021 | */ |
| 1022 | static int setup_with_upstream(const char ***argv) |
| 1023 | { |
| 1024 | struct branch *branch = branch_get(NULL); |
| 1025 | int i; |
| 1026 | const char **args; |
| 1027 | |
| 1028 | if (!branch) |
Ævar Arnfjörð Bjarmason | c7f426d | 2011-04-10 19:34:04 +0000 | [diff] [blame] | 1029 | die(_("No current branch.")); |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 1030 | if (!branch->remote) |
Ævar Arnfjörð Bjarmason | c7f426d | 2011-04-10 19:34:04 +0000 | [diff] [blame] | 1031 | die(_("No remote for the current branch.")); |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 1032 | if (!branch->merge_nr) |
Ævar Arnfjörð Bjarmason | c7f426d | 2011-04-10 19:34:04 +0000 | [diff] [blame] | 1033 | die(_("No default upstream defined for the current branch.")); |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 1034 | |
| 1035 | args = xcalloc(branch->merge_nr + 1, sizeof(char *)); |
| 1036 | for (i = 0; i < branch->merge_nr; i++) { |
| 1037 | if (!branch->merge[i]->dst) |
Ævar Arnfjörð Bjarmason | c7f426d | 2011-04-10 19:34:04 +0000 | [diff] [blame] | 1038 | die(_("No remote tracking branch for %s from %s"), |
Junio C Hamano | 93e535a | 2011-03-23 23:48:24 -0700 | [diff] [blame] | 1039 | branch->merge[i]->src, branch->remote_name); |
| 1040 | args[i] = branch->merge[i]->dst; |
| 1041 | } |
| 1042 | args[i] = NULL; |
| 1043 | *argv = args; |
| 1044 | return i; |
| 1045 | } |
| 1046 | |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1047 | static void write_merge_state(void) |
| 1048 | { |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1049 | const char *filename; |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1050 | int fd; |
| 1051 | struct commit_list *j; |
| 1052 | struct strbuf buf = STRBUF_INIT; |
| 1053 | |
Junio C Hamano | 274a5c0 | 2011-11-07 14:45:10 -0800 | [diff] [blame] | 1054 | for (j = remoteheads; j; j = j->next) { |
| 1055 | unsigned const char *sha1; |
| 1056 | struct commit *c = j->item; |
| 1057 | if (c->util && merge_remote_util(c)->obj) { |
| 1058 | sha1 = merge_remote_util(c)->obj->sha1; |
| 1059 | } else { |
| 1060 | sha1 = c->object.sha1; |
| 1061 | } |
| 1062 | strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1)); |
| 1063 | } |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1064 | filename = git_path("MERGE_HEAD"); |
| 1065 | fd = open(filename, O_WRONLY | O_CREAT, 0666); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1066 | if (fd < 0) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1067 | die_errno(_("Could not open '%s' for writing"), filename); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1068 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1069 | die_errno(_("Could not write to '%s'"), filename); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1070 | close(fd); |
| 1071 | strbuf_addch(&merge_msg, '\n'); |
| 1072 | write_merge_msg(&merge_msg); |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1073 | |
| 1074 | filename = git_path("MERGE_MODE"); |
| 1075 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1076 | if (fd < 0) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1077 | die_errno(_("Could not open '%s' for writing"), filename); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1078 | strbuf_reset(&buf); |
| 1079 | if (!allow_fast_forward) |
| 1080 | strbuf_addf(&buf, "no-ff"); |
| 1081 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) |
Jonathan Nieder | 418c9b1 | 2011-11-16 02:03:36 -0600 | [diff] [blame] | 1082 | die_errno(_("Could not write to '%s'"), filename); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1083 | close(fd); |
| 1084 | } |
| 1085 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1086 | int cmd_merge(int argc, const char **argv, const char *prefix) |
| 1087 | { |
| 1088 | unsigned char result_tree[20]; |
Nguyễn Thái Ngọc Duy | b4fd940 | 2011-08-19 21:50:05 +0700 | [diff] [blame] | 1089 | unsigned char stash[20]; |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1090 | unsigned char head_sha1[20]; |
| 1091 | struct commit *head_commit; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1092 | struct strbuf buf = STRBUF_INIT; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1093 | const char *head_arg; |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1094 | int flag, i, ret = 0; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1095 | int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; |
| 1096 | struct commit_list *common = NULL; |
| 1097 | const char *best_strategy = NULL, *wt_strategy = NULL; |
| 1098 | struct commit_list **remotes = &remoteheads; |
| 1099 | |
Nguyễn Thái Ngọc Duy | da53eec | 2010-10-22 01:49:45 -0500 | [diff] [blame] | 1100 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 1101 | usage_with_options(builtin_merge_usage, builtin_merge_options); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * Check if we are _not_ on a detached HEAD, i.e. if there is a |
| 1105 | * current branch. |
| 1106 | */ |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1107 | branch = resolve_ref("HEAD", head_sha1, 0, &flag); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1108 | if (branch) { |
| 1109 | if (!prefixcmp(branch, "refs/heads/")) |
| 1110 | branch += 11; |
| 1111 | branch = xstrdup(branch); |
| 1112 | } |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1113 | if (!branch || is_null_sha1(head_sha1)) |
| 1114 | head_commit = NULL; |
Nguyễn Thái Ngọc Duy | baf18fc | 2011-09-17 21:57:45 +1000 | [diff] [blame] | 1115 | else |
| 1116 | head_commit = lookup_commit_or_die(head_sha1, "HEAD"); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1117 | |
| 1118 | git_config(git_merge_config, NULL); |
| 1119 | |
Junio C Hamano | 0d8fc3e | 2011-05-04 17:42:51 -0700 | [diff] [blame] | 1120 | if (branch_mergeoptions) |
| 1121 | parse_branch_merge_options(branch_mergeoptions); |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 1122 | argc = parse_options(argc, argv, prefix, builtin_merge_options, |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1123 | builtin_merge_usage, 0); |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 1124 | if (shortlog_len < 0) |
| 1125 | shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; |
Johan Herland | 2a22c1b | 2010-11-09 22:49:58 +0100 | [diff] [blame] | 1126 | |
Jeff King | 99bfc66 | 2011-02-20 04:53:21 -0500 | [diff] [blame] | 1127 | if (verbosity < 0 && show_progress == -1) |
| 1128 | show_progress = 0; |
| 1129 | |
Johan Herland | 35d2fff | 2010-11-09 22:49:59 +0100 | [diff] [blame] | 1130 | if (abort_current_merge) { |
| 1131 | int nargc = 2; |
| 1132 | const char *nargv[] = {"reset", "--merge", NULL}; |
| 1133 | |
| 1134 | if (!file_exists(git_path("MERGE_HEAD"))) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1135 | die(_("There is no merge to abort (MERGE_HEAD missing).")); |
Johan Herland | 35d2fff | 2010-11-09 22:49:59 +0100 | [diff] [blame] | 1136 | |
| 1137 | /* Invoke 'git reset --merge' */ |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1138 | ret = cmd_reset(nargc, nargv, prefix); |
| 1139 | goto done; |
Johan Herland | 35d2fff | 2010-11-09 22:49:59 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
Johan Herland | 2a22c1b | 2010-11-09 22:49:58 +0100 | [diff] [blame] | 1142 | if (read_cache_unmerged()) |
| 1143 | die_resolve_conflict("merge"); |
| 1144 | |
| 1145 | if (file_exists(git_path("MERGE_HEAD"))) { |
| 1146 | /* |
| 1147 | * There is no unmerged entry, don't advise 'git |
| 1148 | * add/rm <file>', just 'git commit'. |
| 1149 | */ |
| 1150 | if (advice_resolve_conflict) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1151 | die(_("You have not concluded your merge (MERGE_HEAD exists).\n" |
| 1152 | "Please, commit your changes before you can merge.")); |
Johan Herland | 2a22c1b | 2010-11-09 22:49:58 +0100 | [diff] [blame] | 1153 | else |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1154 | die(_("You have not concluded your merge (MERGE_HEAD exists).")); |
Johan Herland | 2a22c1b | 2010-11-09 22:49:58 +0100 | [diff] [blame] | 1155 | } |
Jay Soffian | d7e5c0c | 2011-02-19 23:12:27 -0500 | [diff] [blame] | 1156 | if (file_exists(git_path("CHERRY_PICK_HEAD"))) { |
| 1157 | if (advice_resolve_conflict) |
Ævar Arnfjörð Bjarmason | f68f180 | 2011-04-10 19:34:05 +0000 | [diff] [blame] | 1158 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" |
| 1159 | "Please, commit your changes before you can merge.")); |
Jay Soffian | d7e5c0c | 2011-02-19 23:12:27 -0500 | [diff] [blame] | 1160 | else |
Ævar Arnfjörð Bjarmason | f68f180 | 2011-04-10 19:34:05 +0000 | [diff] [blame] | 1161 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).")); |
Jay Soffian | d7e5c0c | 2011-02-19 23:12:27 -0500 | [diff] [blame] | 1162 | } |
Johan Herland | 2a22c1b | 2010-11-09 22:49:58 +0100 | [diff] [blame] | 1163 | resolve_undo_clear(); |
| 1164 | |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 1165 | if (verbosity < 0) |
| 1166 | show_diffstat = 0; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1167 | |
| 1168 | if (squash) { |
| 1169 | if (!allow_fast_forward) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1170 | die(_("You cannot combine --squash with --no-ff.")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1171 | option_commit = 0; |
| 1172 | } |
| 1173 | |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 1174 | if (!allow_fast_forward && fast_forward_only) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1175 | die(_("You cannot combine --no-ff with --ff-only.")); |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 1176 | |
Junio C Hamano | 4e8115f | 2011-04-07 15:57:57 -0700 | [diff] [blame] | 1177 | if (!abort_current_merge) { |
Vincent van Ravesteijn | 5480207 | 2011-11-21 14:30:40 +0100 | [diff] [blame] | 1178 | if (!argc) { |
| 1179 | if (default_to_upstream) |
| 1180 | argc = setup_with_upstream(&argv); |
| 1181 | else |
| 1182 | die(_("No commit specified and merge.defaultToUpstream not set.")); |
| 1183 | } else if (argc == 1 && !strcmp(argv[0], "-")) |
Junio C Hamano | 4e8115f | 2011-04-07 15:57:57 -0700 | [diff] [blame] | 1184 | argv[0] = "@{-1}"; |
| 1185 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1186 | if (!argc) |
| 1187 | usage_with_options(builtin_merge_usage, |
| 1188 | builtin_merge_options); |
| 1189 | |
| 1190 | /* |
| 1191 | * This could be traditional "merge <msg> HEAD <commit>..." and |
| 1192 | * the way we can tell it is to see if the second token is HEAD, |
| 1193 | * but some people might have misused the interface and used a |
| 1194 | * committish that is the same as HEAD there instead. |
| 1195 | * Traditional format never would have "-m" so it is an |
| 1196 | * additional safety measure to check for it. |
| 1197 | */ |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1198 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1199 | if (!have_message && head_commit && |
| 1200 | is_old_style_invocation(argc, argv, head_commit->object.sha1)) { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1201 | strbuf_addstr(&merge_msg, argv[0]); |
| 1202 | head_arg = argv[1]; |
| 1203 | argv += 2; |
| 1204 | argc -= 2; |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1205 | } else if (!head_commit) { |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1206 | struct commit *remote_head; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1207 | /* |
| 1208 | * If the merged head is a valid one there is no reason |
| 1209 | * to forbid "git merge" into a branch yet to be born. |
| 1210 | * We do the same for "git pull". |
| 1211 | */ |
| 1212 | if (argc != 1) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1213 | die(_("Can merge only exactly one commit into " |
| 1214 | "empty head")); |
Paolo Bonzini | 4be636f | 2008-08-21 14:14:18 +0200 | [diff] [blame] | 1215 | if (squash) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1216 | die(_("Squash commit into empty head not supported yet")); |
Paolo Bonzini | 4be636f | 2008-08-21 14:14:18 +0200 | [diff] [blame] | 1217 | if (!allow_fast_forward) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1218 | die(_("Non-fast-forward commit does not make sense into " |
| 1219 | "an empty head")); |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1220 | remote_head = get_merge_parent(argv[0]); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1221 | if (!remote_head) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1222 | die(_("%s - not something we can merge"), argv[0]); |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1223 | read_empty(remote_head->object.sha1, 0); |
| 1224 | update_ref("initial pull", "HEAD", remote_head->object.sha1, |
| 1225 | NULL, 0, DIE_ON_ERR); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1226 | goto done; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1227 | } else { |
Tay Ray Chuan | 97d45bc | 2010-05-11 01:17:48 +0800 | [diff] [blame] | 1228 | struct strbuf merge_names = STRBUF_INIT; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1229 | |
| 1230 | /* We are invoked directly as the first-class UI. */ |
| 1231 | head_arg = "HEAD"; |
| 1232 | |
| 1233 | /* |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1234 | * All the rest are the commits being merged; prepare |
| 1235 | * the standard merge summary message to be appended |
| 1236 | * to the given message. |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1237 | */ |
Tay Ray Chuan | f0ecac2 | 2010-05-11 01:17:52 +0800 | [diff] [blame] | 1238 | for (i = 0; i < argc; i++) |
| 1239 | merge_name(argv[i], &merge_names); |
| 1240 | |
Ramkumar Ramachandra | 96e9420 | 2010-09-08 23:29:54 +0530 | [diff] [blame] | 1241 | if (!have_message || shortlog_len) { |
Junio C Hamano | cbda121 | 2011-11-04 17:35:42 -0700 | [diff] [blame] | 1242 | struct fmt_merge_msg_opts opts; |
| 1243 | memset(&opts, 0, sizeof(opts)); |
| 1244 | opts.add_title = !have_message; |
| 1245 | opts.shortlog_len = shortlog_len; |
| 1246 | |
| 1247 | fmt_merge_msg(&merge_names, &merge_msg, &opts); |
Ramkumar Ramachandra | 1876166 | 2010-09-08 23:29:53 +0530 | [diff] [blame] | 1248 | if (merge_msg.len) |
| 1249 | strbuf_setlen(&merge_msg, merge_msg.len - 1); |
| 1250 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1253 | if (!head_commit || !argc) |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1254 | usage_with_options(builtin_merge_usage, |
| 1255 | builtin_merge_options); |
| 1256 | |
| 1257 | strbuf_addstr(&buf, "merge"); |
| 1258 | for (i = 0; i < argc; i++) |
| 1259 | strbuf_addf(&buf, " %s", argv[i]); |
| 1260 | setenv("GIT_REFLOG_ACTION", buf.buf, 0); |
| 1261 | strbuf_reset(&buf); |
| 1262 | |
| 1263 | for (i = 0; i < argc; i++) { |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1264 | struct commit *commit = get_merge_parent(argv[i]); |
| 1265 | if (!commit) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1266 | die(_("%s - not something we can merge"), argv[i]); |
Miklos Vajna | 18668f5 | 2008-08-28 15:43:00 +0200 | [diff] [blame] | 1267 | remotes = &commit_list_insert(commit, remotes)->next; |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1268 | strbuf_addf(&buf, "GITHEAD_%s", |
| 1269 | sha1_to_hex(commit->object.sha1)); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1270 | setenv(buf.buf, argv[i], 1); |
| 1271 | strbuf_reset(&buf); |
Junio C Hamano | fab47d0 | 2011-11-07 16:29:34 -0800 | [diff] [blame] | 1272 | if (merge_remote_util(commit) && |
| 1273 | merge_remote_util(commit)->obj && |
| 1274 | merge_remote_util(commit)->obj->type == OBJ_TAG) { |
| 1275 | option_edit = 1; |
| 1276 | allow_fast_forward = 0; |
| 1277 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | if (!use_strategies) { |
| 1281 | if (!remoteheads->next) |
| 1282 | add_strategies(pull_twohead, DEFAULT_TWOHEAD); |
| 1283 | else |
| 1284 | add_strategies(pull_octopus, DEFAULT_OCTOPUS); |
| 1285 | } |
| 1286 | |
| 1287 | for (i = 0; i < use_strategies_nr; i++) { |
| 1288 | if (use_strategies[i]->attr & NO_FAST_FORWARD) |
| 1289 | allow_fast_forward = 0; |
| 1290 | if (use_strategies[i]->attr & NO_TRIVIAL) |
| 1291 | allow_trivial = 0; |
| 1292 | } |
| 1293 | |
| 1294 | if (!remoteheads->next) |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1295 | common = get_merge_bases(head_commit, remoteheads->item, 1); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1296 | else { |
| 1297 | struct commit_list *list = remoteheads; |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1298 | commit_list_insert(head_commit, &list); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1299 | common = get_octopus_merge_bases(list); |
| 1300 | free(list); |
| 1301 | } |
| 1302 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1303 | update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1, |
| 1304 | NULL, 0, DIE_ON_ERR); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1305 | |
| 1306 | if (!common) |
| 1307 | ; /* No common ancestors found. We need a real merge. */ |
| 1308 | else if (!remoteheads->next && !common->next && |
| 1309 | common->item == remoteheads->item) { |
| 1310 | /* |
| 1311 | * If head can reach all the merge then we are up to date. |
| 1312 | * but first the most common case of merging one remote. |
| 1313 | */ |
| 1314 | finish_up_to_date("Already up-to-date."); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1315 | goto done; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1316 | } else if (allow_fast_forward && !remoteheads->next && |
| 1317 | !common->next && |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1318 | !hashcmp(common->item->object.sha1, head_commit->object.sha1)) { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1319 | /* Again the most common case of merging one remote. */ |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1320 | struct strbuf msg = STRBUF_INIT; |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1321 | struct commit *commit; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1322 | char hex[41]; |
| 1323 | |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1324 | strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV)); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1325 | |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 1326 | if (verbosity >= 0) |
Ævar Arnfjörð Bjarmason | 2ceb61e | 2011-02-22 23:42:00 +0000 | [diff] [blame] | 1327 | printf(_("Updating %s..%s\n"), |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 1328 | hex, |
| 1329 | find_unique_abbrev(remoteheads->item->object.sha1, |
| 1330 | DEFAULT_ABBREV)); |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 1331 | strbuf_addstr(&msg, "Fast-forward"); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1332 | if (have_message) |
| 1333 | strbuf_addstr(&msg, |
| 1334 | " (no commit created; -m option ignored)"); |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1335 | commit = remoteheads->item; |
Junio C Hamano | b7f7c07 | 2011-12-09 13:37:14 -0800 | [diff] [blame] | 1336 | if (!commit) { |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1337 | ret = 1; |
| 1338 | goto done; |
| 1339 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1340 | |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1341 | if (checkout_fast_forward(head_commit->object.sha1, |
Junio C Hamano | b7f7c07 | 2011-12-09 13:37:14 -0800 | [diff] [blame] | 1342 | commit->object.sha1)) { |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1343 | ret = 1; |
| 1344 | goto done; |
| 1345 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1346 | |
Junio C Hamano | ae8e4c9 | 2011-11-07 13:26:22 -0800 | [diff] [blame] | 1347 | finish(head_commit, commit->object.sha1, msg.buf); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1348 | drop_save(); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1349 | goto done; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1350 | } else if (!remoteheads->next && common->next) |
| 1351 | ; |
| 1352 | /* |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 1353 | * We are not doing octopus and not fast-forward. Need |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1354 | * a real merge. |
| 1355 | */ |
| 1356 | else if (!remoteheads->next && !common->next && option_commit) { |
| 1357 | /* |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 1358 | * We are not doing octopus, not fast-forward, and have |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1359 | * only one common. |
| 1360 | */ |
| 1361 | refresh_cache(REFRESH_QUIET); |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 1362 | if (allow_trivial && !fast_forward_only) { |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1363 | /* See if it is really trivial. */ |
| 1364 | git_committer_info(IDENT_ERROR_ON_NO_NAME); |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1365 | printf(_("Trying really trivial in-index merge...\n")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1366 | if (!read_tree_trivial(common->item->object.sha1, |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1367 | head_commit->object.sha1, |
| 1368 | remoteheads->item->object.sha1)) { |
| 1369 | ret = merge_trivial(head_commit); |
| 1370 | goto done; |
| 1371 | } |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1372 | printf(_("Nope.\n")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1373 | } |
| 1374 | } else { |
| 1375 | /* |
| 1376 | * An octopus. If we can reach all the remote we are up |
| 1377 | * to date. |
| 1378 | */ |
| 1379 | int up_to_date = 1; |
| 1380 | struct commit_list *j; |
| 1381 | |
| 1382 | for (j = remoteheads; j; j = j->next) { |
| 1383 | struct commit_list *common_one; |
| 1384 | |
| 1385 | /* |
| 1386 | * Here we *have* to calculate the individual |
| 1387 | * merge_bases again, otherwise "git merge HEAD^ |
| 1388 | * HEAD^^" would be missed. |
| 1389 | */ |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1390 | common_one = get_merge_bases(head_commit, j->item, 1); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1391 | if (hashcmp(common_one->item->object.sha1, |
| 1392 | j->item->object.sha1)) { |
| 1393 | up_to_date = 0; |
| 1394 | break; |
| 1395 | } |
| 1396 | } |
| 1397 | if (up_to_date) { |
| 1398 | finish_up_to_date("Already up-to-date. Yeeah!"); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1399 | goto done; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 1403 | if (fast_forward_only) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1404 | die(_("Not possible to fast-forward, aborting.")); |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 1405 | |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1406 | /* We are going to make a new commit. */ |
| 1407 | git_committer_info(IDENT_ERROR_ON_NO_NAME); |
| 1408 | |
| 1409 | /* |
| 1410 | * At this point, we need a real merge. No matter what strategy |
| 1411 | * we use, it would operate on the index, possibly affecting the |
| 1412 | * working tree, and when resolved cleanly, have the desired |
| 1413 | * tree in the index -- this means that the index must be in |
| 1414 | * sync with the head commit. The strategies are responsible |
| 1415 | * to ensure this. |
| 1416 | */ |
Nguyễn Thái Ngọc Duy | b4fd940 | 2011-08-19 21:50:05 +0700 | [diff] [blame] | 1417 | if (use_strategies_nr == 1 || |
| 1418 | /* |
| 1419 | * Stash away the local changes so that we can try more than one. |
| 1420 | */ |
| 1421 | save_state(stash)) |
| 1422 | hashcpy(stash, null_sha1); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1423 | |
| 1424 | for (i = 0; i < use_strategies_nr; i++) { |
| 1425 | int ret; |
| 1426 | if (i) { |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1427 | printf(_("Rewinding the tree to pristine...\n")); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1428 | restore_state(head_commit->object.sha1, stash); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1429 | } |
| 1430 | if (use_strategies_nr != 1) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1431 | printf(_("Trying merge strategy %s...\n"), |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1432 | use_strategies[i]->name); |
| 1433 | /* |
| 1434 | * Remember which strategy left the state in the working |
| 1435 | * tree. |
| 1436 | */ |
| 1437 | wt_strategy = use_strategies[i]->name; |
| 1438 | |
| 1439 | ret = try_merge_strategy(use_strategies[i]->name, |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1440 | common, head_commit, head_arg); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1441 | if (!option_commit && !ret) { |
| 1442 | merge_was_ok = 1; |
| 1443 | /* |
| 1444 | * This is necessary here just to avoid writing |
| 1445 | * the tree, but later we will *not* exit with |
| 1446 | * status code 1 because merge_was_ok is set. |
| 1447 | */ |
| 1448 | ret = 1; |
| 1449 | } |
| 1450 | |
| 1451 | if (ret) { |
| 1452 | /* |
| 1453 | * The backend exits with 1 when conflicts are |
| 1454 | * left to be resolved, with 2 when it does not |
| 1455 | * handle the given merge at all. |
| 1456 | */ |
| 1457 | if (ret == 1) { |
| 1458 | int cnt = evaluate_result(); |
| 1459 | |
| 1460 | if (best_cnt <= 0 || cnt <= best_cnt) { |
| 1461 | best_strategy = use_strategies[i]->name; |
| 1462 | best_cnt = cnt; |
| 1463 | } |
| 1464 | } |
| 1465 | if (merge_was_ok) |
| 1466 | break; |
| 1467 | else |
| 1468 | continue; |
| 1469 | } |
| 1470 | |
| 1471 | /* Automerge succeeded. */ |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1472 | write_tree_trivial(result_tree); |
| 1473 | automerge_was_ok = 1; |
| 1474 | break; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * If we have a resulting tree, that means the strategy module |
| 1479 | * auto resolved the merge cleanly. |
| 1480 | */ |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1481 | if (automerge_was_ok) { |
| 1482 | ret = finish_automerge(head_commit, common, result_tree, |
| 1483 | wt_strategy); |
| 1484 | goto done; |
| 1485 | } |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1486 | |
| 1487 | /* |
| 1488 | * Pick the result from the best strategy and have the user fix |
| 1489 | * it up. |
| 1490 | */ |
| 1491 | if (!best_strategy) { |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1492 | restore_state(head_commit->object.sha1, stash); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1493 | if (use_strategies_nr > 1) |
| 1494 | fprintf(stderr, |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1495 | _("No merge strategy handled the merge.\n")); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1496 | else |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1497 | fprintf(stderr, _("Merge with strategy %s failed.\n"), |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1498 | use_strategies[0]->name); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1499 | ret = 2; |
| 1500 | goto done; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1501 | } else if (best_strategy == wt_strategy) |
| 1502 | ; /* We already have its result in the working tree. */ |
| 1503 | else { |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1504 | printf(_("Rewinding the tree to pristine...\n")); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1505 | restore_state(head_commit->object.sha1, stash); |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1506 | printf(_("Using the %s to prepare resolving by hand.\n"), |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1507 | best_strategy); |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1508 | try_merge_strategy(best_strategy, common, head_commit, head_arg); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | if (squash) |
Nguyễn Thái Ngọc Duy | 894642f | 2011-09-17 21:57:44 +1000 | [diff] [blame] | 1512 | finish(head_commit, NULL, NULL); |
Jay Soffian | 66f4b98 | 2011-10-08 14:39:52 -0400 | [diff] [blame] | 1513 | else |
| 1514 | write_merge_state(); |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1515 | |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1516 | if (merge_was_ok) |
Ævar Arnfjörð Bjarmason | bacec47 | 2011-02-22 23:41:59 +0000 | [diff] [blame] | 1517 | fprintf(stderr, _("Automatic merge went well; " |
| 1518 | "stopped before committing as requested\n")); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 1519 | else |
| 1520 | ret = suggest_conflicts(option_renormalize); |
| 1521 | |
| 1522 | done: |
| 1523 | free((char *)branch); |
| 1524 | return ret; |
Miklos Vajna | 1c7b76b | 2008-07-07 19:24:20 +0200 | [diff] [blame] | 1525 | } |