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.
```shell #!/usr/bin/env groovy
// Arguments for git log command. def logArgs = ['max-count': '1', 'pretty': 'format:%an commited %s