Skip to content

Commit 45bc950

Browse files
committed
describe: use argv-array
Instead of using a hand allocated args[] array, use argv-array API to manage the dynamically created list of arguments when invoking name-rev. Signed-off-by: Junio C Hamano <[email protected]>
1 parent b23e0b9 commit 45bc950

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

builtin/describe.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "parse-options.h"
88
#include "diff.h"
99
#include "hash.h"
10+
#include "argv-array.h"
1011

1112
#define SEEN (1u<<0)
1213
#define MAX_TAGS (FLAG_BITS - 1)
@@ -442,24 +443,23 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
442443
die(_("--long is incompatible with --abbrev=0"));
443444

444445
if (contains) {
445-
const char **args = xmalloc((7 + argc) * sizeof(char *));
446-
int i = 0;
447-
args[i++] = "name-rev";
448-
args[i++] = "--name-only";
449-
args[i++] = "--no-undefined";
446+
struct argv_array args;
447+
448+
argv_array_init(&args);
449+
argv_array_pushl(&args, "name-rev", "--name-only", "--no-undefined",
450+
NULL);
450451
if (always)
451-
args[i++] = "--always";
452+
argv_array_push(&args, "--always");
452453
if (!all) {
453-
args[i++] = "--tags";
454-
if (pattern) {
455-
char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
456-
sprintf(s, "--refs=refs/tags/%s", pattern);
457-
args[i++] = s;
458-
}
454+
argv_array_push(&args, "--tags");
455+
if (pattern)
456+
argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
457+
}
458+
while (*argv) {
459+
argv_array_push(&args, *argv);
460+
argv++;
459461
}
460-
memcpy(args + i, argv, argc * sizeof(char *));
461-
args[i + argc] = NULL;
462-
return cmd_name_rev(i + argc, args, prefix);
462+
return cmd_name_rev(args.argc, args.argv, prefix);
463463
}
464464

465465
init_hash(&names);

0 commit comments

Comments
 (0)