]> git.ipfire.org Git - thirdparty/git.git/blob - hook.c
hook API: add a run_hooks() wrapper
[thirdparty/git.git] / hook.c
1 #include "cache.h"
2 #include "hook.h"
3 #include "run-command.h"
4 #include "config.h"
5
6 const 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 }
39
40 int hook_exists(const char *name)
41 {
42 return !!find_hook(name);
43 }
44
45 static 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;
60
61 strvec_push(&cp->args, hook_path);
62 strvec_pushv(&cp->args, hook_cb->options->args.v);
63
64 /* Provide context for errors if necessary */
65 *pp_task_cb = (char *)hook_path;
66
67 /*
68 * This pick_next_hook() will be called again, we're only
69 * running one hook, so indicate that no more work will be
70 * done.
71 */
72 hook_cb->hook_path = NULL;
73
74 return 1;
75 }
76
77 static int notify_start_failure(struct strbuf *out,
78 void *pp_cb,
79 void *pp_task_cp)
80 {
81 struct hook_cb_data *hook_cb = pp_cb;
82 const char *hook_path = pp_task_cp;
83
84 hook_cb->rc |= 1;
85
86 strbuf_addf(out, _("Couldn't start hook '%s'\n"),
87 hook_path);
88
89 return 1;
90 }
91
92 static int notify_hook_finished(int result,
93 struct strbuf *out,
94 void *pp_cb,
95 void *pp_task_cb)
96 {
97 struct hook_cb_data *hook_cb = pp_cb;
98
99 hook_cb->rc |= result;
100
101 return 0;
102 }
103
104 static void run_hooks_opt_clear(struct run_hooks_opt *options)
105 {
106 strvec_clear(&options->env);
107 strvec_clear(&options->args);
108 }
109
110 int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
111 {
112 struct hook_cb_data cb_data = {
113 .rc = 0,
114 .hook_name = hook_name,
115 .options = options,
116 };
117 const char *const hook_path = find_hook(hook_name);
118 int jobs = 1;
119 int ret = 0;
120
121 if (!options)
122 BUG("a struct run_hooks_opt must be provided to run_hooks");
123
124 if (!hook_path && !options->error_if_missing)
125 goto cleanup;
126
127 if (!hook_path) {
128 ret = error("cannot find a hook named %s", hook_name);
129 goto cleanup;
130 }
131
132 cb_data.hook_path = hook_path;
133 run_processes_parallel_tr2(jobs,
134 pick_next_hook,
135 notify_start_failure,
136 notify_hook_finished,
137 &cb_data,
138 "hook",
139 hook_name);
140 ret = cb_data.rc;
141 cleanup:
142 run_hooks_opt_clear(options);
143 return ret;
144 }
145
146 int run_hooks(const char *hook_name)
147 {
148 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
149
150 return run_hooks_opt(hook_name, &opt);
151 }