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