96e7225b31
In order to enable hooks to be run as an external process, by a standalone Git command, or by tools which wrap Git, provide an external means to run all configured hook commands for a given hook event. Most of our hooks require more complex functionality than this, but let's start with the bare minimum required to support our simplest hooks. In terms of implementation the usage_with_options() and "goto usage" pattern here mirrors that of builtin/{commit-graph,multi-pack-index}.c. Some of the implementation here, such as a function being named run_hooks_opt() when it's tasked with running one hook, to using the run_processes_parallel_tr2() API to run with jobs=1 is somewhere between a bit odd and and an overkill for the current features of this "hook run" command and the hook.[ch] API. This code will eventually be able to run multiple hooks declared in config in parallel, by starting out with these names and APIs we reduce the later churn of renaming functions, switching from the run_command() to run_processes_parallel_tr2() API etc. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Acked-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
81 lines
1.7 KiB
C
81 lines
1.7 KiB
C
#include "cache.h"
|
|
#include "builtin.h"
|
|
#include "config.h"
|
|
#include "hook.h"
|
|
#include "parse-options.h"
|
|
#include "strbuf.h"
|
|
#include "strvec.h"
|
|
|
|
#define BUILTIN_HOOK_RUN_USAGE \
|
|
N_("git hook run <hook-name> [-- <hook-args>]")
|
|
|
|
static const char * const builtin_hook_usage[] = {
|
|
BUILTIN_HOOK_RUN_USAGE,
|
|
NULL
|
|
};
|
|
|
|
static const char * const builtin_hook_run_usage[] = {
|
|
BUILTIN_HOOK_RUN_USAGE,
|
|
NULL
|
|
};
|
|
|
|
static int run(int argc, const char **argv, const char *prefix)
|
|
{
|
|
int i;
|
|
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
|
|
const char *hook_name;
|
|
struct option run_options[] = {
|
|
OPT_END(),
|
|
};
|
|
int ret;
|
|
|
|
argc = parse_options(argc, argv, prefix, run_options,
|
|
builtin_hook_run_usage,
|
|
PARSE_OPT_KEEP_DASHDASH);
|
|
|
|
if (!argc)
|
|
goto usage;
|
|
|
|
/*
|
|
* Having a -- for "run" when providing <hook-args> is
|
|
* mandatory.
|
|
*/
|
|
if (argc > 1 && strcmp(argv[1], "--") &&
|
|
strcmp(argv[1], "--end-of-options"))
|
|
goto usage;
|
|
|
|
/* Add our arguments, start after -- */
|
|
for (i = 2 ; i < argc; i++)
|
|
strvec_push(&opt.args, argv[i]);
|
|
|
|
/* Need to take into account core.hooksPath */
|
|
git_config(git_default_config, NULL);
|
|
|
|
hook_name = argv[0];
|
|
opt.error_if_missing = 1;
|
|
ret = run_hooks_opt(hook_name, &opt);
|
|
if (ret < 0) /* error() return */
|
|
ret = 1;
|
|
return ret;
|
|
usage:
|
|
usage_with_options(builtin_hook_run_usage, run_options);
|
|
}
|
|
|
|
int cmd_hook(int argc, const char **argv, const char *prefix)
|
|
{
|
|
struct option builtin_hook_options[] = {
|
|
OPT_END(),
|
|
};
|
|
|
|
argc = parse_options(argc, argv, NULL, builtin_hook_options,
|
|
builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION);
|
|
if (!argc)
|
|
goto usage;
|
|
|
|
if (!strcmp(argv[0], "run"))
|
|
return run(argc, argv, prefix);
|
|
|
|
usage:
|
|
usage_with_options(builtin_hook_usage, builtin_hook_options);
|
|
}
|