Skip to content

Instantly share code, notes, and snippets.

@refo
Last active August 29, 2015 14:26
Show Gist options
  • Save refo/30228b968d19a9a2138f to your computer and use it in GitHub Desktop.
Save refo/30228b968d19a9a2138f to your computer and use it in GitHub Desktop.
Git version and revision information hooks

Write current version and revision to file

Uses this example format: v2.1.102 (13a02b0) where

  • v2.1 is last tag
  • 102is the number of commits made
  • 13a02b0 is the shortened hash of the last commit

If repo is not tagged, v0 will be used as default.

Local git repo

Write version info to VERSION file in the repo root

.git/hooks/post-commit

#!/bin/bash

VER=`git describe --tags 2>/dev/null`
VER=${VER:-v0}
REVHASH=`git rev-parse --short HEAD`
REV=`git log --oneline | wc -l | tr -d " "`
echo "$VER.$REV ($REVHASH)" > VERSION

Remote deployment repo

You may ignore this example as it also includes a line for checkout as a part of my deployment process.

Write version info to VERSION file in the checkout directory defined by $DIR variable. $DIR variable is the live directory in the example below, which suits very well to my deployment process described my git-deploy-checkout.md gist.

/home/user/website.com/stag/git/hooks/post-receive

#!/bin/sh

DIR=../live

GIT_WORK_TREE=$DIR git checkout -f

VER=`git describe --tags 2>/dev/null`
VER=${VER:-v0}
REVHASH=`git rev-parse --short HEAD`
REV=`git log --oneline | wc -l | tr -d " "`
echo "$VER.$REV ($REVHASH)" > $DIR/VERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment