1+ #! /bin/bash
2+
3+ if [ $# -lt 1 ]; then
4+ echo " Please provide a semantic version, 'major', 'minor' or 'patch'"
5+ exit 1
6+ fi
7+
8+
9+ NEW_VERSION=" $1 "
10+ TAG_COMMIT=' HEAD'
11+
12+ # Fetch existing tags before choosing what the next one should be.
13+ echo ' Fetching existing tags...'
14+ git fetch --tags
15+
16+ # Parse major, minor and patch groups of latest tag.
17+ LATEST_RELEASE_TAG=$( git tag | grep -iE ' v.*' | sort -V | tail -n 1)
18+ if [[ -z " ${LATEST_RELEASE_TAG} " ]]; then
19+ echo " No release tags found. Please create an initial release tag first."
20+ exit 1
21+ fi
22+
23+ NEW_RELEASE_MAJOR=$( echo ${LATEST_RELEASE_TAG// v} | cut -d. -f 1)
24+ NEW_RELEASE_MINOR=$( echo ${LATEST_RELEASE_TAG// v} | cut -d. -f 2)
25+ NEW_RELEASE_PATCH=$( echo ${LATEST_RELEASE_TAG// v} | cut -d. -f 3)
26+
27+ # Set the major, minor and patch groups of the new tag.
28+ if [[ " ${NEW_VERSION} " == ' major' ]]; then
29+ NEW_RELEASE_MAJOR=$(( $NEW_RELEASE_MAJOR + 1 ))
30+ NEW_RELEASE_MINOR=' 0'
31+ NEW_RELEASE_PATCH=' 0'
32+ elif [[ " ${NEW_VERSION} " == ' minor' ]]; then
33+ NEW_RELEASE_MINOR=$(( $NEW_RELEASE_MINOR + 1 ))
34+ NEW_RELEASE_PATCH=' 0'
35+ elif [[ " ${NEW_VERSION} " == ' patch' ]]; then
36+ NEW_RELEASE_PATCH=$(( $NEW_RELEASE_PATCH + 1 ))
37+ elif [[ " ${NEW_VERSION} " =~ v.* \. .* \. .* ]]; then
38+ NEW_RELEASE_MAJOR=$( echo ${NEW_VERSION// v} | cut -d. -f 1)
39+ NEW_RELEASE_MINOR=$( echo ${NEW_VERSION// v} | cut -d. -f 2)
40+ NEW_RELEASE_PATCH=$( echo ${NEW_VERSION// v} | cut -d. -f 3)
41+ else
42+ echo " Invalid argument. Please provide a semantic version, 'major', 'minor' or 'patch'"
43+ exit 1
44+ fi
45+
46+ # Construct the new release tag.
47+ echo " Latest release tag is '${LATEST_RELEASE_TAG} '"
48+ NEW_RELEASE=" v${NEW_RELEASE_MAJOR} .${NEW_RELEASE_MINOR} .${NEW_RELEASE_PATCH} "
49+ echo " Creating release '${NEW_RELEASE} '"
50+
51+ # Construct the tag message.
52+ # If the tag message is empty, use the default message.
53+ TAG_MESSAGE_FILE=$( mktemp)
54+ TAG_MESSAGE_HEADER=" Version ${NEW_RELEASE_MAJOR} .${NEW_RELEASE_MINOR} .${NEW_RELEASE_PATCH} "
55+ TAG_MESSAGE_CHANGES=$( git log --pretty=format:" - %s" -n 20 " ${LATEST_RELEASE_TAG} ..${TAG_COMMIT} " )
56+
57+ echo " ${TAG_MESSAGE_HEADER} " > " ${TAG_MESSAGE_FILE} "
58+ echo ' ' >> " ${TAG_MESSAGE_FILE} "
59+ echo " Commits since ${LATEST_RELEASE_TAG} :" >> " ${TAG_MESSAGE_FILE} "
60+ echo " ${TAG_MESSAGE_CHANGES} " >> " ${TAG_MESSAGE_FILE} "
61+ ${EDITOR} " ${TAG_MESSAGE_FILE} "
62+
63+ git tag -a " ${NEW_RELEASE} " -F " ${TAG_MESSAGE_FILE} " " ${TAG_COMMIT} "
64+
65+ while true ; do
66+ read -p " Release tag created, push, delete or keep? [${bold} P${normal} ush/${bold} K${normal} eep/${bold} D${normal} elete]: " choice
67+ case $choice in
68+ [Pp]* ) git push origin ${NEW_RELEASE} ; break ;;
69+ [Kk]* ) break ;;
70+ [Dd]* ) git tag -d ${NEW_RELEASE} ; break ;;
71+ esac
72+ done
0 commit comments