forked from bmeurer/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-run-hooks.c
324 lines (272 loc) · 7.5 KB
/
git-run-hooks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*-
* Generic Git hook program, which runs all hooks from the <basedir>,
* specified using the -b option on the command line, in alphabetic
* order. It will pass all additional command line parameters and all
* input from stdin to each of the hooks.
*
* Copyright (c) 2009 Benedikt Meurer <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* --- global variables --- */
static const char *progname;
/* --- functions --- */
static void xerr(int code, const char *format, ...)
{
const char *errmsg;
va_list ap;
errmsg = strerror(errno);
va_start(ap, format);
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, format, ap);
fprintf(stderr, " (%s)\n", errmsg);
fflush(stderr);
va_end(ap);
exit(code);
}
static void xerrx(int code, const char *format, ...)
{
va_list ap;
va_start(ap, format);
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
fflush(stderr);
va_end(ap);
exit(code);
}
static void *xmalloc(size_t n)
{
void *p;
p = malloc(n);
if (p == NULL)
xerr(EXIT_FAILURE, "Failed to allocate %d bytes of memory", (int)n);
return p;
}
static void *xrealloc(void *p, size_t n)
{
p = (p == NULL) ? malloc(n) : realloc(p, n);
if (p == NULL)
xerr(EXIT_FAILURE, "Failed to allocate %d bytes of memory", (int)n);
return p;
}
static void *xstrdup(const char *s)
{
return (s == NULL) ? NULL : strcpy((char *)xmalloc(strlen(s) + 1), s);
}
static char *buildfilename(const char *s, ...)
{
va_list ap;
char *path;
size_t n;
path = xstrdup(s);
va_start(ap, s);
for (;;) {
n = strlen(path);
while (n > 1 && path[n - 1] == '/')
path[--n] = '\0';
s = va_arg(ap, const char *);
if (s == NULL)
break;
path = (char *)xrealloc(path, n + strlen(s) + 2);
strcat(path, "/");
strcat(path, s);
}
va_end(ap);
return path;
}
static int xstrpcmp(const void *s1, const void *s2)
{
return strcmp(*((const char **) s1), *((const char **) s2));
}
static void sighandler(int signo)
{
(void)signo;
}
static void usage(int code)
{
fprintf(stderr,
"Usage: %s -b BASEDIR -- [ARGS]\n"
"\n"
"Options:\n"
" -b BASEDIR : Specify the BASEDIR of the hooks to execute\n"
" (i.e. /path/to/update.d for the update hook)\n"
"\n"
"Runs all hooks from the specified BASEDIR, passing them the\n"
"remaining ARGS and all data from stdin. If BASEDIR does not\n"
"exist, this program terminates immediately with an exit code\n"
"of 0.\n",
progname);
exit(code);
}
int main(int argc, char **argv)
{
const char *basedir = NULL;
unsigned n, nhooks = 0;
ssize_t i, j, k;
char buffer[1024] = "/tmp/git-generic-hook.XXXXXX";
struct dirent *d;
DIR *dp;
char *path, **nargv, **hooks = NULL, *s;
int ch, fd, status;
pid_t pid, child;
/* setup SIGCHLD and SIGPIPE */
signal(SIGCHLD, sighandler);
signal(SIGPIPE, sighandler);
/* figure out the progname (basename of argv[0]) */
for (path = s = argv[0]; *s != '\0'; ++s)
if (*s == '/')
path = s + 1;
progname = (*path != '\0') ? path : argv[0];
/* parse the command line parameters */
while ((ch = getopt(argc, argv, "b:h")) != -1) {
switch (ch) {
case 'b':
basedir = optarg;
break;
case 'h':
case '?':
usage(EXIT_SUCCESS);
default:
usage(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
/* make sure that -b was specified */
if (basedir == NULL)
usage(EXIT_FAILURE);
/* make sure that $GIT_DIR is set */
if (getenv("GIT_DIR") == NULL)
xerrx(EXIT_FAILURE, "GIT_DIR is unset");
/* create a temporary file and immediately unlink it */
fd = mkstemp(buffer);
if (fd < 0)
xerr(EXIT_FAILURE, "Failed to create temporary file");
if (unlink(buffer) < 0 && errno != ENOENT)
xerr(EXIT_FAILURE, "Failed to unlink temporary file");
/* write stdin to the temporary file */
while ((i = read(STDIN_FILENO, buffer, 1024)) != 0) {
if (i < 0)
xerr(EXIT_FAILURE, "Failed to read from stdin");
for (j = 0; j < i; ) {
k = write(fd, buffer + j, i - j);
if (k < 0)
xerr(EXIT_FAILURE, "Failed to write to temporary file");
j += k;
}
}
/* use temporary file as stdin from now on */
if (dup2(fd, STDIN_FILENO) < 0)
xerr(EXIT_FAILURE, "Failed to read from temporary file");
if (close(fd) < 0)
xerr(EXIT_FAILURE, "Failed to close temporary file");
/* try to open the basedir */
dp = opendir(basedir);
if (dp == NULL && errno != ENOENT) {
xerr(EXIT_FAILURE, "Failed to open directory %s", basedir);
}
else if (dp != NULL) {
/* read the hooks from the basedir */
while ((d = readdir(dp)) != NULL) {
if (*d->d_name == '.')
continue;
path = buildfilename(basedir, d->d_name, NULL);
if (access(path, X_OK) == 0) {
hooks = (char **)xrealloc(hooks, (nhooks + 1) * sizeof(char*));
hooks[nhooks++] = xstrdup(d->d_name);
}
free(path);
}
closedir(dp);
/* sort the hooks */
qsort(hooks, nhooks, sizeof(char *), xstrpcmp);
/* setup the argument vector for the hooks */
nargv = xmalloc((argc + 2) * sizeof(char *));
for (i = 0; i < argc; ++i)
nargv[i + 1] = argv[i];
nargv[argc + 1] = NULL;
/* execute the hooks one by one */
for (n = 0; n < nhooks; ++n) {
/* figure out the absolute path to the hook */
path = buildfilename(basedir, hooks[n], NULL);
/* prepare the argv */
nargv[0] = hooks[n];
/* reset stdin first */
if (lseek(STDIN_FILENO, 0, SEEK_SET) < 0)
xerr(EXIT_FAILURE, "Failed to reset stdin");
/* fork a new process for this hook */
pid = fork();
if (pid < 0) {
xerr(EXIT_FAILURE, "Failed to spawn new process");
}
else if (pid == 0) {
/* execute the hook script */
execv(path, nargv);
fprintf(stderr, "%s: Failed to execute hook %s (%s)\n", progname, path, strerror(errno));
fflush(stderr);
_exit(127);
}
else {
/* wait for the child process to terminate */
while ((child = wait(&status)) != pid) {
if (child < 0)
xerr(EXIT_FAILURE, "Failed to wait for %s to terminate", path);
}
/* check the exit status of the child process */
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
exit(WEXITSTATUS(status));
}
else if (!WIFEXITED(status)) {
xerrx(EXIT_FAILURE, "Hook %s terminated abnormally", path);
}
}
/* cleanup */
free(hooks[n]);
free(path);
}
/* cleanup */
free(nargv);
free(hooks);
}
return EXIT_SUCCESS;
}