Note: Most of these blogs are for my personal reference and at a given time, some of those might just be unpolished drafts.

Leveraging git hooks

Adding tag support to Jekyll Now

This blog is a forked version of a popular github blog project called Jekyll Now. It’s based on ruby and is a really elegant and easy to use tool that I have found. Being bare minimum version it doesn’t have tagging support for blog posts.

I have added tags to all the blog posts. This particular plugin isn’t supported by github so running it just like that doesn’t create ‘tags’. A workaround is to create tags locally (jekyll serve will do it) and then pushing those contents to the github repo.

I have added a small python script under pre-commit git-hook in order to automate this so that all of my posts and their tags are automatically added to my git staging area and subsequently pushed to github.

In order to use this git-hook, create a file .git/hooks/pre-commit and populate it with the following content and make it executable using sudo chmod +x .git/hooks/pre-commit.

Shell Version:

#!/bin/bash
rsync -arv  _site/tag/* ./tag/
git add tag/

This means before committing it will copy all the contents from _site/tag and keep it inside tag which is then added to staging area using git add.

(Used python instead of shell or perl for simplicity)

#!/usr/bin/python
import shutil
import git
shutil.move('_site/tag','.')
repo = git.Repo('.')
repo.git.add('tag/')

Power of githooks

Most of us developers use git hooks on server side mostly as webhooks added in repositories like github or bitbucket. They play a crucial role in facilitating modern development practices like Continuous Integration(CI) and Continuous Deployment (CD). However we can also leverage the power of git hooks on client side as demonstrated by my use case above. This is just an introductory example. There could be other cases where we manually do certain stuffs repeatedly in git command line. Such tasks can be easily orchestrated using git hooks. Any scripting language is supported and thus we can even incorporate high level abstractions using languages like python.

Githooks in git repositories

All the git hosting repositories like github, bitbucket, gitlab, etc implement git hooks on server side for validation purposes. One popular and commonly used example is pre-recieve hook which gets triggerred when objects are pushed to remote but just before the refereces are updated. This helps ensure maintain authorization like who can update remote branch references and so on.

remote: permission denied to update branch $branchName
To git@bitbucket.org:someRemote.git
 ! [remote rejected] $branchName -> $branchName (pre-receive hook declined)
error: failed to push some refs to 'git@bitbucket.org:someRemote.git'

These kind of hooks can be configured if one is maintaining their own git server too so that all the update request to git repository servers are validated (authorization,configuration,coding-standard,etc)

Git hooks to process my latex resume

I have used client side githook to process my latex resume too. After some changes in .latex file those have to be compiled, href links enabled and then converted to pdf. These are just the type of mundane repeatitive tasks. Adding them to a shell script and executing it just before committing was also doing fine but by incorporating that in my githooks I was able to eliminate that extra step.

.git/hooks/pre-commit

latex resume.tex #latex compilation
dvipdfm resume.dvi #dvi conversion
dvips resume.dvi #enabling hyperlinks
ps2pdf resume.ps
git add -A #adding to staging area for commit
Written on July 2, 2018