From c81e6e68c8c18652e87ce8923383954e20fd081c Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 24 Aug 2019 16:30:37 +0400 Subject: [PATCH] semver_git_tag --- bash.bashrc | 1 + tools/semver_git_tag | 104 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100755 tools/semver_git_tag diff --git a/bash.bashrc b/bash.bashrc index 8ac76a9..8028e8b 100644 --- a/bash.bashrc +++ b/bash.bashrc @@ -160,3 +160,4 @@ if [ -f ~/.bash_aliases ]; then fi #PATH=$HOME/.bin:$HOME/DEV/debian-server:$PATH +PATH=$PATH:/opt/debian-bash/tools diff --git a/tools/semver_git_tag b/tools/semver_git_tag new file mode 100755 index 0000000..98e7612 --- /dev/null +++ b/tools/semver_git_tag @@ -0,0 +1,104 @@ +#!/bin/bash + +function usage { + echo 'usage: --major | -M | --minor | -m | --patch | -p' + exit -1 +} + +function setCommand { + #arg1 input, #arg2 return value + declare -n ret=$2 + + case $1 in + -M|--major) + ret='major' ;; + -m|--minor) + ret='minor' ;; + -p|--patch) + ret='patch' ;; + *) # unknown option + echo + echo 'unknown option, quitting' + exit -1 + ;; + esac +} + + +############# main + +# check more than one argument +if [[ $# -gt 1 ]];then usage ;fi + +# check git covered +REPOSITORY=`git config --get remote.origin.url` +if [[ $? -ne 0 ]]; then + echo 'folder not covered by git' + exit -1 +fi + +# check latest tagged version +LAST_TAG=`git rev-list --tags --max-count=1 2>/dev/null` +if [[ $? -eq 0 ]]; then + VERSION=`git describe --tags $LAST_TAG` +else + echo 'not yet tagged!' + VERSION=0.0.0 +fi + +# check belongs to git.artcode.re +if [[ ! ("$REPOSITORY" =~ ^git@artcode.re+) ]]; then + echo "$REPOSITORY => $VERSION" + echo 'folder not covered by git.artcode.re, read-only' + exit -1 +fi + +# split into array +VERSION_BITS=(${VERSION//./ }) +MAJOR=${VERSION_BITS[0]} +MINOR=${VERSION_BITS[1]} +PATCH=${VERSION_BITS[2]} + +REPO_NAME=`basename $REPOSITORY` +REPO_NAME=${REPO_NAME%.git} +if [[ $VERSION != '0.0.0' ]];then + echo "LATEST VERSION of '$REPO_NAME' is '$MAJOR.$MINOR.$PATCH'" + COMMIT_ID=`git log --format="%H" -n 1` + if [[ $COMMIT_ID == $LAST_TAG ]]; then + echo "cannot tag twice the same commit id : $LAST_TAG" + exit -1 + fi +fi + +COMMAND="${1:-ask}" +if [[ $COMMAND == 'ask' ]];then + echo -n "Press 'M' for Major, 'm' for minor, 'p' for patch ? " + read -n1 input + echo + COMMAND="-$input" +fi + +setCommand $COMMAND COMMAND + +case $COMMAND in + "major") + let "MAJOR++" + let "MINOR=0" + let "PATCH=0" + ;; + "minor") + let "MINOR++" + let "PATCH=0" + ;; + "patch") + let "PATCH++" + ;; +esac + +set -e # EXITS when any error occurs + +TAG="$MAJOR.$MINOR.$PATCH" +echo "tagging as $TAG ..." +git tag $TAG +git push origin master --tags +echo done