diff --git a/src/git-tag-version b/src/git-tag-version index 62713c0..c1ba84d 100755 --- a/src/git-tag-version +++ b/src/git-tag-version @@ -42,7 +42,7 @@ function help SYNOPSIS git-tag-version [command] [-m|--message=] [-x|--suffix=] - [-s|--strict] [-n|--non-strict] [-f|--force] + [-s|--strict] [-n|--non-strict] [-f|--force] [] DESCRIPTION A script to use git tags for repository version numbers through a simple @@ -114,7 +114,7 @@ COMMANDS new [-m|--message=] [-x|--suffix=] [-s|--strict] - [-n|--non-strict] + [-n|--non-strict] [] Create a new git version number tag by a simple increment to the given version part major, minor or patch. 'gtv new major' on an existing v0.41.1 creates v1.0.0. @@ -125,15 +125,19 @@ COMMANDS suffixs as a suffix to the tag name itself. Do not allow assignment of mulitple version tags to the same commit using 'strict'. + You may assign to any commit of your choice by additionally + supplying a commit hash. set [-m|--message=] [-x|--suffix=] - [-s|--strict] [-n|--non-strict] [-f|--force] + [-s|--strict] [-n|--non-strict] [-f|--force] [] Assign a specific git version number tag, must be strictly increasing. Add the optional message to annotated tags. Add the optional tag suffix as a suffix to the tag name itself. Do not allow assignment of mulitple version tags to the same commit using 'strict'. If you want to assign a not strictly increasing version number you may do so by using 'force'. + You may assign to any commit of your choice by additionally + supplying a commit hash. EXAMPLES # show current version, eg. 1.21.3 @@ -222,16 +226,18 @@ function strictly-increasing-version # create a version number tag # argument 1: short description # argument 2: version number string "major.minor.patch" +# argiment 3: a specific commit hash the version tag is assigned to function create-version-tag { if [ "$STRICT" = true ]; then ensure-strict-mode fi - git tag -a "v$2${SUFFIX}" -m "version $2: $1" -m "${MESSAGE}" + git tag -a "v$2${SUFFIX}" -m "version $2: $1" -m "${MESSAGE}" $3 } # create new version tag # argument 1: version type [major|minor|patch] +# argiment 3: a specific commit hash the version tag is assigned to function new-version { # detect sub-command @@ -241,7 +247,7 @@ function new-version NEW=$(strictly-increasing-version $1 $CURRENT) echo "Current version: $CURRENT" echo "New $1 version: $NEW" - create-version-tag $1 $NEW + create-version-tag $1 $NEW $2 ;; *) echo "ERROR: Unsupported version type '$1', use major, minor or patch!" @@ -278,6 +284,7 @@ function ensure-strict-mode { # set specific version tag # argument 1: version number string "major.minor.patch" +# argument 2: a specific commit hash to assign a version to function set-version { if ! is-gtv-tag "v$1"; then @@ -328,7 +335,7 @@ function set-version if [ "$VERSION_OK" = true ] || [ "$FORCE" = true ]; then echo "New version: $1" - create-version-tag "manually defined version number" $1 + create-version-tag "manually defined version number" $1 $2 else echo "ERROR: $1 is not a strictly increasing version number. Current is $CURRENT." exit 1; @@ -420,10 +427,10 @@ case "$1" in version ;; "new") - new-version $2 + new-version $2 $3 ;; "set") - set-version $2 + set-version $2 $3 ;; *) show diff --git a/test/05-tag-specific-commit.bats b/test/05-tag-specific-commit.bats new file mode 100644 index 0000000..9fec3f4 --- /dev/null +++ b/test/05-tag-specific-commit.bats @@ -0,0 +1,107 @@ +#!/usr/bin/env bats + +# we rely on a environment variable to point us to our unit-under-test +# syntax see http://stackoverflow.com/a/307735 +: "${GTV:?Need to set environment variable 'GTV' to absolute gtv script path}" +if [ ! -x "$GTV" ]; then + echo "Environment variable 'GTV' needs to point to a executable gtv script with absolute path" + exit 1 +fi + +function setup { + export TEST_DIR=$(mktemp -d) + cd $TEST_DIR + + git init + # we need to provide a basic git configuration - on travis there is none + git config user.email "noreply@travis-ci.org" + git config user.name "travis build git user" + + date > file + git add . + git commit -m "initial" +} + +function teardown { + rm -rf $TEST_DIR +} + +@test "create new patch version on specific commit" { + run ${GTV} init + + run date > file + run git add * + run git commit -m "commit to be tagged" + TAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run date > file2 + run git add * + run git commit -m "commit stays untagged" + UNTAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run ${GTV} new patch ${TAGGED_COMMIT} + + run git rev-list -n 1 v0.0.1 + [ "$output" != "$UNTAGGED_COMMIT" ] + [ "$output" = "$TAGGED_COMMIT" ] +} + +@test "create new minor version on specific commit" { + run ${GTV} init + + run date > file + run git add * + run git commit -m "commit to be tagged" + TAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run date > file2 + run git add * + run git commit -m "commit stays untagged" + UNTAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run ${GTV} new minor ${TAGGED_COMMIT} + + run git rev-list -n 1 v0.1.0 + [ "$output" != "$UNTAGGED_COMMIT" ] + [ "$output" = "$TAGGED_COMMIT" ] +} + +@test "create new major version on specific commit" { + run ${GTV} init + + run date > file + run git add * + run git commit -m "commit to be tagged" + TAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run date > file2 + run git add * + run git commit -m "commit stays untagged" + UNTAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run ${GTV} new major ${TAGGED_COMMIT} + + run git rev-list -n 1 v1.0.0 + [ "$output" != "$UNTAGGED_COMMIT" ] + [ "$output" = "$TAGGED_COMMIT" ] +} + +@test "set new specific version on specific commit" { + run ${GTV} init + + run date > file + run git add * + run git commit -m "commit to be tagged" + TAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run date > file2 + run git add * + run git commit -m "commit stays untagged" + UNTAGGED_COMMIT=$(git rev-parse --verify HEAD) + + run ${GTV} set 1.2.3 ${TAGGED_COMMIT} + + run git rev-list -n 1 v1.2.3 + [ "$output" != "$UNTAGGED_COMMIT" ] + [ "$output" = "$TAGGED_COMMIT" ] +}