1 #include "git-compat-util.h"
6 #include "run-command.h"
9 #include "environment.h"
12 const char *find_hook(const char *name
)
14 static struct strbuf path
= STRBUF_INIT
;
19 strbuf_git_path(&path
, "hooks/%s", name
);
20 found_hook
= access(path
.buf
, X_OK
) >= 0;
21 #ifdef STRIP_EXTENSION
25 strbuf_addstr(&path
, STRIP_EXTENSION
);
26 found_hook
= access(path
.buf
, X_OK
) >= 0;
33 if (errno
== EACCES
&& advice_enabled(ADVICE_IGNORED_HOOK
)) {
34 static struct string_list advise_given
= STRING_LIST_INIT_DUP
;
36 if (!string_list_lookup(&advise_given
, name
)) {
37 string_list_insert(&advise_given
, name
);
38 advise(_("The '%s' hook was ignored because "
39 "it's not set as executable.\n"
40 "You can disable this warning with "
41 "`git config advice.ignoredHook false`."),
50 int hook_exists(const char *name
)
52 return !!find_hook(name
);
55 static int pick_next_hook(struct child_process
*cp
,
56 struct strbuf
*out UNUSED
,
58 void **pp_task_cb UNUSED
)
60 struct hook_cb_data
*hook_cb
= pp_cb
;
61 const char *hook_path
= hook_cb
->hook_path
;
67 strvec_pushv(&cp
->env
, hook_cb
->options
->env
.v
);
68 /* reopen the file for stdin; run_command closes it. */
69 if (hook_cb
->options
->path_to_stdin
) {
71 cp
->in
= xopen(hook_cb
->options
->path_to_stdin
, O_RDONLY
);
73 cp
->stdout_to_stderr
= 1;
74 cp
->trace2_hook_name
= hook_cb
->hook_name
;
75 cp
->dir
= hook_cb
->options
->dir
;
77 strvec_push(&cp
->args
, hook_path
);
78 strvec_pushv(&cp
->args
, hook_cb
->options
->args
.v
);
81 * This pick_next_hook() will be called again, we're only
82 * running one hook, so indicate that no more work will be
85 hook_cb
->hook_path
= NULL
;
90 static int notify_start_failure(struct strbuf
*out UNUSED
,
92 void *pp_task_cp UNUSED
)
94 struct hook_cb_data
*hook_cb
= pp_cb
;
101 static int notify_hook_finished(int result
,
102 struct strbuf
*out UNUSED
,
104 void *pp_task_cb UNUSED
)
106 struct hook_cb_data
*hook_cb
= pp_cb
;
107 struct run_hooks_opt
*opt
= hook_cb
->options
;
109 hook_cb
->rc
|= result
;
111 if (opt
->invoked_hook
)
112 *opt
->invoked_hook
= 1;
117 static void run_hooks_opt_clear(struct run_hooks_opt
*options
)
119 strvec_clear(&options
->env
);
120 strvec_clear(&options
->args
);
123 int run_hooks_opt(const char *hook_name
, struct run_hooks_opt
*options
)
125 struct strbuf abs_path
= STRBUF_INIT
;
126 struct hook_cb_data cb_data
= {
128 .hook_name
= hook_name
,
131 const char *const hook_path
= find_hook(hook_name
);
133 const struct run_process_parallel_opts opts
= {
134 .tr2_category
= "hook",
135 .tr2_label
= hook_name
,
140 .get_next_task
= pick_next_hook
,
141 .start_failure
= notify_start_failure
,
142 .task_finished
= notify_hook_finished
,
148 BUG("a struct run_hooks_opt must be provided to run_hooks");
150 if (options
->invoked_hook
)
151 *options
->invoked_hook
= 0;
153 if (!hook_path
&& !options
->error_if_missing
)
157 ret
= error("cannot find a hook named %s", hook_name
);
161 cb_data
.hook_path
= hook_path
;
163 strbuf_add_absolute_path(&abs_path
, hook_path
);
164 cb_data
.hook_path
= abs_path
.buf
;
167 run_processes_parallel(&opts
);
170 strbuf_release(&abs_path
);
171 run_hooks_opt_clear(options
);
175 int run_hooks(const char *hook_name
)
177 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
179 return run_hooks_opt(hook_name
, &opt
);
182 int run_hooks_l(const char *hook_name
, ...)
184 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
188 va_start(ap
, hook_name
);
189 while ((arg
= va_arg(ap
, const char *)))
190 strvec_push(&opt
.args
, arg
);
193 return run_hooks_opt(hook_name
, &opt
);