Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple commands are not working in git post-receive

I'm using git with trac. After push I want two thing to be done:

  1. Sending email to development team with diff
  2. If there is some special phrase in commit message (like "see #1"), then I want the commit message to be placed in trac ticket.

The first thing is solved by git-commit-notifier. It works perfectly after I have created post-receive hook:

#!/bin/sh

/var/lib/gems/1.8/bin/git-commit-notifier /etc/git-commit-notifier.yml

My second requirement can be solved as discribed at http://trac-hacks.org/wiki/GitPlugin#post-receivehookscripts. It also works perfectly with such post-receive hook:

#!/bin/sh

/var/trac/testgit/commit-updater

Both 2 things works when they are separate. But I need to combine them. So I have created post-receive hook:

#!/bin/sh

/var/trac/testgit/commit-updater
/var/lib/gems/1.8/bin/git-commit-notifier /etc/git-commit-notifier.yml

It is very funny, but this is not working. The commands run perfectly well when the run separately, but only first one works when they are placed into post-receive hook.

If I have such hook:

#!/bin/sh

/var/trac/testgit/commit-updater
/var/lib/gems/1.8/bin/git-commit-notifier /etc/git-commit-notifier.yml

I do receive the following error

/var/lib/gems/1.8/gems/git-commit-notifier-0.8.0/bin/git-commit-notifier:12: undefined method `strip' for nil:NilClass (NoMethodError)
        from /var/lib/gems/1.8/bin/git-commit-notifier:19:in `load'
        from /var/lib/gems/1.8/bin/git-commit-notifier:19

But if I change to order of this 2 commands I do not receive any errors, but only the first command works.

I will appreciate any help. I'm trying to solve this problem for a long time and I have no ideas.

like image 480
bessarabov Avatar asked Aug 10 '10 11:08

bessarabov


People also ask

What is a Githook?

Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature - no need to download anything. Git hooks are run locally. These hook scripts are only limited by a developer's imagination.

How to enable Git hook?

Implementing Git Hooks Upon initializing a new project, Git populates the hooks folder with template files. To enable the hook scripts, simply remove the . sample extension from the file name. Git will automatically execute the scripts based on the naming.

Can I push Git hooks?

No. Hooks are per-repository and are never pushed.

What is pre commit hook in Git?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


2 Answers

Assuming my comment is correct, and commit-updater is eating all of stdin, this should do the trick:

#!/bin/sh

FILE=`mktemp`
cat - > $FILE
cat $FILE | /var/trac/testgit/commit-updater
cat $FILE | /var/lib/gems/1.8/bin/git-commit-notifier /etc/git-commit-notifier.yml
rm $FILE
like image 117
ngoozeff Avatar answered Sep 29 '22 01:09

ngoozeff


I found ngoozeff's solution useful, but I had to make a few additions. At first, the script should fail if one of the hook fails. At second, some hooks may expect arguments. In my case the gitzilla hook was like that.

For me the following worked for combining gitzilla and gitolite hooks:

#!/bin/sh

FILE=`mktemp`
cat - > $FILE
cat $FILE | $GIT_DIR/hooks/update.gitzilla $* || exit 1 
cat $FILE | $GIT_DIR/hooks/update.gitolite $* || exit 1
rm $FILE

Note the $* and the exit statements. You can also use the $GIT_DIR variable. The update.gitzilla and update.gitolite files are symbolic links.

like image 39
espakm Avatar answered Sep 29 '22 01:09

espakm