Skip to content

Instantly share code, notes, and snippets.

@Toshinaki
Last active February 23, 2023 17:16
Show Gist options
  • Save Toshinaki/28a7a51def44eb36519d9180c889a35c to your computer and use it in GitHub Desktop.
Save Toshinaki/28a7a51def44eb36519d9180c889a35c to your computer and use it in GitHub Desktop.
Detect latest version tag and create a new version based on that and commit type. Only work for "major.minor.patch" format.
#!/bin/bash
# get current tag
curr=`git describe --tags --abbrev=0` || curr=""
newTag=""
# check if tag exists
if [ -n "$curr" ]
then
# got latest tag
# check tag format
if [[ $curr =~ [0-9]\.[0-9]\.[0-9] ]]
then
# valid version tag
IFS=. read major minor patch <<< "$curr"
echo "Current version: $curr"
echo " Major: $major"
echo " Minor: $minor"
echo " Patch: $patch"
action="$1"
msg="$2"
echo "major minor patch" | tr " " "\n" | grep -F -q -x "$action" || action=""
if [ -z "$action" ]
then
msg="$1"
# no argument provided
echo "Please pick the commit type:"
select t in "breaking changes" "new features" "bug fixes"; do
case $t in
"breaking changes" ) action="major"; break;;
"new features" ) action="minor"; break;;
"bug fixes" ) action="patch"; break;;
esac
done
fi
# make the new version
case $action in
"major" ) newTag="$(($major + 1)).0.0"; break;;
"minor" ) newTag="$major.$(($minor + 1)).0"; break;;
"patch" ) newTag="$major.$minor.$(($patch + 1))"; break;;
esac
echo "Creating a new $action version: $newTag"
else
# invalid version tag
echo "Invalid version tag: $curr"
# prompt for a valid version
while true
do
read -p "Please input a version with format: 0.0.0 (0.1.0):" newTag
# default to 0.1.0
if [ -z "$newTag" ]
then
newTag="0.1.0"
break
fi
# check tag format
if [[ $newTag =~ [0-9]\.[0-9]\.[0-9] ]]
then
break
fi
done
fi
else
# no tag yet, set initial version
echo "Please pick initial version:"
select ver in "0.1.0" "1.0.0"; do
case $ver in
"0.1.0" ) newTag="0.1.0"; break;;
"1.0.0" ) newTag="1.0.0"; break;;
esac
done
fi
if [ -z "$msg" ]
then
read -p "Tag message: (leave blank if not needed)" msg
fi
# final confirm
while true
do
read -p "Push as version: $newTag? (Y/n)" yn
case $yn in
[Yy]*|"" )
if [ -n "$msg" ]
then
git tag -a "$newTag" -m "$msg"
else
git tag -a "$newTag"
fi
git push origin $newTag
echo "Done!"
break;;
[Nn]* ) exit;;
esac
done
#!/bin/bash
# with ability to do add commit and push
# may be not that useful though...
# get current tag
curr=`git describe --tags --abbrev=0` || curr=""
newTag=""
# check if tag exists
if [ -n "$curr" ]
then
# got latest tag
# check tag format
if [[ $curr =~ [0-9]\.[0-9]\.[0-9] ]]
then
# valid version tag
IFS=. read major minor patch <<< "$curr"
echo "Current version: $curr"
echo " Major: $major"
echo " Minor: $minor"
echo " Patch: $patch"
action="$1"
msg="$2"
echo "major minor patch" | tr " " "\n" | grep -F -q -x "$action" || action=""
if [ -z "$action" ]
then
msg="$1"
# no argument provided
echo "Please pick the commit type:"
select t in "breaking changes" "new features" "bug fixes"; do
case $t in
"breaking changes" ) action="major"; break;;
"new features" ) action="minor"; break;;
"bug fixes" ) action="patch"; break;;
esac
done
fi
# make the new version
case $action in
"major" ) newTag="$(($major + 1)).0.0"; break;;
"minor" ) newTag="$major.$(($minor + 1)).0"; break;;
"patch" ) newTag="$major.$minor.$(($patch + 1))"; break;;
esac
echo "Creating a new $action version: $newTag"
else
# invalid version tag
echo "Invalid version tag: $curr"
# prompt for a valid version
while true
do
read -p "Please input a version with format: 0.0.0 (0.1.0):" newTag
# default to 0.1.0
if [ -z "$newTag" ]
then
newTag="0.1.0"
break
fi
# check tag format
if [[ $newTag =~ [0-9]\.[0-9]\.[0-9] ]]
then
break
fi
done
fi
else
# no tag yet, set initial version
echo "Please pick initial version:"
select ver in "0.1.0" "1.0.0"; do
case $ver in
"0.1.0" ) newTag="0.1.0"; break;;
"1.0.0" ) newTag="1.0.0"; break;;
esac
done
fi
# check for uncommitted changes
uncommitted=false
git diff-index --quiet HEAD -- || uncommitted=true
if [ "$uncommitted" = true ]
then
while true
do
read -p "Detected uncommitted changed. Do you want to do commit first? (Y/n)" yn
case $yn in
[Yy]*|"" )
# do commit
# get commit message
read -p "Commit message: " commitMsg
if [ -z "$commitMsg" ]
then
commitMsg="Commit for version $newTag"
fi
# confirm before commit
while true
do
read -p "Commit with message: \"$commitMsg\"? (Y/n)" yn
case $yn in
[Yy]*|"" )
git add -A
git commit -m "$commitMsg"
break;;
[Nn]* ) break;;
esac
done
break;;
[Nn]* ) break;;
esac
done
fi
# check for unpushed commits
commitCount=`git rev-list @{u}..@ --count`
if [ $commitCount -gt 0 ]
then
# confirm before commit
while true
do
read -p "Push local commits? (Y/n)" yn
case $yn in
[Yy]*|"" )
git push origin HEAD
echo "Done!"
git log -n 1
break;;
[Nn]* ) break;;
esac
done
fi
if [ -z "$msg" ]
then
read -p "Tag message: (leave blank if not needed)" msg
fi
# final confirm
while true
do
read -p "Push as version: $newTag? (Y/n)" yn
case $yn in
[Yy]*|"" )
if [ -n "$msg" ]
then
git tag -a "$newTag" -m "$msg"
else
git tag -a "$newTag"
fi
git push origin $newTag
echo "Done!"
break;;
[Nn]* ) exit;;
esac
done
@Toshinaki
Copy link
Author

New to bash, and not expert at git.
Not fully tested.

Any advice or bug report is very welcomed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment