Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Leveraging Git Hooks

DZone's Guide to

Leveraging Git Hooks

Here we take a look at this developer's solution to utilizing an open source project that does not have tagging support from Github.

· Open Source Zone ·
Free Resource

DON’T STRESS! Assess your OSS. Get your free code scanner from FlexeraFlexNet Code Aware scans Java, NuGet, and NPM packages.

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 the 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.

#!/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 Git Hooks

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.

Git Hooks to Process My Latex Resume:

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

Following is my pre-commit script:

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





Try FlexNet Code Aware Today! A free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and license compliance issues.

Topics:
linux ,git ,latex ,jekyll ,blog ,git hook ,resume

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}