1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-08 10:06:25 +02:00
git/builtin/hook.c
Emily Shaffer 0414b3891c hook: support a --to-stdin=<path> option
Expose the "path_to_stdin" API added in the preceding commit in the
"git hook run" command.

For now we won't be using this command interface outside of the tests,
but exposing this functionality makes it easier to test the hook
API. The plan is to use this to extend the "sendemail-validate"
hook[1][2].

1. https://lore.kernel.org/git/ad152e25-4061-9955-d3e6-a2c8b1bd24e7@amd.com
2. https://lore.kernel.org/git/20230120012459.920932-1-michael.strawbridge@amd.com

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-02-08 12:50:03 -08:00

83 lines
1.9 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 [--ignore-missing] [--to-stdin=<path>] <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;
int ignore_missing = 0;
const char *hook_name;
struct option run_options[] = {
OPT_BOOL(0, "ignore-missing", &ignore_missing,
N_("silently ignore missing requested <hook-name>")),
OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
N_("file to read into hooks' stdin")),
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];
if (!ignore_missing)
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)
{
parse_opt_subcommand_fn *fn = NULL;
struct option builtin_hook_options[] = {
OPT_SUBCOMMAND("run", &fn, run),
OPT_END(),
};
argc = parse_options(argc, argv, NULL, builtin_hook_options,
builtin_hook_usage, 0);
return fn(argc, argv, prefix);
}