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

Google Maps API: Um exemplo prático e comentado

Google Maps API: Um exemplo prático e comentado

  • December 6, 2006

Esta semana montei um serviço bem simples para poder testar o uso da API do Google Maps. O serviço foi oferecido aos congressistas do evento PHP Conference Brasil 2006 e era bem simples: cada um entrava e deixava marcado seu ponto de origem, com isso obteríamos um mapa demonstrando de onde os participantes vieram e quantos kilometros viajaram.

Ainda restam aplicar algumas funções à este serviço, mas ele serviu como meu teste inicial. Resolvi então abrir o código fonte e o fluxo de dados do serviço para que vocês possam conhecer e quem sabe começar utilizar a Google Maps API também.

Read More
OpenX 2.5 beta - Primeiras impressões

OpenX 2.5 beta - Primeiras impressões

  • March 28, 2008

Foi anunciado hoje o primeiro beta público do OpenX 2.5, a primeira versão puramente OpenX a ser lançada, digo isso por ser a primeira versão que incorpora as milhares de linhas que forma re-escritas, criando quase que praticamente uma nova aplicação. Eu já botei minhas mãos nessa versão e vou descrever a seguir minhas primeiras impressões.

Read More
A new beginning

A new beginning

  • April 16, 2008

I had never really understood the real reason to have a blog and write articles, until my good friend Leonardo França motivated me into creating this blog. Quickly I realized that because of the blog I begun once more to learn new stuff, mainly to be able to write new articles.

Read More