Skip to content

Commit 5d40a17

Browse files
peffgitster
authored andcommitted
run_hook: use argv_array API
This was a pretty straightforward use, so it really doesn't save that many lines. Still, perhaps it's a little bit more readable. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bf0b01 commit 5d40a17

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

run-command.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "run-command.h"
33
#include "exec_cmd.h"
4+
#include "argv-array.h"
45

56
static inline void close_pair(int fd[2])
67
{
@@ -609,26 +610,23 @@ int finish_async(struct async *async)
609610
int run_hook(const char *index_file, const char *name, ...)
610611
{
611612
struct child_process hook;
612-
const char **argv = NULL, *env[2];
613+
struct argv_array argv = ARGV_ARRAY_INIT;
614+
const char *p, *env[2];
613615
char index[PATH_MAX];
614616
va_list args;
615617
int ret;
616-
size_t i = 0, alloc = 0;
617618

618619
if (access(git_path("hooks/%s", name), X_OK) < 0)
619620
return 0;
620621

621622
va_start(args, name);
622-
ALLOC_GROW(argv, i + 1, alloc);
623-
argv[i++] = git_path("hooks/%s", name);
624-
while (argv[i-1]) {
625-
ALLOC_GROW(argv, i + 1, alloc);
626-
argv[i++] = va_arg(args, const char *);
627-
}
623+
argv_array_push(&argv, git_path("hooks/%s", name));
624+
while ((p = va_arg(args, const char *)))
625+
argv_array_push(&argv, p);
628626
va_end(args);
629627

630628
memset(&hook, 0, sizeof(hook));
631-
hook.argv = argv;
629+
hook.argv = argv.argv;
632630
hook.no_stdin = 1;
633631
hook.stdout_to_stderr = 1;
634632
if (index_file) {
@@ -639,6 +637,6 @@ int run_hook(const char *index_file, const char *name, ...)
639637
}
640638

641639
ret = run_command(&hook);
642-
free(argv);
640+
argv_array_clear(&argv);
643641
return ret;
644642
}

0 commit comments

Comments
 (0)