forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag_version.sh
More file actions
executable file
·196 lines (174 loc) · 5.89 KB
/
tag_version.sh
File metadata and controls
executable file
·196 lines (174 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "$(dirname "${BASH_SOURCE[0]}")")/lib.sh"
cdroot
usage() {
cat <<EOH
Usage: ./version_tag.sh [--dry-run] [--old-version <version>] [--ref <ref>] <--major | --minor | --patch>
This script should be called to tag a new release. It will take the suggested
increment (major, minor, patch) and optionally promote e.g. patch -> minor if
there are breaking changes between the previous version and the given --ref
(or HEAD).
Pass --old-version optionally to ensure that the version is bumped from the
provided version instead of the latest tag (for use in release.sh).
This script will create a git tag, it should only be called by release.sh or in
CI.
EOH
}
dry_run=0
old_version=
ref=HEAD
increment=
force=0
args="$(getopt -o h -l dry-run,help,old-version:,ref:,major,minor,patch,force -- "$@")"
eval set -- "$args"
while true; do
case "$1" in
--dry-run)
dry_run=1
shift
;;
--old-version)
old_version="$2"
shift 2
;;
--ref)
ref="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
--major | --minor | --patch)
if [[ -n $increment ]]; then
error "Cannot specify multiple version increments."
fi
increment=${1#--}
shift
;;
--force)
force=1
shift
;;
--)
shift
break
;;
*)
error "Unrecognized option: $1"
;;
esac
done
# Check dependencies.
dependencies git
ref_name=${ref:-HEAD}
ref=$(git rev-parse "${ref_name}")
if [[ -z $increment ]]; then
error "No version increment provided."
fi
if [[ -z $old_version ]]; then
old_version="$(git describe --abbrev=0 "$ref^1" --always)"
fi
# shellcheck source=scripts/release/check_commit_metadata.sh
source "$SCRIPT_DIR/check_commit_metadata.sh" "$old_version" "$ref"
prev_increment=$increment
if ((COMMIT_METADATA_BREAKING == 1)); then
if [[ $increment == patch ]]; then
increment=minor
fi
if [[ $prev_increment != "$increment" ]]; then
if ((force == 1)); then
log "Breaking change detected but --force provided, would use \"$increment\" but keeping \"$prev_increment\"."
increment=$prev_increment
else
log "Breaking change detected, changing version increment from \"$prev_increment\" to \"$increment\"."
fi
else
log "Breaking change detected, provided increment is sufficient, using \"$increment\" increment."
fi
else
log "No breaking changes detected, using \"$increment\" increment."
fi
mapfile -d . -t version_parts <<<"${old_version#v}"
release_branch_prefix="release/"
release_ff=0
case "$increment" in
patch)
release_branch="${release_branch_prefix}${version_parts[0]}.${version_parts[1]}"
branch_contains_ref=$(git branch --contains "${ref}" --list "${release_branch}" --format='%(refname)')
if [[ -z $branch_contains_ref ]]; then
# Allow patch if we can fast-forward to ref, no need for dry-run here
# since we're not checking out the branch and deleting it afterwards.
git branch --no-track "${release_branch}-ff" "${release_branch}"
# We're using git fetch here to perform a fast-forward on a
# non-checked-out branch. The "." uses the local repo as remote (faster).
if ! git fetch --quiet . "${ref}":"${release_branch}-ff"; then
git branch --quiet --delete --force "${release_branch}-ff"
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."
fi
git branch --quiet --delete --force "${release_branch}-ff"
release_ff=1
fi
version_parts[2]=$((version_parts[2] + 1))
;;
minor)
version_parts[1]=$((version_parts[1] + 1))
version_parts[2]=0
;;
major)
version_parts[0]=$((version_parts[0] + 1))
version_parts[1]=0
version_parts[2]=0
;;
*)
error "Unrecognized version increment."
;;
esac
release_branch="${release_branch_prefix}${version_parts[0]}.${version_parts[1]}"
new_version="v${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
log "Old version: $old_version"
log "New version: $new_version"
log "Release branch: $release_branch"
tag_exists=$(git tag --list "$new_version")
if [[ -n ${tag_exists} ]]; then
error "Tag ${new_version} already exists."
fi
if [[ ${increment} = patch ]]; then
if ((release_ff == 1)); then
log "Fast-forwarding release branch"
maybedryrun "$dry_run" git checkout "${release_branch}"
maybedryrun "$dry_run" git merge --ff-only "${ref}"
else
log "Using existing release branch"
maybedryrun "$dry_run" git checkout "${release_branch}"
fi
else
remote_branch_exists=$(git branch --remotes --list "*/${release_branch}" --format='%(refname)')
local_branch_exists=$(git branch --list "${release_branch}" --format='%(refname)')
if [[ -n ${remote_branch_exists} ]] || [[ -n ${local_branch_exists} ]]; then
if [[ ${prev_increment} == patch ]]; then
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."
fi
fi
if [[ -n ${local_branch_exists} ]]; then
# If it exists, ensure that this release branch points to the provided ref.
release_branch_ref=$(git rev-parse "${release_branch}")
if [[ ${release_branch_ref} != "${ref}" ]]; then
error "Local release branch ${release_branch} already exists, but does not point to the provided ref (${ref_name})."
fi
log "Using existing release branch"
maybedryrun "$dry_run" git checkout "${release_branch}"
else
log "Creating new release branch"
maybedryrun "$dry_run" git checkout -b "${release_branch}" "${ref}"
fi
fi
# Ensure the ref is in the release branch.
branch_contains_ref=$(git branch --contains "${ref}" --list "${release_branch}" --format='%(refname)')
if ((!dry_run)) && [[ -z $branch_contains_ref ]]; then
error "Provided ref (${ref_name}) is not in the required release branch (${release_branch})."
fi
maybedryrun "$dry_run" git tag -a "$new_version" -m "Release $new_version" "$ref"
echo "${release_branch} ${new_version}"