]> git.ipfire.org Git - thirdparty/git.git/blame - hook.c
Merge branch 'ab/fewer-the-index-macros'
[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;
29fda24d 57 strvec_pushv(&cp->env, hook_cb->options->env.v);
96e7225b
ES
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
96e7225b
ES
65 /*
66 * This pick_next_hook() will be called again, we're only
67 * running one hook, so indicate that no more work will be
68 * done.
69 */
70 hook_cb->hook_path = NULL;
71
72 return 1;
73}
74
75static int notify_start_failure(struct strbuf *out,
76 void *pp_cb,
77 void *pp_task_cp)
78{
79 struct hook_cb_data *hook_cb = pp_cb;
96e7225b
ES
80
81 hook_cb->rc |= 1;
82
96e7225b
ES
83 return 1;
84}
85
86static int notify_hook_finished(int result,
87 struct strbuf *out,
88 void *pp_cb,
89 void *pp_task_cb)
90{
91 struct hook_cb_data *hook_cb = pp_cb;
a8cc5943 92 struct run_hooks_opt *opt = hook_cb->options;
96e7225b
ES
93
94 hook_cb->rc |= result;
95
a8cc5943
ÆAB
96 if (opt->invoked_hook)
97 *opt->invoked_hook = 1;
98
96e7225b
ES
99 return 0;
100}
101
102static void run_hooks_opt_clear(struct run_hooks_opt *options)
103{
104 strvec_clear(&options->env);
105 strvec_clear(&options->args);
106}
107
108int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
109{
1a3017d9 110 struct strbuf abs_path = STRBUF_INIT;
96e7225b
ES
111 struct hook_cb_data cb_data = {
112 .rc = 0,
113 .hook_name = hook_name,
114 .options = options,
115 };
116 const char *const hook_path = find_hook(hook_name);
96e7225b 117 int ret = 0;
6e5ba0ba
ÆAB
118 const struct run_process_parallel_opts opts = {
119 .tr2_category = "hook",
120 .tr2_label = hook_name,
121
122 .processes = 1,
123 .ungroup = 1,
124
125 .get_next_task = pick_next_hook,
126 .start_failure = notify_start_failure,
127 .task_finished = notify_hook_finished,
128
129 .data = &cb_data,
130 };
96e7225b
ES
131
132 if (!options)
133 BUG("a struct run_hooks_opt must be provided to run_hooks");
134
a8cc5943
ÆAB
135 if (options->invoked_hook)
136 *options->invoked_hook = 0;
137
96e7225b
ES
138 if (!hook_path && !options->error_if_missing)
139 goto cleanup;
140
141 if (!hook_path) {
142 ret = error("cannot find a hook named %s", hook_name);
143 goto cleanup;
144 }
145
146 cb_data.hook_path = hook_path;
1a3017d9
ES
147 if (options->dir) {
148 strbuf_add_absolute_path(&abs_path, hook_path);
149 cb_data.hook_path = abs_path.buf;
150 }
151
6e5ba0ba 152 run_processes_parallel(&opts);
96e7225b
ES
153 ret = cb_data.rc;
154cleanup:
1a3017d9 155 strbuf_release(&abs_path);
96e7225b
ES
156 run_hooks_opt_clear(options);
157 return ret;
158}
474c119f
ÆAB
159
160int run_hooks(const char *hook_name)
161{
162 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
163
164 return run_hooks_opt(hook_name, &opt);
165}
ab81cf24
ÆAB
166
167int run_hooks_l(const char *hook_name, ...)
168{
169 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
170 va_list ap;
171 const char *arg;
172
173 va_start(ap, hook_name);
174 while ((arg = va_arg(ap, const char *)))
175 strvec_push(&opt.args, arg);
176 va_end(ap);
177
178 return run_hooks_opt(hook_name, &opt);
179}