Git fork
at reftables-rust 195 lines 4.4 kB view raw
1#include "git-compat-util.h" 2#include "abspath.h" 3#include "advice.h" 4#include "gettext.h" 5#include "hook.h" 6#include "path.h" 7#include "run-command.h" 8#include "config.h" 9#include "strbuf.h" 10#include "environment.h" 11#include "setup.h" 12 13const char *find_hook(struct repository *r, const char *name) 14{ 15 static struct strbuf path = STRBUF_INIT; 16 17 int found_hook; 18 19 repo_git_path_replace(r, &path, "hooks/%s", name); 20 found_hook = access(path.buf, X_OK) >= 0; 21#ifdef STRIP_EXTENSION 22 if (!found_hook) { 23 int err = errno; 24 25 strbuf_addstr(&path, STRIP_EXTENSION); 26 found_hook = access(path.buf, X_OK) >= 0; 27 if (!found_hook) 28 errno = err; 29 } 30#endif 31 32 if (!found_hook) { 33 if (errno == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) { 34 static struct string_list advise_given = STRING_LIST_INIT_DUP; 35 36 if (!string_list_lookup(&advise_given, name)) { 37 string_list_insert(&advise_given, name); 38 advise(_("The '%s' hook was ignored because " 39 "it's not set as executable.\n" 40 "You can disable this warning with " 41 "`git config set advice.ignoredHook false`."), 42 path.buf); 43 } 44 } 45 return NULL; 46 } 47 return path.buf; 48} 49 50int hook_exists(struct repository *r, const char *name) 51{ 52 return !!find_hook(r, name); 53} 54 55static int pick_next_hook(struct child_process *cp, 56 struct strbuf *out UNUSED, 57 void *pp_cb, 58 void **pp_task_cb UNUSED) 59{ 60 struct hook_cb_data *hook_cb = pp_cb; 61 const char *hook_path = hook_cb->hook_path; 62 63 if (!hook_path) 64 return 0; 65 66 cp->no_stdin = 1; 67 strvec_pushv(&cp->env, hook_cb->options->env.v); 68 /* reopen the file for stdin; run_command closes it. */ 69 if (hook_cb->options->path_to_stdin) { 70 cp->no_stdin = 0; 71 cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY); 72 } 73 cp->stdout_to_stderr = 1; 74 cp->trace2_hook_name = hook_cb->hook_name; 75 cp->dir = hook_cb->options->dir; 76 77 strvec_push(&cp->args, hook_path); 78 strvec_pushv(&cp->args, hook_cb->options->args.v); 79 80 /* 81 * This pick_next_hook() will be called again, we're only 82 * running one hook, so indicate that no more work will be 83 * done. 84 */ 85 hook_cb->hook_path = NULL; 86 87 return 1; 88} 89 90static int notify_start_failure(struct strbuf *out UNUSED, 91 void *pp_cb, 92 void *pp_task_cp UNUSED) 93{ 94 struct hook_cb_data *hook_cb = pp_cb; 95 96 hook_cb->rc |= 1; 97 98 return 1; 99} 100 101static int notify_hook_finished(int result, 102 struct strbuf *out UNUSED, 103 void *pp_cb, 104 void *pp_task_cb UNUSED) 105{ 106 struct hook_cb_data *hook_cb = pp_cb; 107 struct run_hooks_opt *opt = hook_cb->options; 108 109 hook_cb->rc |= result; 110 111 if (opt->invoked_hook) 112 *opt->invoked_hook = 1; 113 114 return 0; 115} 116 117static void run_hooks_opt_clear(struct run_hooks_opt *options) 118{ 119 strvec_clear(&options->env); 120 strvec_clear(&options->args); 121} 122 123int run_hooks_opt(struct repository *r, const char *hook_name, 124 struct run_hooks_opt *options) 125{ 126 struct strbuf abs_path = STRBUF_INIT; 127 struct hook_cb_data cb_data = { 128 .rc = 0, 129 .hook_name = hook_name, 130 .options = options, 131 }; 132 const char *const hook_path = find_hook(r, hook_name); 133 int ret = 0; 134 const struct run_process_parallel_opts opts = { 135 .tr2_category = "hook", 136 .tr2_label = hook_name, 137 138 .processes = 1, 139 .ungroup = 1, 140 141 .get_next_task = pick_next_hook, 142 .start_failure = notify_start_failure, 143 .task_finished = notify_hook_finished, 144 145 .data = &cb_data, 146 }; 147 148 if (!options) 149 BUG("a struct run_hooks_opt must be provided to run_hooks"); 150 151 if (options->invoked_hook) 152 *options->invoked_hook = 0; 153 154 if (!hook_path && !options->error_if_missing) 155 goto cleanup; 156 157 if (!hook_path) { 158 ret = error("cannot find a hook named %s", hook_name); 159 goto cleanup; 160 } 161 162 cb_data.hook_path = hook_path; 163 if (options->dir) { 164 strbuf_add_absolute_path(&abs_path, hook_path); 165 cb_data.hook_path = abs_path.buf; 166 } 167 168 run_processes_parallel(&opts); 169 ret = cb_data.rc; 170cleanup: 171 strbuf_release(&abs_path); 172 run_hooks_opt_clear(options); 173 return ret; 174} 175 176int run_hooks(struct repository *r, const char *hook_name) 177{ 178 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; 179 180 return run_hooks_opt(r, hook_name, &opt); 181} 182 183int run_hooks_l(struct repository *r, const char *hook_name, ...) 184{ 185 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; 186 va_list ap; 187 const char *arg; 188 189 va_start(ap, hook_name); 190 while ((arg = va_arg(ap, const char *))) 191 strvec_push(&opt.args, arg); 192 va_end(ap); 193 194 return run_hooks_opt(r, hook_name, &opt); 195}