Git fork
at reftables-rust 94 lines 2.6 kB view raw
1#ifndef HOOK_H 2#define HOOK_H 3#include "strvec.h" 4 5struct repository; 6 7struct run_hooks_opt 8{ 9 /* Environment vars to be set for each hook */ 10 struct strvec env; 11 12 /* Args to be passed to each hook */ 13 struct strvec args; 14 15 /* Emit an error if the hook is missing */ 16 unsigned int error_if_missing:1; 17 18 /** 19 * An optional initial working directory for the hook, 20 * translates to "struct child_process"'s "dir" member. 21 */ 22 const char *dir; 23 24 /** 25 * A pointer which if provided will be set to 1 or 0 depending 26 * on if a hook was started, regardless of whether or not that 27 * was successful. I.e. if the underlying start_command() was 28 * successful this will be set to 1. 29 * 30 * Used for avoiding TOCTOU races in code that would otherwise 31 * call hook_exist() after a "maybe hook run" to see if a hook 32 * was invoked. 33 */ 34 int *invoked_hook; 35 36 /** 37 * Path to file which should be piped to stdin for each hook. 38 */ 39 const char *path_to_stdin; 40}; 41 42#define RUN_HOOKS_OPT_INIT { \ 43 .env = STRVEC_INIT, \ 44 .args = STRVEC_INIT, \ 45} 46 47struct hook_cb_data { 48 /* rc reflects the cumulative failure state */ 49 int rc; 50 const char *hook_name; 51 const char *hook_path; 52 struct run_hooks_opt *options; 53}; 54 55/* 56 * Returns the path to the hook file, or NULL if the hook is missing 57 * or disabled. Note that this points to static storage that will be 58 * overwritten by further calls to find_hook and run_hook_*. 59 */ 60const char *find_hook(struct repository *r, const char *name); 61 62/** 63 * A boolean version of find_hook() 64 */ 65int hook_exists(struct repository *r, const char *hookname); 66 67/** 68 * Takes a `hook_name`, resolves it to a path with find_hook(), and 69 * runs the hook for you with the options specified in "struct 70 * run_hooks opt". Will free memory associated with the "struct run_hooks_opt". 71 * 72 * Returns the status code of the run hook, or a negative value on 73 * error(). 74 */ 75int run_hooks_opt(struct repository *r, const char *hook_name, 76 struct run_hooks_opt *options); 77 78/** 79 * A wrapper for run_hooks_opt() which provides a dummy "struct 80 * run_hooks_opt" initialized with "RUN_HOOKS_OPT_INIT". 81 */ 82int run_hooks(struct repository *r, const char *hook_name); 83 84/** 85 * Like run_hooks(), a wrapper for run_hooks_opt(). 86 * 87 * In addition to the wrapping behavior provided by run_hooks(), this 88 * wrapper takes a list of strings terminated by a NULL 89 * argument. These things will be used as positional arguments to the 90 * hook. This function behaves like the old run_hook_le() API. 91 */ 92LAST_ARG_MUST_BE_NULL 93int run_hooks_l(struct repository *r, const char *hook_name, ...); 94#endif