git: Ensure the tagging script is idempotent

This commit is contained in:
Paul Morgan
2017-12-20 20:26:22 +00:00
parent 62ca12608d
commit 2e86d7c5ab

44
tag.sh
View File

@@ -1,14 +1,50 @@
#!/bin/bash #!/bin/bash
# TODO: don't run if current HEAD is already tagged (ensure this is idempotent) set -eEu
# take current HEAD with new version set -o pipefail
################################################################################
# Tag and push a new release of the git repo.
# There are no arguments for this script.
################################################################################
# Variables:
#
# v: the old tag version, such as 0.0.13
# t: the new tag version, such as 0.0.14
# h: the head version, such as 0.0.13-40-g62ca126-dirty
v=`git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --abbrev=0` v=`git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --abbrev=0`
t=`echo "${v%.*}.$((${v##*.}+1))"` # increment version t=`echo "${v%.*}.$((${v##*.}+1))"` # increment version
echo "Version $t is now tagged!" h="$(git describe --tags --dirty --always)"
echo "Pushing $t to origin..."
if [[ $# -gt 0 ]]; then
echo "ERR: $0 does not take arguments." >&2
exit 1
fi
# Never tag a dirty git tree.
if [[ "${h}" =~ dirty ]]; then
echo "ERR: git tree is dirty. Commit or stash changes before tagging." >&2
exit 1
fi
# Be idempotent.
if [[ "${h}" == "${v}" ]]; then
echo "INFO: HEAD \"${h}\" is equivalent to the current (old) version tag \"${v}\"; nothing to do."
exit 0
fi
# Give the user a chance to abort.
echo "WARN: About to tag \"${h}\" as \"${t}\" and push."
echo "Press ^C within 3s to abort." echo "Press ^C within 3s to abort."
sleep 3s sleep 3s
# Tag and push.
echo "release: tag $t" | git tag --file=- --sign $t echo "release: tag $t" | git tag --file=- --sign $t
echo "INFO: Version $t is now tagged."
echo "INFO: Pushing $t to origin."
git push origin $t git push origin $t
# Be informative.
GIT_PAGER=cat git diff --stat "$v" "$t" GIT_PAGER=cat git diff --stat "$v" "$t"
if which contrib.sh 2>/dev/null; then contrib.sh "$v"; fi if which contrib.sh 2>/dev/null; then contrib.sh "$v"; fi
echo -e "run 'git log $v..$t' to see what has changed since $v" echo -e "run 'git log $v..$t' to see what has changed since $v"