Skip to content

Instantly share code, notes, and snippets.

@micaelbergeron
Last active July 4, 2023 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micaelbergeron/d0b9180415c2c5e992e7 to your computer and use it in GitHub Desktop.
Save micaelbergeron/d0b9180415c2c5e992e7 to your computer and use it in GitHub Desktop.
Git pre-commit hook to automatically write version numbers.
#!/bin/bash
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Author: Micael Bergeron <micael.bergeron@solutionstlm.com>
# Goal:
# Automatic version numbering from the latest tag on the branch.
# The output format is <branch name>-<tag>-<HEAD sha>-<dirty?>
versionfilename=$(git config hooks.versionfilename)
if [[ -z $versionfilename ]]
then
versionfilename=".version"
fi
# Version number
#echo \# Generated using git pre-commit hook > $versionfilename
echo -n $(git rev-parse --abbrev-ref HEAD) > $versionfilename
echo -n "-" >> $versionfilename
echo $(git describe --tags --long --dirty) >> $versionfilename
exec git add $versionfilename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment