versiontag updated. Supports remove command and --force option
Tags: tools, git, bash
Published: 2018-07-29 10:11:19

---
title: versiontag updated. Supports remove command and --force option
published: true
description: A side-project to create a simple tool to maintain semantic versioning
tags: tools, git, bash
---

Some days ago I published a simple utility to manage semantic versioning in git projects.

{% link https://dev.to/franiglesias/automate-semantic-versioning-in-git-ah9 %}

Today, I've just updated it to manage a `--force` option to avoid the confirmation questions and do the tagging in one shot.

Also, I've added a `remove` command that helps to remove the last tag, useful if you tag the development without being properly done.

The project is here

{% github franiglesias/versiontag %}

And this is the current code for the utility. Feel free to suggest any improvement. I'm learning bash scripting so it would be great to have any help to better organize the code and improve it in any way.

```bash
#!/bin/bash

function execute {
    cmd=("$@")
	echo
    echo "Executing ${cmd[*]} ..."
	echo
    # execute the command:
    "${cmd[@]}"
}

function confirm() {
    read -p "$1 ([y]es or [N]o): "
    case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
        y|yes) echo "yes" ;;
        *)     echo "no" ;;
    esac
}

function getMessage() {
	if [[ -z "$@" ]]
	then
		echo false;
	fi
	
	echo $@
}

function showHelp() {
	echo
	echo "versiontag"
	echo "=========="
	echo
	echo "Automates version tag creation."
	echo 
	echo "Commands: patch|minor|major|current 'Tag message'"
	echo "          remove"
	echo "          current"
	echo
	echo "Examples:"
	echo "---------"
	echo 
	echo "Show current version: ./versiontag current"
	echo "Patch version:        ./versiontag patch 'Fix broken view'"
	echo "Minor version:        ./versiontag minor 'Add Customer filter by email'"
	echo "Major version:        ./versiontag major 'Blog module'"
	echo "Remove last version:  ./versiontag remove"
	echo
	exit 0
}

function showSuccess() {
	echo
	echo "Tag v$major.$minor.$patch was created in local repository."
	echo
	echo "Push it:"
	echo
	echo "    git push origin v$major.$minor.$patch"
	echo
	echo "Delete tag:"
	echo
	echo "    git tag -d v$major.$minor.$patch"
	echo
}


lasttag=`git describe --tags --long` || true

if [[ -z "$lasttag" ]]; then
	lasttag='v0.0.0'
fi

cleantag=${lasttag%%-*}

parts=(${cleantag//./ })

major=${parts[0]:1}
minor=${parts[1]}
patch=${parts[2]}

major=${major:0}
minor=${minor:0}
patch=${patch:0}

last="v$major.$minor.$patch"

mode=''
force=false

while [ -n "$1" ]
do
	case "$1" 
	in
		-f|--force)
			force=true;;
		-h|--help)
			showHelp
			exit 0;;
		patch) 
			patch=$((patch + 1)) 
			msg=$(getMessage "$2")
			shift 1
			mode='patch';;
		minor) 
			minor=$((minor + 1))
			patch=0 
			msg=$(getMessage "$2")
			shift 1
			mode='minor';;
		major) 
			major=$((major + 1)); minor=0
			patch=0 
			msg=$(getMessage "$2")
			shift 1
			mode='major';;
		current)
			echo "Current version: $last"
			exit 0;;
		remove)
			if [[ "yes" == $(confirm "Do you want to remove $last?") ]]; then
				execute git tag -d "$last"
				execute git push origin ":$last"
			fi
			exit 0;;
		help)
			showHelp;;
		*)
			echo "$1 option is not valid"
			exit 1;;
	esac
	
	shift 1;
done

if [[ false == "$msg" ]]; then
	echo
	echo "No message."
	exit 1;
fi

if [[ -z "$mode" ]]; then
	showHelp;
	exit 0;
fi	 

echo "Current version : $last"
echo "New tag         : v$major.$minor.$patch"
echo
echo "Message         : $msg"

if [[ "$force" = true ]]; then
	execute git tag -a "v$major.$minor.$patch" -m "$msg ($mode version)"
	execute git push origin "v$major.$minor.$patch"
	echo 
	echo "Version tag created and pushed"
	exit 0;
fi

if [[ "no" == $(confirm "Do you agree?") ]]; then
	echo
	echo "No tag was created"
	exit 0;
fi

execute git tag -a "v$major.$minor.$patch" -m "$msg ($mode version)"
	
showSuccess;
	
if [[ "yes" == $(confirm "Do you want to push this tag right now?") ]]; then
	execute git push origin "v$major.$minor.$patch"
fi

exit 0
```