Skip to content

Commit 5c1753b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3bdd552 commit 5c1753b

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

merge.c

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,23 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
1818
const char **xopts, struct commit_list *common,
1919
const char *head_arg, struct commit_list *remotes)
2020
{
21-
const char **args;
22-
int i = 0, x = 0, ret;
21+
struct argv_array args = ARGV_ARRAY_INIT;
22+
int i, ret;
2323
struct commit_list *j;
24-
struct strbuf buf = STRBUF_INIT;
2524

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]);
4828
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);
5132
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+
5438
discard_cache();
5539
if (read_cache() < 0)
5640
die(_("failed to read the cache"));

0 commit comments

Comments
 (0)