Reputation: 1343
I have a server side hook (post-update) that will run after every code push I do. Is there any way (or maybe another hook?) to run it even if there are no new changes to push?
Example:
$ git push origin master
remote: Hook running....
$
$ git push origin master
Everything up-to-date
$
I'd like it to be run again. Is that possible?
Upvotes: 3
Views: 8244
Reputation: 38701
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.
Upvotes: 0
Reputation: 87451
Create a pre-receive
hook, make it exit 1
, and from the client git push origin master HEAD:non-existing-branch
. This will trigger the pre-receive
hook, even if there are no changes to master
.
To avoid error messages, make the pre-receive
hook exit successfully (exit 0
), and manually remove the the non-existing-branch
from the post-receive
hook. However, this creates a small time window (when the file non-existing-branch
exists) with the git push ...
initiated from another client won't run the hooks.
Upvotes: 1