Skip to content

Commit

Permalink
Assign tag version to specific commit by supplying commit hash
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
jola5 committed Jul 5, 2017
1 parent 4687369 commit b882a0b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/git-tag-version
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function help
SYNOPSIS
git-tag-version [command] [-m|--message=<message>] [-x|--suffix=<suffix>]
[-s|--strict] [-n|--non-strict] [-f|--force]
[-s|--strict] [-n|--non-strict] [-f|--force] [<commit>]
DESCRIPTION
A script to use git tags for repository version numbers through a simple
Expand Down Expand Up @@ -114,7 +114,7 @@ COMMANDS
new <major|minor|patch> [-m|--message=<message>]
[-x|--suffix=<suffix>] [-s|--strict]
[-n|--non-strict]
[-n|--non-strict] [<commit>]
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.
Expand All @@ -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 <version> [-m|--message=<message>] [-x|--suffix=<suffix>]
[-s|--strict] [-n|--non-strict] [-f|--force]
[-s|--strict] [-n|--non-strict] [-f|--force] [<commit>]
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
Expand Down Expand Up @@ -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
Expand All @@ -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!"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions test/05-tag-specific-commit.bats
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
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" ]
}

0 comments on commit b882a0b

Please sign in to comment.