forked from gchq/stroom-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_release.sh
executable file
·128 lines (102 loc) · 4.53 KB
/
tag_release.sh
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
#!/usr/bin/env bash
# This script creates and pushes a git annotated tag with a commit message taken from
# the appropriate section of the CHANGELOG.md.
set -e
setup_echo_colours() {
#Shell Colour constants for use in 'echo -e'
# shellcheck disable=SC2034
{
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
LGREY='\e[37m'
DGREY='\e[90m'
NC='\033[0m' # No Color
}
}
error_exit() {
msg="$1"
echo -e "${RED}ERROR${GREEN}: ${msg}${NC}"
echo
exit 1
}
main() {
# Git tags should match this regex to be a release tag
readonly RELEASE_VERSION_REGEX='^v[0-9]+\.[0-9]+.*$'
setup_echo_colours
echo
local -r changelog_file="CHANGELOG.md"
if [ $# -ne 1 ]; then
echo -e "${RED}ERROR${GREEN}: Missing version argument${NC}"
echo -e "${GREEN}Usage: ${BLUE}./tag_release.sh version${NC}"
echo -e "${GREEN}e.g: ${BLUE}./tag_release.sh v2.1-beta.20${NC}"
echo
echo -e "${GREEN}This script will extract the changes from the ${BLUE}CHANGELOG.md${GREEN} file for the passed${NC}"
echo -e "${GREEN}version tag and create an annotated git commit with it. The tag commit will be pushed${NC}"
echo -e "${GREEN}to the origin.${NC}"
exit 1
fi
local version=$1
local curr_date
curr_date="$(date +%Y-%m-%d)"
if [[ ! "${version}" =~ ${RELEASE_VERSION_REGEX} ]]; then
error_exit "Version [${BLUE}${version}${GREEN}] does not match the release version regex ${BLUE}${RELEASE_VERSION_REGEX}${NC}"
fi
if [ ! -f "${changelog_file}" ]; then
error_exit "The file ${BLUE}${changelog_file}${GREEN} does not exist in the current directory.${NC}"
fi
if ! git rev-parse --show-toplevel > /dev/null 2>&1; then
error_exit "You are not in a git repository. This script should be run from the root of a repository.${NC}"
fi
if git tag | grep -q "^${version}$"; then
error_exit "This repository has already been tagged with [${BLUE}${version}${GREEN}].${NC}"
fi
if ! grep -q "^\s*##\s*\[${version}\]" "${changelog_file}"; then
error_exit "Version [${BLUE}${version}${GREEN}] is not in the CHANGELOG.${NC}"
fi
if ! grep -q "^\s*##\s*\[${version}\] - ${curr_date}" "${changelog_file}"; then
error_exit "Cannot find a heading with today's date [${BLUE}## [${version}] - ${curr_date}${GREEN}] in the CHANGELOG.${NC}"
fi
if ! grep -q "^\[${version}\]:" "${changelog_file}"; then
echo -e "${RED}ERROR${GREEN}: Version [${BLUE}${version}${GREEN}] does not have a link entry at the bottom of the CHANGELOG.${NC}"
echo -e "${GREEN}e.g.:${NC}"
echo -e "${BLUE}[v1.0-beta.17]: https://github.com/gchq/stroom-query/compare/v2.1-beta.19...v2.1-beta.20${NC}"
echo
exit 1
fi
if [ "$(git status --porcelain 2>/dev/null | wc -l)" -ne 0 ]; then
error_exit "There are uncommitted changes or untracked files. Commit them before tagging.${NC}"
fi
local change_text
# delete all lines up to and including the desired version header
# then output all lines until quitting when you hit the next
# version header
change_text="$(sed "1,/^\s*##\s*\[${version}\]/d;/## \[/Q" "${changelog_file}")"
# Add the release version as the top line of the commit msg, followed by
# two new lines
change_text="${version}\n\n${change_text}"
# Remove any repeated blank lines with cat -s
change_text="$(echo -e "${change_text}" | cat -s)"
echo -e "${GREEN}You are about to create the git tag ${BLUE}${version}${GREEN} with the following commit message.${NC}"
echo -e "${GREEN}If this is pretty empty then you need to add some entries to the ${BLUE}${changelog_file}${NC}"
echo -e "${DGREY}------------------------------------------------------------------------${NC}"
echo -e "${YELLOW}${change_text}${NC}"
echo -e "${DGREY}------------------------------------------------------------------------${NC}"
read -rsp $'Press "y" to continue, any other key to cancel.\n' -n1 keyPressed
if [ "$keyPressed" = 'y' ] || [ "$keyPressed" = 'Y' ]; then
echo
echo -e "${GREEN}Tagging the current commit${NC}"
echo -e "${change_text}" | git tag -a --file - "${version}"
echo -e "${GREEN}Pushing the new tag${NC}"
git push origin "${version}"
echo -e "${GREEN}Done.${NC}"
echo
else
echo
echo -e "${GREEN}Exiting without tagging a commit${NC}"
echo
exit 0
fi
}
main "$@"