]> git.ipfire.org Git - thirdparty/git.git/blob - hook.c
Merge branch 'jk/bundle-use-dash-for-stdfiles'
[thirdparty/git.git] / hook.c
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "gettext.h"
4 #include "hook.h"
5 #include "run-command.h"
6 #include "config.h"
7 #include "strbuf.h"
8
9 const char *find_hook(const char *name)
10 {
11 static struct strbuf path = STRBUF_INIT;
12
13 strbuf_reset(&path);
14 strbuf_git_path(&path, "hooks/%s", name);
15 if (access(path.buf, X_OK) < 0) {
16 int err = errno;
17
18 #ifdef STRIP_EXTENSION
19 strbuf_addstr(&path, STRIP_EXTENSION);
20 if (access(path.buf, X_OK) >= 0)
21 return path.buf;
22 if (errno == EACCES)
23 err = errno;
24 #endif
25
26 if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
27 static struct string_list advise_given = STRING_LIST_INIT_DUP;
28
29 if (!string_list_lookup(&advise_given, name)) {
30 string_list_insert(&advise_given, name);
31 advise(_("The '%s' hook was ignored because "
32 "it's not set as executable.\n"
33 "You can disable this warning with "
34 "`git config advice.ignoredHook false`."),
35 path.buf);
36 }
37 }
38 return NULL;
39 }
40 return path.buf;
41 }
42
43 int hook_exists(const char *name)
44 {
45 return !!find_hook(name);
46 }
47
48 static int pick_next_hook(struct child_process *cp,
49 struct strbuf *out UNUSED,
50 void *pp_cb,
51 void **pp_task_cb UNUSED)
52 {
53 struct hook_cb_data *hook_cb = pp_cb;
54 const char *hook_path = hook_cb->hook_path;
55
56 if (!hook_path)
57 return 0;
58
59 cp->no_stdin = 1;
60 strvec_pushv(&cp->env, hook_cb->options->env.v);
61 /* reopen the file for stdin; run_command closes it. */
62 if (hook_cb->options->path_to_stdin) {
63 cp->no_stdin = 0;
64 cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY);
65 }
66 cp->stdout_to_stderr = 1;
67 cp->trace2_hook_name = hook_cb->hook_name;
68 cp->dir = hook_cb->options->dir;
69
70 strvec_push(&cp->args, hook_path);
71 strvec_pushv(&cp->args, hook_cb->options->args.v);
72
73 /*
74 * This pick_next_hook() will be called again, we're only
75 * running one hook, so indicate that no more work will be
76 * done.
77 */
78 hook_cb->hook_path = NULL;
79
80 return 1;
81 }
82
83 static int notify_start_failure(struct strbuf *out UNUSED,
84 void *pp_cb,
85 void *pp_task_cp UNUSED)
86 {
87 struct hook_cb_data *hook_cb = pp_cb;
88
89 hook_cb->rc |= 1;
90
91 return 1;
92 }
93
94 static int notify_hook_finished(int result,
95 struct strbuf *out UNUSED,
96 void *pp_cb,
97 void *pp_task_cb UNUSED)
98 {
99 struct hook_cb_data *hook_cb = pp_cb;
100 struct run_hooks_opt *opt = hook_cb->options;
101
102 hook_cb->rc |= result;
103
104 if (opt->invoked_hook)
105 *opt->invoked_hook = 1;
106
107 return 0;
108 }
109
110 static void run_hooks_opt_clear(struct run_hooks_opt *options)
111 {
112 strvec_clear(&options->env);
113 strvec_clear(&options->args);
114 }
115
116 int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
117 {
118 struct strbuf abs_path = STRBUF_INIT;
119 struct hook_cb_data cb_data = {
120 .rc = 0,
121 .hook_name = hook_name,
122 .options = options,
123 };
124 const char *const hook_path = find_hook(hook_name);
125 int ret = 0;
126 const struct run_process_parallel_opts opts = {
127 .tr2_category = "hook",
128 .tr2_label = hook_name,
129
130 .processes = 1,
131 .ungroup = 1,
132
133 .get_next_task = pick_next_hook,
134 .start_failure = notify_start_failure,
135 .task_finished = notify_hook_finished,
136
137 .data = &cb_data,
138 };
139
140 if (!options)
141 BUG("a struct run_hooks_opt must be provided to run_hooks");
142
143 if (options->invoked_hook)
144 *options->invoked_hook = 0;
145
146 if (!hook_path && !options->error_if_missing)
147 goto cleanup;
148
149 if (!hook_path) {
150 ret = error("cannot find a hook named %s", hook_name);
151 goto cleanup;
152 }
153
154 cb_data.hook_path = hook_path;
155 if (options->dir) {
156 strbuf_add_absolute_path(&abs_path, hook_path);
157 cb_data.hook_path = abs_path.buf;
158 }
159
160 run_processes_parallel(&opts);
161 ret = cb_data.rc;
162 cleanup:
163 strbuf_release(&abs_path);
164 run_hooks_opt_clear(options);
165 return ret;
166 }
167
168 int run_hooks(const char *hook_name)
169 {
170 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
171
172 return run_hooks_opt(hook_name, &opt);
173 }
174
175 int run_hooks_l(const char *hook_name, ...)
176 {
177 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
178 va_list ap;
179 const char *arg;
180
181 va_start(ap, hook_name);
182 while ((arg = va_arg(ap, const char *)))
183 strvec_push(&opt.args, arg);
184 va_end(ap);
185
186 return run_hooks_opt(hook_name, &opt);
187 }