Groovy Goodness: Using Groovy for Git Hooks

Git supports hooks, which are scripts that are fired when certain events happens. The scripts are simply shell scripts and we can use Groovy to run those scripts. We must make Groovy the script language with the hash-bang (#!) header in the Git hook script file. And then we are ready to go and use Groovy as the script language for the Git hooks.

Git hooks are placed in the .git/hooks directory of our project. We create an example script that will use growlnotify to create a notification message with information from the Git commit action. growlnotify is a command-line tool for Mac OSX to send out messages to Growl. Other operating systems also have tools to create notification message from the command-line.

We must create the file post-commit in the directory .git/hooks. The file must have execute rights: $ chmod +x post-commit. In the first line of the file we make sure Groovy is used. In the rest of the script we use Groovy to invoke git log and get information about the commit. Then we create a message and invoke growlnotify with the correct arguments so Growl can show the message.

In the following screenshot we see the output shown with Growl:

(Written with Groovy 2.0.4 and Git 1.7.5.4)

Original article

This entry was posted in Coding, Groovy and tagged , by mrhaki. Bookmark the permalink.

About mrhaki

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in. You can contact me via Google+ or @mrhaki.

Leave a Reply

Your email address will not be published. Required fields are marked *