interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / hook.c

210 lines · 5647 bytes Raw Blame History
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 "thread-utils.h"
9
10#define BUILTIN_HOOK_RUN_USAGE \
11 N_("git hook run [--allow-unknown-hook-name] [--ignore-missing] [--to-stdin=<path>] [(-j|--jobs) <n>]\n" \
12 "<hook-name> [-- <hook-args>]")
13#define BUILTIN_HOOK_LIST_USAGE \
14 N_("git hook list [--allow-unknown-hook-name] [-z] [--show-scope] <hook-name>")
15
16static const char * const builtin_hook_usage[] = {
17 BUILTIN_HOOK_RUN_USAGE,
18 BUILTIN_HOOK_LIST_USAGE,
19 NULL
20};
21
22static const char * const builtin_hook_run_usage[] = {
23 BUILTIN_HOOK_RUN_USAGE,
24 NULL
25};
26
27static int list(int argc, const char **argv, const char *prefix,
28 struct repository *repo)
29{
30 static const char *const builtin_hook_list_usage[] = {
31 BUILTIN_HOOK_LIST_USAGE,
32 NULL
33 };
34 struct string_list *head;
35 struct string_list_item *item;
36 const char *hookname = NULL;
37 int line_terminator = '\n';
38 int show_scope = 0;
39 int allow_unknown = 0;
40 int ret = 0;
41
42 struct option list_options[] = {
43 OPT_SET_INT('z', NULL, &line_terminator,
44 N_("use NUL as line terminator"), '\0'),
45 OPT_BOOL(0, "show-scope", &show_scope,
46 N_("show the config scope that defined each hook")),
47 OPT_BOOL(0, "allow-unknown-hook-name", &allow_unknown,
48 N_("allow running a hook with a non-native hook name")),
49 OPT_END(),
50 };
51
52 argc = parse_options(argc, argv, prefix, list_options,
53 builtin_hook_list_usage, 0);
54
55 /*
56 * The only unnamed argument provided should be the hook-name; if we add
57 * arguments later they probably should be caught by parse_options.
58 */
59 if (argc != 1)
60 usage_msg_opt(_("you must specify a hook event name to list"),
61 builtin_hook_list_usage, list_options);
62
63 hookname = argv[0];
64
65 if (!allow_unknown && !is_known_hook(hookname)) {
66 error(_("unknown hook event '%s';\n"
67 "use --allow-unknown-hook-name to allow non-native hook names"),
68 hookname);
69 return 1;
70 }
71
72 head = list_hooks(repo, hookname, NULL);
73
74 if (!head->nr) {
75 warning(_("no hooks found for event '%s'"), hookname);
76 ret = 1; /* no hooks found */
77 goto cleanup;
78 }
79
80 for_each_string_list_item(item, head) {
81 struct hook *h = item->util;
82
83 switch (h->kind) {
84 case HOOK_TRADITIONAL:
85 printf("%s%s%c",
86 h->event_disabled ? "event-disabled\t" : "",
87 _("hook from hookdir"), line_terminator);
88 break;
89 case HOOK_CONFIGURED: {
90 const char *name = h->u.configured.friendly_name;
91 const char *scope = show_scope ?
92 config_scope_name(h->u.configured.scope) : NULL;
93 /*
94 * Show the most relevant disable reason. Event-level
95 * takes precedence: if the whole event is off, that
96 * is what the user needs to know. The per-hook
97 * "disabled" surfaces once the event is re-enabled.
98 */
99 const char *disability =
100 h->event_disabled ? "event-disabled\t" :
101 h->u.configured.disabled ? "disabled\t" :
102 "";
103 if (scope)
104 printf("%s\t%s%s%c", scope, disability, name,
105 line_terminator);
106 else
107 printf("%s%s%c", disability, name,
108 line_terminator);
109 break;
110 }
111 default:
112 BUG("unknown hook kind");
113 }
114 }
115
116cleanup:
117 string_list_clear_func(head, hook_free);
118 free(head);
119 return ret;
120}
121
122static int run(int argc, const char **argv, const char *prefix,
123 struct repository *repo UNUSED)
124{
125 int i;
126 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
127 int ignore_missing = 0;
128 int allow_unknown = 0;
129 int jobs = 0;
130 const char *hook_name;
131 struct option run_options[] = {
132 OPT_BOOL(0, "allow-unknown-hook-name", &allow_unknown,
133 N_("allow running a hook with a non-native hook name")),
134 OPT_BOOL(0, "ignore-missing", &ignore_missing,
135 N_("silently ignore missing requested <hook-name>")),
136 OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
137 N_("file to read into hooks' stdin")),
138 OPT_INTEGER('j', "jobs", &jobs,
139 N_("run up to <n> hooks simultaneously (-1 for CPU count)")),
140 OPT_END(),
141 };
142 int ret;
143
144 argc = parse_options(argc, argv, prefix, run_options,
145 builtin_hook_run_usage,
146 PARSE_OPT_KEEP_DASHDASH);
147
148 if (jobs == -1)
149 opt.jobs = online_cpus();
150 else if (jobs < 0)
151 die(_("invalid value for -j: %d"
152 " (use -1 for CPU count or a"
153 " positive integer)"), jobs);
154 else
155 opt.jobs = jobs;
156
157 if (!argc)
158 goto usage;
159
160 /*
161 * Having a -- for "run" when providing <hook-args> is
162 * mandatory.
163 */
164 if (argc > 1 && strcmp(argv[1], "--") &&
165 strcmp(argv[1], "--end-of-options"))
166 goto usage;
167
168 /* Add our arguments, start after -- */
169 for (i = 2 ; i < argc; i++)
170 strvec_push(&opt.args, argv[i]);
171
172 /* Need to take into account core.hooksPath */
173 repo_config(the_repository, git_default_config, NULL);
174
175 hook_name = argv[0];
176
177 if (!allow_unknown && !is_known_hook(hook_name)) {
178 error(_("unknown hook event '%s';\n"
179 "use --allow-unknown-hook-name to allow non-native hook names"),
180 hook_name);
181 return 1;
182 }
183
184 if (!ignore_missing)
185 opt.error_if_missing = 1;
186 ret = run_hooks_opt(the_repository, hook_name, &opt);
187 if (ret < 0) /* error() return */
188 ret = 1;
189 return ret;
190usage:
191 usage_with_options(builtin_hook_run_usage, run_options);
192}
193
194int cmd_hook(int argc,
195 const char **argv,
196 const char *prefix,
197 struct repository *repo)
198{
199 parse_opt_subcommand_fn *fn = NULL;
200 struct option builtin_hook_options[] = {
201 OPT_SUBCOMMAND("run", &fn, run),
202 OPT_SUBCOMMAND("list", &fn, list),
203 OPT_END(),
204 };
205
206 argc = parse_options(argc, argv, NULL, builtin_hook_options,
207 builtin_hook_usage, 0);
208
209 return fn(argc, argv, prefix, repo);
210}