]> git.ipfire.org Git - thirdparty/git.git/blame - hook.c
Merge branch 'tl/ls-tree-oid-only'
[thirdparty/git.git] / hook.c
CommitLineData
5e3aba33
ÆAB
1#include "cache.h"
2#include "hook.h"
3#include "run-command.h"
96e7225b 4#include "config.h"
5e3aba33
ÆAB
5
6const char *find_hook(const char *name)
7{
8 static struct strbuf path = STRBUF_INIT;
9
10 strbuf_reset(&path);
11 strbuf_git_path(&path, "hooks/%s", name);
12 if (access(path.buf, X_OK) < 0) {
13 int err = errno;
14
15#ifdef STRIP_EXTENSION
16 strbuf_addstr(&path, STRIP_EXTENSION);
17 if (access(path.buf, X_OK) >= 0)
18 return path.buf;
19 if (errno == EACCES)
20 err = errno;
21#endif
22
23 if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
24 static struct string_list advise_given = STRING_LIST_INIT_DUP;
25
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`."),
32 path.buf);
33 }
34 }
35 return NULL;
36 }
37 return path.buf;
38}
330155ed
ES
39
40int hook_exists(const char *name)
41{
42 return !!find_hook(name);
43}
96e7225b
ES
44
45static int pick_next_hook(struct child_process *cp,
46 struct strbuf *out,
47 void *pp_cb,
48 void **pp_task_cb)
49{
50 struct hook_cb_data *hook_cb = pp_cb;
51 const char *hook_path = hook_cb->hook_path;
52
53 if (!hook_path)
54 return 0;
55
56 cp->no_stdin = 1;
57 strvec_pushv(&cp->env_array, hook_cb->options->env.v);
58 cp->stdout_to_stderr = 1;
59 cp->trace2_hook_name = hook_cb->hook_name;
1a3017d9 60 cp->dir = hook_cb->options->dir;
96e7225b
ES
61
62 strvec_push(&cp->args, hook_path);
63 strvec_pushv(&cp->args, hook_cb->options->args.v);
64
65 /* Provide context for errors if necessary */
66 *pp_task_cb = (char *)hook_path;
67
68 /*
69 * This pick_next_hook() will be called again, we're only
70 * running one hook, so indicate that no more work will be
71 * done.
72 */
73 hook_cb->hook_path = NULL;
74
75 return 1;
76}
77
78static int notify_start_failure(struct strbuf *out,
79 void *pp_cb,
80 void *pp_task_cp)
81{
82 struct hook_cb_data *hook_cb = pp_cb;
83 const char *hook_path = pp_task_cp;
84
85 hook_cb->rc |= 1;
86
87 strbuf_addf(out, _("Couldn't start hook '%s'\n"),
88 hook_path);
89
90 return 1;
91}
92
93static int notify_hook_finished(int result,
94 struct strbuf *out,
95 void *pp_cb,
96 void *pp_task_cb)
97{
98 struct hook_cb_data *hook_cb = pp_cb;
a8cc5943 99 struct run_hooks_opt *opt = hook_cb->options;
96e7225b
ES
100
101 hook_cb->rc |= result;
102
a8cc5943
ÆAB
103 if (opt->invoked_hook)
104 *opt->invoked_hook = 1;
105
96e7225b
ES
106 return 0;
107}
108
109static void run_hooks_opt_clear(struct run_hooks_opt *options)
110{
111 strvec_clear(&options->env);
112 strvec_clear(&options->args);
113}
114
115int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
116{
1a3017d9 117 struct strbuf abs_path = STRBUF_INIT;
96e7225b
ES
118 struct hook_cb_data cb_data = {
119 .rc = 0,
120 .hook_name = hook_name,
121 .options = options,
122 };
123 const char *const hook_path = find_hook(hook_name);
124 int jobs = 1;
125 int ret = 0;
126
127 if (!options)
128 BUG("a struct run_hooks_opt must be provided to run_hooks");
129
a8cc5943
ÆAB
130 if (options->invoked_hook)
131 *options->invoked_hook = 0;
132
96e7225b
ES
133 if (!hook_path && !options->error_if_missing)
134 goto cleanup;
135
136 if (!hook_path) {
137 ret = error("cannot find a hook named %s", hook_name);
138 goto cleanup;
139 }
140
141 cb_data.hook_path = hook_path;
1a3017d9
ES
142 if (options->dir) {
143 strbuf_add_absolute_path(&abs_path, hook_path);
144 cb_data.hook_path = abs_path.buf;
145 }
146
96e7225b
ES
147 run_processes_parallel_tr2(jobs,
148 pick_next_hook,
149 notify_start_failure,
150 notify_hook_finished,
151 &cb_data,
152 "hook",
153 hook_name);
154 ret = cb_data.rc;
155cleanup:
1a3017d9 156 strbuf_release(&abs_path);
96e7225b
ES
157 run_hooks_opt_clear(options);
158 return ret;
159}
474c119f
ÆAB
160
161int run_hooks(const char *hook_name)
162{
163 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
164
165 return run_hooks_opt(hook_name, &opt);
166}
ab81cf24
ÆAB
167
168int run_hooks_l(const char *hook_name, ...)
169{
170 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
171 va_list ap;
172 const char *arg;
173
174 va_start(ap, hook_name);
175 while ((arg = va_arg(ap, const char *)))
176 strvec_push(&opt.args, arg);
177 va_end(ap);
178
179 return run_hooks_opt(hook_name, &opt);
180}