Git fork
at reftables-rust 87 lines 2.1 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#include "builtin.h" 3#include "config.h" 4#include "environment.h" 5#include "gettext.h" 6#include "hook.h" 7#include "parse-options.h" 8#include "strvec.h" 9 10#define BUILTIN_HOOK_RUN_USAGE \ 11 N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]") 12 13static const char * const builtin_hook_usage[] = { 14 BUILTIN_HOOK_RUN_USAGE, 15 NULL 16}; 17 18static const char * const builtin_hook_run_usage[] = { 19 BUILTIN_HOOK_RUN_USAGE, 20 NULL 21}; 22 23static int run(int argc, const char **argv, const char *prefix, 24 struct repository *repo UNUSED) 25{ 26 int i; 27 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; 28 int ignore_missing = 0; 29 const char *hook_name; 30 struct option run_options[] = { 31 OPT_BOOL(0, "ignore-missing", &ignore_missing, 32 N_("silently ignore missing requested <hook-name>")), 33 OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"), 34 N_("file to read into hooks' stdin")), 35 OPT_END(), 36 }; 37 int ret; 38 39 argc = parse_options(argc, argv, prefix, run_options, 40 builtin_hook_run_usage, 41 PARSE_OPT_KEEP_DASHDASH); 42 43 if (!argc) 44 goto usage; 45 46 /* 47 * Having a -- for "run" when providing <hook-args> is 48 * mandatory. 49 */ 50 if (argc > 1 && strcmp(argv[1], "--") && 51 strcmp(argv[1], "--end-of-options")) 52 goto usage; 53 54 /* Add our arguments, start after -- */ 55 for (i = 2 ; i < argc; i++) 56 strvec_push(&opt.args, argv[i]); 57 58 /* Need to take into account core.hooksPath */ 59 repo_config(the_repository, git_default_config, NULL); 60 61 hook_name = argv[0]; 62 if (!ignore_missing) 63 opt.error_if_missing = 1; 64 ret = run_hooks_opt(the_repository, hook_name, &opt); 65 if (ret < 0) /* error() return */ 66 ret = 1; 67 return ret; 68usage: 69 usage_with_options(builtin_hook_run_usage, run_options); 70} 71 72int cmd_hook(int argc, 73 const char **argv, 74 const char *prefix, 75 struct repository *repo) 76{ 77 parse_opt_subcommand_fn *fn = NULL; 78 struct option builtin_hook_options[] = { 79 OPT_SUBCOMMAND("run", &fn, run), 80 OPT_END(), 81 }; 82 83 argc = parse_options(argc, argv, NULL, builtin_hook_options, 84 builtin_hook_usage, 0); 85 86 return fn(argc, argv, prefix, repo); 87}