merge: narrow scope of merge_names

In order to pass the list of parents to fmt_merge_msg(), cmd_merge() uses this strbuf to create something that look like FETCH_HEAD that describes commits that are being merged. This is necessary only when we are creating the merge commit message ourselves, but was done unconditionally. Move the variable and the logic to populate it to confine them in a block that needs them. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 25, 2015 at 12:31 UTC 018b3fbc7e66439786492662b88cb34d7d000621
1 file changed +7 -4
builtin/merge.c
+7 -4
@@ -1236,8 +1236,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1236 argc -= 2;
1237 remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv);
1238 } else {
1239 - struct strbuf merge_names = STRBUF_INIT;
1240 -
1239 /* We are invoked directly as the first-class UI. */
1240 head_arg = "HEAD";
1241
@@ -1247,11 +1245,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1245 * to the given message.
1246 */
1247 remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv);
1250 - for (p = remoteheads; p; p = p->next)
1251 - merge_name(merge_remote_util(p->item)->name, &merge_names);
1248
1249 if (!have_message || shortlog_len) {
1250 + struct strbuf merge_names = STRBUF_INIT;
1251 struct fmt_merge_msg_opts opts;
1252 +
1253 + for (p = remoteheads; p; p = p->next)
1254 + merge_name(merge_remote_util(p->item)->name, &merge_names);
1255 +
1256 memset(&opts, 0, sizeof(opts));
1257 opts.add_title = !have_message;
1258 opts.shortlog_len = shortlog_len;
@@ -1260,6 +1261,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1261 fmt_merge_msg(&merge_names, &merge_msg, &opts);
1262 if (merge_msg.len)
1263 strbuf_setlen(&merge_msg, merge_msg.len - 1);
1264 +
1265 + strbuf_release(&merge_names);
1266 }
1267 }
1268