3 #include "run-command.h"
6 const char *find_hook(const char *name
)
8 static struct strbuf path
= STRBUF_INIT
;
11 strbuf_git_path(&path
, "hooks/%s", name
);
12 if (access(path
.buf
, X_OK
) < 0) {
15 #ifdef STRIP_EXTENSION
16 strbuf_addstr(&path
, STRIP_EXTENSION
);
17 if (access(path
.buf
, X_OK
) >= 0)
23 if (err
== EACCES
&& advice_enabled(ADVICE_IGNORED_HOOK
)) {
24 static struct string_list advise_given
= STRING_LIST_INIT_DUP
;
26 if (!string_list_lookup(&advise_given
, name
)) {
27 string_list_insert(&advise_given
, name
);
28 advise(_("The '%s' hook was ignored because "
29 "it's not set as executable.\n"
30 "You can disable this warning with "
31 "`git config advice.ignoredHook false`."),
40 int hook_exists(const char *name
)
42 return !!find_hook(name
);
45 static int pick_next_hook(struct child_process
*cp
,
50 struct hook_cb_data
*hook_cb
= pp_cb
;
51 const char *hook_path
= hook_cb
->hook_path
;
57 strvec_pushv(&cp
->env
, hook_cb
->options
->env
.v
);
58 cp
->stdout_to_stderr
= 1;
59 cp
->trace2_hook_name
= hook_cb
->hook_name
;
60 cp
->dir
= hook_cb
->options
->dir
;
62 strvec_push(&cp
->args
, hook_path
);
63 strvec_pushv(&cp
->args
, hook_cb
->options
->args
.v
);
65 /* Provide context for errors if necessary */
66 *pp_task_cb
= (char *)hook_path
;
69 * This pick_next_hook() will be called again, we're only
70 * running one hook, so indicate that no more work will be
73 hook_cb
->hook_path
= NULL
;
78 static int notify_start_failure(struct strbuf
*out
,
82 struct hook_cb_data
*hook_cb
= pp_cb
;
83 const char *hook_path
= pp_task_cp
;
87 strbuf_addf(out
, _("Couldn't start hook '%s'\n"),
93 static int notify_hook_finished(int result
,
98 struct hook_cb_data
*hook_cb
= pp_cb
;
99 struct run_hooks_opt
*opt
= hook_cb
->options
;
101 hook_cb
->rc
|= result
;
103 if (opt
->invoked_hook
)
104 *opt
->invoked_hook
= 1;
109 static void run_hooks_opt_clear(struct run_hooks_opt
*options
)
111 strvec_clear(&options
->env
);
112 strvec_clear(&options
->args
);
115 int run_hooks_opt(const char *hook_name
, struct run_hooks_opt
*options
)
117 struct strbuf abs_path
= STRBUF_INIT
;
118 struct hook_cb_data cb_data
= {
120 .hook_name
= hook_name
,
123 const char *const hook_path
= find_hook(hook_name
);
128 BUG("a struct run_hooks_opt must be provided to run_hooks");
130 if (options
->invoked_hook
)
131 *options
->invoked_hook
= 0;
133 if (!hook_path
&& !options
->error_if_missing
)
137 ret
= error("cannot find a hook named %s", hook_name
);
141 cb_data
.hook_path
= hook_path
;
143 strbuf_add_absolute_path(&abs_path
, hook_path
);
144 cb_data
.hook_path
= abs_path
.buf
;
147 run_processes_parallel_ungroup
= 1;
148 run_processes_parallel_tr2(jobs
,
150 notify_start_failure
,
151 notify_hook_finished
,
157 strbuf_release(&abs_path
);
158 run_hooks_opt_clear(options
);
162 int run_hooks(const char *hook_name
)
164 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
166 return run_hooks_opt(hook_name
, &opt
);
169 int run_hooks_l(const char *hook_name
, ...)
171 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
175 va_start(ap
, hook_name
);
176 while ((arg
= va_arg(ap
, const char *)))
177 strvec_push(&opt
.args
, arg
);
180 return run_hooks_opt(hook_name
, &opt
);