builtin/bundle: have unbundle check for repo before opening its bundle
[git/gitster.git] / builtin / hook.c
blob5234693a94b40e2a6dd7aa4a8d724f0ac7b06a3b
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hook.h"
5 #include "parse-options.h"
6 #include "strvec.h"
8 #define BUILTIN_HOOK_RUN_USAGE \
9 N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
11 static const char * const builtin_hook_usage[] = {
12 BUILTIN_HOOK_RUN_USAGE,
13 NULL
16 static const char * const builtin_hook_run_usage[] = {
17 BUILTIN_HOOK_RUN_USAGE,
18 NULL
21 static int run(int argc, const char **argv, const char *prefix)
23 int i;
24 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
25 int ignore_missing = 0;
26 const char *hook_name;
27 struct option run_options[] = {
28 OPT_BOOL(0, "ignore-missing", &ignore_missing,
29 N_("silently ignore missing requested <hook-name>")),
30 OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
31 N_("file to read into hooks' stdin")),
32 OPT_END(),
34 int ret;
36 argc = parse_options(argc, argv, prefix, run_options,
37 builtin_hook_run_usage,
38 PARSE_OPT_KEEP_DASHDASH);
40 if (!argc)
41 goto usage;
44 * Having a -- for "run" when providing <hook-args> is
45 * mandatory.
47 if (argc > 1 && strcmp(argv[1], "--") &&
48 strcmp(argv[1], "--end-of-options"))
49 goto usage;
51 /* Add our arguments, start after -- */
52 for (i = 2 ; i < argc; i++)
53 strvec_push(&opt.args, argv[i]);
55 /* Need to take into account core.hooksPath */
56 git_config(git_default_config, NULL);
58 hook_name = argv[0];
59 if (!ignore_missing)
60 opt.error_if_missing = 1;
61 ret = run_hooks_opt(hook_name, &opt);
62 if (ret < 0) /* error() return */
63 ret = 1;
64 return ret;
65 usage:
66 usage_with_options(builtin_hook_run_usage, run_options);
69 int cmd_hook(int argc, const char **argv, const char *prefix)
71 parse_opt_subcommand_fn *fn = NULL;
72 struct option builtin_hook_options[] = {
73 OPT_SUBCOMMAND("run", &fn, run),
74 OPT_END(),
77 argc = parse_options(argc, argv, NULL, builtin_hook_options,
78 builtin_hook_usage, 0);
80 return fn(argc, argv, prefix);