Created
July 23, 2015 07:09
-
-
Save bartoszmajsak/4eef01fb4cee143b17d5 to your computer and use it in GitHub Desktop.
Git hook to change mvn version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This way you can customize which branches should be skipped when | |
# prepending commit message. | |
if [ -z "$BRANCHES_TO_SKIP" ]; then | |
BRANCHES_TO_SKIP=(master develop test) | |
fi | |
if [ -z "$MAIN_BRANCH" ]; then | |
MAIN_BRANCH="develop" | |
fi | |
PREV_HEAD="$1" | |
NEW_HEAD="$2" | |
BRANCH_SWITCH="$3" | |
if [ -z "$BRANCH_SWITCH" ] || [ $BRANCH_SWITCH -eq 0 ]; then | |
exit 0 # this was a file checkout | |
fi | |
ZERO="0000000000000000000000000000000000000000" | |
if [ "$PREV_HEAD" = "$ZERO" ]; then | |
exit 0 # this was a clone | |
fi | |
BRANCH_NAME=`git rev-parse --abbrev-ref HEAD` | |
git show-ref --verify --quiet refs/heads/$BRANCH_NAME | |
if [ $? -ne 0 ]; then | |
# not a branch | |
exit 0 | |
fi | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
BRANCH_NAME="${BRANCH_NAME##*/}" | |
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$") | |
# Switching version based on the branch name | |
if ! [ $BRANCH_EXCLUDED -eq 1 ]; then | |
cd `git root` | |
# get current version of the top level pom | |
CURRENT_VERSION=$(git cat-file blob $MAIN_BRANCH:pom.xml | mvn --non-recursive help:evaluate -Dexpression=project.version --file /dev/stdin | grep -v '\[.*') | |
# extract version suffix | |
PREFIX=$(echo $CURRENT_VERSION | cut -d \- -f 1) | |
# build new version | |
NEW_VERSION=$PREFIX-$BRANCH_NAME-SNAPSHOT | |
BRANCH_VERSION=$(git cat-file blob HEAD:pom.xml | /neo/apps/apache-maven/bin/mvn --non-recursive help:evaluate -Dexpression=project.version --file /dev/stdin | grep -v '\[.*') | |
# run maven versions plugin to set new version | |
if ! [ "$BRANCH_VERSION" = "$NEW_VERSION" ]; then | |
echo "Changing artifact version from '[$CURRENT_VERSION]' to [$NEW_VERSION]" | |
mvn versions:set -DgenerateBackupPoms=false -DnewVersion=$NEW_VERSION | |
echo "New artifact version [$NEW_VERSION] for branch [$BRANCH_NAME]" | |
COMMIT_MSG="Changed artifact version from '[$CURRENT_VERSION]' to [$NEW_VERSION]" | |
git commit -am"$COMMIT_MSG" | |
fi | |
cd - | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment