Merge branch 'sl/worktree-sparse'
[alt-git.git] / builtin / hook.c
blob88051795c7f5bf016fc5856167c5d350827dff2e
1 #include "cache.h"
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "hook.h"
6 #include "parse-options.h"
7 #include "strbuf.h"
8 #include "strvec.h"
10 #define BUILTIN_HOOK_RUN_USAGE \
11 N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
13 static const char * const builtin_hook_usage[] = {
14 BUILTIN_HOOK_RUN_USAGE,
15 NULL
18 static const char * const builtin_hook_run_usage[] = {
19 BUILTIN_HOOK_RUN_USAGE,
20 NULL
23 static int run(int argc, const char **argv, const char *prefix)
25 int i;
26 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
27 int ignore_missing = 0;
28 const char *hook_name;
29 struct option run_options[] = {
30 OPT_BOOL(0, "ignore-missing", &ignore_missing,
31 N_("silently ignore missing requested <hook-name>")),
32 OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
33 N_("file to read into hooks' stdin")),
34 OPT_END(),
36 int ret;
38 argc = parse_options(argc, argv, prefix, run_options,
39 builtin_hook_run_usage,
40 PARSE_OPT_KEEP_DASHDASH);
42 if (!argc)
43 goto usage;
46 * Having a -- for "run" when providing <hook-args> is
47 * mandatory.
49 if (argc > 1 && strcmp(argv[1], "--") &&
50 strcmp(argv[1], "--end-of-options"))
51 goto usage;
53 /* Add our arguments, start after -- */
54 for (i = 2 ; i < argc; i++)
55 strvec_push(&opt.args, argv[i]);
57 /* Need to take into account core.hooksPath */
58 git_config(git_default_config, NULL);
60 hook_name = argv[0];
61 if (!ignore_missing)
62 opt.error_if_missing = 1;
63 ret = run_hooks_opt(hook_name, &opt);
64 if (ret < 0) /* error() return */
65 ret = 1;
66 return ret;
67 usage:
68 usage_with_options(builtin_hook_run_usage, run_options);
71 int cmd_hook(int argc, const char **argv, const char *prefix)
73 parse_opt_subcommand_fn *fn = NULL;
74 struct option builtin_hook_options[] = {
75 OPT_SUBCOMMAND("run", &fn, run),
76 OPT_END(),
79 argc = parse_options(argc, argv, NULL, builtin_hook_options,
80 builtin_hook_usage, 0);
82 return fn(argc, argv, prefix);