3

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?

5
  • What about the post-receive hook? Also, is the post-update hook really not being run or does it just not do anything in the presence of an empty argument list? FYI git help hooks gives you a man page for git hooks.
    – asm
    Commented Nov 15, 2012 at 16:25
  • 2
    Tried all of *-update and *-receive, same happens. if no changes, then no hook is run (what makes sense if I were using hooks for what they are intended and not for triggering a build process even if there are no changes...)
    – Matt
    Commented Nov 16, 2012 at 12:49
  • This seems a weird requirement. If you push, and there is nothing to push, nothing happens on the server. Why would you want to trigger something because nothing happened? Could you explain what problem you are trying to solve?
    – sleske
    Commented Apr 13, 2013 at 10:03
  • @sleske - I am having the same problem, because I'm trying out different combos of 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 to git push and have the remote repo trigger a script regardless, the debugging process would be easier for me (I think :))
    – sdaau
    Commented Apr 22, 2013 at 19:08
  • @sdaau: Can't you just whip up a small script that does the "add, commit, push" dance?
    – sleske
    Commented Apr 23, 2013 at 14:58

2 Answers 2

1

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.

2
  • How will this work? If you have a pre-receive hook with exit 1, all pushes will fail. Also, this means the client must execute a special push command, not just plain git push. And if the client must run a special command anyway, why not just have it directly trigger whateve the no-op push was supposed to trigger?
    – sleske
    Commented Apr 13, 2013 at 10:05
  • 1
    @sleske: The pre-receive hook receives the name non-existing-branch on its stdin, so the exit 1 can be made conditional. And please note that my answer includes an exit 0 as well. I think the special push arguments (including master) can be made default using git config. It just needs a bit of experimenting using the initial ideas in my answer.
    – pts
    Commented Apr 13, 2013 at 10:48
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.