Refactor run_update_hook to be more useful

This is a simple refactoring of run_update_hook to allow the function
to be passed the name of the hook it runs and also to build the
argument list from a list of struct commands, rather than just one
struct command.

The refactoring is to support new pre-receive and post-receive
hooks that will be given the entire list of struct commands,
rather than just one struct command.  These new hooks will follow
in another patch.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Shawn O. Pearce 2007-03-07 16:51:09 -05:00 коммит произвёл Junio C Hamano
Родитель 3e6e152c74
Коммит c8dd277109
1 изменённых файлов: 44 добавлений и 19 удалений

Просмотреть файл

@ -67,18 +67,45 @@ struct command {
static struct command *commands;
static char update_hook[] = "hooks/update";
static const char update_hook[] = "hooks/update";
static int run_update_hook(const char *refname,
char *old_hex, char *new_hex)
static int run_hook(const char *hook_name,
struct command *first_cmd,
int single)
{
int code;
struct command *cmd;
int argc, code;
const char **argv;
if (access(update_hook, X_OK) < 0)
for (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {
if (!cmd->error_string)
argc += 3;
if (single)
break;
}
if (!argc || access(hook_name, X_OK) < 0)
return 0;
code = run_command_opt(RUN_COMMAND_NO_STDIN
| RUN_COMMAND_STDOUT_TO_STDERR,
update_hook, refname, old_hex, new_hex, NULL);
argv = xmalloc(sizeof(*argv) * (2 + argc));
argv[0] = hook_name;
for (argc = 1, cmd = first_cmd; cmd; cmd = cmd->next) {
if (!cmd->error_string) {
argv[argc++] = xstrdup(cmd->ref_name);
argv[argc++] = xstrdup(sha1_to_hex(cmd->old_sha1));
argv[argc++] = xstrdup(sha1_to_hex(cmd->new_sha1));
}
if (single)
break;
}
argv[argc] = NULL;
code = run_command_v_opt(argv,
RUN_COMMAND_NO_STDIN | RUN_COMMAND_STDOUT_TO_STDERR);
while (--argc > 0)
free((char*)argv[argc]);
free(argv);
switch (code) {
case 0:
return 0;
@ -91,11 +118,11 @@ static int run_update_hook(const char *refname,
case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
return error("waitpid is confused");
case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
return error("%s died of signal", update_hook);
return error("%s died of signal", hook_name);
case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
return error("%s died strangely", update_hook);
return error("%s died strangely", hook_name);
default:
error("%s exited with error code %d", update_hook, -code);
error("%s exited with error code %d", hook_name, -code);
return -code;
}
}
@ -105,7 +132,6 @@ static int update(struct command *cmd)
const char *name = cmd->ref_name;
unsigned char *old_sha1 = cmd->old_sha1;
unsigned char *new_sha1 = cmd->new_sha1;
char new_hex[41], old_hex[41];
struct ref_lock *lock;
cmd->error_string = NULL;
@ -115,13 +141,10 @@ static int update(struct command *cmd)
name);
}
strcpy(new_hex, sha1_to_hex(new_sha1));
strcpy(old_hex, sha1_to_hex(old_sha1));
if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
cmd->error_string = "bad pack";
return error("unpack should have generated %s, "
"but I can't find it!", new_hex);
"but I can't find it!", sha1_to_hex(new_sha1));
}
if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
!is_null_sha1(old_sha1) &&
@ -140,7 +163,7 @@ static int update(struct command *cmd)
return error("denying non-fast forward;"
" you should pull first");
}
if (run_update_hook(name, old_hex, new_hex)) {
if (run_hook(update_hook, cmd, 1)) {
cmd->error_string = "hook declined";
return error("hook declined to update %s", name);
}
@ -150,7 +173,8 @@ static int update(struct command *cmd)
cmd->error_string = "failed to delete";
return error("failed to delete %s", name);
}
fprintf(stderr, "%s: %s -> deleted\n", name, old_hex);
fprintf(stderr, "%s: %s -> deleted\n", name,
sha1_to_hex(old_sha1));
}
else {
lock = lock_any_ref_for_update(name, old_sha1);
@ -162,7 +186,8 @@ static int update(struct command *cmd)
cmd->error_string = "failed to write";
return -1; /* error() already called */
}
fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
fprintf(stderr, "%s: %s -> %s\n", name,
sha1_to_hex(old_sha1), sha1_to_hex(new_sha1));
}
return 0;
}