Move run_hook() from builtin-commit.c into run-command.c (libgit)
A function that runs a hook is used in several Git commands. builtin-commit.c has the one that is most general for cases without piping. The one in builtin-gc.c prints some useful warnings. This patch moves a merged version of these variants into libgit and lets the other builtins use this libified run_hook(). The run_hook() function used in receive-pack.c feeds the standard input of the pre-receive or post-receive hooks. This function is renamed to run_receive_hook() because the libified run_hook() cannot handle this. Mentored-by: Daniel Barkalow <barkalow@iabervon.org> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Stephan Beyer <s-beyer@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stephan Beyer committed
Jan 16, 2009 at 20:09 UTC
ae98a0089ff7f7641ed15ddd595797de56eb49f1
7 files changed
+57
-113
builtin-checkout.c
+5
-17
@@ -38,25 +38,13 @@ struct checkout_opts {
38
static int post_checkout_hook(struct commit *old, struct commit *new,
39
int changed)
40
{
41
- struct child_process proc;
42
- const char *name = git_path("hooks/post-checkout");
43
- const char *argv[5];
44
-
45
- if (access(name, X_OK) < 0)
46
- return 0;
47
-
48
- memset(&proc, 0, sizeof(proc));
49
- argv[0] = name;
50
- argv[1] = sha1_to_hex(old ? old->object.sha1 : null_sha1);
51
- argv[2] = sha1_to_hex(new ? new->object.sha1 : null_sha1);
41
+ return run_hook(NULL, "post-checkout",
42
+ sha1_to_hex(old ? old->object.sha1 : null_sha1),
43
+ sha1_to_hex(new ? new->object.sha1 : null_sha1),
44
+ changed ? "1" : "0", NULL);
45
/* "new" can be NULL when checking out from the index before
46
a commit exists. */
54
- argv[3] = changed ? "1" : "0";
55
- argv[4] = NULL;
56
- proc.argv = argv;
57
- proc.no_stdin = 1;
58
- proc.stdout_to_stderr = 1;
59
- return run_command(&proc);
47
+
48
}
49
50
static int update_some(const unsigned char *sha1, const char *base, int baselen,
builtin-commit.c
-34
@@ -361,40 +361,6 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
361
return s.commitable;
362
}
363
364
-static int run_hook(const char *index_file, const char *name, ...)
365
-{
366
- struct child_process hook;
367
- const char *argv[10], *env[2];
368
- char index[PATH_MAX];
369
- va_list args;
370
- int i;
371
-
372
- va_start(args, name);
373
- argv[0] = git_path("hooks/%s", name);
374
- i = 0;
375
- do {
376
- if (++i >= ARRAY_SIZE(argv))
377
- die ("run_hook(): too many arguments");
378
- argv[i] = va_arg(args, const char *);
379
- } while (argv[i]);
380
- va_end(args);
381
-
382
- snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
383
- env[0] = index;
384
- env[1] = NULL;
385
-
386
- if (access(argv[0], X_OK) < 0)
387
- return 0;
388
-
389
- memset(&hook, 0, sizeof(hook));
390
- hook.argv = argv;
391
- hook.no_stdin = 1;
392
- hook.stdout_to_stderr = 1;
393
- hook.env = env;
394
-
395
- return run_command(&hook);
396
-}
397
-
364
static int is_a_merge(const unsigned char *sha1)
365
{
366
struct commit *commit = lookup_commit(sha1);
builtin-gc.c
+1
-29
@@ -144,34 +144,6 @@ static int too_many_packs(void)
144
return gc_auto_pack_limit <= cnt;
145
}
146
147
-static int run_hook(void)
148
-{
149
- const char *argv[2];
150
- struct child_process hook;
151
- int ret;
152
-
153
- argv[0] = git_path("hooks/pre-auto-gc");
154
- argv[1] = NULL;
155
-
156
- if (access(argv[0], X_OK) < 0)
157
- return 0;
158
-
159
- memset(&hook, 0, sizeof(hook));
160
- hook.argv = argv;
161
- hook.no_stdin = 1;
162
- hook.stdout_to_stderr = 1;
163
-
164
- ret = start_command(&hook);
165
- if (ret) {
166
- warning("Could not spawn %s", argv[0]);
167
- return ret;
168
- }
169
- ret = finish_command(&hook);
170
- if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
171
- warning("%s exited due to uncaught signal", argv[0]);
172
- return ret;
173
-}
174
-
147
static int need_to_gc(void)
148
{
149
/*
@@ -194,7 +166,7 @@ static int need_to_gc(void)
166
else if (!too_many_loose_objects())
167
return 0;
168
197
- if (run_hook())
169
+ if (run_hook(NULL, "pre-auto-gc", NULL))
170
return 0;
171
return 1;
172
}
builtin-merge.c
+1
-30
@@ -300,35 +300,6 @@ static void squash_message(void)
300
strbuf_release(&out);
301
}
302
303
-static int run_hook(const char *name)
304
-{
305
- struct child_process hook;
306
- const char *argv[3], *env[2];
307
- char index[PATH_MAX];
308
-
309
- argv[0] = git_path("hooks/%s", name);
310
- if (access(argv[0], X_OK) < 0)
311
- return 0;
312
-
313
- snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", get_index_file());
314
- env[0] = index;
315
- env[1] = NULL;
316
-
317
- if (squash)
318
- argv[1] = "1";
319
- else
320
- argv[1] = "0";
321
- argv[2] = NULL;
322
-
323
- memset(&hook, 0, sizeof(hook));
324
- hook.argv = argv;
325
- hook.no_stdin = 1;
326
- hook.stdout_to_stderr = 1;
327
- hook.env = env;
328
-
329
- return run_command(&hook);
330
-}
331
-
303
static void finish(const unsigned char *new_head, const char *msg)
304
{
305
struct strbuf reflog_message = STRBUF_INIT;
@@ -374,7 +345,7 @@ static void finish(const unsigned char *new_head, const char *msg)
345
}
346
347
/* Run a post-merge hook */
377
- run_hook("post-merge");
348
+ run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
349
350
strbuf_release(&reflog_message);
351
}
builtin-receive-pack.c
+3
-3
@@ -136,7 +136,7 @@ static int hook_status(int code, const char *hook_name)
136
}
137
}
138
139
-static int run_hook(const char *hook_name)
139
+static int run_receive_hook(const char *hook_name)
140
{
141
static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
142
struct command *cmd;
@@ -358,7 +358,7 @@ static void execute_commands(const char *unpacker_error)
358
return;
359
}
360
361
- if (run_hook(pre_receive_hook)) {
361
+ if (run_receive_hook(pre_receive_hook)) {
362
while (cmd) {
363
cmd->error_string = "pre-receive hook declined";
364
cmd = cmd->next;
@@ -627,7 +627,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
627
unlink(pack_lockfile);
628
if (report_status)
629
report(unpack_status);
630
- run_hook(post_receive_hook);
630
+ run_receive_hook(post_receive_hook);
631
run_update_post_hook(commands);
632
}
633
return 0;
run-command.c
+45
@@ -342,3 +342,48 @@ int finish_async(struct async *async)
342
#endif
343
return ret;
344
}
345
+
346
+int run_hook(const char *index_file, const char *name, ...)
347
+{
348
+ struct child_process hook;
349
+ const char *argv[10], *env[2];
350
+ char index[PATH_MAX];
351
+ va_list args;
352
+ int ret;
353
+ int i;
354
+
355
+ va_start(args, name);
356
+ argv[0] = git_path("hooks/%s", name);
357
+ i = 0;
358
+ do {
359
+ if (++i >= ARRAY_SIZE(argv))
360
+ die("run_hook(): too many arguments");
361
+ argv[i] = va_arg(args, const char *);
362
+ } while (argv[i]);
363
+ va_end(args);
364
+
365
+ if (access(argv[0], X_OK) < 0)
366
+ return 0;
367
+
368
+ memset(&hook, 0, sizeof(hook));
369
+ hook.argv = argv;
370
+ hook.no_stdin = 1;
371
+ hook.stdout_to_stderr = 1;
372
+ if (index_file) {
373
+ snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
374
+ env[0] = index;
375
+ env[1] = NULL;
376
+ hook.env = env;
377
+ }
378
+
379
+ ret = start_command(&hook);
380
+ if (ret) {
381
+ warning("Could not spawn %s", argv[0]);
382
+ return ret;
383
+ }
384
+ ret = finish_command(&hook);
385
+ if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
386
+ warning("%s exited due to uncaught signal", argv[0]);
387
+
388
+ return ret;
389
+}
run-command.h
+2
@@ -49,6 +49,8 @@ int start_command(struct child_process *);
49
int finish_command(struct child_process *);
50
int run_command(struct child_process *);
51
52
+extern int run_hook(const char *index_file, const char *name, ...);
53
+
54
#define RUN_COMMAND_NO_STDIN 1
55
#define RUN_GIT_CMD 2 /*If this is to be git sub-command */
56
#define RUN_COMMAND_STDOUT_TO_STDERR 4