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 / hook.h

289 lines · 8600 bytes Raw Blame History
1#ifndef HOOK_H
2#define HOOK_H
3#include "config.h"
4#include "run-command.h"
5#include "string-list.h"
6#include "strmap.h"
7#include "strvec.h"
8
9struct repository;
10
11typedef void (*hook_data_free_fn)(void *data);
12typedef void *(*hook_data_alloc_fn)(void *init_ctx);
13
14/**
15 * Represents a hook command to be run.
16 * Hooks can be:
17 * 1. "traditional" (found in the hooks directory)
18 * 2. "configured" (defined in Git's configuration via hook.<friendly-name>.event).
19 * The 'kind' field determines which part of the union 'u' is valid.
20 */
21struct hook {
22 enum {
23 HOOK_TRADITIONAL,
24 HOOK_CONFIGURED,
25 } kind;
26 union {
27 struct {
28 const char *path;
29 } traditional;
30 struct {
31 const char *friendly_name;
32 const char *command;
33 enum config_scope scope;
34 bool disabled;
35 } configured;
36 } u;
37
38 /**
39 * Whether this hook's event is disabled with
40 * `hook.<event>.enabled = false`. This applies to configured hooks
41 * and the hookdir hook alike.
42 */
43 bool event_disabled;
44
45 /**
46 * Whether this hook may run in parallel with other hooks for the same
47 * event. Only useful for configured (named) hooks. Traditional hooks
48 * always default to 0 (serial). Set via `hook.<name>.parallel = true`.
49 */
50 bool parallel;
51
52 /**
53 * Opaque data pointer used to keep internal state across callback calls.
54 *
55 * It can be accessed directly via the third hook callback arg:
56 * struct ... *state = pp_task_cb;
57 *
58 * The caller is responsible for managing the memory for this data by
59 * providing alloc/free callbacks to `run_hooks_opt`.
60 *
61 * Only useful when using `run_hooks_opt.feed_pipe`, otherwise ignore it.
62 */
63 void *feed_pipe_cb_data;
64
65 /**
66 * Callback to free `feed_pipe_cb_data`.
67 *
68 * It is called automatically and points to the `feed_pipe_cb_data_free`
69 * provided via the `run_hook_opt` parameter.
70 */
71 hook_data_free_fn data_free;
72};
73
74struct run_hooks_opt {
75 /* Environment vars to be set for each hook */
76 struct strvec env;
77
78 /* Args to be passed to each hook */
79 struct strvec args;
80
81 /* Emit an error if the hook is missing */
82 unsigned int error_if_missing:1;
83
84 /**
85 * Number of processes to parallelize across.
86 *
87 * If > 1, output will be buffered and de-interleaved (ungroup=0).
88 * If == 1, output will be real-time (ungroup=1).
89 * If == 0, the 'hook.jobs' config is used or, if the config is unset,
90 * defaults to 1 (serial execution).
91 */
92 unsigned int jobs;
93
94 /**
95 * An optional initial working directory for the hook,
96 * translates to "struct child_process"'s "dir" member.
97 */
98 const char *dir;
99
100 /**
101 * A pointer which if provided will be set to 1 or 0 depending
102 * on if a hook was started, regardless of whether or not that
103 * was successful. I.e. if the underlying start_command() was
104 * successful this will be set to 1.
105 *
106 * Used for avoiding TOCTOU races in code that would otherwise
107 * call hook_exist() after a "maybe hook run" to see if a hook
108 * was invoked.
109 */
110 int *invoked_hook;
111
112 /**
113 * Send the hook's stdout to stderr.
114 *
115 * This is the default behavior for all hooks except pre-push,
116 * which keeps stdout and stderr separate for backwards compatibility.
117 * When parallel execution is requested (jobs > 1), get_hook_jobs()
118 * overrides this to 1 for all hooks so run-command can de-interleave
119 * their outputs correctly.
120 */
121 unsigned int stdout_to_stderr:1;
122
123 /**
124 * Path to file which should be piped to stdin for each hook.
125 */
126 const char *path_to_stdin;
127
128 /**
129 * Callback used to incrementally feed a child hook stdin pipe.
130 *
131 * Useful especially if a hook consumes large quantities of data
132 * (e.g. a list of all refs in a client push), so feeding it via
133 * in-memory strings or slurping to/from files is inefficient.
134 * While the callback allows piecemeal writing, it can also be
135 * used for smaller inputs, where it gets called only once.
136 *
137 * Add hook callback initialization context to `feed_pipe_ctx`.
138 * Add hook callback internal state to `feed_pipe_cb_data`.
139 *
140 */
141 feed_pipe_fn feed_pipe;
142
143 /**
144 * Opaque data pointer used to pass context to `feed_pipe_fn`.
145 *
146 * It can be accessed via the second callback arg 'pp_cb':
147 * ((struct hook_cb_data *) pp_cb)->hook_cb->options->feed_pipe_ctx;
148 *
149 * The caller is responsible for managing the memory for this data.
150 * Only useful when using `run_hooks_opt.feed_pipe`, otherwise ignore it.
151 */
152 void *feed_pipe_ctx;
153
154 /**
155 * Some hooks need to create a fresh `feed_pipe_cb_data` internal state,
156 * so they can keep track of progress without affecting one another.
157 *
158 * If provided, this function will be called to alloc & initialize the
159 * `feed_pipe_cb_data` for each hook.
160 *
161 * The `feed_pipe_ctx` pointer can be used to pass initialization data.
162 */
163 hook_data_alloc_fn feed_pipe_cb_data_alloc;
164
165 /**
166 * Called to free the memory initialized by `feed_pipe_cb_data_alloc`.
167 *
168 * Must always be provided when `feed_pipe_cb_data_alloc` is provided.
169 */
170 hook_data_free_fn feed_pipe_cb_data_free;
171};
172
173/**
174 * Default initializer for hooks. Parallelism is opt-in: .jobs = 0 defers to
175 * the 'hook.jobs' config, falling back to serial (1) if unset.
176 */
177#define RUN_HOOKS_OPT_INIT { \
178 .env = STRVEC_INIT, \
179 .args = STRVEC_INIT, \
180 .stdout_to_stderr = 1, \
181 .jobs = 0, \
182}
183
184/**
185 * Initializer for hooks that must always run sequentially regardless of
186 * 'hook.jobs'. Use this when git knows the hook cannot safely be parallelized
187 * .jobs = 1 is non-overridable.
188 */
189#define RUN_HOOKS_OPT_INIT_FORCE_SERIAL { \
190 .env = STRVEC_INIT, \
191 .args = STRVEC_INIT, \
192 .stdout_to_stderr = 1, \
193 .jobs = 1, \
194}
195
196struct hook_cb_data {
197 /* rc reflects the cumulative failure state */
198 int rc;
199 const char *hook_name;
200
201 /**
202 * A list of hook commands/paths to run for the 'hook_name' event.
203 *
204 * The 'string' member of each item holds the path (for traditional hooks)
205 * or the unique friendly-name for hooks specified in configs.
206 * The 'util' member of each item points to the corresponding struct hook.
207 */
208 struct string_list *hook_command_list;
209
210 /* Iterator/cursor for the above list, pointing to the next hook to run. */
211 size_t hook_to_run_index;
212
213 struct run_hooks_opt *options;
214};
215
216/**
217 * Provides a list of hook commands to run for the 'hookname' event.
218 *
219 * This function consolidates hooks from two sources:
220 * 1. The config-based hooks (not yet implemented).
221 * 2. The "traditional" hook found in the repository hooks directory
222 * (e.g., .git/hooks/pre-commit).
223 *
224 * The list is ordered by execution priority.
225 *
226 * The caller is responsible for freeing the memory of the returned list
227 * using string_list_clear() and free().
228 */
229struct string_list *list_hooks(struct repository *r, const char *hookname,
230 struct run_hooks_opt *options);
231
232/**
233 * Frees a struct hook stored as the util pointer of a string_list_item.
234 * Suitable for use as a string_list_clear_func_t callback.
235 */
236void hook_free(void *p, const char *str);
237
238/**
239 * Frees the hook configuration cache stored in `struct repository`.
240 * Called by repo_clear().
241 */
242void hook_cache_clear(struct strmap *cache);
243
244/**
245 * Returns true if `name` is a recognized hook event name
246 * (e.g. "pre-commit", "post-receive").
247 */
248bool is_known_hook(const char *name);
249
250/**
251 * Returns the path to the hook file, or NULL if the hook is missing
252 * or disabled. Note that this points to static storage that will be
253 * overwritten by further calls to find_hook and run_hook_*.
254 */
255const char *find_hook(struct repository *r, const char *name);
256
257/**
258 * A boolean version of find_hook()
259 */
260int hook_exists(struct repository *r, const char *hookname);
261
262/**
263 * Takes a `hook_name`, resolves it to a path with find_hook(), and
264 * runs the hook for you with the options specified in "struct
265 * run_hooks opt". Will free memory associated with the "struct run_hooks_opt".
266 *
267 * Returns the status code of the run hook, or a negative value on
268 * error().
269 */
270int run_hooks_opt(struct repository *r, const char *hook_name,
271 struct run_hooks_opt *options);
272
273/**
274 * A wrapper for run_hooks_opt() which provides a dummy "struct
275 * run_hooks_opt" initialized with "RUN_HOOKS_OPT_INIT".
276 */
277int run_hooks(struct repository *r, const char *hook_name);
278
279/**
280 * Like run_hooks(), a wrapper for run_hooks_opt().
281 *
282 * In addition to the wrapping behavior provided by run_hooks(), this
283 * wrapper takes a list of strings terminated by a NULL
284 * argument. These things will be used as positional arguments to the
285 * hook. This function behaves like the old run_hook_le() API.
286 */
287LAST_ARG_MUST_BE_NULL
288int run_hooks_l(struct repository *r, const char *hook_name, ...);
289#endif