Sync with 2.39.4
[git.git] / hook.c
blob655e2fe44eca57e01a3dadfe20b8322603f175ce
1 #include "cache.h"
2 #include "hook.h"
3 #include "run-command.h"
4 #include "config.h"
6 static int identical_to_template_hook(const char *name, const char *path)
8 const char *env = getenv("GIT_CLONE_TEMPLATE_DIR");
9 const char *template_dir = get_template_dir(env && *env ? env : NULL);
10 struct strbuf template_path = STRBUF_INIT;
11 int found_template_hook, ret;
13 strbuf_addf(&template_path, "%s/hooks/%s", template_dir, name);
14 found_template_hook = access(template_path.buf, X_OK) >= 0;
15 #ifdef STRIP_EXTENSION
16 if (!found_template_hook) {
17 strbuf_addstr(&template_path, STRIP_EXTENSION);
18 found_template_hook = access(template_path.buf, X_OK) >= 0;
20 #endif
21 if (!found_template_hook)
22 return 0;
24 ret = do_files_match(template_path.buf, path);
26 strbuf_release(&template_path);
27 return ret;
30 const char *find_hook(const char *name)
32 static struct strbuf path = STRBUF_INIT;
34 int found_hook;
36 strbuf_reset(&path);
37 strbuf_git_path(&path, "hooks/%s", name);
38 found_hook = access(path.buf, X_OK) >= 0;
39 #ifdef STRIP_EXTENSION
40 if (!found_hook) {
41 int err = errno;
43 strbuf_addstr(&path, STRIP_EXTENSION);
44 found_hook = access(path.buf, X_OK) >= 0;
45 if (!found_hook)
46 errno = err;
48 #endif
50 if (!found_hook) {
51 if (errno == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
52 static struct string_list advise_given = STRING_LIST_INIT_DUP;
54 if (!string_list_lookup(&advise_given, name)) {
55 string_list_insert(&advise_given, name);
56 advise(_("The '%s' hook was ignored because "
57 "it's not set as executable.\n"
58 "You can disable this warning with "
59 "`git config advice.ignoredHook false`."),
60 path.buf);
63 return NULL;
65 if (!git_hooks_path && git_env_bool("GIT_CLONE_PROTECTION_ACTIVE", 0) &&
66 !identical_to_template_hook(name, path.buf))
67 die(_("active `%s` hook found during `git clone`:\n\t%s\n"
68 "For security reasons, this is disallowed by default.\n"
69 "If this is intentional and the hook should actually "
70 "be run, please\nrun the command again with "
71 "`GIT_CLONE_PROTECTION_ACTIVE=false`"),
72 name, path.buf);
73 return path.buf;
76 int hook_exists(const char *name)
78 return !!find_hook(name);
81 static int pick_next_hook(struct child_process *cp,
82 struct strbuf *out,
83 void *pp_cb,
84 void **pp_task_cb)
86 struct hook_cb_data *hook_cb = pp_cb;
87 const char *hook_path = hook_cb->hook_path;
89 if (!hook_path)
90 return 0;
92 cp->no_stdin = 1;
93 strvec_pushv(&cp->env, hook_cb->options->env.v);
94 /* reopen the file for stdin; run_command closes it. */
95 if (hook_cb->options->path_to_stdin) {
96 cp->no_stdin = 0;
97 cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY);
99 cp->stdout_to_stderr = 1;
100 cp->trace2_hook_name = hook_cb->hook_name;
101 cp->dir = hook_cb->options->dir;
103 strvec_push(&cp->args, hook_path);
104 strvec_pushv(&cp->args, hook_cb->options->args.v);
107 * This pick_next_hook() will be called again, we're only
108 * running one hook, so indicate that no more work will be
109 * done.
111 hook_cb->hook_path = NULL;
113 return 1;
116 static int notify_start_failure(struct strbuf *out,
117 void *pp_cb,
118 void *pp_task_cp)
120 struct hook_cb_data *hook_cb = pp_cb;
122 hook_cb->rc |= 1;
124 return 1;
127 static int notify_hook_finished(int result,
128 struct strbuf *out,
129 void *pp_cb,
130 void *pp_task_cb)
132 struct hook_cb_data *hook_cb = pp_cb;
133 struct run_hooks_opt *opt = hook_cb->options;
135 hook_cb->rc |= result;
137 if (opt->invoked_hook)
138 *opt->invoked_hook = 1;
140 return 0;
143 static void run_hooks_opt_clear(struct run_hooks_opt *options)
145 strvec_clear(&options->env);
146 strvec_clear(&options->args);
149 int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
151 struct strbuf abs_path = STRBUF_INIT;
152 struct hook_cb_data cb_data = {
153 .rc = 0,
154 .hook_name = hook_name,
155 .options = options,
157 const char *const hook_path = find_hook(hook_name);
158 int ret = 0;
159 const struct run_process_parallel_opts opts = {
160 .tr2_category = "hook",
161 .tr2_label = hook_name,
163 .processes = 1,
164 .ungroup = 1,
166 .get_next_task = pick_next_hook,
167 .start_failure = notify_start_failure,
168 .task_finished = notify_hook_finished,
170 .data = &cb_data,
173 if (!options)
174 BUG("a struct run_hooks_opt must be provided to run_hooks");
176 if (options->invoked_hook)
177 *options->invoked_hook = 0;
179 if (!hook_path && !options->error_if_missing)
180 goto cleanup;
182 if (!hook_path) {
183 ret = error("cannot find a hook named %s", hook_name);
184 goto cleanup;
187 cb_data.hook_path = hook_path;
188 if (options->dir) {
189 strbuf_add_absolute_path(&abs_path, hook_path);
190 cb_data.hook_path = abs_path.buf;
193 run_processes_parallel(&opts);
194 ret = cb_data.rc;
195 cleanup:
196 strbuf_release(&abs_path);
197 run_hooks_opt_clear(options);
198 return ret;
201 int run_hooks(const char *hook_name)
203 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
205 return run_hooks_opt(hook_name, &opt);
208 int run_hooks_l(const char *hook_name, ...)
210 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
211 va_list ap;
212 const char *arg;
214 va_start(ap, hook_name);
215 while ((arg = va_arg(ap, const char *)))
216 strvec_push(&opt.args, arg);
217 va_end(ap);
219 return run_hooks_opt(hook_name, &opt);