72
72
# Check dependencies.
73
73
dependencies git
74
74
75
+ ref_name=${ref:- HEAD}
76
+ ref=$( git rev-parse " ${ref_name} " )
77
+
75
78
if [[ -z $increment ]]; then
76
79
error " No version increment provided."
77
80
fi
78
81
79
82
if [[ -z $old_version ]]; then
80
83
old_version=" $( git describe --abbrev=0 " $ref ^1" --always) "
81
84
fi
82
- ref_name=${ref}
83
- ref=$( git rev-parse --short " $ref " )
84
85
85
86
# shellcheck source=scripts/release/check_commit_metadata.sh
86
87
source " $SCRIPT_DIR /check_commit_metadata.sh" " $old_version " " $ref "
@@ -110,17 +111,19 @@ release_ff=0
110
111
case " $increment " in
111
112
patch)
112
113
release_branch=" ${release_branch_prefix}${version_parts[0]} .${version_parts[1]} "
113
- branch_contains_ref=$( git branch --remotes -- contains " ${ref} " --list " */ ${release_branch} " --format=' %(refname)' )
114
+ branch_contains_ref=$( git branch --contains " ${ref} " --list " ${release_branch} " --format=' %(refname)' )
114
115
if [[ -z $branch_contains_ref ]]; then
115
116
# Allow patch if we can fast-forward to ref, no need for dry-run here
116
117
# since we're not checking out the branch and deleting it afterwards.
117
- git branch --no-track " ${release_branch} -ff" " origin/${release_branch} "
118
- if ! git merge --ff-only --into-name " ${release_branch} -ff" " ${ref} " > /dev/null 2>&1 ; then
119
- git branch -D " ${release_branch} -ff"
118
+ git branch --no-track " ${release_branch} -ff" " ${release_branch} "
119
+ # We're using git fetch here to perform a fast-forward on a
120
+ # non-checked-out branch. The "." uses the local repo as remote (faster).
121
+ if ! git fetch --quiet . " ${ref} " :" ${release_branch} -ff" ; then
122
+ git branch --quiet --delete --force " ${release_branch} -ff"
120
123
error " Provided ref (${ref_name} ) is not in the required release branch (${release_branch} ) and cannot be fast-forwarded, unable to increment patch version. Please increment minor or major."
121
124
fi
125
+ git branch --quiet --delete --force " ${release_branch} -ff"
122
126
release_ff=1
123
- git branch -D " ${release_branch} -ff"
124
127
fi
125
128
version_parts[2]=$(( version_parts[2 ] + 1 ))
126
129
;;
@@ -144,6 +147,12 @@ new_version="v${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
144
147
log " Old version: $old_version "
145
148
log " New version: $new_version "
146
149
log " Release branch: $release_branch "
150
+
151
+ tag_exists=$( git tag --list " $new_version " )
152
+ if [[ -n ${tag_exists} ]]; then
153
+ error " Tag ${new_version} already exists."
154
+ fi
155
+
147
156
if [[ ${increment} = patch ]]; then
148
157
if (( release_ff == 1 )) ; then
149
158
log " Fast-forwarding release branch"
@@ -154,9 +163,38 @@ if [[ ${increment} = patch ]]; then
154
163
maybedryrun " $dry_run " git checkout " ${release_branch} "
155
164
fi
156
165
else
157
- log " Creating new release branch"
158
- maybedryrun " $dry_run " git checkout -b " ${release_branch} " " ${ref} "
166
+ remote_branch_exists=$( git branch --remotes --list " */${release_branch} " --format=' %(refname)' )
167
+ local_branch_exists=$( git branch --list " ${release_branch} " --format=' %(refname)' )
168
+ if [[ -n ${remote_branch_exists} ]] || [[ -n ${local_branch_exists} ]]; then
169
+ if [[ ${prev_increment} == patch ]]; then
170
+ error " Release branch ${release_branch} already exists, impossible upgrade from \" ${prev_increment} \" to \" ${increment} \" detected. Please check your ref (${ref_name} ) and that no incompatible commits were cherry-picked."
171
+ fi
172
+ fi
173
+
174
+ if [[ -n ${remote_branch_exists} ]]; then
175
+ error " Release branch ${release_branch} already exists on remote, please check your ref."
176
+ fi
177
+
178
+ if [[ -n ${local_branch_exists} ]]; then
179
+ # If it exists, ensure that this release branch points to the provided ref.
180
+ release_branch_ref=$( git rev-parse " ${release_branch} " )
181
+ if [[ ${release_branch_ref} != " ${ref} " ]]; then
182
+ error " Local release branch ${release_branch} already exists, but does not point to the provided ref (${ref_name} )."
183
+ fi
184
+ log " Using existing release branch"
185
+ maybedryrun " $dry_run " git checkout " ${release_branch} "
186
+ else
187
+ log " Creating new release branch"
188
+ maybedryrun " $dry_run " git checkout -b " ${release_branch} " " ${ref} "
189
+ fi
190
+ fi
191
+
192
+ # Ensure the ref is in the release branch.
193
+ branch_contains_ref=$( git branch --contains " ${ref} " --list " ${release_branch} " --format=' %(refname)' )
194
+ if [[ -z $branch_contains_ref ]]; then
195
+ error " Provided ref (${ref_name} ) is not in the required release branch (${release_branch} )."
159
196
fi
197
+
160
198
maybedryrun " $dry_run " git tag -a " $new_version " -m " Release $new_version " " $ref "
161
199
162
200
echo " ${release_branch} ${new_version} "
0 commit comments