-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-make-release
executable file
·249 lines (215 loc) · 8.26 KB
/
git-make-release
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash -e
EDITOR=${EDITOR:-editor}
RELEASE_BRANCH=0
RELEASE_PATCH=0
RELEASE_MINOR=0
RELEASE_MAJOR=0
# Parse options
OPTS=$(getopt -o b,p,m,M --long release-branch,patch,minor,major -n 'parse-options' -- "$@")
eval set -- "$OPTS"
while true; do
case "$1" in
-b | --release-branch ) RELEASE_BRANCH=1 ; shift ;;
-p | --patch ) RELEASE_PATCH=1 ; shift ;;
-m | --minor ) RELEASE_MINOR=1 ; shift ;;
-M | --major ) RELEASE_MAJOR=1 ; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
# RELEASE_PATCH, RELEASE_MINOR and RELEASE_MAJOR are mutually exclusive
if [ $((RELEASE_PATCH + RELEASE_MINOR + RELEASE_MAJOR)) -gt 1 ]; then
echo "Options --patch, --minor and --major are mutually exclusive!"
exit 1
fi
# patch, minor and major are not allowed in combination with release-branch
if [ $((RELEASE_PATCH + RELEASE_MINOR + RELEASE_MAJOR)) -gt 0 -a $RELEASE_BRANCH -eq 1 ]; then
echo "Options --patch, --minor and --major are not allowed in combination with --release-branch!"
exit 1
fi
#
# https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
script_home=$(dirname "$(readlink -f "$0")")
if ! git rev-parse --git-dir 2>/dev/null ; then
echo "$PWD is not a git repository"
exit 1
fi
REPO_TOPLEVEL=$(git rev-parse --show-toplevel)
# make sure currently the master branch is checked out
CURRENT_BRANCH=$(git branch | grep "^*" | sed -e 's|^* ||')
if [ "${CURRENT_BRANCH}" != "master" ] && [ "${CURRENT_BRANCH}" != "main" ] && [ "${RELEASE_BRANCH}" = "0" ]; then
echo "Your current branch is ${CURRENT_BRANCH} and not master. Please merge your changes into the master branch first!"
echo "Or did you mean to git-make-release --release-branch?"
exit 1
fi
# check for uncommitted changes
if [ -n "$(git status --porcelain -uno)" ]; then
echo "You have uncommitted changes in your working copy. Please commit everything first!"
exit 1
fi
# obtain the latest tag and version
IS_TAGGED=1
LAST_TAG=$(git describe --abbrev=0 --tags) || IS_TAGGED=0
if [ $IS_TAGGED == 1 ]; then
echo "Last tag: ${LAST_TAG}"
LAST_VERSION=$(echo "$LAST_TAG" | sed -e 's|^v||')
echo "Last version: ${LAST_VERSION}"
SHOW_CHANGES_COMMAND="${script_home}/git-show-changes-since-last-tag"
else
LAST_VERSION="initial commit"
SHOW_CHANGES_COMMAND="git log --graph"
echo "No previous tagged version found."
fi
# extract version from CMakeLists.txt
MAJOR=$(grep -i 'set(${PROJECT_NAME}_MAJOR_VERSION' "${REPO_TOPLEVEL}"/CMakeLists.txt | sed -e 's|^set(${PROJECT_NAME}_MAJOR_VERSION ||i' -e 's|)$||')
MINOR=$(grep -i 'set(${PROJECT_NAME}_MINOR_VERSION' "${REPO_TOPLEVEL}"/CMakeLists.txt | sed -e 's|^set(${PROJECT_NAME}_MINOR_VERSION ||i' -e 's|)$||')
PATCH=$(grep -i 'set(${PROJECT_NAME}_PATCH_VERSION' "${REPO_TOPLEVEL}"/CMakeLists.txt | sed -e 's|^set(${PROJECT_NAME}_PATCH_VERSION ||i' -e 's|)$||')
# check if we could extract the version from CMakeLists.txt, enter it manually otherwise
if [ -z "$MAJOR" -o -z "$MINOR" -o -z "$PATCH" ]; then
echo "Cannot detect version from CMakeLists.txt!"
if [ "${RELEASE_PATCH}" = "1" -o "${RELEASE_MINOR}" = "1" -o "${RELEASE_MAJOR}" = "1" ]; then
echo "Try again without the --patch, --minor or --major option to enter the version manually."
exit 1
fi
echo -n "Enter new MAJOR version: "
read -r MAJOR
echo -n "Enter new MINOR version: "
read -r MINOR
echo -n "Enter new PATCH version: "
read -r PATCH
fi
# strip leading 0 from MAJOR, MINOR and PATCH
MAJOR=${MAJOR#0}
MINOR=${MINOR#0}
PATCH=${PATCH#0}
# calculate the next version if --patch, --minor or --major was given
if [ "${RELEASE_PATCH}" = "1" ]; then
PATCH=$((PATCH + 1))
elif [ "${RELEASE_MINOR}" = "1" ]; then
MINOR=$((MINOR + 1))
PATCH=0
elif [ "${RELEASE_MAJOR}" = "1" ]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
fi
# make sure the version numbers are two digits
MAJOR=$( printf "%02d" "${MAJOR}" )
MINOR=$( printf "%02d" "${MINOR}" )
PATCH=$( printf "%02d" "${PATCH}" )
NEXT_VERSION=${MAJOR}.${MINOR}.${PATCH}
echo "The new version will be: ${NEXT_VERSION}"
# check that the new version is different from the current
if [ "$NEXT_VERSION" == "$LAST_VERSION" ]; then
echo "The version in CMakeLists.txt is the same as the last tag. Please update first!"
rm -f "$CHANGES"
exit 1
fi
# write new version to CMakeLists.txt, if --patch, --minor or --major was given
if [ "${RELEASE_PATCH}" = "1" ] || [ "${RELEASE_MINOR}" = "1" ] || [ "${RELEASE_MAJOR}" = "1" ]; then
sed -i "${REPO_TOPLEVEL}"/CMakeLists.txt \
-e "s|set(\${PROJECT_NAME}_MAJOR_VERSION .*|set(\${PROJECT_NAME}_MAJOR_VERSION ${MAJOR})|i"
sed -i "${REPO_TOPLEVEL}"/CMakeLists.txt \
-e "s|set(\${PROJECT_NAME}_MINOR_VERSION .*|set(\${PROJECT_NAME}_MINOR_VERSION ${MINOR})|i"
sed -i "${REPO_TOPLEVEL}"/CMakeLists.txt \
-e "s|set(\${PROJECT_NAME}_PATCH_VERSION .*|set(\${PROJECT_NAME}_PATCH_VERSION ${PATCH})|i"
# commit the version bump
git checkout -b "${USER}/version-bump-${NEXT_VERSION}"
git add "${REPO_TOPLEVEL}"/CMakeLists.txt
git commit -m "chore: Bump version to ${NEXT_VERSION}"
git push
# check if repo is on github or gitlab, then create a PR/MR using the glab/gh CLI
origin=$(git remote get-url origin)
if [[ $origin = *gitlab* ]]; then
glab mr create --title "chore: Bump version to ${NEXT_VERSION}" --description "This MR bumps the version to ${NEXT_VERSION}" --target-branch master
elif [[ $origin = *github* ]]; then
gh pr create --title "chore: Bump version to ${NEXT_VERSION}" --body "This PR bumps the version to ${NEXT_VERSION}" --base master
else
echo "No gitlab or github remote found, please create a PR/MR manually."
exit
fi
# ask whether to merge the MR/PR and do so via the CLI
echo -n "Do you want to merge the PR/MR (y/N)? "
read -r ANSWER
if [ "${ANSWER,,}" != "y" ]; then
echo "Aborted."
exit 1
fi
if [[ $origin = *gitlab* ]]; then
glab mr merge -r -d
elif [[ $origin = *github* ]]; then
gh pr merge -r -d
else
echo "We should never end up here!"
exit 1
fi
# switch back to previous branch and delete the version bump branch
git checkout "${CURRENT_BRANCH}"
git pull
if [[ $origin = *gitlab* ]]; then
git branch -d "${USER}/version-bump-${NEXT_VERSION}"
fi
fi
# build next tag
if [ "${LAST_TAG:0:1}" == "v" ]; then
NEXT_TAG=v${NEXT_VERSION}
else
NEXT_TAG=${NEXT_VERSION}
fi
echo "The new tag will be: ${NEXT_TAG}"
# determine release type (major, minor or patch)
IFS='.' read -r -a LAST_VERSION_ARRAY <<< "$LAST_VERSION"
RELEASE_TYPE="PATCH"
if [ "${LAST_VERSION_ARRAY[0]}" != "${MAJOR}" ]; then
RELEASE_TYPE="MAJOR"
if [ "${MINOR}" -ne 0 ] || [ "${PATCH}" -ne 0 ]; then
echo "*** Warning: Major release, but minor and patch is not zero!"
fi
elif [ "${LAST_VERSION_ARRAY[1]}" != "${MINOR}" ]; then
RELEASE_TYPE="MINOR"
if [ "${PATCH}" -ne 0 ]; then
echo "*** Warning: Minor release, but patch is not zero!"
fi
fi
if [[ "${CURRENT_BRANCH}" != "master" ]] && [[ "${CURRENT_BRANCH}" != "main" ]]; then
RELEASE_TYPE="BACKPORT"
fi
echo "Release type: ${RELEASE_TYPE}"
# obtain the change log since the latest tag
CHANGES=$(mktemp)
git log "${LAST_VERSION}"..HEAD --pretty=%s | grep -v '^project-template:' | grep -v '(project-template): ' \
| grep -v '^Merge pull request ' | grep -v '^Merge branch ' | grep -v '^Merge project-template (automated)' \
| grep -v '^chore: Bump version to ' \
| awk '{print "- " $0}' > "$CHANGES"
# prepend header to the changelog
CHANGES_WITH_HEADER=$(mktemp)
{
echo "New ${RELEASE_TYPE} release ${NEXT_VERSION}"
echo ""
echo "Changes since ${LAST_VERSION}:"
echo ""
cat "$CHANGES"
} > "$CHANGES_WITH_HEADER"
# edit the changelog
"${EDITOR}" "${CHANGES_WITH_HEADER}"
# ask whether to proceed
echo ""
echo -n "Do you want to proceed with creating the tag (y/N)? "
read -r ANSWER
if [ "$ANSWER" != "y" ]; then
echo "Aborted."
rm -f "$CHANGES"
exit 1
fi
# now make the tag
git tag -a "${NEXT_TAG}" -F "${CHANGES_WITH_HEADER}"
# delete the changelog files
rm -f "$CHANGES" "$CHANGES_WITH_HEADER"
# push the tag after user confirmation
echo -n "Do you want to push the tag to the remote repository (y/N)? "
read -r ANSWER
if [ "$ANSWER" != "y" ]; then
echo "Aborted."
exit 1
fi
git push --tags