Skip to content

Commit b076d94

Browse files
committed
Merged in feature/PROTO-266-automatic-version-update (pull request #188)
Feature/PROTO-266 automatic version update
2 parents 1999c12 + 1c6603f commit b076d94

2 files changed

Lines changed: 184 additions & 17 deletions

File tree

release_to_github.sh

Lines changed: 149 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,163 @@
3030
# the Netherlands
3131
#
3232

33+
# Function to show usage information
34+
show_usage() {
35+
echo "Usage: ./release_to_github.sh [OPTIONS]"
36+
echo "Options:"
37+
echo " --version MAJOR.MINOR.PATCH Set version number (required)"
38+
echo " --stage Stage changes in release branch"
39+
echo " --release Perform release merge and tagging"
40+
echo ""
41+
echo "Example:"
42+
echo " ./release_to_github.sh --version 4.0.1 --stage # Stage changes"
43+
echo " ./release_to_github.sh --version 4.0.1 --release # Perform release"
44+
}
3345

34-
if [ $# -eq 0 ]; then
35-
echo "Please provice version number: \"./release_to_github.sh X.Y.Z\""
36-
else
37-
git fetch --prune
46+
# Function to parse and validate version
47+
parse_version() {
48+
if ! echo "$1" | grep -E "^[0-9]+\.[0-9]+\.[0-9]+$" > /dev/null; then
49+
echo "Error: Version must be in format MAJOR.MINOR.PATCH"
50+
exit 1
51+
fi
3852

39-
git checkout develop
40-
git pull
41-
git push github develop
53+
VERSION_MAJOR=$(echo "$1" | cut -d. -f1)
54+
VERSION_MINOR=$(echo "$1" | cut -d. -f2)
55+
VERSION_PATCH=$(echo "$1" | cut -d. -f3)
56+
}
4257

43-
git checkout master
44-
git pull
45-
git push github master
58+
# Function to update version files
59+
update_version_files() {
60+
# Update Version.h
61+
sed -i "s/#define EMBEDDEDPROTO_VERSION_MAJOR [0-9]*/#define EMBEDDEDPROTO_VERSION_MAJOR $VERSION_MAJOR/" src/Version.h
62+
sed -i "s/#define EMBEDDEDPROTO_VERSION_MINOR [0-9]*/#define EMBEDDEDPROTO_VERSION_MINOR $VERSION_MINOR/" src/Version.h
63+
sed -i "s/#define EMBEDDEDPROTO_VERSION_PATCH [0-9]*/#define EMBEDDEDPROTO_VERSION_PATCH $VERSION_PATCH/" src/Version.h
64+
sed -i "s/#define EMBEDDEDPROTO_VERSION_STRING \".*\"/#define EMBEDDEDPROTO_VERSION_STRING \"$VERSION\"/" src/Version.h
65+
66+
# Update version.json
67+
echo "{" > EmbeddedProto/version.json
68+
echo " \"version\": \"$VERSION\"" >> EmbeddedProto/version.json
69+
echo "}" >> EmbeddedProto/version.json
70+
}
4671

72+
# Function to create staging branch
73+
create_stage() {
74+
BRANCH="release/$VERSION"
75+
76+
# Create and switch to release branch
77+
git checkout -b "$BRANCH"
78+
79+
# Update version files
80+
update_version_files
81+
82+
# Stage and commit changes
83+
git add src/Version.h EmbeddedProto/version.json
84+
git commit -m "Preparing for release $VERSION"
85+
86+
# Push to both remotes
87+
git push origin "$BRANCH"
88+
git push github "$BRANCH"
89+
}
90+
91+
# Function to perform release
92+
perform_release() {
93+
BRANCH="release/$VERSION"
94+
95+
# Ensure we're on the release branch
96+
git checkout "$BRANCH"
97+
98+
# Merge into master
99+
git checkout master
100+
git merge --no-ff "$BRANCH" -m "Merge release $VERSION into master"
101+
102+
# Merge into develop
103+
git checkout develop
104+
git merge --no-ff "$BRANCH" -m "Merge release $VERSION into develop"
105+
106+
# Create version tag
47107
git tag -d latest
48108
git push --delete origin latest
49109
git push --delete github latest
50-
110+
51111
git tag latest
52112
git push origin latest
53113
git push github latest
114+
115+
git tag "$VERSION"
116+
git push origin "$VERSION"
117+
git push github "$VERSION"
118+
119+
# Push master and develop
120+
git checkout master
121+
git push origin master
122+
git push github master
123+
124+
git checkout develop
125+
git push origin develop
126+
git push github develop
127+
128+
# Clean up release branch
129+
git branch -d "$BRANCH"
130+
git push origin --delete "$BRANCH"
131+
git push github --delete "$BRANCH"
132+
}
133+
134+
# Parse command line arguments
135+
VERSION=""
136+
DO_STAGE=false
137+
DO_RELEASE=false
138+
139+
while [ "$#" -gt 0 ]; do
140+
case "$1" in
141+
--version)
142+
VERSION="$2"
143+
shift 2
144+
;;
145+
--stage)
146+
DO_STAGE=true
147+
shift
148+
;;
149+
--release)
150+
DO_RELEASE=true
151+
shift
152+
;;
153+
--help)
154+
show_usage
155+
exit 0
156+
;;
157+
*)
158+
echo "Unknown option: $1"
159+
show_usage
160+
exit 1
161+
;;
162+
esac
163+
done
164+
165+
# Validate arguments
166+
if [ -z "$VERSION" ]; then
167+
echo "Error: Version is required"
168+
show_usage
169+
exit 1
170+
fi
171+
172+
# Parse version into components
173+
parse_version "$VERSION"
174+
175+
# Fetch latest changes
176+
git fetch --prune
177+
178+
# Execute requested operations
179+
if [ "$DO_STAGE" = true ]; then
180+
create_stage
181+
fi
182+
183+
if [ "$DO_RELEASE" = true ]; then
184+
perform_release
185+
fi
54186

