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