Versioning git projects

vi release.sh

#!/bin/sh

# get the current version
current_version=$(cat VERSION)
major=$(echo $current_version | sed -r 's/([0-9]+).([0-9]+).([0-9]+)/\1/g')
minor=$(echo $current_version | sed -r 's/([0-9]+).([0-9]+).([0-9]+)/\2/g')
patch=$(echo $current_version | sed -r 's/([0-9]+).([0-9]+).([0-9]+)/\3/g')
echo "Current version: $major.$minor.$patch"
echo ""


# Update the index
git update-index -q --ignore-submodules --refresh
err=0

# Disallow unstaged changes in the working tree
if ! git diff-files --quiet --ignore-submodules --
then
    echo >&2 "You have unstaged changes:"
    git diff-files --name-status -r --ignore-submodules -- >&2
    err=1
fi

# Disallow uncommitted changes in the index
if ! git diff-index --cached --quiet HEAD --ignore-submodules --
then
    echo >&2 "You have uncommitted changes:"
    git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2
    err=1
fi

if [ $err = 1 ]
then
    echo >&2 "Please commit or stash them."
    exit 1
fi


# up the version
case "$1" in
    --major)
        version="$((major+1)).0.0"
        ;;
    --minor)
        version="$major.$((minor+1)).0"
        ;;
    --patch)
        version="$major.$minor.$((patch+1))"
        ;;
    --version)
        version="$2"
        ;;
    *)
        echo "Usage: ./release.sh [--major|--minor|--patch|--version x.y.z]"
        exit 1
        ;;
esac

if [ "$version" == "$current_version" ]; then
    echo "Version not increased: $current_version -> $version"
    exit 1
fi

echo "$version" > VERSION
echo "Version changed: $current_version -> $version"


# run build
./build.sh

# tag the version
git add -A
git commit -m "version $version"
git tag -a "$version" -m "version $version"
git push
git push --tags