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 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
106 hook_cb
->hook_path
= NULL
;
111 static int notify_start_failure(struct strbuf
*out
,
115 struct hook_cb_data
*hook_cb
= pp_cb
;
122 static int notify_hook_finished(int result
,
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;
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
= {
149 .hook_name
= hook_name
,
152 const char *const hook_path
= find_hook(hook_name
);
154 const struct run_process_parallel_opts opts
= {
155 .tr2_category
= "hook",
156 .tr2_label
= hook_name
,
161 .get_next_task
= pick_next_hook
,
162 .start_failure
= notify_start_failure
,
163 .task_finished
= notify_hook_finished
,
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
)
178 ret
= error("cannot find a hook named %s", hook_name
);
182 cb_data
.hook_path
= hook_path
;
184 strbuf_add_absolute_path(&abs_path
, hook_path
);
185 cb_data
.hook_path
= abs_path
.buf
;
188 run_processes_parallel(&opts
);
191 strbuf_release(&abs_path
);
192 run_hooks_opt_clear(options
);
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
;
209 va_start(ap
, hook_name
);
210 while ((arg
= va_arg(ap
, const char *)))
211 strvec_push(&opt
.args
, arg
);
214 return run_hooks_opt(hook_name
, &opt
);