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
This commit is contained in:
commit
1afcde6da1
@ -52,6 +52,21 @@ Functions
|
||||
Wait for the completion of an asynchronous function that was
|
||||
started with start_async().
|
||||
|
||||
`run_hook`::
|
||||
|
||||
Run a hook.
|
||||
The first argument is a pathname to an index file, or NULL
|
||||
if the hook uses the default index file or no index is needed.
|
||||
The second argument is the name of the hook.
|
||||
The further arguments correspond to the hook arguments.
|
||||
The last argument has to be NULL to terminate the arguments list.
|
||||
If the hook does not exist or is not executable, the return
|
||||
value will be zero.
|
||||
If it is executable, the hook will be executed and the exit
|
||||
status of the hook is returned.
|
||||
On execution, .stdout_to_stderr and .no_stdin will be set.
|
||||
(See below.)
|
||||
|
||||
|
||||
Data structures
|
||||
---------------
|
||||
|
@ -38,23 +38,13 @@ struct checkout_opts {
|
||||
static int post_checkout_hook(struct commit *old, struct commit *new,
|
||||
int changed)
|
||||
{
|
||||
struct child_process proc;
|
||||
const char *name = git_path("hooks/post-checkout");
|
||||
const char *argv[5];
|
||||
return run_hook(NULL, "post-checkout",
|
||||
sha1_to_hex(old ? old->object.sha1 : null_sha1),
|
||||
sha1_to_hex(new ? new->object.sha1 : null_sha1),
|
||||
changed ? "1" : "0", NULL);
|
||||
/* "new" can be NULL when checking out from the index before
|
||||
a commit exists. */
|
||||
|
||||
if (access(name, X_OK) < 0)
|
||||
return 0;
|
||||
|
||||
memset(&proc, 0, sizeof(proc));
|
||||
argv[0] = name;
|
||||
argv[1] = xstrdup(sha1_to_hex(old ? old->object.sha1 : null_sha1));
|
||||
argv[2] = xstrdup(sha1_to_hex(new->object.sha1));
|
||||
argv[3] = changed ? "1" : "0";
|
||||
argv[4] = NULL;
|
||||
proc.argv = argv;
|
||||
proc.no_stdin = 1;
|
||||
proc.stdout_to_stderr = 1;
|
||||
return run_command(&proc);
|
||||
}
|
||||
|
||||
static int update_some(const unsigned char *sha1, const char *base, int baselen,
|
||||
|
@ -361,40 +361,6 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
|
||||
return s.commitable;
|
||||
}
|
||||
|
||||
static int run_hook(const char *index_file, const char *name, ...)
|
||||
{
|
||||
struct child_process hook;
|
||||
const char *argv[10], *env[2];
|
||||
char index[PATH_MAX];
|
||||
va_list args;
|
||||
int i;
|
||||
|
||||
va_start(args, name);
|
||||
argv[0] = git_path("hooks/%s", name);
|
||||
i = 0;
|
||||
do {
|
||||
if (++i >= ARRAY_SIZE(argv))
|
||||
die ("run_hook(): too many arguments");
|
||||
argv[i] = va_arg(args, const char *);
|
||||
} while (argv[i]);
|
||||
va_end(args);
|
||||
|
||||
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
|
||||
env[0] = index;
|
||||
env[1] = NULL;
|
||||
|
||||
if (access(argv[0], X_OK) < 0)
|
||||
return 0;
|
||||
|
||||
memset(&hook, 0, sizeof(hook));
|
||||
hook.argv = argv;
|
||||
hook.no_stdin = 1;
|
||||
hook.stdout_to_stderr = 1;
|
||||
hook.env = env;
|
||||
|
||||
return run_command(&hook);
|
||||
}
|
||||
|
||||
static int is_a_merge(const unsigned char *sha1)
|
||||
{
|
||||
struct commit *commit = lookup_commit(sha1);
|
||||
|
30
builtin-gc.c
30
builtin-gc.c
@ -144,34 +144,6 @@ static int too_many_packs(void)
|
||||
return gc_auto_pack_limit <= cnt;
|
||||
}
|
||||
|
||||
static int run_hook(void)
|
||||
{
|
||||
const char *argv[2];
|
||||
struct child_process hook;
|
||||
int ret;
|
||||
|
||||
argv[0] = git_path("hooks/pre-auto-gc");
|
||||
argv[1] = NULL;
|
||||
|
||||
if (access(argv[0], X_OK) < 0)
|
||||
return 0;
|
||||
|
||||
memset(&hook, 0, sizeof(hook));
|
||||
hook.argv = argv;
|
||||
hook.no_stdin = 1;
|
||||
hook.stdout_to_stderr = 1;
|
||||
|
||||
ret = start_command(&hook);
|
||||
if (ret) {
|
||||
warning("Could not spawn %s", argv[0]);
|
||||
return ret;
|
||||
}
|
||||
ret = finish_command(&hook);
|
||||
if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
|
||||
warning("%s exited due to uncaught signal", argv[0]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int need_to_gc(void)
|
||||
{
|
||||
/*
|
||||
@ -194,7 +166,7 @@ static int need_to_gc(void)
|
||||
else if (!too_many_loose_objects())
|
||||
return 0;
|
||||
|
||||
if (run_hook())
|
||||
if (run_hook(NULL, "pre-auto-gc", NULL))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
@ -300,35 +300,6 @@ static void squash_message(void)
|
||||
strbuf_release(&out);
|
||||
}
|
||||
|
||||
static int run_hook(const char *name)
|
||||
{
|
||||
struct child_process hook;
|
||||
const char *argv[3], *env[2];
|
||||
char index[PATH_MAX];
|
||||
|
||||
argv[0] = git_path("hooks/%s", name);
|
||||
if (access(argv[0], X_OK) < 0)
|
||||
return 0;
|
||||
|
||||
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", get_index_file());
|
||||
env[0] = index;
|
||||
env[1] = NULL;
|
||||
|
||||
if (squash)
|
||||
argv[1] = "1";
|
||||
else
|
||||
argv[1] = "0";
|
||||
argv[2] = NULL;
|
||||
|
||||
memset(&hook, 0, sizeof(hook));
|
||||
hook.argv = argv;
|
||||
hook.no_stdin = 1;
|
||||
hook.stdout_to_stderr = 1;
|
||||
hook.env = env;
|
||||
|
||||
return run_command(&hook);
|
||||
}
|
||||
|
||||
static void finish(const unsigned char *new_head, const char *msg)
|
||||
{
|
||||
struct strbuf reflog_message = STRBUF_INIT;
|
||||
@ -374,7 +345,7 @@ static void finish(const unsigned char *new_head, const char *msg)
|
||||
}
|
||||
|
||||
/* Run a post-merge hook */
|
||||
run_hook("post-merge");
|
||||
run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
|
||||
|
||||
strbuf_release(&reflog_message);
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ static int hook_status(int code, const char *hook_name)
|
||||
}
|
||||
}
|
||||
|
||||
static int run_hook(const char *hook_name)
|
||||
static int run_receive_hook(const char *hook_name)
|
||||
{
|
||||
static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
|
||||
struct command *cmd;
|
||||
@ -358,7 +358,7 @@ static void execute_commands(const char *unpacker_error)
|
||||
return;
|
||||
}
|
||||
|
||||
if (run_hook(pre_receive_hook)) {
|
||||
if (run_receive_hook(pre_receive_hook)) {
|
||||
while (cmd) {
|
||||
cmd->error_string = "pre-receive hook declined";
|
||||
cmd = cmd->next;
|
||||
@ -627,7 +627,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
|
||||
unlink(pack_lockfile);
|
||||
if (report_status)
|
||||
report(unpack_status);
|
||||
run_hook(post_receive_hook);
|
||||
run_receive_hook(post_receive_hook);
|
||||
run_update_post_hook(commands);
|
||||
}
|
||||
return 0;
|
||||
|
@ -342,3 +342,48 @@ int finish_async(struct async *async)
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
int run_hook(const char *index_file, const char *name, ...)
|
||||
{
|
||||
struct child_process hook;
|
||||
const char **argv = NULL, *env[2];
|
||||
char index[PATH_MAX];
|
||||
va_list args;
|
||||
int ret;
|
||||
size_t i = 0, alloc = 0;
|
||||
|
||||
if (access(git_path("hooks/%s", name), X_OK) < 0)
|
||||
return 0;
|
||||
|
||||
va_start(args, name);
|
||||
ALLOC_GROW(argv, i + 1, alloc);
|
||||
argv[i++] = git_path("hooks/%s", name);
|
||||
while (argv[i-1]) {
|
||||
ALLOC_GROW(argv, i + 1, alloc);
|
||||
argv[i++] = va_arg(args, const char *);
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
memset(&hook, 0, sizeof(hook));
|
||||
hook.argv = argv;
|
||||
hook.no_stdin = 1;
|
||||
hook.stdout_to_stderr = 1;
|
||||
if (index_file) {
|
||||
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
|
||||
env[0] = index;
|
||||
env[1] = NULL;
|
||||
hook.env = env;
|
||||
}
|
||||
|
||||
ret = start_command(&hook);
|
||||
free(argv);
|
||||
if (ret) {
|
||||
warning("Could not spawn %s", argv[0]);
|
||||
return ret;
|
||||
}
|
||||
ret = finish_command(&hook);
|
||||
if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
|
||||
warning("%s exited due to uncaught signal", argv[0]);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -49,6 +49,8 @@ int start_command(struct child_process *);
|
||||
int finish_command(struct child_process *);
|
||||
int run_command(struct child_process *);
|
||||
|
||||
extern int run_hook(const char *index_file, const char *name, ...);
|
||||
|
||||
#define RUN_COMMAND_NO_STDIN 1
|
||||
#define RUN_GIT_CMD 2 /*If this is to be git sub-command */
|
||||
#define RUN_COMMAND_STDOUT_TO_STDERR 4
|
||||
|
Loading…
Reference in New Issue
Block a user