Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "builtin.h" |
| 3 | #include "config.h" |
| 4 | #include "hook.h" |
| 5 | #include "parse-options.h" |
| 6 | #include "strbuf.h" |
| 7 | #include "strvec.h" |
| 8 | |
| 9 | #define BUILTIN_HOOK_RUN_USAGE \ |
Emily Shaffer | 0414b38 | 2023-02-08 20:21:15 +0100 | [diff] [blame] | 10 | N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]") |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 11 | |
| 12 | static const char * const builtin_hook_usage[] = { |
| 13 | BUILTIN_HOOK_RUN_USAGE, |
| 14 | NULL |
| 15 | }; |
| 16 | |
| 17 | static const char * const builtin_hook_run_usage[] = { |
| 18 | BUILTIN_HOOK_RUN_USAGE, |
| 19 | NULL |
| 20 | }; |
| 21 | |
| 22 | static int run(int argc, const char **argv, const char *prefix) |
| 23 | { |
| 24 | int i; |
| 25 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
Ævar Arnfjörð Bjarmason | 0d3979c | 2021-12-22 04:59:37 +0100 | [diff] [blame] | 26 | int ignore_missing = 0; |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 27 | const char *hook_name; |
| 28 | struct option run_options[] = { |
Ævar Arnfjörð Bjarmason | 0d3979c | 2021-12-22 04:59:37 +0100 | [diff] [blame] | 29 | OPT_BOOL(0, "ignore-missing", &ignore_missing, |
| 30 | N_("silently ignore missing requested <hook-name>")), |
Emily Shaffer | 0414b38 | 2023-02-08 20:21:15 +0100 | [diff] [blame] | 31 | OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"), |
| 32 | N_("file to read into hooks' stdin")), |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 33 | OPT_END(), |
| 34 | }; |
| 35 | int ret; |
| 36 | |
| 37 | argc = parse_options(argc, argv, prefix, run_options, |
| 38 | builtin_hook_run_usage, |
| 39 | PARSE_OPT_KEEP_DASHDASH); |
| 40 | |
| 41 | if (!argc) |
| 42 | goto usage; |
| 43 | |
| 44 | /* |
| 45 | * Having a -- for "run" when providing <hook-args> is |
| 46 | * mandatory. |
| 47 | */ |
| 48 | if (argc > 1 && strcmp(argv[1], "--") && |
| 49 | strcmp(argv[1], "--end-of-options")) |
| 50 | goto usage; |
| 51 | |
| 52 | /* Add our arguments, start after -- */ |
| 53 | for (i = 2 ; i < argc; i++) |
| 54 | strvec_push(&opt.args, argv[i]); |
| 55 | |
| 56 | /* Need to take into account core.hooksPath */ |
| 57 | git_config(git_default_config, NULL); |
| 58 | |
| 59 | hook_name = argv[0]; |
Ævar Arnfjörð Bjarmason | 0d3979c | 2021-12-22 04:59:37 +0100 | [diff] [blame] | 60 | if (!ignore_missing) |
| 61 | opt.error_if_missing = 1; |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 62 | ret = run_hooks_opt(hook_name, &opt); |
| 63 | if (ret < 0) /* error() return */ |
| 64 | ret = 1; |
| 65 | return ret; |
| 66 | usage: |
| 67 | usage_with_options(builtin_hook_run_usage, run_options); |
| 68 | } |
| 69 | |
| 70 | int cmd_hook(int argc, const char **argv, const char *prefix) |
| 71 | { |
SZEDER Gábor | f83736c | 2022-08-19 18:04:04 +0200 | [diff] [blame] | 72 | parse_opt_subcommand_fn *fn = NULL; |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 73 | struct option builtin_hook_options[] = { |
SZEDER Gábor | f83736c | 2022-08-19 18:04:04 +0200 | [diff] [blame] | 74 | OPT_SUBCOMMAND("run", &fn, run), |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 75 | OPT_END(), |
| 76 | }; |
| 77 | |
| 78 | argc = parse_options(argc, argv, NULL, builtin_hook_options, |
SZEDER Gábor | f83736c | 2022-08-19 18:04:04 +0200 | [diff] [blame] | 79 | builtin_hook_usage, 0); |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 80 | |
SZEDER Gábor | f83736c | 2022-08-19 18:04:04 +0200 | [diff] [blame] | 81 | return fn(argc, argv, prefix); |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 82 | } |