builtin/merge.c: collect other parents early

Move the code around to populate remoteheads list early in the process before any decision regarding twohead vs octopus and fast-forwardness is made. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 17, 2012 at 11:31 UTC b5d887f90692515a6e9c99e7683533d12df3ade2
1 file changed +27 -13
builtin/merge.c
+27 -13
@@ -1137,6 +1137,21 @@ static int default_edit_option(void)
1137 st_stdin.st_mode == st_stdout.st_mode);
1138 }
1139
1140 +static struct commit_list *collect_parents(int argc, const char **argv)
1141 +{
1142 + int i;
1143 + struct commit_list *remoteheads = NULL;
1144 + struct commit_list **remotes = &remoteheads;
1145 +
1146 + for (i = 0; i < argc; i++) {
1147 + struct commit *commit = get_merge_parent(argv[i]);
1148 + if (!commit)
1149 + die(_("%s - not something we can merge"), argv[i]);
1150 + remotes = &commit_list_insert(commit, remotes)->next;
1151 + }
1152 + *remotes = NULL;
1153 + return remoteheads;
1154 +}
1155
1156 int cmd_merge(int argc, const char **argv, const char *prefix)
1157 {
@@ -1150,8 +1165,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1165 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1166 struct commit_list *common = NULL;
1167 const char *best_strategy = NULL, *wt_strategy = NULL;
1153 - struct commit_list *remoteheads = NULL;
1154 - struct commit_list **remotes = &remoteheads;
1168 + struct commit_list *remoteheads, *p;
1169 void *branch_to_free;
1170
1171 if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -1256,6 +1270,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1270 head_arg = argv[1];
1271 argv += 2;
1272 argc -= 2;
1273 + remoteheads = collect_parents(argc, argv);
1274 } else if (!head_commit) {
1275 struct commit *remote_head;
1276 /*
@@ -1271,7 +1286,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1286 if (!allow_fast_forward)
1287 die(_("Non-fast-forward commit does not make sense into "
1288 "an empty head"));
1274 - remote_head = get_merge_parent(argv[0]);
1289 + remoteheads = collect_parents(argc, argv);
1290 + remote_head = remoteheads->item;
1291 if (!remote_head)
1292 die(_("%s - not something we can merge"), argv[0]);
1293 read_empty(remote_head->object.sha1, 0);
@@ -1289,8 +1305,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1305 * the standard merge summary message to be appended
1306 * to the given message.
1307 */
1292 - for (i = 0; i < argc; i++)
1293 - merge_name(argv[i], &merge_names);
1308 + remoteheads = collect_parents(argc, argv);
1309 + for (p = remoteheads; p; p = p->next)
1310 + merge_name(merge_remote_util(p->item)->name, &merge_names);
1311
1312 if (!have_message || shortlog_len) {
1313 struct fmt_merge_msg_opts opts;
@@ -1309,19 +1326,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1326 builtin_merge_options);
1327
1328 strbuf_addstr(&buf, "merge");
1312 - for (i = 0; i < argc; i++)
1313 - strbuf_addf(&buf, " %s", argv[i]);
1329 + for (p = remoteheads; p; p = p->next)
1330 + strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
1331 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1332 strbuf_reset(&buf);
1333
1317 - for (i = 0; i < argc; i++) {
1318 - struct commit *commit = get_merge_parent(argv[i]);
1319 - if (!commit)
1320 - die(_("%s - not something we can merge"), argv[i]);
1321 - remotes = &commit_list_insert(commit, remotes)->next;
1334 + for (p = remoteheads; p; p = p->next) {
1335 + struct commit *commit = p->item;
1336 strbuf_addf(&buf, "GITHEAD_%s",
1337 sha1_to_hex(commit->object.sha1));
1324 - setenv(buf.buf, argv[i], 1);
1338 + setenv(buf.buf, merge_remote_util(commit)->name, 1);
1339 strbuf_reset(&buf);
1340 if (!fast_forward_only &&
1341 merge_remote_util(commit) &&