Git 2.39.4
[git.git] / hook.c
blob632b537b9933ad4f4011a3b3b316a1de5b99de21
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 cp->stdout_to_stderr = 1;
95 cp->trace2_hook_name = hook_cb->hook_name;
96 cp->dir = hook_cb->options->dir;
98 strvec_push(&cp->args, hook_path);
99 strvec_pushv(&cp->args, hook_cb->options->args.v);
102 * This pick_next_hook() will be called again, we're only
103 * running one hook, so indicate that no more work will be
104 * done.
106 hook_cb->hook_path = NULL;
108 return 1;
111 static int notify_start_failure(struct strbuf *out,
112 void *pp_cb,
113 void *pp_task_cp)
115 struct hook_cb_data *hook_cb = pp_cb;
117 hook_cb->rc |= 1;
119 return 1;
122 static int notify_hook_finished(int result,
123 struct strbuf *out,
124 void *pp_cb,
125 void *pp_task_cb)
127 struct hook_cb_data *hook_cb = pp_cb;
128 struct run_hooks_opt *opt = hook_cb->options;
130 hook_cb->rc |= result;
132 if (opt->invoked_hook)
133 *opt->invoked_hook = 1;
135 return 0;
138 static void run_hooks_opt_clear(struct run_hooks_opt *options)
140 strvec_clear(&options->env);
141 strvec_clear(&options->args);
144 int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
146 struct strbuf abs_path = STRBUF_INIT;
147 struct hook_cb_data cb_data = {
148 .rc = 0,
149 .hook_name = hook_name,
150 .options = options,
152 const char *const hook_path = find_hook(hook_name);
153 int ret = 0;
154 const struct run_process_parallel_opts opts = {
155 .tr2_category = "hook",
156 .tr2_label = hook_name,
158 .processes = 1,
159 .ungroup = 1,
161 .get_next_task = pick_next_hook,
162 .start_failure = notify_start_failure,
163 .task_finished = notify_hook_finished,
165 .data = &cb_data,
168 if (!options)
169 BUG("a struct run_hooks_opt must be provided to run_hooks");
171 if (options->invoked_hook)
172 *options->invoked_hook = 0;
174 if (!hook_path && !options->error_if_missing)
175 goto cleanup;
177 if (!hook_path) {
178 ret = error("cannot find a hook named %s", hook_name);
179 goto cleanup;
182 cb_data.hook_path = hook_path;
183 if (options->dir) {
184 strbuf_add_absolute_path(&abs_path, hook_path);
185 cb_data.hook_path = abs_path.buf;
188 run_processes_parallel(&opts);
189 ret = cb_data.rc;
190 cleanup:
191 strbuf_release(&abs_path);
192 run_hooks_opt_clear(options);
193 return ret;
196 int run_hooks(const char *hook_name)
198 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
200 return run_hooks_opt(hook_name, &opt);
203 int run_hooks_l(const char *hook_name, ...)
205 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
206 va_list ap;
207 const char *arg;
209 va_start(ap, hook_name);
210 while ((arg = va_arg(ap, const char *)))
211 strvec_push(&opt.args, arg);
212 va_end(ap);
214 return run_hooks_opt(hook_name, &opt);