3 #include "run-command.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;
21 if (!found_template_hook
)
24 ret
= do_files_match(template_path
.buf
, path
);
26 strbuf_release(&template_path
);
30 const char *find_hook(const char *name
)
32 static struct strbuf path
= STRBUF_INIT
;
37 strbuf_git_path(&path
, "hooks/%s", name
);
38 found_hook
= access(path
.buf
, X_OK
) >= 0;
39 #ifdef STRIP_EXTENSION
43 strbuf_addstr(&path
, STRIP_EXTENSION
);
44 found_hook
= access(path
.buf
, X_OK
) >= 0;
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`."),
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`"),
76 int hook_exists(const char *name
)
78 return !!find_hook(name
);
81 static int pick_next_hook(struct child_process
*cp
,
86 struct hook_cb_data
*hook_cb
= pp_cb
;
87 const char *hook_path
= hook_cb
->hook_path
;
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
) {
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
111 hook_cb
->hook_path
= NULL
;
116 static int notify_start_failure(struct strbuf
*out
,
120 struct hook_cb_data
*hook_cb
= pp_cb
;
127 static int notify_hook_finished(int result
,
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;
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
= {
154 .hook_name
= hook_name
,
157 const char *const hook_path
= find_hook(hook_name
);
159 const struct run_process_parallel_opts opts
= {
160 .tr2_category
= "hook",
161 .tr2_label
= hook_name
,
166 .get_next_task
= pick_next_hook
,
167 .start_failure
= notify_start_failure
,
168 .task_finished
= notify_hook_finished
,
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
)
183 ret
= error("cannot find a hook named %s", hook_name
);
187 cb_data
.hook_path
= hook_path
;
189 strbuf_add_absolute_path(&abs_path
, hook_path
);
190 cb_data
.hook_path
= abs_path
.buf
;
193 run_processes_parallel(&opts
);
196 strbuf_release(&abs_path
);
197 run_hooks_opt_clear(options
);
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
;
214 va_start(ap
, hook_name
);
215 while ((arg
= va_arg(ap
, const char *)))
216 strvec_push(&opt
.args
, arg
);
219 return run_hooks_opt(hook_name
, &opt
);