55-
git tag "$1"
56-
git push origin "$1"
57-
git push github "$1"
58-
fi
187+
# If no operation specified, show usage
188+
if [ "$DO_STAGE" = false ] && [ "$DO_RELEASE" = false ]; then
189+
echo "Error: Either --stage or --release must be specified"
190+
show_usage
191+
exit 1
192+
fi

setup.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import subprocess
3737
import os
3838
import json
39-
import sys
39+
import re
4040

4141

4242
def build_proto():
@@ -64,6 +64,24 @@ def run(self):
6464
build_proto()
6565
super().run()
6666

67+
def get_current_branch():
68+
result = subprocess.run(
69+
["git", "rev-parse", "--abbrev-ref", "HEAD"],
70+
capture_output=True,
71+
text=True,
72+
check=True
73+
)
74+
return result.stdout.strip()
75+
76+
def get_unique_commit_count(base_branch="main"):
77+
result = subprocess.run(
78+
["git", "rev-list", "--count", f"{base_branch}..HEAD"],
79+
capture_output=True,
80+
text=True,
81+
check=True
82+
)
83+
return result.stdout.strip()
84+
6785
def get_version():
6886
version_file = 'EmbeddedProto/version.json'
6987
build_number = os.getenv('GITHUB_RUN_NUMBER', '0')
@@ -72,7 +90,22 @@ def get_version():
7290
version_data = json.load(f)
7391

7492
base_version = version_data.get('version', '0.0.0')
75-
full_version = f"{base_version}.dev{build_number}"
93+
branch_name = get_current_branch()
94+
95+
if branch_name == "master":
96+
# Final release version (no suffix)
97+
full_version = base_version
98+
99+
elif re.fullmatch(r'release/\d+\.\d+\.\d+', branch_name):
100+
# Release candidate version
101+
base_branch = 'develop' # Release candidates are branched of develop.
102+
rc_number = get_unique_commit_count(base_branch)
103+
full_version = f"{base_version}rc{rc_number}"
104+
105+
else:
106+
# Development or feature branch
107+
full_version = f"{base_version}.dev{build_number}"
108+
76109
return full_version
77110

78111

0 commit comments

Comments
 (0)