Many thanks to @pts for the answer (used all my votes today, can't immediately upvote it :)
); for those (like me) having a slight problem understanding how it functions exactly, here is a simple command line log, demonstrating the use of non-existing-branch
to trigger pre-receive
:
# create original repo:
$ cd /tmp
$ mkdir repotest_git
$ cd repotest_git/
$ git init
Initialized empty Git repository in /tmp/repotest_git/.git/
$ git config user.name "Your Name"
$ git config user.email [email protected]
# populate repo with 1 commit:
$ echo "test" > test.txt
$ git add test.txt
$ git commit -m "initial commit"
[master (root-commit) 2b60608] initial commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$
# create remote "bare" repo; and add pre-receive hook there:
$ cd /tmp
$ git clone --bare repotest_git repotest-remote.git
Cloning into bare repository 'repotest-remote.git'...
done.
$ cd repotest-remote.git
$
$ cat > hooks/pre-receive <<"EOF"
#!/bin/sh
echo from pre-receive: HELLO_VAR [$HELLO_VAR]
exit 1
EOF
$ chmod +x hooks/pre-receive
# go back to original repo, add a remote reference
# to the newly created remote "bare" repo; update with pull:
$ cd /tmp
$ cd repotest_git
$ git remote add origin file:///tmp/repotest-remote.git
$ git pull origin master
From file:///tmp/repotest-remote
* branch master -> FETCH_HEAD
Already up-to-date.
# try testing the hook script;
# since we don't have any changes, Everything is
# up-to-date, and the pre-receive won't trigger:
$ git push
Everything up-to-date
# try testing with HEAD:non-existing-branch
# pre-receive gets triggered - and
# we can see variable is not there:
$ git push origin master HEAD:non-existing-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: from pre-receive: HELLO_VAR []
To file:///tmp/repotest-remote.git
! [remote rejected] HEAD -> non-existing-branch (pre-receive hook declined)
error: failed to push some refs to 'file:///tmp/repotest-remote.git'
# try testing again, this time specify
# env. variable on command line....
# pre-receive gets triggered - and
# we can see variable is there:
$ HELLO_VAR=hello git push origin master HEAD:non-existing-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: from pre-receive: HELLO_VAR [hello]
To file:///tmp/repotest-remote.git
! [remote rejected] HEAD -> non-existing-branch (pre-receive hook declined)
error: failed to push some refs to 'file:///tmp/repotest-remote.git'
Here, for local work, everything works as expected with variables; but, obviously, if your remote repo is behind servers etc, it can get problematic to see how/where variables ended up - so it would be mighty useful to debug that aspect only, without having to do changes to files + git add
/commit
/push
every time, just to trigger the script.
post-receive
hook? Also, is thepost-update
hook really not being run or does it just not do anything in the presence of an empty argument list? FYIgit help hooks
gives you a man page for git hooks.GIT_SSH
(that as of now don't work); for now, I just want to print variables; and for testing, in order to have the scripts trigger, I'd have to do change ->git add
->git commit
->git push
, which is somewhat of a chore. If I could just togit push
and have the remote repo trigger a script regardless, the debugging process would be easier for me (I think:)
)