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.

comments powered by Disqus

Related Posts

Review: Essential PHP Security

Review: Essential PHP Security

  • January 26, 2009

Even having being published in 2005, the book “Essential PHP Security” addresses a very up-to-date topic even today.

Read More
Blogosfera em destaque

Blogosfera em destaque

  • July 31, 2006

Me chamou atenção a capa da Época deste final de semana que destacava a palavra “Blogs”

Read More
Análise: Essential PHP Security

Análise: Essential PHP Security

  • January 26, 2009

Embora publicado em 2005, o livro “Essential PHP Security” trata de um assunto que até hoje é muito atual.

Read More