Skip to content

Commit 1afcde6

Browse files
committed
Merge branch 'sb/hook-cleanup'
* sb/hook-cleanup: run_hook(): allow more than 9 hook arguments run_hook(): check the executability of the hook before filling argv api-run-command.txt: talk about run_hook() Move run_hook() from builtin-commit.c into run-command.c (libgit) checkout: don't crash on file checkout before running post-checkout hook
2 parents 35e6afd + 14e6298 commit 1afcde6

File tree

8 files changed

+73
-112
lines changed

8 files changed

+73
-112
lines changed

Documentation/technical/api-run-command.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ Functions
5252
Wait for the completion of an asynchronous function that was
5353
started with start_async().
5454

55+
`run_hook`::
56+
57+
Run a hook.
58+
The first argument is a pathname to an index file, or NULL
59+
if the hook uses the default index file or no index is needed.
60+
The second argument is the name of the hook.
61+
The further arguments correspond to the hook arguments.
62+
The last argument has to be NULL to terminate the arguments list.
63+
If the hook does not exist or is not executable, the return
64+
value will be zero.
65+
If it is executable, the hook will be executed and the exit
66+
status of the hook is returned.
67+
On execution, .stdout_to_stderr and .no_stdin will be set.
68+
(See below.)
69+
5570

5671
Data structures
5772
---------------

builtin-checkout.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +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];
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. */
4447

45-
if (access(name, X_OK) < 0)
46-
return 0;
47-
48-
memset(&proc, 0, sizeof(proc));
49-
argv[0] = name;
50-
argv[1] = xstrdup(sha1_to_hex(old ? old->object.sha1 : null_sha1));
51-
argv[2] = xstrdup(sha1_to_hex(new->object.sha1));
52-
argv[3] = changed ? "1" : "0";
53-
argv[4] = NULL;
54-
proc.argv = argv;
55-
proc.no_stdin = 1;
56-
proc.stdout_to_stderr = 1;
57-
return run_command(&proc);
5848
}
5949

6050
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 = NULL, *env[2];
350+
char index[PATH_MAX];
351+
va_list args;
352+
int ret;
353+
size_t i = 0, alloc = 0;
354+
355+
if (access(git_path("hooks/%s", name), X_OK) < 0)
356+
return 0;
357+
358+
va_start(args, name);
359+
ALLOC_GROW(argv, i + 1, alloc);
360+
argv[i++] = git_path("hooks/%s", name);
361+
while (argv[i-1]) {
362+
ALLOC_GROW(argv, i + 1, alloc);
363+
argv[i++] = va_arg(args, const char *);
364+
}
365+
va_end(args);
366+
367+
memset(&hook, 0, sizeof(hook));
368+
hook.argv = argv;
369+
hook.no_stdin = 1;
370+
hook.stdout_to_stderr = 1;
371+
if (index_file) {
372+
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
373+
env[0] = index;
374+
env[1] = NULL;
375+
hook.env = env;
376+
}
377+
378+
ret = start_command(&hook);
379+
free(argv);
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)