Merge branch 'ab/config-based-hooks-base' into seen
[git] / hook.h
1 #ifndef HOOK_H
2 #define HOOK_H
3 #include "strbuf.h"
4 #include "strvec.h"
5 #include "run-command.h"
6
7 struct hook {
8         /* The path to the hook */
9         const char *hook_path;
10
11         /*
12          * Use this to keep state for your feed_pipe_fn if you are using
13          * run_hooks_opt.feed_pipe. Otherwise, do not touch it.
14          */
15         void *feed_pipe_cb_data;
16 };
17
18 struct run_hooks_opt
19 {
20         /* Environment vars to be set for each hook */
21         struct strvec env;
22
23         /* Args to be passed to each hook */
24         struct strvec args;
25
26         /* Number of threads to parallelize across */
27         int jobs;
28
29         /* Resolve and run the "absolute_path(hook)" instead of
30          * "hook". Used for "git worktree" hooks
31          */
32         int absolute_path;
33
34         /* Path to initial working directory for subprocess */
35         const char *dir;
36
37         /* Path to file which should be piped to stdin for each hook */
38         const char *path_to_stdin;
39
40         /*
41          * Callback and state pointer to ask for more content to pipe to stdin.
42          * Will be called repeatedly, for each hook. See
43          * hook.c:pipe_from_stdin() for an example. Keep per-hook state in
44          * hook.feed_pipe_cb_data (per process). Keep initialization context in
45          * feed_pipe_ctx (shared by all processes).
46          *
47          * See 'pipe_from_string_list()' for info about how to specify a
48          * string_list as the stdin input instead of writing your own handler.
49          */
50         feed_pipe_fn feed_pipe;
51         void *feed_pipe_ctx;
52
53         /*
54          * Populate this to capture output and prevent it from being printed to
55          * stderr. This will be passed directly through to
56          * run_command:run_parallel_processes(). See t/helper/test-run-command.c
57          * for an example.
58          */
59         consume_sideband_fn consume_sideband;
60
61         /*
62          * A pointer which if provided will be set to 1 or 0 depending
63          * on if a hook was invoked (i.e. existed), regardless of
64          * whether or not that was successful. Used for avoiding
65          * TOCTOU races in code that would otherwise call hook_exist()
66          * after a "maybe hook run" to see if a hook was invoked.
67          */
68         int *invoked_hook;
69 };
70
71 #define RUN_HOOKS_OPT_INIT { \
72         .jobs = 1, \
73         .env = STRVEC_INIT, \
74         .args = STRVEC_INIT, \
75 }
76
77 /*
78  * To specify a 'struct string_list', set 'run_hooks_opt.feed_pipe_ctx' to the
79  * string_list and set 'run_hooks_opt.feed_pipe' to 'pipe_from_string_list()'.
80  * This will pipe each string in the list to stdin, separated by newlines.  (Do
81  * not inject your own newlines.)
82  */
83 int pipe_from_string_list(struct strbuf *pipe, void *pp_cb, void *pp_task_cb);
84
85 /*
86  * Callback provided to feed_pipe_fn and consume_sideband_fn.
87  */
88 struct hook_cb_data {
89         int rc;
90         const char *hook_name;
91         struct hook *run_me;
92         struct run_hooks_opt *options;
93         int *invoked_hook;
94 };
95
96 /*
97  * Returns the path to the hook file, or NULL if the hook is missing
98  * or disabled. Note that this points to static storage that will be
99  * overwritten by further calls to find_hook and run_hook_*.
100  */
101 const char *find_hook(const char *name);
102
103 /*
104  * A boolean version of find_hook()
105  */
106 int hook_exists(const char *hookname);
107
108 void run_hooks_opt_clear(struct run_hooks_opt *o);
109
110 /*
111  * Calls find_hook(hookname) and runs the hooks (if any) with
112  * run_found_hooks().
113  */
114 int run_hooks(const char *hook_name, struct run_hooks_opt *options);
115
116 /*
117  * Takes an already resolved hook and runs it. Internally the simpler
118  * run_hooks() will call this.
119  */
120 int run_found_hooks(const char *hookname, const char *hook_path,
121                     struct run_hooks_opt *options);
122 #endif