1+ #! /bin/bash
2+
3+ # Colors for output
4+ GREEN=' \033[0;32m'
5+ YELLOW=' \033[1;33m'
6+ RED=' \033[0;31m'
7+ NC=' \033[0m' # No Color
8+
9+ # Function to print messages with color
10+ print_message () {
11+ echo -e " ${GREEN} $1 ${NC} "
12+ }
13+
14+ print_warning () {
15+ echo -e " ${YELLOW} $1 ${NC} "
16+ }
17+
18+ print_error () {
19+ echo -e " ${RED} $1 ${NC} "
20+ }
21+
22+ # Get the latest tag
23+ get_latest_tag () {
24+ git fetch --tags
25+ latest_tag=$( git tag -l " v*" --sort=-v:refname | head -n 1)
26+
27+ if [ -z " $latest_tag " ]; then
28+ print_warning " No existing version tags found. Starting with v0.1.0."
29+ latest_tag=" v0.1.0"
30+ else
31+ print_message " Latest version tag: $latest_tag "
32+ fi
33+
34+ # Strip the 'v' prefix for version calculations
35+ latest_version=${latest_tag# v}
36+ }
37+
38+ # Increment version
39+ increment_version () {
40+ # Split version into parts
41+ IFS=' .' read -r -a version_parts <<< " $latest_version"
42+
43+ major=${version_parts[0]}
44+ minor=${version_parts[1]}
45+ patch=${version_parts[2]}
46+
47+ case $1 in
48+ major)
49+ major=$(( major + 1 ))
50+ minor=0
51+ patch=0
52+ ;;
53+ minor)
54+ minor=$(( minor + 1 ))
55+ patch=0
56+ ;;
57+ patch|* )
58+ patch=$(( patch + 1 ))
59+ ;;
60+ esac
61+
62+ new_version=" $major .$minor .$patch "
63+ new_tag=" v$new_version "
64+ }
65+
66+ # Main script
67+
68+ # Check if we're in a git repository
69+ if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then
70+ print_error " Not in a git repository!"
71+ exit 1
72+ fi
73+
74+ # Get the latest tag
75+ get_latest_tag
76+
77+ # Process command line arguments
78+ if [ $# -eq 0 ]; then
79+ # No arguments - show interactive menu
80+ echo " Current version: $latest_tag "
81+ echo " What would you like to do?"
82+ echo " 1) Increment patch version (default)"
83+ echo " 2) Increment minor version"
84+ echo " 3) Increment major version"
85+ echo " 4) Specify exact version"
86+
87+ read -p " Select option [1-4] (default: 1): " option
88+
89+ case $option in
90+ 2) increment_version minor ;;
91+ 3) increment_version major ;;
92+ 4)
93+ read -p " Enter version (without 'v' prefix): " user_version
94+ new_version=$user_version
95+ new_tag=" v$new_version "
96+ ;;
97+ * ) increment_version patch ;;
98+ esac
99+ else
100+ # Version provided as command line argument
101+ if [[ $1 == v* ]]; then
102+ new_tag=$1
103+ new_version=${new_tag# v}
104+ else
105+ new_version=$1
106+ new_tag=" v$new_version "
107+ fi
108+ fi
109+
110+ print_message " Creating new version tag: $new_tag "
111+
112+ # Check if there are uncomitted changes
113+ if [ -n " $( git status --porcelain) " ]; then
114+ print_warning " You have uncommitted changes. It's recommended to commit them before creating a new release."
115+ read -p " Continue anyway? (y/n): " continue_choice
116+ if [[ $continue_choice != " y" && $continue_choice != " Y" ]]; then
117+ print_message " Aborting. Please commit your changes first."
118+ exit 0
119+ fi
120+ fi
121+
122+ # Create and push the tag
123+ print_message " Creating tag $new_tag with message 'Release $new_tag '..."
124+ git tag -a " $new_tag " -m " Release $new_tag "
125+
126+ print_message " Pushing tag to remote..."
127+ git push origin " $new_tag "
128+
129+ print_message " Version $new_tag has been created and pushed!"
130+ print_message " GitHub Actions workflow should start automatically."
131+ print_message " Check the progress at: https://github.com/$( git remote get-url origin | sed ' s/.*github.com[:/]\(.*\).git/\1/' ) /actions"
0 commit comments