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:

###ewd!xho/Aeinblcleilerceno<eas/wtasabs/rdecadu[[suee-$YNhsv;pyyn)/n]]ttd"**eotoRicyun))hrnoeceaCox"domiPmptlupo;esos;aesesrereriianinnpnssustwttaealrbllel;yleoapswnh,dporaAasspnsspoie/.gtc"nio;scn;ssDotuldmeipn?ast[soYe/tknie]cy::bdo"uamrypdn;break;;

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

Automação Residencial usando PHP

Automação Residencial usando PHP

  • January 18, 2007

Ao concluir meu curso de graduação, Engenharia da Computação, desenvolvi este projeto final. Desde que iniciei o blog me prometi postar um artigo sobre o mesmo e disponibilizar o projeto na sua integridade, então agora sim cumpro minha promessa.

Read More
Desenvolvendo plugins para WordPress

Desenvolvendo plugins para WordPress

  • March 10, 2008

Uma peça chave do sucesso do WordPress é justamente sua capacidade de aceitar plugins e widgets, além , é claro, dos milhares de programadores que desenvolvem os mais diversos plugins que fazem de tudo um pouco.

Algum tempo atrás fui abordado pelo Manoel Lemos do BlogBlogs para discutirmos uma missão, criar um plugin para o WordPress, baseado na API do site. Comecei entao minha saga atrás de como desenvolver um plugin. O material estava lá, espalhado em vários sites, mas estava lá.

Então agora com o sucesso do BBUinfo e na véspera de novos projetos, decidi tentar consolidar este conhecimento adquirido e focar ele no público brasileiro. Vou procurar mostrar o caminho das pedras de como começar e onde buscar os dados necessário para integrar seu código ao do WP.

Read More
Problem solving technique #1: Taking a mental break

Problem solving technique #1: Taking a mental break

  • February 4, 2011

Developers are modern day artists whose masterpieces are not hung on walls but stretched out thin on web servers all over the world, yes that is very poetic, but I really try to look at developers as artists and puzzle solvers. To become better developers your skill-set must include creativity and problem solving skills and of course a artistic touch.

Read More