X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=run-command.c;h=b05c734d05e99cd009a0df26f0fc95fa13ae6f25;hb=a162e78df082d4f885bda2e51067d9788a7f65e4;hp=44fccc9d5ef4d01eb3c73d6ce8cfbb0cfff0362b;hpb=8bb78b7201bd8a34347bf4f8bcb9b70952553ca0;p=git.git diff --git a/run-command.c b/run-command.c index 44fccc9d5..b05c734d0 100644 --- a/run-command.c +++ b/run-command.c @@ -352,3 +352,48 @@ int finish_async(struct async *async) #endif return ret; } + +int run_hook(const char *index_file, const char *name, ...) +{ + struct child_process hook; + const char **argv = NULL, *env[2]; + char index[PATH_MAX]; + va_list args; + int ret; + size_t i = 0, alloc = 0; + + if (access(git_path("hooks/%s", name), X_OK) < 0) + return 0; + + va_start(args, name); + ALLOC_GROW(argv, i + 1, alloc); + argv[i++] = git_path("hooks/%s", name); + while (argv[i-1]) { + ALLOC_GROW(argv, i + 1, alloc); + argv[i++] = va_arg(args, const char *); + } + va_end(args); + + memset(&hook, 0, sizeof(hook)); + hook.argv = argv; + hook.no_stdin = 1; + hook.stdout_to_stderr = 1; + if (index_file) { + snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); + env[0] = index; + env[1] = NULL; + hook.env = env; + } + + ret = start_command(&hook); + free(argv); + if (ret) { + warning("Could not spawn %s", argv[0]); + return ret; + } + ret = finish_command(&hook); + if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL) + warning("%s exited due to uncaught signal", argv[0]); + + return ret; +}