Skip to content

Instantly share code, notes, and snippets.

@castamir
Created September 6, 2014 23:22
Show Gist options
  • Save castamir/c47a5f0b75468734addd to your computer and use it in GitHub Desktop.
Save castamir/c47a5f0b75468734addd to your computer and use it in GitHub Desktop.
Client side new git version release written in bash
#!/bin/sh
MASTER="master"
DEV="deploy"
VENDOR_DIR="libs/composer" #default composer vendor dir is just vendor
OS_NAME=`uname`
OS=${OS_NAME:0:5}
if [ $OS == "MINGW" ]; then #windows GitBash
NULL_OUTPUT="nul"
PHP_INI="win"
else
NULL_OUTPUT="/dev/null"
PHP_INI="unix"
fi
#test for unstaged changes
count=(`git status | grep -c 'nothing to commit, working directory clean'`)
if [ $count -ne 1 ]; then
echo "Unstaged changes"
exit
fi
#update dependencies and test
composer update
$VENDOR_DIR/bin/tester tests -s -c tests/php-${PHP_INI}.ini
#fetch
git fetch > $NULL_OUTPUT 2> $NULL_OUTPUT
#duplicate detection
version=`git describe --abbrev=0 --tags`
tagHash=`git rev-list $version | head -n 1`
commitHash=`git rev-parse HEAD`
if [ $tagHash == $commitHash ]; then
echo "Already up-to-date!"
exit
fi
#get current branch
branch=(`git rev-parse --abbrev-ref HEAD`)
#pull
git checkout $MASTER > $NULL_OUTPUT 2> $NULL_OUTPUT
git pull > $NULL_OUTPUT 2> $NULL_OUTPUT
#test merge
exists=`git show-ref refs/heads/$DEV`
if [ -n "$exists" ]; then
git branch -D $DEV > $NULL_OUTPUT
fi
git checkout -b $DEV > $NULL_OUTPUT 2> $NULL_OUTPUT
conflicts=(`git merge $branch | grep -c 'CONFLICT'`)
#test for no conflicts
if [ $conflicts -ne 0 ]; then
echo "Merge conflict detected";
git merge --abort
git checkout -- .
git checkout $branch
exit
fi
echo "No conflict found -- OK --"
#merge
git checkout -- . > $NULL_OUTPUT
git checkout $MASTER > $NULL_OUTPUT 2> $NULL_OUTPUT
git merge $branch > $NULL_OUTPUT
git commit -am "merge with branch $branch" > $NULL_OUTPUT
git push
echo "Synced with upstream -- OK --"
#version parsing
vp=(`echo $version | tr '.' ' '`)
release=${vp[0]}
major=${vp[1]}
minor=${vp[2]}
#new version preparation
if [ "$1" = "major" ]; then
major=$((major+1))
minor=0
elif [ "$1" = "minor" ]; then
minor=$((minor+1))
else
minor=$((minor+1))
fi
newVersion="$release.$major.$minor"
#get all commit messages between last tag and the current commit
message=`git log --format=%B $tagHash...$commitHash`
#create new tag
git tag -a $newVersion -m "$message"
git push --tags > $NULL_OUTPUT 2> $NULL_OUTPUT
echo "Version $newVersion released!"
#return to the starting branch
git checkout $branch > $NULL_OUTPUT 2> $NULL_OUTPUT
git merge $MASTER > $NULL_OUTPUT 2> $NULL_OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment