pre-push hook support
cover pre-push hook support
There have been at least a couple of submissions to add support for a pre-push hook, which were rejected at least partially because they didn't provide enough information to a hook script for it to determine what was to be pushed any better than a separate wrapper around the 'git push' command would be able to do. In this series I attempt to address that problem.
The first two patches in this series do a little bit of refactoring in order to make it easier to call hooks with a variable number of arguments.
The third patch actually adds support for calling a pre-push hook. If it exists, it will be called with the name and URL of the destination remote (if a named remote isn't being used, the URL will be supplied for both) followed by another argument for each ref being pushed; these arguments take the form:
<local ref>:<local sha1>:<remote ref>:<remote sha1>
This should provide enough information for a script to easily determine the set of commits that is being pushed, and thus make a decision if that should be allowed.
The final patch adds a sample pre-push hook script which will deny attempts to push commits that are marked as a work in progress.
Aaron Schrab (4):
hooks: Add function to check if a hook exists
hooks: support variable number of parameters
push: Add support for pre-push hooks
Add sample pre-push hook script
Documentation/githooks.txt | 28 ++++++++ builtin/push.c | 1 + run-command.c | 35 ++++++--- run-command.h | 3 + t/t5571-pre-push-hook.sh | 145 ++++++++++++++++++++++++++++++++++++++ templates/hooks--pre-push.sample | 63 +++++++++++++++++ transport.c | 25 +++++++ transport.h | 1 + 8 files changed, 292 insertions(+), 9 deletions(-) create mode 100755 t/t5571-pre-push-hook.sh create mode 100644 templates/hooks--pre-push.sample
Signature · 1 line
1.7.10.4
PATCH 1/4 hooks: Add function to check if a hook exists
Create find_hook() function to determine if a given hook exists and is executable. If it is the path to the script will be returned, otherwise NULL is returned.
This is in support for an upcoming run_hook_argv() function which will expect the full path to the hook script as the first element in the argv_array. This also makes it simple for places that can use a hook to check if a hook exists before doing, possibly lengthy, setup work which would be pointless if no such hook is present.
The returned value is left as a static value from get_pathname() rather than a duplicate because it is anticipated that the return value will either be used as a boolean, or immediately added to an argv_array list which would result in it being duplicated at that point.
--- run-command.c | 15 +++++++++++++-- run-command.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-)
run-command.c +13 -2
@@ -735,6 +735,15 @@ int finish_async(struct async *async)#endif } +char *find_hook (const char *name) +{ +char *path =git_path ("hooks/%s" , name); +if (access (path,X_OK ) <0 ) + path =NULL ; + +return path; +} +int run_hook (const char *index_file,const char *name, ...) {struct child_process hook;
@@ -744,11 +753,13 @@ int run_hook(const char *index_file, const char *name, ...)va_list args;int ret; -if (access (git_path ("hooks/%s" , name),X_OK ) <0 ) + p =find_hook (name); +if (!p)return 0 ; +argv_array_push (&argv, p); +va_start (args, name); -argv_array_push (&argv,git_path ("hooks/%s" , name)); while ((p = va_arg(args, const char *)))argv_array_push (&argv, p);va_end (args);
run-command.h +1 -0
@@ -45,6 +45,7 @@ int start_command(struct child_process *);int finish_command (struct child_process *);int run_command (struct child_process *); +extern char *find_hook (const char *name);extern int run_hook (const char *index_file,const char *name, ...);#define RUN_COMMAND_NO_STDIN 1
Signature · 1 line
1.7.10.4
Re: [PATCH 1/4] hooks: Add function to check if a hook exists
Quoted 3 lines · Aaron Schrab
Create
find_hook()function to determine if a given hook exists and is executable. If it is the path to the script will be returned, otherwise NULL is returned.
Sounds like a sensible thing to do. To make sure the API is also sensible, all the existing hooks should be updated to use this API, no?
Quoted 3 lines
This is in support for an upcoming
run_hook_argv()function which will expect the full path to the hook script as the first element in theargv_array.
There is currently a public function called run_hook() that squats on the good name with a kludgy API that is too specific to using separate index file. Back when it was a private helper in the implementation of "git commit", it was perfectly fine, but it was exported without giving much thought on the API.
If you are introducing a new run_hook_* function, give it a generic enough API that lets all the existing hook callers to use it. I would imagine that the API requirement may be modelled after run_command() API so that we can pass argv[] and tweak the hook's environ[], as well as feeding its stdin and possibly reading from its stdout. That would be very useful.
Re: [PATCH 1/4] hooks: Add function to check if a hook exists
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Aaron Schrab
Create
find_hook()function to determine if a given hook exists and is executable. If it is the path to the script will be returned, otherwise NULL is returned.Sounds like a sensible thing to do. To make sure the API is also sensible, all the existing hooks should be updated to use this API, no?
I'd been trying to keep the changes limited. I'll see about modifying the existing places that run hooks in v2 of the series.
Quoted 10 lines
Quoted 3 lines
This is in support for an upcoming
run_hook_argv()function which will expect the full path to the hook script as the first element in theargv_array.There is currently a public function called
run_hook()that squats on the good name with a kludgy API that is too specific to using separate index file. Back when it was a private helper in the implementation of "git commit", it was perfectly fine, but it was exported without giving much thought on the API.
Quoted 6 lines
If you are introducing a new run_hook_* function, give it a generic enough API that lets all the existing hook callers to use it. I would imagine that the API requirement may be modelled after
run_command()API so that we can pass argv[] and tweak the hook's environ[], as well as feeding its stdin and possibly reading from its stdout. That would be very useful.
I think the attraction of the run_hook() API is its simplicity. It's currently a fairly thin wrapper around the run_command() API. I suspect that if the run_hook() API were made generic enough to support all of the existing hook callers it would greatly complicate the existing calls to run_hook() while not providing much benefit to hook callers which can't currently use it beyond what run_command() offers.
Since I'm going to be changing the interface for this hook in v2 of the series so that it will be more complicated than can be readily addressed with the run_hook() API (and will have use a fixed number of arguments anyway) I'll be dropping the run_hook_argv() function.
Re: [PATCH 1/4] hooks: Add function to check if a hook exists
Quoted 4 lines · Aaron Schrab
Since I'm going to be changing the interface for this hook in v2 of the series so that it will be more complicated than can be readily addressed with the
run_hook()API (and will have use a fixed number of arguments anyway) I'll be dropping therun_hook_argv()function.
Just to make sure there is no misunderstanding (sorry for sending the message without finishing it with this clarification at the end in the first place). I didn't mean that converting all of the existing callers must come earlier than introducing a new hook invoker.
I just wanted to make sure that we are aware that we are adding to our technical debt, if we are adding another that is also specialized; as the proposed interface looked sufficiently generic, it would be the ideal one to make _other_ ones thin wrappers around it to unify the various codepaths.
Thanks.
PATCH 2/4 hooks: support variable number of parameters
In reply to Aaron SchrabDefine the run_hook_argv() function to allow hooks to be created where the number of parameters to be passed is variable. The existing run_hook() function uses stdarg to allow it to receive a variable number of arguments, but the number of arguments that a given caller is passing is fixed at compile time. This function will allow the caller of a hook to determine the number of arguments to pass when preparing to call the hook.
The first use of this function will be for a pre-push hook which will add an argument for every reference which is to be pushed.
--- run-command.c | 20 +++++++++++++------- run-command.h | 2 ++ 2 files changed, 15 insertions(+), 7 deletions(-)
run-command.c +13 -7
@@ -2,7 +2,6 @@#include "run-command.h" #include "exec_cmd.h" #include "sigchain.h" -#include "argv-array.h" #ifndef SHELL_PATH # define SHELL_PATH "/bin/sh"
@@ -746,10 +745,8 @@ char *find_hook(const char *name)int run_hook (const char *index_file,const char *name, ...) { -struct child_process hook;struct argv_array argv =ARGV_ARRAY_INIT ; -const char *p, *env[2 ]; -char index[PATH_MAX ]; +const char *p;va_list args;int ret;
@@ -764,6 +761,17 @@ int run_hook(const char *index_file, const char *name, ...)argv_array_push (&argv, p);va_end (args); + ret =run_hook_argv (index_file, argv); +argv_array_clear (&argv); +return ret; +} + +int run_hook_argv (const char *index_file,struct argv_array argv) +{ +struct child_process hook; +char index[PATH_MAX ]; +const char *env[2 ]; +memset (&hook,0 ,sizeof (hook)); hook.argv = argv.argv; hook.no_stdin =1 ;
@@ -775,7 +783,5 @@ int run_hook(const char *index_file, const char *name, ...) hook.env = env; } - ret =run_command (&hook); -argv_array_clear (&argv); -return ret; +return run_command (&hook); }
run-command.h +2 -0
@@ -1,6 +1,7 @@#ifndef RUN_COMMAND_H #define RUN_COMMAND_H +#include "argv-array.h" #ifndef NO_PTHREADS #include <pthread.h> #endif
@@ -47,6 +48,7 @@ int run_command(struct child_process *);extern char *find_hook (const char *name);extern int run_hook (const char *index_file,const char *name, ...); +extern int run_hook_argv (const char *index_file,struct argv_array );#define RUN_COMMAND_NO_STDIN 1#define RUN_GIT_CMD 2/*If this is to be git sub-command */
Signature · 1 line
1.7.10.4
PATCH 3/4 push: Add support for pre-push hooks
In reply to Aaron SchrabAdd support for a pre-push hook which can be used to determine if the set of refs to be pushed is suitable for the target repository. The hook should be supplied with:
1. name of the remote being used, or the URL if not using a named remote
2. the URL to which we're pushing
3. descriptions of what references are to be pushed
Each reference to be pushed should be described in a separate parameter to the hook script in the form:
<local ref>:<local sha1>:<remote ref>:<remote sha1>
This will allow the script to determine if the push is acceptable based on the target repository and branch(es), the commits which are to be pushed, and even the source branches in some cases.
--- Documentation/githooks.txt | 28 +++++++++ builtin/push.c | 1 + t/t5571-pre-push-hook.sh | 145 ++++++++++++++++++++++++++++++++++++++++++++ transport.c | 25 ++++++++ transport.h | 1 + 5 files changed, 200 insertions(+) create mode 100755 t/t5571-pre-push-hook.sh
Documentation/githooks.txt +28 -0
@@ -176,6 +176,34 @@ save and restore any form of metadata associated with the working tree (eg: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl for an example of how to do this. +pre-push +~~~~~~~~ + +This hook is called by 'git push' and can be used to prevent a push from +taking place. The hook is called with a variable number of parameters. + +The first parameters provide the name and location of the destination +remote, if a named remote is not being used both values will be the same. + +Remaining parameters provide information about the commits which are to be +pushed and the ref names being used. These arguments take the form: + + <local ref>:<local sha1>:<remote ref>:<remote sha1> + +For instance, if the command +git push origin master:foreign+ were run the +hook would be called with a third arugment similar to: + + refs/heads/master:67890:refs/heads/foreign:12345 + +although the full, 40-character SHA1s would be supplied. If the foreign ref +does not yet exist the `<remote SHA1>` will be 40 `0`. If a ref is to be +deleted, the `<local ref>` will be supplied as `(delete)` and the `<local +SHA1>` will be 40 `0`. If the local commit was specified by something other +than a name which could be expanded (such as `HEAD~`, or a SHA1) it will be +supplied as it was originally given. + +If this hook exits with a non-zero status, 'git push' will abort. + [[pre-receive]] pre-receive ~~~~~~~~~~~
builtin/push.c +1 -0
@@ -399,6 +399,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)OPT_BOOL (0 ,"progress" , &progress,N_ ("force progress reporting" )),OPT_BIT (0 ,"prune" , &flags,N_ ("prune locally removed refs" ),TRANSPORT_PUSH_PRUNE ), +OPT_BIT (0 ,"no-verify" , &flags,N_ ("bypass pre-push hook" ),TRANSPORT_PUSH_NO_HOOK ), OPT_END() };
t/t5571-pre-push-hook.sh new file +145 -0
145 lines not on this page · raw message
transport.c +25 -0
@@ -1052,6 +1052,7 @@ int transport_push(struct transport *transport,int porcelain = flags &TRANSPORT_PUSH_PORCELAIN ;int pretend = flags &TRANSPORT_PUSH_DRY_RUN ;int push_ret, ret, err; +char *hook;if (flags &TRANSPORT_PUSH_ALL ) match_flags |=MATCH_REFS_ALL ;
@@ -1069,6 +1070,30 @@ int transport_push(struct transport *transport, flags &TRANSPORT_PUSH_MIRROR , flags & TRANSPORT_PUSH_FORCE); +if (!(flags &TRANSPORT_PUSH_NO_HOOK ) && (hook =find_hook ("pre-push" ))) { +struct ref *r; +struct argv_array argv =ARGV_ARRAY_INIT ; + +argv_array_push (&argv, hook); +argv_array_push (&argv, transport->remote->name); +argv_array_push (&argv, transport->url); + +for (r = remote_refs; r; r = r->next) { +if (!r->peer_ref)continue ; +if (r->status ==REF_STATUS_REJECT_NONFASTFORWARD )continue ; +if (r->status ==REF_STATUS_UPTODATE )continue ; + +argv_array_pushf (&argv,"%s:%s:%s:%s" , + r->peer_ref->name,sha1_to_hex (r->new_sha1), + r->name,sha1_to_hex (r->old_sha1)); + } + + ret =run_hook_argv (NULL , argv); +argv_array_clear (&argv); +if (ret) +return -1 ; + } +if ((flags &TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND ) && !is_bare_repository ()) {struct ref *ref = remote_refs;for (; ref; ref = ref->next)
transport.h +1 -0
@@ -104,6 +104,7 @@ struct transport {#define TRANSPORT_RECURSE_SUBMODULES_CHECK 64#define TRANSPORT_PUSH_PRUNE 128#define TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND 256 +#define TRANSPORT_PUSH_NO_HOOK 512#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)#define TRANSPORT_SUMMARY (x) (int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
Signature · 1 line
1.7.10.4
PATCH 4/4 Add sample pre-push hook script
In reply to Aaron SchrabCreate a sample of a script for a pre-push hook. The main purpose is to illustrate how a script may parse the parameters which are supplied to such a hook. The script may also be useful to some people as-is for avoiding to push commits which are marked as a work in progress.
--- templates/hooks--pre-push.sample | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 templates/hooks--pre-push.sample
templates/hooks--pre-push.sample new file +63 -0
@@ -0,0 +1,63 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. +# Called by "git push" after it has checked the remote status, but before +# anything has been pushed. If this script exits with a non-zero status +# nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Further arguments provide information about the commits which are being +# pushed in the form: +# +# <local ref>:<local sha1>:<remote ref>:<remote sha1> +# +# This sample shows how to prevent push of commits where the log +# message starts with "WIP" (work in progress). + +remote="$1" +url="$2" +shift 2 + +z40=0000000000000000000000000000000000000000 + +old_ifs="$IFS" +for to_push in "$@" +do + # Split the value into its parts + IFS=: + set -- $to_push + IFS="$old_ifs" + + local_ref="$1" + local_sha="$2" + remote_ref="$3" + remote_sha="$4" + + if [ "$local_sha" = $z40 ] + then + range='' + # Handle deletes + else + if [ "$remote_sha" = $z40 ] + then + range="$local_sha" + else + range="$remote_sha..$local_sha" + fi + + commit=`git rev-list -n 1 --grep '^WIP' "$range"` + if [ -n "$commit" ] + then + echo "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0
Signature · 1 line
1.7.10.4
Quoted 10 lines · Aaron Schrab
There have been at least a couple of submissions to add support for a pre-push hook, which were rejected at least partially because they didn't provide enough information to a hook script for it to determine what was to be pushed any better than a separate wrapper around the 'git push' command would be able to do. In this series I attempt to address that problem.
The first two patches in this series do a little bit of refactoring in order to make it easier to call hooks with a variable number of arguments.
Quoted 7 lines
The third patch actually adds support for calling a pre-push hook. If it exists, it will be called with the name and URL of the destination remote (if a named remote isn't being used, the URL will be supplied for both) followed by another argument for each ref being pushed; these arguments take the form:
<local ref>:<local sha1>:<remote ref>:<remote sha1>
One lesson we learned long time ago while doing hooks is to avoid unbound number of command line arguments and instead feed them from the standard input. I think this should do the same.
Quoted 3 lines
This should provide enough information for a script to easily determine the set of commits that is being pushed, and thus make a decision if that should be allowed.
How does the hook communicate its decision to the calling Git?
Will it be "all-or-none", or "I'll allow these but not those"?
Quoted 3 lines · Junio C Hamano
One lesson we learned long time ago while doing hooks is to avoid unbound number of command line arguments and instead feed them from the standard input. I think this should do the same.
Good point. I had been trying to keep the interface for this hook as close as possible to the ones for other client-side hooks on the theory that less development effort may go into those than for server-side hooks. But thinking on that more I certainly see that this could easily run into limits on argument length on some systems; especially when it's likely that each of those arguments is likely to be over 100 bytes long.
I'll work on an updated version which sends the variable length information over a pipe, using the command-line arguments only to pass the remote name and URL.
Quoted 3 lines
How does the hook communicate its decision to the calling Git?
Will it be "all-or-none", or "I'll allow these but not those"?
Currently it just uses the exit code to communicate that back, so it's all-or-none. I think I'll keep that in the updated version as well.
A future enhancement could modify the protocol to support reading from the hook's stdout the names of remote refs which are to be rejected, I think that just having the option for all-or-nothing is a good starting point.
Quoted 5 lines · Aaron Schrab
Quoted 1 line · Junio C Hamano
Will it be "all-or-none", or "I'll allow these but not those"?
Currently it just uses the exit code to communicate that back, so it's all-or-none. I think I'll keep that in the updated version as well.
Thanks; that sounds sensible.
v2 cover pre-push hook support
In reply to Aaron SchrabMain changes since the initial version:
* The first patch converts the existing hook callers to use the new find_hook() function.
* Information about what is to be pushed is now sent over a pipe rather than passed as command-line parameters.
Aaron Schrab (3):
hooks: Add function to check if a hook exists
push: Add support for pre-push hooks
Add sample pre-push hook script
Documentation/githooks.txt | 29 +++++++++ builtin/commit.c | 6 +- builtin/push.c | 1 + builtin/receive-pack.c | 25 ++++---- run-command.c | 15 ++++- run-command.h | 1 + t/t5571-pre-push-hook.sh | 129 +++++++++++++++++++++++++++++++++++++++ templates/hooks--pre-push.sample | 53 ++++++++++++++++ transport.c | 60 ++++++++++++++++++ transport.h | 1 + 10 files changed, 300 insertions(+), 20 deletions(-) create mode 100755 t/t5571-pre-push-hook.sh create mode 100644 templates/hooks--pre-push.sample
Signature · 1 line
1.8.1.340.g425b78d
PATCH v2 1/3 hooks: Add function to check if a hook exists
Create find_hook() function to determine if a given hook exists and is executable. If it is, the path to the script will be returned, otherwise NULL is returned.
This encapsulates the tests that are used to check for the existence of a hook in one place, making it easier to modify those checks if that is found to be necessary. This also makes it simple for places that can use a hook to check if a hook exists before doing, possibly lengthy, setup work which would be pointless if no such hook is present.
The returned value is left as a static value from get_pathname() rather than a duplicate because it is anticipated that the return value will either be used as a boolean, immediately added to an argv_array list which would result in it being duplicated at that point, or used to actually run the command without much intervening work. Callers which need to hold onto the returned value for a longer time are expected to duplicate the return value themselves.
--- builtin/commit.c | 6 ++---- builtin/receive-pack.c | 25 +++++++++++-------------- run-command.c | 15 +++++++++++++-- run-command.h | 1 + 4 files changed, 27 insertions(+), 20 deletions(-)
builtin/commit.c +2 -4
@@ -1327,8 +1327,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)return git_status_config (k, v, s); } -static const char post_rewrite_hook[] ="hooks/post-rewrite" ; -static int run_rewrite_hook (const unsigned char *oldsha1,const unsigned char *newsha1) {
@@ -1339,10 +1337,10 @@ static int run_rewrite_hook(const unsigned char *oldsha1,int code;size_t n; -if (access (git_path (post_rewrite_hook),X_OK ) <0 ) + argv[0 ] =find_hook ("post-rewrite" ); +if (!argv[0 ])return 0 ; - argv[0 ] =git_path (post_rewrite_hook); argv[1 ] ="amend" ; argv[2 ] =NULL ;
builtin/receive-pack.c +11 -14
@@ -182,9 +182,6 @@ struct command {char ref_name[FLEX_ARRAY ];/* more */ }; -static const char pre_receive_hook[] ="hooks/pre-receive" ; -static const char post_receive_hook[] ="hooks/post-receive" ; -static void rp_error (const char *err, ...) __attribute__((format (printf,1 ,2 )));static void rp_warning (const char *err, ...) __attribute__((format (printf,1 ,2 )));
@@ -242,10 +239,10 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_staconst char *argv[2 ];int code; -if (access (hook_name,X_OK ) <0 ) + argv[0 ] =find_hook (hook_name); +if (!argv[0 ])return 0 ; - argv[0 ] = hook_name; argv[1 ] =NULL ;memset (&proc,0 ,sizeof (proc));
@@ -331,15 +328,14 @@ static int run_receive_hook(struct command *commands, const char *hook_name,static int run_update_hook (struct command *cmd) { -static const char update_hook[] ="hooks/update" ;const char *argv[5 ];struct child_process proc;int code; -if (access (update_hook,X_OK ) <0 ) + argv[0 ] =find_hook ("update" ); +if (!argv[0 ])return 0 ; - argv[0 ] = update_hook; argv[1 ] = cmd->ref_name; argv[2 ] =sha1_to_hex (cmd->old_sha1); argv[3 ] =sha1_to_hex (cmd->new_sha1);
@@ -532,24 +528,25 @@ static const char *update(struct command *cmd) } } -static char update_post_hook[] ="hooks/post-update" ; -static void run_update_post_hook (struct command *commands) {struct command *cmd;int argc;const char **argv;struct child_process proc; +char *hook; + hook =find_hook ("post-update" );for (argc =0 , cmd = commands; cmd; cmd = cmd->next) {if (cmd->error_string || cmd->did_not_exist)continue ; argc++; } -if (!argc ||access (update_post_hook,X_OK ) <0 ) +if (!argc || !hook)return ; + argv =xmalloc (sizeof (*argv) * (2 + argc)); - argv[0 ] = update_post_hook; + argv[0 ] = hook;for (argc =1 , cmd = commands; cmd; cmd = cmd->next) {char *p;
@@ -704,7 +701,7 @@ static void execute_commands(struct command *commands, const char *unpacker_erro 0, &cmd))set_connectivity_errors (commands); -if (run_receive_hook (commands, pre_receive_hook,0 )) { +if (run_receive_hook (commands,"pre-receive" ,0 )) {for (cmd = commands; cmd; cmd = cmd->next) {if (!cmd->error_string) cmd->error_string ="pre-receive hook declined" ;
@@ -994,7 +991,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)unlink_or_warn (pack_lockfile);if (report_status)report (commands, unpack_status); -run_receive_hook (commands, post_receive_hook,1 ); +run_receive_hook (commands,"post-receive" ,1 );run_update_post_hook (commands);if (auto_gc) {const char *argv_gc_auto[] = {
run-command.c +13 -2
@@ -735,6 +735,15 @@ int finish_async(struct async *async)#endif } +char *find_hook (const char *name) +{ +char *path =git_path ("hooks/%s" , name); +if (access (path,X_OK ) <0 ) + path =NULL ; + +return path; +} +int run_hook (const char *index_file,const char *name, ...) {struct child_process hook;
@@ -744,11 +753,13 @@ int run_hook(const char *index_file, const char *name, ...)va_list args;int ret; -if (access (git_path ("hooks/%s" , name),X_OK ) <0 ) + p =find_hook (name); +if (!p)return 0 ; +argv_array_push (&argv, p); +va_start (args, name); -argv_array_push (&argv,git_path ("hooks/%s" , name)); while ((p = va_arg(args, const char *)))argv_array_push (&argv, p);va_end (args);
run-command.h +1 -0
@@ -45,6 +45,7 @@ int start_command(struct child_process *);int finish_command (struct child_process *);int run_command (struct child_process *); +extern char *find_hook (const char *name);extern int run_hook (const char *index_file,const char *name, ...);#define RUN_COMMAND_NO_STDIN 1
Signature · 1 line
1.8.1.340.g425b78d
PATCH v2 2/3 push: Add support for pre-push hooks
In reply to Aaron SchrabAdd support for a pre-push hook which can be used to determine if the set of refs to be pushed is suitable for the target repository. The hook is run with two arguments specifying the name and location of the destination repository.
Information about what is to be pushed is provided by sending lines of the following form to the hook's standard input:
<local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
If the hook exits with a non-zero status, the push will be aborted.
This will allow the script to determine if the push is acceptable based on the target repository and branch(es), the commits which are to be pushed, and even the source branches in some cases.
--- Documentation/githooks.txt | 29 ++++++++++ builtin/push.c | 1 + t/t5571-pre-push-hook.sh | 129 +++++++++++++++++++++++++++++++++++++++++++++ transport.c | 60 +++++++++++++++++++++ transport.h | 1 + 5 files changed, 220 insertions(+) create mode 100755 t/t5571-pre-push-hook.sh
Documentation/githooks.txt +29 -0
@@ -176,6 +176,35 @@ save and restore any form of metadata associated with the working tree (eg: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl for an example of how to do this. +pre-push +~~~~~~~~ + +This hook is called by 'git push' and can be used to prevent a push from taking +place. The hook is called with two parameters which provide the name and +location of the destination remote, if a named remote is not being used both +values will be the same. + +Information about what is to be pushed is provided on the hook's standard +input with lines of the form: + + <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF + +For instance, if the command +git push origin master:foreign+ were run the +hook would receive a line like the following: + + refs/heads/master 67890 refs/heads/foreign 12345 + +although the full, 40-character SHA1s would be supplied. If the foreign ref +does not yet exist the `<remote SHA1>` will be 40 `0`. If a ref is to be +deleted, the `<local ref>` will be supplied as `(delete)` and the `<local +SHA1>` will be 40 `0`. If the local commit was specified by something other +than a name which could be expanded (such as `HEAD~`, or a SHA1) it will be +supplied as it was originally given. + +If this hook exits with a non-zero status, 'git push' will abort without +pushing anything. Information about why the push is rejected may be sent +to the user by writing to standard error. + [[pre-receive]] pre-receive ~~~~~~~~~~~
builtin/push.c +1 -0
@@ -407,6 +407,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)OPT_BOOL (0 ,"progress" , &progress,N_ ("force progress reporting" )),OPT_BIT (0 ,"prune" , &flags,N_ ("prune locally removed refs" ),TRANSPORT_PUSH_PRUNE ), +OPT_BIT (0 ,"no-verify" , &flags,N_ ("bypass pre-push hook" ),TRANSPORT_PUSH_NO_HOOK ), OPT_END() };
t/t5571-pre-push-hook.sh new file +129 -0
129 lines not on this page · raw message
transport.c +60 -0
72 lines not on this page · raw message
transport.h +1 -0
@@ -104,6 +104,7 @@ struct transport {#define TRANSPORT_RECURSE_SUBMODULES_CHECK 64#define TRANSPORT_PUSH_PRUNE 128#define TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND 256 +#define TRANSPORT_PUSH_NO_HOOK 512#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)#define TRANSPORT_SUMMARY (x) (int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
Signature · 1 line
1.8.1.340.g425b78d
Re: [PATCH v2 2/3] push: Add support for pre-push hooks
Quoted 9 lines · Aaron Schrab
t/t5571-pre-push-hook.sh | 129 +++++++++++++++++++++++++++++++++++++++++++++t/t5571-pre-push-hook.sh new file +2
-0@@ -0,0 +1,129 @@ +#!/bin/sh +
t/t5571-pre-push-hook.sh line 12
Quoted 10 lines
t/t5571-pre-push-hook.sh new file +10
-0+test_description='check pre-push hooks' +. ./test-lib.sh + +# Setup hook that always succeeds +HOOKDIR="$(git rev-parse --git-dir)/hooks" +HOOK="$HOOKDIR/pre-push" +mkdir -p "$HOOKDIR" +write_script "$HOOK" <<EOF +exit 0 +EOF
As this script is expected to read from the pipe, if this exits before the parent has a chance to write to the pipe, the parent can be killed with sigpipe.
At least the attached patch is necessary.
In the longer term, we may want to discuss what should happen when the hook exited without even reading what we fed. My gut feeling is that we can still trust its exit status (a hook that was badly coded so it wanted to read from us and use that information to decide but somehow died before fully reading from us is not likely to exit with zero status, so we wouldn't diagnosing breakage as a success), but there may be downsides for being that lax.
If we decide we want to be lax, then the call site of this hook and the pre-receive hook (is there any other "take info from the standard input" hook?) need to be modified so that they ignore sigpipe, I think.
There was a related discussion around this issue about a year ago.
http://thread.gmane.org/gmane.comp.version-control.git/180346/focus=186291
t/t5571-pre-push-hook.sh | 3 +++ 1 file changed, 3 insertions(+)
t/t5571-pre-push-hook.sh +3 -0
@@ -8,6 +8,7 @@ HOOKDIR="$(git rev-parse --git-dir)/hooks" HOOK="$HOOKDIR/pre-push" mkdir -p "$HOOKDIR" write_script "$HOOK" <<EOF +cat >/dev/null exit 0 EOF
@@ -19,6 +20,7 @@ test_expect_success 'setup' ' git push parent1 HEAD:foreign ' write_script "$HOOK" <<EOF +cat >/dev/null exit 1 EOF
@@ -38,6 +40,7 @@ COMMIT2="$(git rev-parse HEAD)" export COMMIT2 write_script "$HOOK" <<'EOF' +cat >/dev/null echo "$1" >actual echo "$2" >>actual cat >>actual
Re: [PATCH v2 2/3] push: Add support for pre-push hooks
Quoted 1 line · Junio C Hamano
At least the attached patch is necessary.
Sorry, but the last hunk (see below) is not. It breaks the hook.
Quoted 20 lines
In the longer term, we may want to discuss what should happen when the hook exited without even reading what we fed. My gut feeling is that we can still trust its exit status (a hook that was badly coded so it wanted to read from us and use that information to decide but somehow died before fully reading from us is not likely to exit with zero status, so we wouldn't diagnosing breakage as a success), but there may be downsides for being that lax.
If we decide we want to be lax, then the call site of this hook and the pre-receive hook (is there any other "take info from the standard input" hook?) need to be modified so that they ignore sigpipe, I think.
There was a related discussion around this issue about a year ago.
http://thread.gmane.org/gmane.comp.version-control.git/180346/focus=186291
...
t/t5571-pre-push-hook.sh COMMIT2="$(git rev-parse HEAD)" · line 46
Quoted 7 lines
t/t5571-pre-push-hook.sh +1
-0@@ -38,6 +40,7 @@ COMMIT2="$(git rev-parse HEAD)" export COMMIT2 write_script "$HOOK" <<'EOF' +cat >/dev/null echo "$1" >actual echo "$2" >>actual cat >>actual
As this one wants to keep the incoming data to "actual", we do not want the extra "cat" to slurp everything in. Sorry for not being careful.
Re: [PATCH v2 2/3] push: Add support for pre-push hooks
In reply to Aaron SchrabQuoted 38 lines · Aaron Schrab
Add support for a pre-push hook which can be used to determine if the set of refs to be pushed is suitable for the target repository. The hook is run with two arguments specifying the name and location of the destination repository.
Information about what is to be pushed is provided by sending lines of the following form to the hook's standard input:
<local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
If the hook exits with a non-zero status, the push will be aborted.
This will allow the script to determine if the push is acceptable based on the target repository and branch(es), the commits which are to be pushed, and even the source branches in some cases.
Signed-off-by Aaron Schrab <[email protected]>--- Documentation/githooks.txt | 29 ++++++++++ builtin/push.c | 1 + t/t5571-pre-push-hook.sh | 129 +++++++++++++++++++++++++++++++++++++++++++++ transport.c | 60 +++++++++++++++++++++ transport.h | 1 + 5 files changed, 220 insertions(+) create mode 100755 t/t5571-pre-push-hook.shDocumentation/githooks.txt +4
-0@@ -176,6 +176,35 @@ save and restore any form of metadata associated with the working tree (eg: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl for an example of how to do this. +pre-push +~~~~~~~~ + +This hook is called by 'git push' and can be used to prevent a push from taking
Documentation/githooks.txt save and restore any form of metadata associated with the working tree · line 192
Quoted 10 lines
Documentation/githooks.txt +10
-0+place. The hook is called with two parameters which provide the name and +location of the destination remote, if a named remote is not being used both +values will be the same. + +Information about what is to be pushed is provided on the hook's standard +input with lines of the form: + + <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF + +For instance, if the command +git push origin master:foreign+ were run the
Just being curious, but why use +monospace text+ here? Most of the new text use monospace text literally instead in this patch.
Documentation/githooks.txt save and restore any form of metadata associated with the working tree
Quoted 5 lines
Documentation/githooks.txt +5
-0+hook would receive a line like the following: + + refs/heads/master 67890 refs/heads/foreign 12345 + +although the full, 40-character SHA1s would be supplied.
Perhaps ellipses are called for here?
refs/heads/master 67890... refs/heads/foreign 12345...
(the above abbreviates full 40-hexdigits for illustration purposes only)
Documentation/githooks.txt save and restore any form of metadata associated with the working tree · line 206
Quoted 10 lines
Documentation/githooks.txt +10
-0+If the foreign ref +does not yet exist the `<remote SHA1>` will be 40 `0`. If a ref is to be +deleted, the `<local ref>` will be supplied as `(delete)` and the `<local +SHA1>` will be 40 `0`. If the local commit was specified by something other +than a name which could be expanded (such as `HEAD~`, or a SHA1) it will be +supplied as it was originally given. + +If this hook exits with a non-zero status, 'git push' will abort without +pushing anything. Information about why the push is rejected may be sent +to the user by writing to standard error.
s/standard error/& of the hook/; perhaps? It is unclear who does the writing and it can be misunderstood that git-push will write to standard error upon seeing your hook that silently exits.
builtin/push.c int cmd_push(int argc, const char **argv, const char *prefix) · line 412
Quoted 11 lines
builtin/push.c +1
-0@@ -407,6 +407,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)OPT_BOOL (0 ,"progress" , &progress,N_ ("force progress reporting" )),OPT_BIT (0 ,"prune" , &flags,N_ ("prune locally removed refs" ),TRANSPORT_PUSH_PRUNE ), +OPT_BIT (0 ,"no-verify" , &flags,N_ ("bypass pre-push hook" ),TRANSPORT_PUSH_NO_HOOK ), OPT_END() };
So to countermand this, you have to say --no-no-verify? Wouldn't it be more natural to introduce a --verify option that turns the bit on, which automatically gives you --no-verify to turn it off? A bit in a flag word can be initialized to true before the flag word is given to the parse_options() machinery to make the field default to true, no?
PATCH v2 3/3 Add sample pre-push hook script
In reply to Aaron SchrabCreate a sample of a script for a pre-push hook. The main purpose is to illustrate how a script may parse the information which is supplied to such a hook. The script may also be useful to some people as-is for avoiding to push commits which are marked as a work in progress.
--- templates/hooks--pre-push.sample | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 templates/hooks--pre-push.sample
templates/hooks--pre-push.sample new file +53 -0
@@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# <local ref> <local sha1> <remote ref> <remote sha1> +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +z40=0000000000000000000000000000000000000000 + +IFS=' ' +while read local_ref local_sha remote_ref remote_sha +do + if [ "$local_sha" = $z40 ] + then + # Handle delete + else + if [ "$remote_sha" = $z40 ] + then + # New branch, examine all commits + range="$local_sha" + else + # Update to existing branch, examine new commits + range="$remote_sha..$local_sha" + fi + + # Check for WIP commit + commit=`git rev-list -n 1 --grep '^WIP' "$range"` + if [ -n "$commit" ] + then + echo "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0
Signature · 1 line
1.8.1.340.g425b78d
Re: [PATCH v2 3/3] Add sample pre-push hook script
Quoted 27 lines · Aaron Schrab
Create a sample of a script for a pre-push hook. The main purpose is to illustrate how a script may parse the information which is supplied to such a hook. The script may also be useful to some people as-is for avoiding to push commits which are marked as a work in progress.
Signed-off-by Aaron Schrab <[email protected]>--- templates/hooks--pre-push.sample | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 templates/hooks--pre-push.sampletemplates/hooks--pre-push.sample new file +10
-0@@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done
templates/hooks--pre-push.sample line 20
Quoted 10 lines
templates/hooks--pre-push.sample new file +10
-0+# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# <local ref> <local sha1> <remote ref> <remote sha1> +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress).
An example for a plausible use case is nice to have. I would prefer to see any new shell script to follow the Git style, though.
templates/hooks--pre-push.sample line 32
Quoted 11 lines
templates/hooks--pre-push.sample new file +11
-0+remote="$1" +url="$2" + +z40=0000000000000000000000000000000000000000 + +IFS=' ' +while read local_ref local_sha remote_ref remote_sha +do + if [ "$local_sha" = $z40 ] + then + # Handle delete
... by doing what?
Quoted 21 lines
templates/hooks--pre-push.sample new file +21
-0+ else + if [ "$remote_sha" = $z40 ] + then + # New branch, examine all commits + range="$local_sha" + else + # Update to existing branch, examine new commits + range="$remote_sha..$local_sha" + fi + + # Check for WIP commit + commit=`git rev-list -n 1 --grep '^WIP' "$range"` + if [ -n "$commit" ] + then + echo "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0
Quoted 11 lines · Aaron Schrab
Main changes since the initial version:
* The first patch converts the existing hook callers to use the new
find_hook()function.
* Information about what is to be pushed is now sent over a pipe rather than passed as command-line parameters.Aaron Schrab (3):
hooks: Add function to check if a hook exists
push: Add support for pre-push hooks
Add sample pre-push hook script
Getting much nicer. Thanks.
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Aaron Schrab
Main changes since the initial version:
* The first patch converts the existing hook callers to use the new
find_hook()function.
* Information about what is to be pushed is now sent over a pipe rather than passed as command-line parameters.
Quoted 6 lines
Quoted 4 lines
Aaron Schrab (3):
hooks: Add function to check if a hook exists
push: Add support for pre-push hooks
Add sample pre-push hook scriptGetting much nicer. Thanks.
Hmph, t5571 seems to be flaky in that it sometimes fails but passes when run again. Something timing dependent is going on???
Quoted 16 lines · Junio C Hamano
Quoted 14 lines · Junio C Hamano
Quoted 11 lines · Aaron Schrab
Main changes since the initial version:
* The first patch converts the existing hook callers to use the new
find_hook()function.
* Information about what is to be pushed is now sent over a pipe rather than passed as command-line parameters.Aaron Schrab (3):
hooks: Add function to check if a hook exists
push: Add support for pre-push hooks
Add sample pre-push hook script
Quoted 4 lines
Quoted 1 line
Getting much nicer. Thanks.
Hmph, t5571 seems to be flaky in that it sometimes fails but passes when run again. Something timing dependent is going on???
With this patch applied, repeatedly try to
- make sure "foreign" ref does not exist; and
- attempt pushing the HEAD:foreign to create the "foreign" ref
until it fails, I can get it stop before the output scrolls off of my 114 line terminal. Then when I revert the changes to transport.[ch] and builtin/push.c in this series, the test will keep going.
Wait. The sample hook used in the test _is_ fed some input but it exits without reading any. What happens when we fork it, and it completes execution before we even have a chance to feed a single byte? Wont' we get a sigpipe and die?
Yup, I think that is what is missing from run_pre_push_hook() implementation.
t/t5571-pre-push-hook.sh +8 -1
@@ -16,8 +16,15 @@ test_expect_success 'setup' ' git init --bare repo1 && git remote add parent1 repo1 && test_commit one && - git push parent1 HEAD:foreign + while : + do + git push parent1 :refs/heads/foreign && + git push parent1 HEAD:foreign || break + done ' + +exit + write_script "$HOOK" <<EOF exit 1 EOF