Using Git Hooks to delegate routine tasks

My team currently uses GitHub as the center of our development process, taking full advantage of Pull Requests and all that. That means we use git and our process is to always develop using feature-branches, making PRs for each feature and then merging them in after code review.

This means we switch branches a lot, either to see someone’s code and review it or to kickoff a new branch for a new feature. Over time this scenario seemed to happen a few times:

  • Switch to master and pull in updates
  • Start working in a new feature in a new branch
  • Write something and run tests or view the site
  • OMG!! everything is broken!
  • turn to team: “I’m getting an error on the stuff you added, did you get it?”
  • team says: “No”
  • me: “ah dammit, did not pull in required dependencies” or “ah dammit, I did not regenerate assets” (this may or not be preceded by hours of pointless debugging)

Basically I keep forgetting that every time I switch branches I should get up-to-date dependencies and compile a new CSS to get all the latest shiny. So in order to make this automatic I decided to setup a git hook that did that for me.

Creating a Git Hook

Git has lots of hook points, so just pick one that fits your flow. In my case I picked the post-checkout hook, since I want this to happen every time I switch to another branch.

To create a new hook, you need to create a file in the .git/hooks folder with the name of the hook you want to attach to, in my case post-checkout, make sure this file is executable and then code your hook in shell script format.

chmod +x .git/hooks/post-checkout

Writing a on-demand hook

Sometimes you just flip between branches to check up on things and get some updated code, so you may not always need to run composer and install assets, so I also baked in a confirmation step to allow me to abort in case I’m doing something else.

For now all I needed was to run Composer and generate my assets (less to css and such), so this is what I got:

#!/bin/bash
#

# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty

while true; do
    read -p "Run Composer install and Assetic Dump? [Y/n]: " yn
    case $yn in
        [Yy]* ) composer install; php app/console assetic:dump; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

Wrap up

Git Hooks are pretty cool, if your workflow is build around git like lots of team do nowadays the hooks are a cool place to tie in your day to day stuff that tends to be forgotten and generate unneeded debugging and such. Take a look at available hooks and think about your routine, I’m pretty sure you will find something you can delegate out to git and stop worrying about it everyday.

0 thoughts on “Using Git Hooks to delegate routine tasks

  1. Hi,

    the problem with git hooks is that they are local. So, if you have a team of people working on the same project, you'll need to find a way to share this hooks and write them in a way that is portable enough (git runs on several OSes). Doable, but not elegant.

    A better way to go, IMHO, is a build script. Better even use something like phing (http://www.phing.info/), which has a layer of abstraction for cross-platform support. Then, getting the project updates is just a single command like 'phing' or 'phing update', which will pull changes, clear cache, update DB, and do anything else you want it to. And your build script can be a part of the project's source code too. So now every member of the team runs the same command to update his version.

  2. This is my first look about such kind of writing concept. That is very difference and handy to me for my writing career. This find more also very handy one look to me for more writing help.

Leave a Reply