]>
Commit | Line | Data |
---|---|---|
96e7225b ES |
1 | #include "builtin.h" |
2 | #include "config.h" | |
f394e093 | 3 | #include "gettext.h" |
96e7225b ES |
4 | #include "hook.h" |
5 | #include "parse-options.h" | |
96e7225b ES |
6 | #include "strvec.h" |
7 | ||
8 | #define BUILTIN_HOOK_RUN_USAGE \ | |
0414b389 | 9 | N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]") |
96e7225b ES |
10 | |
11 | static const char * const builtin_hook_usage[] = { | |
12 | BUILTIN_HOOK_RUN_USAGE, | |
13 | NULL | |
14 | }; | |
15 | ||
16 | static const char * const builtin_hook_run_usage[] = { | |
17 | BUILTIN_HOOK_RUN_USAGE, | |
18 | NULL | |
19 | }; | |
20 | ||
21 | static int run(int argc, const char **argv, const char *prefix) | |
22 | { | |
23 | int i; | |
24 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; | |
0d3979c1 | 25 | int ignore_missing = 0; |
96e7225b ES |
26 | const char *hook_name; |
27 | struct option run_options[] = { | |
0d3979c1 ÆAB |
28 | OPT_BOOL(0, "ignore-missing", &ignore_missing, |
29 | N_("silently ignore missing requested <hook-name>")), | |
0414b389 ES |
30 | OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"), |
31 | N_("file to read into hooks' stdin")), | |
96e7225b ES |
32 | OPT_END(), |
33 | }; | |
34 | int ret; | |
35 | ||
36 | argc = parse_options(argc, argv, prefix, run_options, | |
37 | builtin_hook_run_usage, | |
38 | PARSE_OPT_KEEP_DASHDASH); | |
39 | ||
40 | if (!argc) | |
41 | goto usage; | |
42 | ||
43 | /* | |
44 | * Having a -- for "run" when providing <hook-args> is | |
45 | * mandatory. | |
46 | */ | |
47 | if (argc > 1 && strcmp(argv[1], "--") && | |
48 | strcmp(argv[1], "--end-of-options")) | |
49 | goto usage; | |
50 | ||
51 | /* Add our arguments, start after -- */ | |
52 | for (i = 2 ; i < argc; i++) | |
53 | strvec_push(&opt.args, argv[i]); | |
54 | ||
55 | /* Need to take into account core.hooksPath */ | |
56 | git_config(git_default_config, NULL); | |
57 | ||
58 | hook_name = argv[0]; | |
0d3979c1 ÆAB |
59 | if (!ignore_missing) |
60 | opt.error_if_missing = 1; | |
96e7225b ES |
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); | |
67 | } | |
68 | ||
69 | int cmd_hook(int argc, const char **argv, const char *prefix) | |
70 | { | |
f83736ce | 71 | parse_opt_subcommand_fn *fn = NULL; |
96e7225b | 72 | struct option builtin_hook_options[] = { |
f83736ce | 73 | OPT_SUBCOMMAND("run", &fn, run), |
96e7225b ES |
74 | OPT_END(), |
75 | }; | |
76 | ||
77 | argc = parse_options(argc, argv, NULL, builtin_hook_options, | |
f83736ce | 78 | builtin_hook_usage, 0); |
96e7225b | 79 | |
f83736ce | 80 | return fn(argc, argv, prefix); |
96e7225b | 81 | } |