run_hook(): allow more than 9 hook arguments
This is done using the ALLOC_GROW macro. Signed-off-by: Stephan Beyer <s-beyer@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stephan Beyer committed
Jan 17, 2009 at 04:02 UTC
14e6298f1215da503f0f65b63e13d674dd781868
2 files changed
+10
-10
Documentation/technical/api-run-command.txt
+1
-1
@@ -58,7 +58,7 @@ Functions
58
The first argument is a pathname to an index file, or NULL
59
if the hook uses the default index file or no index is needed.
60
The second argument is the name of the hook.
61
- The further arguments (up to 9) correspond to the hook arguments.
61
+ The further arguments correspond to the hook arguments.
62
The last argument has to be NULL to terminate the arguments list.
63
If the hook does not exist or is not executable, the return
64
value will be zero.
run-command.c
+9
-9
@@ -346,23 +346,22 @@ int finish_async(struct async *async)
346
int run_hook(const char *index_file, const char *name, ...)
347
{
348
struct child_process hook;
349
- const char *argv[10], *env[2];
349
+ const char **argv = NULL, *env[2];
350
char index[PATH_MAX];
351
va_list args;
352
int ret;
353
- int i;
353
+ size_t i = 0, alloc = 0;
354
355
if (access(git_path("hooks/%s", name), X_OK) < 0)
356
return 0;
357
358
va_start(args, name);
359
- argv[0] = git_path("hooks/%s", name);
360
- i = 0;
361
- do {
362
- if (++i >= ARRAY_SIZE(argv))
363
- die("run_hook(): too many arguments");
364
- argv[i] = va_arg(args, const char *);
365
- } while (argv[i]);
359
+ ALLOC_GROW(argv, i + 1, alloc);
360
+ argv[i++] = git_path("hooks/%s", name);
361
+ while (argv[i-1]) {
362
+ ALLOC_GROW(argv, i + 1, alloc);
363
+ argv[i++] = va_arg(args, const char *);
364
+ }
365
va_end(args);
366
367
memset(&hook, 0, sizeof(hook));
@@ -377,6 +376,7 @@ int run_hook(const char *index_file, const char *name, ...)
376
}
377
378
ret = start_command(&hook);
379
+ free(argv);
380
if (ret) {
381
warning("Could not spawn %s", argv[0]);
382
return ret;