Skip to content

Commit b5d887f

Browse files
committed
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 <[email protected]>
1 parent 4c57bd2 commit b5d887f

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

builtin/merge.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,21 @@ static int default_edit_option(void)
11371137
st_stdin.st_mode == st_stdout.st_mode);
11381138
}
11391139

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+
}
11401155

11411156
int cmd_merge(int argc, const char **argv, const char *prefix)
11421157
{
@@ -1150,8 +1165,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11501165
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
11511166
struct commit_list *common = NULL;
11521167
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;
11551169
void *branch_to_free;
11561170

11571171
if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -1256,6 +1270,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12561270
head_arg = argv[1];
12571271
argv += 2;
12581272
argc -= 2;
1273+
remoteheads = collect_parents(argc, argv);
12591274
} else if (!head_commit) {
12601275
struct commit *remote_head;
12611276
/*
@@ -1271,7 +1286,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12711286
if (!allow_fast_forward)
12721287
die(_("Non-fast-forward commit does not make sense into "
12731288
"an empty head"));
1274-
remote_head = get_merge_parent(argv[0]);
1289+
remoteheads = collect_parents(argc, argv);
1290+
remote_head = remoteheads->item;
12751291
if (!remote_head)
12761292
die(_("%s - not something we can merge"), argv[0]);
12771293
read_empty(remote_head->object.sha1, 0);
@@ -1289,8 +1305,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12891305
* the standard merge summary message to be appended
12901306
* to the given message.
12911307
*/
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);
12941311

12951312
if (!have_message || shortlog_len) {
12961313
struct fmt_merge_msg_opts opts;
@@ -1309,19 +1326,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13091326
builtin_merge_options);
13101327

13111328
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);
13141331
setenv("GIT_REFLOG_ACTION", buf.buf, 0);
13151332
strbuf_reset(&buf);
13161333

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;
13221336
strbuf_addf(&buf, "GITHEAD_%s",
13231337
sha1_to_hex(commit->object.sha1));
1324-
setenv(buf.buf, argv[i], 1);
1338+
setenv(buf.buf, merge_remote_util(commit)->name, 1);
13251339
strbuf_reset(&buf);
13261340
if (!fast_forward_only &&
13271341
merge_remote_util(commit) &&

0 commit comments

Comments
 (0)