/
chag
executable file
·236 lines (197 loc) · 6.19 KB
/
chag
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
#!/usr/bin/env bash
# Parses changelog entries / create annotated git tags from a changelog entry.
#
# Author: Michael Dowling <https://github.com/mtdowling>
# License: MIT <https://github.com/mtdowling/chag/blob/master/LICENSE>
# URL: https://github.com/mtdowling/chag
set -e
# Clear out script variables
unset FILENAME TAG SIGN FORCE ADDV
version() {
echo "chag 1.1.3"
}
usage() {
version
cat <<EOT
Usage: chag <command> [<options>] [<args>]
Options:
--help Displays this message.
--version Displays the version number.
Commands:
contents Get the contents of a changelog entry.
entries List all versions in a changelog file.
latest Get the latest tag in a changelog.
tag Create an annotated git tag based on a changelog entry.
update Updates the version and date of the most recent changelog entry.
EOT
exit 0
}
contents_usage() {
cat <<EOT
Usage: chag contents [--help] [--file <path>] [--tag <tag>]
Outputs the contents of a changelog entry from a changelog file. If no
--tag option is provided, then the top-most entry in the changelog is
parsed.
Options:
--file Path to changelog. Defaults to CHANGELOG.md
--tag Tag version string to parse. Defaults to the latest.
--help Displays this message.
EOT
exit 0
}
tag_usage() {
cat <<EOT
Usage: chag tag [--help] [--file <path>] [--addv] [-s|--sign] [-f|--force]
Parses a changelog entry for the given tag and creates an annotated git
tag based on the changelog entry version and contents.
Options:
--file Path to changelog. Defaults to CHANGELOG.md
--addv Pass to prepend a "v" to the git tag (e.g., "v2.0.1")
--sign|-s Make a GPG-signed tag, using the default git e-mail address key.
--force|-f Delete an existing tag if present.
--help Displays this message.
EOT
exit 0
}
latest_usage() {
cat <<EOT
Usage: chag latest [--help] [--file <path>]
Get the latest changelog entry version from a CHANGELOG.
Options:
--file Path to changelog. Defaults to CHANGELOG.md
--help Displays this message.
EOT
exit 0
}
entries_usage() {
cat <<EOT
Usage: chag entries [--help] [--file <path>]
Lists all of the version numbers in a changelog file, separated by new lines.
Options:
--file Path to changelog. Defaults to CHANGELOG.md
--help Displays this message.
EOT
exit 0
}
update_usage() {
cat <<EOT
Usage: chag update [--help] [--file <path>] TAG
Updates the version and date of the most recent changelog entry.
Options:
--file Path to changelog. Defaults to CHANGELOG.md
--help Displays this message.
Arguments:
TAG Version number to set on the entry.
EOT
exit 0
}
# exits with an error message
die() {
echo "[FAILURE] $@" 1>&2
exit 1
}
# Gets the contents of a changelog entry
contents() {
get_entry ${TAG:=$(latest)}
}
tag() {
TAG=$(latest)
`git diff --quiet HEAD` || die 'Working directory not clean'
(entries | grep -q $TAG) || die "$TAG not found"
[ "$TAG" == "Unreleased" ] && die 'Refusing to tag an "Unreleased" version'
# Prepend a "v" if required or just use the provided TAG argument.
local real_tag="$TAG"
if [ ! -z "$ADDV" ]; then
local real_tag="v${real_tag}"
fi
# Run the command and get the exit code
local cmd="git tag $SIGN $FORCE -a -F - $real_tag"
# Strip extra spaces from the command
cmd=$(echo $cmd | sed 's/\s+/\s/')
echo "Tagging $real_tag with the following annotation:"
echo "===[ BEGIN ]==="; get_entry $TAG; echo "===[ END ]==="
echo "Running git command: $cmd"
(get_entry $TAG | $cmd) || die "Failed tagging $real_tag"
echo "[SUCCESS] Tagged $real_tag"
}
# Updates the top changelog entry with the provided version and today's date
update() {
TAG=${1:?"update requires a TAG"}
start=$(find_entry $(latest))
local replacement="## $TAG - $(date +%Y-%m-%d)"
perl -i -pe "s/.*/$replacement/ if \$.==${start}" $FILENAME
echo "[SUCCESS] Updated ${FILENAME} with ${TAG}"
}
# Get a list of entry versions separated by newlines
entries() {
entries_and_lines | cut -d ':' -f 2
}
# Prints each heading "line_number:line" from the changelog separated w/ "\n"
entries_and_lines() {
# Find each entry line number and version, separated by colons.
# Remove the H2 markdown syntax, don't take the date, and trim the string.
grep -n -E -e '^##\s+.+' $FILENAME \
| sed -E 's/## //' \
| cut -d ' ' -f 1 \
| sed -e 's/^ *//' -e 's/ *$//'
}
# Gets the latest version number from the changelog.
latest() {
entries | head -n1 | cut -d ':' -f 1
}
# Get the line number of a specific changelog entry.
find_entry() {
result=$(entries_and_lines | grep -E ":${1//./\\.}$" | cut -d ':' -f 1)
[ "" == "$result" ] && die "Tag not found"
echo $result
}
# Get the line number of the next entry after the given entry.
find_next_entry() {
content=$(entries_and_lines)
echo $(trim "${content#*$1}") | head -n1 | cut -d ':' -f 1
}
# Get the contents of an entry
get_entry() {
found_line=$(find_entry $1)
end_line=$(($(find_next_entry $1) - 1))
if [ $end_line -eq -1 ]; then
end_line=$(wc -l < $FILENAME)
fi
trim "$(sed -n "$((found_line + 1))","$end_line"p < $FILENAME)"
}
# Removes leading and trailing whitespace from the provided input.
trim() {
local var=$@
var="${var#"${var%%[![:space:]]*}"}"
var="${var%"${var##*[![:space:]]}"}"
echo "$var"
}
# Show help if no options are provided or if "--help" is passed.
[ $# -eq 0 ] || [ "$1" == "--help" ] && usage
# Show the version if just --version is provided.
[ "$1" == '--version' ] && version && exit 0
# Ensure a valid command was passed.
VALID="contents|tag|latest|entries|update"
[[ $VALID == *$1* ]] || die "Available commands: ${VALID}"
COMMAND="$1"; shift
while [ $# -gt 0 ]; do
opt="$1"
shift
case "$opt" in
--help) "${COMMAND}_usage";;
--file) FILENAME=${1:?"--file cannot be empty"}; shift;;
--tag) TAG=${1:?"--tag cannot be empty"}; shift;;
--addv) ADDV=1 ;;
--sign|-s) SIGN="--sign" ;;
--force|-f) FORCE="--force" ;;
--*) die "Unknown option '$opt'" ;;
*) break ;;
esac
done
# Find the default changelog if needed.
if [ -z "$FILENAME" ]; then
FILENAME=`pwd`"/CHANGELOG.md"
[ -f "$FILENAME" ] || FILENAME=`pwd`"/CHANGELOG"
fi
[ -f "$FILENAME" ] || die "File not found: $FILENAME"
$COMMAND $opt $@