Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c24b562

Browse files
authored
chore(scripts): fix release tagging sanity checks (coder#13073)
1 parent 46dced9 commit c24b562

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

scripts/release.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,17 @@ fi
139139
log "Fetching ${branch} and tags from ${remote}..."
140140
git fetch --quiet --tags "${remote}" "$branch"
141141

142-
# Resolve to the latest ref on origin/main unless otherwise specified.
143-
ref_name=${ref:-${remote}/${branch}}
144-
ref=$(git rev-parse --short "${ref_name}")
142+
# Resolve to the current commit unless otherwise specified.
143+
ref_name=${ref:-HEAD}
144+
ref=$(git rev-parse "${ref_name}")
145145

146146
# Make sure that we're running the latest release script.
147147
script_diff=$(git diff --name-status "${remote}/${branch}" -- scripts/release.sh)
148148
if [[ ${script_check} = 1 ]] && [[ -n ${script_diff} ]]; then
149149
error "Release script is out-of-date. Please check out the latest version and try again."
150150
fi
151151

152-
# Make sure no other release contains this ref.
152+
# Make sure no other remote release contains this ref.
153153
release_contains_ref="$(git branch --remotes --contains "${ref}" --list "${remote}/release/*" --format='%(refname)')"
154154
if [[ -n ${release_contains_ref} ]]; then
155155
error "Ref ${ref_name} is already part of another release: $(git describe --always "${ref}") on ${release_contains_ref#"refs/remotes/${remote}/"}."
@@ -180,7 +180,7 @@ source "$SCRIPT_DIR/release/check_commit_metadata.sh" "$old_version" "$ref"
180180
trap - EXIT
181181
log
182182

183-
tag_version_args=(--old-version "$old_version" --ref "$ref" --"$increment")
183+
tag_version_args=(--old-version "$old_version" --ref "$ref_name" --"$increment")
184184
if ((force == 1)); then
185185
tag_version_args+=(--force)
186186
fi

scripts/release/tag_version.sh

+47-9
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ done
7272
# Check dependencies.
7373
dependencies git
7474

75+
ref_name=${ref:-HEAD}
76+
ref=$(git rev-parse "${ref_name}")
77+
7578
if [[ -z $increment ]]; then
7679
error "No version increment provided."
7780
fi
7881

7982
if [[ -z $old_version ]]; then
8083
old_version="$(git describe --abbrev=0 "$ref^1" --always)"
8184
fi
82-
ref_name=${ref}
83-
ref=$(git rev-parse --short "$ref")
8485

8586
# shellcheck source=scripts/release/check_commit_metadata.sh
8687
source "$SCRIPT_DIR/check_commit_metadata.sh" "$old_version" "$ref"
@@ -110,17 +111,19 @@ release_ff=0
110111
case "$increment" in
111112
patch)
112113
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)')
114115
if [[ -z $branch_contains_ref ]]; then
115116
# Allow patch if we can fast-forward to ref, no need for dry-run here
116117
# 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"
120123
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."
121124
fi
125+
git branch --quiet --delete --force "${release_branch}-ff"
122126
release_ff=1
123-
git branch -D "${release_branch}-ff"
124127
fi
125128
version_parts[2]=$((version_parts[2] + 1))
126129
;;
@@ -144,6 +147,12 @@ new_version="v${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
144147
log "Old version: $old_version"
145148
log "New version: $new_version"
146149
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+
147156
if [[ ${increment} = patch ]]; then
148157
if ((release_ff == 1)); then
149158
log "Fast-forwarding release branch"
@@ -154,9 +163,38 @@ if [[ ${increment} = patch ]]; then
154163
maybedryrun "$dry_run" git checkout "${release_branch}"
155164
fi
156165
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})."
159196
fi
197+
160198
maybedryrun "$dry_run" git tag -a "$new_version" -m "Release $new_version" "$ref"
161199

162200
echo "${release_branch} ${new_version}"

0 commit comments

Comments
 (0)