Skip to content

Commit ae98a00

Browse files
sbeyergitster
authored andcommitted
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Stephan Beyer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2292ce4 commit ae98a00

File tree

7 files changed

+57
-113
lines changed

7 files changed

+57
-113
lines changed

builtin-checkout.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,13 @@ struct checkout_opts {
3838
static int post_checkout_hook(struct commit *old, struct commit *new,
3939
int changed)
4040
{
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);
5245
/* "new" can be NULL when checking out from the index before
5346
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+
6048
}
6149

6250
static int update_some(const unsigned char *sha1, const char *base, int baselen,

builtin-commit.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -361,40 +361,6 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
361361
return s.commitable;
362362
}
363363

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-
398364
static int is_a_merge(const unsigned char *sha1)
399365
{
400366
struct commit *commit = lookup_commit(sha1);

builtin-gc.c

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,6 @@ static int too_many_packs(void)
144144
return gc_auto_pack_limit <= cnt;
145145
}
146146

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-
175147
static int need_to_gc(void)
176148
{
177149
/*
@@ -194,7 +166,7 @@ static int need_to_gc(void)
194166
else if (!too_many_loose_objects())
195167
return 0;
196168

197-
if (run_hook())
169+
if (run_hook(NULL, "pre-auto-gc", NULL))
198170
return 0;
199171
return 1;
200172
}

builtin-merge.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,35 +300,6 @@ static void squash_message(void)
300300
strbuf_release(&out);
301301
}
302302

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-
332303
static void finish(const unsigned char *new_head, const char *msg)
333304
{
334305
struct strbuf reflog_message = STRBUF_INIT;
@@ -374,7 +345,7 @@ static void finish(const unsigned char *new_head, const char *msg)
374345
}
375346

376347
/* Run a post-merge hook */
377-
run_hook("post-merge");
348+
run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
378349

379350
strbuf_release(&reflog_message);
380351
}

builtin-receive-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int hook_status(int code, const char *hook_name)
136136
}
137137
}
138138

139-
static int run_hook(const char *hook_name)
139+
static int run_receive_hook(const char *hook_name)
140140
{
141141
static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
142142
struct command *cmd;
@@ -358,7 +358,7 @@ static void execute_commands(const char *unpacker_error)
358358
return;
359359
}
360360

361-
if (run_hook(pre_receive_hook)) {
361+
if (run_receive_hook(pre_receive_hook)) {
362362
while (cmd) {
363363
cmd->error_string = "pre-receive hook declined";
364364
cmd = cmd->next;
@@ -627,7 +627,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
627627
unlink(pack_lockfile);
628628
if (report_status)
629629
report(unpack_status);
630-
run_hook(post_receive_hook);
630+
run_receive_hook(post_receive_hook);
631631
run_update_post_hook(commands);
632632
}
633633
return 0;

run-command.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,48 @@ int finish_async(struct async *async)
342342
#endif
343343
return ret;
344344
}
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ int start_command(struct child_process *);
4949
int finish_command(struct child_process *);
5050
int run_command(struct child_process *);
5151

52+
extern int run_hook(const char *index_file, const char *name, ...);
53+
5254
#define RUN_COMMAND_NO_STDIN 1
5355
#define RUN_GIT_CMD 2 /*If this is to be git sub-command */
5456
#define RUN_COMMAND_STDOUT_TO_STDERR 4

0 commit comments

Comments
 (0)