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
committed by Junio C Hamano
parent 3e6e152c74
commit c8dd277109

View File

@ -67,18 +67,45 @@ struct command {
static struct command *commands; 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, static int run_hook(const char *hook_name,
char *old_hex, char *new_hex) 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; return 0;
code = run_command_opt(RUN_COMMAND_NO_STDIN
| RUN_COMMAND_STDOUT_TO_STDERR, argv = xmalloc(sizeof(*argv) * (2 + argc));
update_hook, refname, old_hex, new_hex, NULL); 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) { switch (code) {
case 0: case 0:
return 0; return 0;
@ -91,11 +118,11 @@ static int run_update_hook(const char *refname,
case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
return error("waitpid is confused"); return error("waitpid is confused");
case -ERR_RUN_COMMAND_WAITPID_SIGNAL: 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: case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
return error("%s died strangely", update_hook); return error("%s died strangely", hook_name);
default: default:
error("%s exited with error code %d", update_hook, -code); error("%s exited with error code %d", hook_name, -code);
return -code; return -code;
} }
} }
@ -105,7 +132,6 @@ static int update(struct command *cmd)
const char *name = cmd->ref_name; const char *name = cmd->ref_name;
unsigned char *old_sha1 = cmd->old_sha1; unsigned char *old_sha1 = cmd->old_sha1;
unsigned char *new_sha1 = cmd->new_sha1; unsigned char *new_sha1 = cmd->new_sha1;
char new_hex[41], old_hex[41];
struct ref_lock *lock; struct ref_lock *lock;
cmd->error_string = NULL; cmd->error_string = NULL;
@ -115,13 +141,10 @@ static int update(struct command *cmd)
name); 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)) { if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
cmd->error_string = "bad pack"; cmd->error_string = "bad pack";
return error("unpack should have generated %s, " 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) && if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
!is_null_sha1(old_sha1) && !is_null_sha1(old_sha1) &&
@ -140,7 +163,7 @@ static int update(struct command *cmd)
return error("denying non-fast forward;" return error("denying non-fast forward;"
" you should pull first"); " 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"; cmd->error_string = "hook declined";
return error("hook declined to update %s", name); return error("hook declined to update %s", name);
} }
@ -150,7 +173,8 @@ static int update(struct command *cmd)
cmd->error_string = "failed to delete"; cmd->error_string = "failed to delete";
return error("failed to delete %s", name); 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 { else {
lock = lock_any_ref_for_update(name, old_sha1); 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"; cmd->error_string = "failed to write";
return -1; /* error() already called */ 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; return 0;
} }