summaryrefslogtreecommitdiff
path: root/receive-pack.c
diff options
context:
space:
mode:
authorJosef Weidendorfer <Josef.Weidendorfer@gmx.de>2005-07-31 21:17:43 +0200
committerJunio C Hamano <junkio@cox.net>2005-07-31 23:30:59 -0700
commitb1bf95bba26c8de1581f401dcab1f743bad7fc1d (patch)
tree08c71676e67172279e3ac13a76d3eafa20167d68 /receive-pack.c
parent9f6cf65e66c441d99f4838b6b74971d48c4efc72 (diff)
downloadgit-b1bf95bba26c8de1581f401dcab1f743bad7fc1d.tar.gz
[PATCH] Added hook in git-receive-pack
Just before updating a ref, $GIT_DIR/hooks/update refname old-sha1 new-sha1 is called if executable. The hook can decline the ref to be updated by exiting with a non-zero status, or allow it to be updated by exiting with a zero status. The mechanism also allows e.g sending of a mail with pushed commits on the remote repository. Documentation update with an example hook is included. jc: The credits of the basic idea and initial implementation go to Josef, but I ended up rewriting major parts of his patch, so bugs are all mine. Also I changed the semantics for the hook from his original version (which were post-update hook) so that the hook can optionally decline to update the ref, and also can be used to implement the overall cleanups. The latter was primarily to implement a suggestion from Linus that calling update-server-info should be made optional. Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'receive-pack.c')
-rw-r--r--receive-pack.c88
1 files changed, 58 insertions, 30 deletions
diff --git a/receive-pack.c b/receive-pack.c
index d4e82bf993..85bf8569f8 100644
--- a/receive-pack.c
+++ b/receive-pack.c
@@ -1,11 +1,12 @@
#include "cache.h"
#include "refs.h"
#include "pkt-line.h"
+#include "run-command.h"
#include <sys/wait.h>
static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
-static const char *unpacker = "git-unpack-objects";
+static const char unpacker[] = "git-unpack-objects";
static int show_ref(const char *path, const unsigned char *sha1)
{
@@ -55,6 +56,38 @@ static int verify_old_ref(const char *name, char *hex_contents)
return 0;
}
+static char update_hook[] = "hooks/update";
+
+static int run_update_hook(const char *refname,
+ char *old_hex, char *new_hex)
+{
+ int code;
+
+ if (access(update_hook, X_OK) < 0)
+ return 0;
+ code = run_command(update_hook, refname, old_hex, new_hex, NULL);
+ switch (code) {
+ case 0:
+ return 0;
+ case -ERR_RUN_COMMAND_FORK:
+ die("hook fork failed");
+ case -ERR_RUN_COMMAND_EXEC:
+ die("hook execute failed");
+ case -ERR_RUN_COMMAND_WAITPID:
+ die("waitpid failed");
+ case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
+ die("waitpid is confused");
+ case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
+ fprintf(stderr, "%s died of signal", update_hook);
+ return -1;
+ case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
+ die("%s died strangely", update_hook);
+ default:
+ error("%s exited with error code %d", update_hook, -code);
+ return -code;
+ }
+}
+
static void update(const char *name, unsigned char *old_sha1, unsigned char *new_sha1)
{
char new_hex[60], *old_hex, *lock_name;
@@ -90,11 +123,16 @@ static void update(const char *name, unsigned char *old_sha1, unsigned char *new
unlink(lock_name);
die("%s changed during push", name);
}
- if (rename(lock_name, name) < 0) {
+ if (run_update_hook(name, old_hex, new_hex)) {
+ unlink(lock_name);
+ fprintf(stderr, "hook declined to update %s\n", name);
+ }
+ else if (rename(lock_name, name) < 0) {
unlink(lock_name);
die("unable to replace %s", name);
}
- fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
+ else
+ fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
}
@@ -110,7 +148,7 @@ static void execute_commands(void)
update(cmd->ref_name, cmd->old_sha1, cmd->new_sha1);
cmd = cmd->next;
}
- update_server_info(0);
+ run_update_hook("", NULL, NULL);
}
static void read_head_info(void)
@@ -145,34 +183,24 @@ static void read_head_info(void)
static void unpack(void)
{
- pid_t pid = fork();
-
- if (pid < 0)
+ int code = run_command(unpacker, NULL);
+ switch (code) {
+ case 0:
+ return 0;
+ case -ERR_RUN_COMMAND_FORK:
die("unpack fork failed");
- if (!pid) {
- execlp(unpacker, unpacker, NULL);
+ case -ERR_RUN_COMMAND_EXEC:
die("unpack execute failed");
- }
-
- for (;;) {
- int status, code;
- int retval = waitpid(pid, &status, 0);
-
- if (retval < 0) {
- if (errno == EINTR)
- continue;
- die("waitpid failed (%s)", strerror(retval));
- }
- if (retval != pid)
- die("waitpid is confused");
- if (WIFSIGNALED(status))
- die("%s died of signal %d", unpacker, WTERMSIG(status));
- if (!WIFEXITED(status))
- die("%s died out of really strange complications", unpacker);
- code = WEXITSTATUS(status);
- if (code)
- die("%s exited with error code %d", unpacker, code);
- return;
+ case -ERR_RUN_COMMAND_WAITPID:
+ die("waitpid failed");
+ case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
+ die("waitpid is confused");
+ case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
+ die("%s died of signal", unpacker);
+ case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
+ die("%s died strangely", unpacker);
+ default:
+ die("%s exited with error code %d", unpacker, -code);
}
}