|
| 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 \ |
| 10 | + N_("git hook run [--ignore-missing] <hook-name> [-- <hook-args>]") |
| 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; |
| 26 | + int ignore_missing = 0; |
| 27 | + const char *hook_name; |
| 28 | + struct option run_options[] = { |
| 29 | + OPT_BOOL(0, "ignore-missing", &ignore_missing, |
| 30 | + N_("silently ignore missing requested <hook-name>")), |
| 31 | + OPT_END(), |
| 32 | + }; |
| 33 | + int ret; |
| 34 | + |
| 35 | + argc = parse_options(argc, argv, prefix, run_options, |
| 36 | + builtin_hook_run_usage, |
| 37 | + PARSE_OPT_KEEP_DASHDASH); |
| 38 | + |
| 39 | + if (!argc) |
| 40 | + goto usage; |
| 41 | + |
| 42 | + /* |
| 43 | + * Having a -- for "run" when providing <hook-args> is |
| 44 | + * mandatory. |
| 45 | + */ |
| 46 | + if (argc > 1 && strcmp(argv[1], "--") && |
| 47 | + strcmp(argv[1], "--end-of-options")) |
| 48 | + goto usage; |
| 49 | + |
| 50 | + /* Add our arguments, start after -- */ |
| 51 | + for (i = 2 ; i < argc; i++) |
| 52 | + strvec_push(&opt.args, argv[i]); |
| 53 | + |
| 54 | + /* Need to take into account core.hooksPath */ |
| 55 | + git_config(git_default_config, NULL); |
| 56 | + |
| 57 | + hook_name = argv[0]; |
| 58 | + if (!ignore_missing) |
| 59 | + opt.error_if_missing = 1; |
| 60 | + ret = run_hooks_opt(hook_name, &opt); |
| 61 | + if (ret < 0) /* error() return */ |
| 62 | + ret = 1; |
| 63 | + return ret; |
| 64 | +usage: |
| 65 | + usage_with_options(builtin_hook_run_usage, run_options); |
| 66 | +} |
| 67 | + |
| 68 | +int cmd_hook(int argc, const char **argv, const char *prefix) |
| 69 | +{ |
| 70 | + struct option builtin_hook_options[] = { |
| 71 | + OPT_END(), |
| 72 | + }; |
| 73 | + |
| 74 | + argc = parse_options(argc, argv, NULL, builtin_hook_options, |
| 75 | + builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION); |
| 76 | + if (!argc) |
| 77 | + goto usage; |
| 78 | + |
| 79 | + if (!strcmp(argv[0], "run")) |
| 80 | + return run(argc, argv, prefix); |
| 81 | + |
| 82 | +usage: |
| 83 | + usage_with_options(builtin_hook_usage, builtin_hook_options); |
| 84 | +} |
0 commit comments