2021-12-22 11:59:27 +08:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "config.h"
|
2023-03-21 14:25:54 +08:00
|
|
|
#include "gettext.h"
|
2021-12-22 11:59:27 +08:00
|
|
|
#include "hook.h"
|
|
|
|
#include "parse-options.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "strvec.h"
|
|
|
|
|
|
|
|
#define BUILTIN_HOOK_RUN_USAGE \
|
2023-02-09 03:21:15 +08:00
|
|
|
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
|
2021-12-22 11:59:27 +08:00
|
|
|
|
|
|
|
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;
|
2021-12-22 11:59:37 +08:00
|
|
|
int ignore_missing = 0;
|
2021-12-22 11:59:27 +08:00
|
|
|
const char *hook_name;
|
|
|
|
struct option run_options[] = {
|
2021-12-22 11:59:37 +08:00
|
|
|
OPT_BOOL(0, "ignore-missing", &ignore_missing,
|
|
|
|
N_("silently ignore missing requested <hook-name>")),
|
2023-02-09 03:21:15 +08:00
|
|
|
OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
|
|
|
|
N_("file to read into hooks' stdin")),
|
2021-12-22 11:59:27 +08:00
|
|
|
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];
|
2021-12-22 11:59:37 +08:00
|
|
|
if (!ignore_missing)
|
|
|
|
opt.error_if_missing = 1;
|
2021-12-22 11:59:27 +08:00
|
|
|
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)
|
|
|
|
{
|
2022-08-20 00:04:04 +08:00
|
|
|
parse_opt_subcommand_fn *fn = NULL;
|
2021-12-22 11:59:27 +08:00
|
|
|
struct option builtin_hook_options[] = {
|
2022-08-20 00:04:04 +08:00
|
|
|
OPT_SUBCOMMAND("run", &fn, run),
|
2021-12-22 11:59:27 +08:00
|
|
|
OPT_END(),
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, NULL, builtin_hook_options,
|
2022-08-20 00:04:04 +08:00
|
|
|
builtin_hook_usage, 0);
|
2021-12-22 11:59:27 +08:00
|
|
|
|
2022-08-20 00:04:04 +08:00
|
|
|
return fn(argc, argv, prefix);
|
2021-12-22 11:59:27 +08:00
|
|
|
}
|