Last active
September 7, 2018 08:31
-
-
Save matusnovak/eba81343f5578c38cf0a9bc22b548727 to your computer and use it in GitHub Desktop.
Calculate the next Git tag for your project on AppVeyor
This file contains hidden or 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
REM This tiny Windows script will calculate the next version number for | |
REM your Git project using Git tags. This is meant for CI such as AppVeyor. | |
REM | |
REM For example: | |
REM You create a Git project with an initial commit and a tag "v1.0.0" | |
REM Running this script will do nothing in that case (because there are | |
REM no new commits since the latest tag). | |
REM But, if you create another commit and run this script, the calculated | |
REM version (the next tag) will be v1.0.1. This is calculated based | |
REM on the latest git commit and if the latest tag points to the commit hash. | |
REM If the latest tag was created before the latest commit, the tag is | |
REM incremented. (No actual tag is created! Only a new number calculated). | |
@ECHO OFF | |
SETLOCAL | |
for /f %%i in ('git rev-parse HEAD') do SET LATEST_COMMIT_HASH=%%i | |
echo Hash: %LATEST_COMMIT_HASH% | |
git describe --abbrev=0 --tags --exact-match %LATEST_COMMIT_HASH% || goto :increment | |
goto :usecurrent | |
:increment | |
echo Incrementing tag... | |
for /f %%i in ('git describe --abbrev^=0 --tags') do SET LATEST_TAG=%%i | |
echo Latest tag: %LATEST_TAG% | |
for /F "tokens=1,2,3 delims=.-" %%a in ("%LATEST_TAG%") do ( | |
SET MAJOR=%%a | |
SET MINOR=%%b | |
SET PATCH=%%c | |
) | |
set /A PATCH+=1 | |
set GITHUB_RELEASE_TAG=%MAJOR%.%MINOR%.%PATCH% | |
echo GitHub Release tag: %GITHUB_RELEASE_TAG% | |
goto :end | |
:usecurrent | |
echo Using the same tag... | |
for /f %%i in ('git describe --abbrev^=0 --tags') do SET LATEST_TAG=%%i | |
echo Latest tag: %LATEST_TAG% | |
set GITHUB_RELEASE_TAG=%LATEST_TAG% | |
echo GitHub Release tag: %GITHUB_RELEASE_TAG% | |
:end | |
appveyor SetVariable -Name APPVEYOR_REPO_TAG_NAME -Value %GITHUB_RELEASE_TAG% |
This file contains hidden or 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 | |
set -e | |
LATEST_COMMIT_HASH=`git rev-parse HEAD` | |
echo "Hash: $LATEST_COMMIT_HASH" | |
if git describe --abbrev=0 --tags --exact-match $LATEST_COMMIT_HASH ; then | |
echo "Using the same tag..." | |
LATEST_TAG=`git describe --abbrev=0 --tags` | |
GITHUB_RELEASE_TAG="$LATEST_TAG" | |
else | |
echo "Incrementing tag..." | |
LATEST_TAG=`git describe --abbrev=0 --tags` | |
echo "Latest tag: $LATEST_TAG" | |
tokens=(${LATEST_TAG//./ }) | |
MAJOR=${tokens[0]} | |
MINOR=${tokens[1]} | |
PATCH=${tokens[2]} | |
PATCH=$((PATCH+1)) | |
GITHUB_RELEASE_TAG="$MAJOR.$MINOR.$PATCH" | |
echo "GitHub Release tag: $GITHUB_RELEASE_TAG" | |
git tag -a "$GITHUB_RELEASE_TAG" -m "$GITHUB_RELEASE_TAG" | |
fi | |
export GITHUB_RELEASE_TAG=$GITHUB_RELEASE_TAG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment