#!/bin/bash

## Automatically generate a file with git branch and revision info
##
## Example:
##   [master]v2.0.0-beta-191(a830382)
## Install:
##   cp git-create-revisioninfo-hook.sh .git/hooks/post-commit
##   cp git-create-revisioninfo-hook.sh .git/hooks/post-checkout
##   cp git-create-revisioninfo-hook.sh .git/hooks/post-merge
##   chmod +x .git/hooks/post-*
LC_ALL=C

local_branch="$(git rev-parse --abbrev-ref HEAD)"

valid_branch_regex="^(debian|redhat)\/[a-z0-9._-]+$"

if [[ ! $local_branch =~ $valid_branch_regex ]]
then
    #skip the hook if we are on a debian or redhat branch
    exit 0
fi

FILENAME='IIRrational/auto_version.py'

#exec 1>&2
branch=`git rev-parse --abbrev-ref HEAD`
shorthash=`git log --pretty=format:'%h' -n 1`
longhash=`git log --pretty=format:'%H' -n 1`
latesttag=`git describe --tags --abbrev=0`

cat << EOF > $FILENAME
"""
AUTOGENERATED by git post-commit hook. Note that these are always from the PREVIOUS commit, and so may be out of date by one log entry
"""
git_branch    = "$branch"
git_shorthash = "$shorthash"
git_longhash  = "$longhash"
git_last_tag  = "$latesttag"
EOF

git add $FILENAME
