Skip to contentSkip to conversation

Make git-update-ref invoke the update and post-update hooks

RFC PATCH In review Petr Baudis 2 participants 2 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Make git-update-ref invoke the update and post-update hooks

Someone raised a concern that the update and post-update hooks are not invoked at fetch time in the similar way as they are invoked at push time, and the idea sort of makes sense. But this patch goes further - it makes Git invoke those hooks each time a ref is updated in a repository using the git-update-ref command, which I believe makes a lot of sense as well - the behaviour is consistent with the current pushing behaviour and you suddenly finally get a hook where you can properly notify even about fast-forwards etc.

---

 Documentation/git-update-ref.txt |    3 ++
 Documentation/hooks.txt          |   12 ++++--
 Makefile                         |    2 +
 builtin-update-ref.c             |   12 ++++++
 hooks.c                          |   68 ++++++++++++++++++++++++++++++++++++
 hooks.h                          |   15 ++++++++
 receive-pack.c                   |   71 +-------------------------------------
 7 files changed, 106 insertions(+), 77 deletions(-)
Documentation/git-update-ref.txt +3 -0
index 71bcb79..bd20d05 100644
@@ -51,6 +51,9 @@ for reading but not for writing (so we'l
 ref symlink to some other tree, if you have copied a whole
 archive by creating a symlink tree).
 
+Just before updating the reference, the update hook is invoked.
+After updating the reference, the post-update hook is invoked.
+
 With `-d` flag, it deletes the named <ref> after verifying it
 still contains <oldvalue>.
 
Documentation/hooks.txt +7 -5
index 517f49b..53fa270 100644
@@ -99,8 +99,9 @@ update
 This hook is invoked by `git-receive-pack` on the remote repository,
 which happens when a `git push` is done on a local repository.
 Just before updating the ref on the remote repository, the update hook
-is invoked.  Its exit status determines the success or failure of
-the ref update.
+is invoked.  It is also invoked on the local repository anytime a ref
+value is updated (that includes mainly fetches, merges, commits).
+Its exit status determines the success or failure of the ref update.
 
 The hook executes once for each ref to be updated, and takes
 three parameters:
@@ -135,9 +136,10 @@ post-update
 -----------
 
 This hook is invoked by `git-receive-pack` on the remote repository,
-which happens when a `git push` is done on a local repository.
-It executes on the remote repository once after all the refs have
-been updated.
+which happens when a `git push` is done on a local repository
+It is also invoked on the local repository anytime a ref value is
+updated (that includes mainly fetches, merges, commits).  It executes
+on the repository once after all the refs have been updated.
 
 It takes a variable number of parameters, each of which is the
 name of ref that was actually updated.
Makefile +1 -1
index 2962c2e..8e58585 100644
@@ -263,7 +263,7 @@ LIB_OBJS = \
 	fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
 	write_or_die.o trace.o list-objects.o grep.o \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
-	color.o wt-status.o archive-zip.o archive-tar.o
+	color.o wt-status.o archive-zip.o archive-tar.o hooks.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
builtin-update-ref.c +11 -1
index b34e598..546cbb1 100644
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "refs.h"
 #include "builtin.h"
+#include "hooks.h"
 
 static const char git_update_ref_usage[] =
 "git-update-ref [-m <reason>] (-d <refname> <value> | <refname> <value> [<oldval>])";
@@ -11,6 +12,7 @@ int cmd_update_ref(int argc, const char 
 	struct ref_lock *lock;
 	unsigned char sha1[20], oldsha1[20];
 	int i, delete;
+	struct command *cmd;
 
 	delete = 0;
 	setup_ident();
@@ -63,9 +65,17 @@ int cmd_update_ref(int argc, const char 
 	lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL);
 	if (!lock)
 		return 1;
+	if (run_update_hook(refname, oldval, value)) {
+		unlock_ref(lock);
+		return 1;
+	}
 	if (write_ref_sha1(lock, sha1, msg) < 0)
 		return 1;
-
 	/* write_ref_sha1 always unlocks the ref, no need to do it explicitly */
+
+	cmd = xmalloc(sizeof(struct command) + strlen(refname) + 1);
+	strcpy(cmd->ref_name, refname);
+	run_update_post_hook(cmd);
+
 	return 0;
 }
hooks.c new file +68 -0

68 lines not on this page · raw message

hooks.h new file +15 -0
new file mode 100644
index 0000000..8ddeca3
@@ -0,0 +1,15 @@
+#ifndef HOOKS_H
+#define HOOKS_H
+
+int run_update_hook(const char *refname, const char *old_hex, const char *new_hex);
+
+struct command {
+	struct command *next;
+	const char *error_string;
+	unsigned char old_sha1[20];
+	unsigned char new_sha1[20];
+	char ref_name[FLEX_ARRAY]; /* more */
+};
+void run_update_post_hook(struct command *cmd);
+
+#endif
receive-pack.c +1 -70

91 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · Petr Baudis

Someone raised a concern that the update and post-update hooks are not invoked at fetch time in the similar way as they are invoked at push time, and the idea sort of makes sense. But this patch goes further - it makes Git invoke those hooks each time a ref is updated in a repository using the git-update-ref command, which I believe makes a lot of sense as well - the behaviour is consistent with the current pushing behaviour and you suddenly finally get a hook where you can properly notify even about fast-forwards etc.

In principle I do not have problem with this approach per-se, but I wonder if we were to do this we might want to make receive-pack.c::update() and cmd_update_ref() call the same underlying function, and make that underlying function implement this "ask the hook if updating is ok" dance. It might even make sense to have update-ref honor deny_non_fast_forwards for that matter (I am mildly doubtful of this last point, though).