Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active March 24, 2020 04:03
Show Gist options
  • Save afiqiqmal/eba12b75af00e39e137f7ff27e01be27 to your computer and use it in GitHub Desktop.
Save afiqiqmal/eba12b75af00e39e137f7ff27e01be27 to your computer and use it in GitHub Desktop.
Pre commit Versioning
  1. Create a file at the root directory and save as name .version.json
{
    "version": "3",
    "build_number": "3.21.7"
}
  1. Go to project folder .git/hooks/pre-commit and paste the code here

  2. Then run git commit -m "Initial Commit"

Notes: this sample need jq. please install first

#!/bin/sh
if ! type "jq" > /dev/null; then
echo "Please install jq command to commit. run command 'brew install jq'"
exit 1
fi
currentVersion=$(jq .version .version.json | bc -l)
currentBuildNumber=$(jq .build_number .version.json | bc -l)
echo "================================================"
echo "Current version is: $currentVersion"
echo "Current build version is: $currentBuildNumber"
echo "================================================"
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
read -p "Please select the level of code changes? 1-Bug fix 2-Minor 3-Major [1]: " level
echo "\n";
if [ "$level" = "" ]; then
level=1
fi
case $level in
[1] )
break
;;
[2] )
break
;;
[3] )
break
;;
* )
echo "Enter the correct number lah! Itu pun susah nak ikut ke? Hanat sungguh"
exit 1
;;
esac
minor=$(echo $currentBuildNumber | awk -F'.' '{print $2}')
bug=$(echo $currentBuildNumber | awk -F'.' '{print $3}')
if [ "$level" = 3 ]; then
nextVersion=$(( $currentVersion + 1 ))
echo "\n";
read -p "Current version is $currentVersion. Increase version number to $nextVersion? (Y|n) :" yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] )
currentVersion=$nextVersion
echo "Version increase to $nextVersion"
echo "\n";
break
;;
[Nn] )
exit
;;
* )
echo "Please answer Y or N lah. Itu pun susah nak ikut ke? Hanat sungguh"
exit 1
;;
esac
minor=0
bug=0
fi
if [ "$level" = 2 ]; then
minor=$(( $minor + 1 ))
bug=0
fi
if [ "$level" = 1 ]; then
bug=$(( $bug + 1 ))
fi
currentBuildNumber="$currentVersion.$minor.$bug"
echo "Build number change to $currentBuildNumber"
echo "\n";
sleep 5
git add .
echo "You can push now. :)"
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment