merge: use argv_array when spawning merge strategy

This is shorter, and avoids a rather complicated set of allocation and free steps. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 19, 2014 at 17:29 UTC 5c1753b198a96ed6abaf6c09db4623df703005a6
1 file changed +13 -29
merge.c
+13 -29
@@ -18,39 +18,23 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
18 const char **xopts, struct commit_list *common,
19 const char *head_arg, struct commit_list *remotes)
20 {
21 - const char **args;
22 - int i = 0, x = 0, ret;
21 + struct argv_array args = ARGV_ARRAY_INIT;
22 + int i, ret;
23 struct commit_list *j;
24 - struct strbuf buf = STRBUF_INIT;
24
26 - args = xmalloc((4 + xopts_nr + commit_list_count(common) +
27 - commit_list_count(remotes)) * sizeof(char *));
28 - strbuf_addf(&buf, "merge-%s", strategy);
29 - args[i++] = buf.buf;
30 - for (x = 0; x < xopts_nr; x++) {
31 - char *s = xmalloc(strlen(xopts[x])+2+1);
32 - strcpy(s, "--");
33 - strcpy(s+2, xopts[x]);
34 - args[i++] = s;
35 - }
36 - for (j = common; j; j = j->next)
37 - args[i++] = xstrdup(merge_argument(j->item));
38 - args[i++] = "--";
39 - args[i++] = head_arg;
40 - for (j = remotes; j; j = j->next)
41 - args[i++] = xstrdup(merge_argument(j->item));
42 - args[i] = NULL;
43 - ret = run_command_v_opt(args, RUN_GIT_CMD);
44 - strbuf_release(&buf);
45 - i = 1;
46 - for (x = 0; x < xopts_nr; x++)
47 - free((void *)args[i++]);
25 + argv_array_pushf(&args, "merge-%s", strategy);
26 + for (i = 0; i < xopts_nr; i++)
27 + argv_array_pushf(&args, "--%s", xopts[i]);
28 for (j = common; j; j = j->next)
49 - free((void *)args[i++]);
50 - i += 2;
29 + argv_array_push(&args, merge_argument(j->item));
30 + argv_array_push(&args, "--");
31 + argv_array_push(&args, head_arg);
32 for (j = remotes; j; j = j->next)
52 - free((void *)args[i++]);
53 - free(args);
33 + argv_array_push(&args, merge_argument(j->item));
34 +
35 + ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
36 + argv_array_clear(&args);
37 +
38 discard_cache();
39 if (read_cache() < 0)
40 die(_("failed to read the cache"));