1+ #! /bin/bash
2+
3+ # Initializes a new release branch from the trunk branch
4+
5+ readonly TRUNK_BRANCH=" main"
6+
7+ source ./print.sh
8+ source ./helpers.sh
9+
10+ # accept a version from the user
11+ describe " Initializing a new release branch..."
12+ read -p " Release Version (X.Y.Z): " version
13+
14+ if [[ ! $version =~ ^[0-9]+.[0-9]+.[0-9]+$ ]]
15+ then
16+ error " Release version must be in semantic versioning format (X.Y.Z)"
17+ exit 1
18+ fi
19+
20+ # Ensure we don't have any tags which match the provided version
21+ describe " Checking for existing tags..."
22+ if git ls-remote --tags origin | grep -q " v$version " ; then
23+ error " A tag for v$version already exists. Please use a different version number"
24+ exit 1
25+ fi
26+
27+ current_branch=$( git rev-parse --abbrev-ref HEAD)
28+ release_branch=" release/v$version "
29+ remote_trunk_branch=" origin/$TRUNK_BRANCH "
30+
31+ # Define the target branch for the release. By default we use the remote trunk, but if the hotfix
32+ # flag is provided, we prompt for a tag to base the release on.
33+ release_target=$remote_trunk_branch
34+ while [[ $# -gt 0 ]]; do
35+ case " $1 " in
36+ --hotfix)
37+ describe " Hotfix flag detected. Please provide a tag to base the release on"
38+ read -p " Tag: " tag
39+ release_target=" tags/v$tag "
40+
41+ describe " Fetching all remote tags"
42+ git fetch --all --tags
43+ ;;
44+ esac
45+ shift
46+ done
47+
48+ # Update remote tracking branches
49+ fetch_remote_branches
50+
51+ # Create a release branch from the tip of the remote main branch
52+ describe " Creating $release_branch targeting $release_target ..."
53+ git checkout -b $release_branch $release_target
54+ if [ $? -ne 0 ]; then
55+ error " Failed to create release branch $release_branch "
56+ exit 1
57+ fi
58+
59+ # Push the release branch to origin
60+ describe " Pushing $release_branch to origin..."
61+ git push -u origin $release_branch
62+ if [ $? -ne 0 ]; then
63+ error " Failed to publish $release_branch . Rolling back..."
64+
65+ git checkout $current_branch ;
66+ git branch -D $release_branch ;
67+ exit 1
68+ fi
69+
70+ # Reset the branch back to the user's current branch
71+ describe " Reset local branch back to original branch..."
72+ git checkout $current_branch
73+
74+ describe " Successfully initialized $release_branch !"
0 commit comments