Skip to content

Instantly share code, notes, and snippets.

@sanketsaurav
Created September 24, 2015 07:09
Show Gist options
  • Save sanketsaurav/be224658129918cda841 to your computer and use it in GitHub Desktop.
Save sanketsaurav/be224658129918cda841 to your computer and use it in GitHub Desktop.
SemVer for Git implemented as pre-commit hook
#!/bin/bash
# Allows us to read user input below, assigns STDIN to keyboard
exec < /dev/tty
git rev-parse --show-toplevel | cd
file="VERSION"
if [ -f "$file" ]
then
echo "$file file found."
version=$(<$file)
echo "$version"
a=( ${version//./ } ) # replace points, split into array
((a[2]++)) # increment revision (or other part)
version="${a[0]}.${a[1]}.${a[2]}"
echo "$version"
echo "$version" > $file
else
echo "$file file not found."
echo "Touching file VERSION..."
touch $file
echo "Please set an initial version in format major.minor.patch (defaults to 0.0.1)"
if read version && [ -z "$version" ]
then
echo "Setting project version to 0.0.1"
echo "0.0.1" > $file
else
while [[ !( $version =~ ^(\d+\.)?(\d+\.)?(\*|\d+)$ ) ]]
do
echo "Version string should be major.minor.patch (defaults to 0.0.1)"
read version
done
echo "Setting project version to $version"
echo "$version" > $file
fi
fi
git add $file
# Closing STDIN
exec <&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment