1 #include "git-compat-util.h"
7 #include "run-command.h"
10 #include "environment.h"
13 const char *find_hook(const char *name
)
15 static struct strbuf path
= STRBUF_INIT
;
20 strbuf_git_path(&path
, "hooks/%s", name
);
21 found_hook
= access(path
.buf
, X_OK
) >= 0;
22 #ifdef STRIP_EXTENSION
26 strbuf_addstr(&path
, STRIP_EXTENSION
);
27 found_hook
= access(path
.buf
, X_OK
) >= 0;
34 if (errno
== EACCES
&& advice_enabled(ADVICE_IGNORED_HOOK
)) {
35 static struct string_list advise_given
= STRING_LIST_INIT_DUP
;
37 if (!string_list_lookup(&advise_given
, name
)) {
38 string_list_insert(&advise_given
, name
);
39 advise(_("The '%s' hook was ignored because "
40 "it's not set as executable.\n"
41 "You can disable this warning with "
42 "`git config advice.ignoredHook false`."),
51 int hook_exists(const char *name
)
53 return !!find_hook(name
);
56 static int pick_next_hook(struct child_process
*cp
,
57 struct strbuf
*out UNUSED
,
59 void **pp_task_cb UNUSED
)
61 struct hook_cb_data
*hook_cb
= pp_cb
;
62 const char *hook_path
= hook_cb
->hook_path
;
68 strvec_pushv(&cp
->env
, hook_cb
->options
->env
.v
);
69 /* reopen the file for stdin; run_command closes it. */
70 if (hook_cb
->options
->path_to_stdin
) {
72 cp
->in
= xopen(hook_cb
->options
->path_to_stdin
, O_RDONLY
);
74 cp
->stdout_to_stderr
= 1;
75 cp
->trace2_hook_name
= hook_cb
->hook_name
;
76 cp
->dir
= hook_cb
->options
->dir
;
78 strvec_push(&cp
->args
, hook_path
);
79 strvec_pushv(&cp
->args
, hook_cb
->options
->args
.v
);
82 * This pick_next_hook() will be called again, we're only
83 * running one hook, so indicate that no more work will be
86 hook_cb
->hook_path
= NULL
;
91 static int notify_start_failure(struct strbuf
*out UNUSED
,
93 void *pp_task_cp UNUSED
)
95 struct hook_cb_data
*hook_cb
= pp_cb
;
102 static int notify_hook_finished(int result
,
103 struct strbuf
*out UNUSED
,
105 void *pp_task_cb UNUSED
)
107 struct hook_cb_data
*hook_cb
= pp_cb
;
108 struct run_hooks_opt
*opt
= hook_cb
->options
;
110 hook_cb
->rc
|= result
;
112 if (opt
->invoked_hook
)
113 *opt
->invoked_hook
= 1;
118 static void run_hooks_opt_clear(struct run_hooks_opt
*options
)
120 strvec_clear(&options
->env
);
121 strvec_clear(&options
->args
);
124 int run_hooks_opt(const char *hook_name
, struct run_hooks_opt
*options
)
126 struct strbuf abs_path
= STRBUF_INIT
;
127 struct hook_cb_data cb_data
= {
129 .hook_name
= hook_name
,
132 const char *const hook_path
= find_hook(hook_name
);
134 const struct run_process_parallel_opts opts
= {
135 .tr2_category
= "hook",
136 .tr2_label
= hook_name
,
141 .get_next_task
= pick_next_hook
,
142 .start_failure
= notify_start_failure
,
143 .task_finished
= notify_hook_finished
,
149 BUG("a struct run_hooks_opt must be provided to run_hooks");
151 if (options
->invoked_hook
)
152 *options
->invoked_hook
= 0;
154 if (!hook_path
&& !options
->error_if_missing
)
158 ret
= error("cannot find a hook named %s", hook_name
);
162 cb_data
.hook_path
= hook_path
;
164 strbuf_add_absolute_path(&abs_path
, hook_path
);
165 cb_data
.hook_path
= abs_path
.buf
;
168 run_processes_parallel(&opts
);
171 strbuf_release(&abs_path
);
172 run_hooks_opt_clear(options
);
176 int run_hooks(const char *hook_name
)
178 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
180 return run_hooks_opt(hook_name
, &opt
);
183 int run_hooks_l(const char *hook_name
, ...)
185 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
189 va_start(ap
, hook_name
);
190 while ((arg
= va_arg(ap
, const char *)))
191 strvec_push(&opt
.args
, arg
);
194 return run_hooks_opt(hook_name
, &opt
);