Skip to content

Instantly share code, notes, and snippets.

@toopay
Created March 23, 2014 18:48
Show Gist options
  • Save toopay/9727687 to your computer and use it in GitHub Desktop.
Save toopay/9727687 to your computer and use it in GitHub Desktop.
Git SemVer release savant
#!/bin/bash
# Git Tag SemVer helpers
# Usage : !release <repo> <strategy>
# !release some_repo patch|minor|major
# Get arguments of repository and release strategy...
REPO=$1
if [ "" == "$2" ]
then
STRATEGY="patch"
else
STRATEGY=$2
fi
case "$STRATEGY" in
"patch") echo "Preparing $REPO patch release."
;;
"minor") echo "Preparing $REPO minor release."
;;
"major") echo "Preparing $REPO major release."
;;
*) echo "Invalid strategy : $2, using patch strategy as failover. Preparing $REPO patch release."
STRATEGY="patch"
;;
esac
# Initialization
ROOT_DIR=$(eval echo $HOME)
REPO_DIR="$ROOT_DIR/$REPO"
if [ ! -d "$REPO_DIR" ]; then
# Attempt to fetch
echo "Attempt to fetch $REPO"
cd $ROOT_DIR;git clone git@github.com:your-github-username/$REPO.git --quiet
if [ $? -eq 0 ]
then
echo "Caching new repo : $REPO"
else
echo "Invalid repo ($REPO), aborted."
exit
fi
else
# Update
echo "Updating to latest revision"
{
cd $REPO_DIR
git checkout master
git reset --hard
git fetch origin
git rebase origin/master
} &> /dev/null
fi
cd $REPO_DIR
# Get necessary information
CURRENT_VERSION_STRING=(`git describe --tags $(git rev-list --tags --max-count=1)`)
CURRENT_VERSION=(`echo $CURRENT_VERSION_STRING | tr '.' ' '`)
V_MAJOR_STRING=${CURRENT_VERSION[0]}
# Determine semver comps
V_MAJOR=`echo $V_MAJOR_STRING | sed 's/v//'`
V_MINOR=${CURRENT_VERSION[1]}
V_PATCH=${CURRENT_VERSION[2]}
OLD_VERSION="$V_MAJOR_STRING.$V_MINOR.$V_PATCH"
# Validating old version against latest head hash
OLD_VERSION_HASH=(`git rev-list $OLD_VERSION | head -n 1`)
HEAD_HASH=(`git log --pretty=format:"%H" -n 1`)
DIFF=(`git log --pretty=format:"* %s %h" $OLD_VERSION_HASH..$HEAD_HASH`)
if [ "" == "$DIFF" ]; then
# No new changes yet
echo "There are no new commits since $OLD_VERSION"
exit
fi
case "$STRATEGY" in
"patch")
V_NEW_MAJOR="$V_MAJOR"
V_NEW_MINOR="$V_MINOR"
V_NEW_PATCH=$((V_PATCH + 1))
;;
"minor")
V_NEW_MAJOR="$V_MAJOR"
V_NEW_MINOR=$((V_MINOR + 1))
V_NEW_PATCH="0"
;;
"major")
V_NEW_MAJOR=$((V_MAJOR + 1))
V_NEW_MINOR="0"
V_NEW_PATCH="0"
;;
esac
NEW_VERSION="v$V_NEW_MAJOR.$V_NEW_MINOR.$V_NEW_PATCH"
echo "Current Version: $OLD_VERSION"
echo "Target Version: $NEW_VERSION"
# Creating the tag
{
git tag $NEW_VERSION
git push origin --tags
} &> /dev/null
echo "Tag $NEW_VERSION created."
echo "All Done! "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment