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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#!/bin/sh
#
# Sets roverlay's version.
#
# Usage: setver [-S,--src <dir>] [--pretend] [--suffix <str>] [--reset]
# [--git-add] [--git-commit] [--force-commit] [--git-tag]
# [+,pbump|++,mbump|Mbump|[setver] <ver>]
#
# Actions:
# +, pbump -- increase patchlevel by one
# ++, mbump -- increase minor version by one and set patchlevel to 0
# Mbump -- increase major version by one and set minor/patchlevel to 0
# setver <ver> -- set a specific version
#
# Options:
# -S, --src <dir> -- set roverlay source directory (default: $PWD)
# --pretend -- just show what would be done
# --suffix <str> -- replace old version suffix with <str>
# -l, --list-files -- just list files that would be edited by this script
# (usage example: setver -l | xargs git checkout HEAD --)
# --reset -- check out VERSION file from git HEAD before doing anything
# --git-add -- git-add modified files
# --git-commit -- commit changes (commit message: "roverlay <newver>")
# --force-commit -- enforce git commit (allow other files)
# --git-tag -- run git-tag <newver> after git-commit
#
set -e
set -u
readonly IFS_DEFAULT="${IFS}"
readonly PY_FILES_TO_EDIT="roverlay/core.py setup.py"
readonly X_GIT="${X_GIT:-git}"
# get_git_commit_message ( new_version, **commit_msg! )
#
# Creates a commit message and stores it in $commit_msg.
# An empty str can be set if no commit should be made.
#
get_git_commit_message() {
commit_msg="roverlay ${1:?}"
}
# get_git_tag ( new_version, **tag! )
#
# Creates a git tag str and stores it in $tag.
# An empty str suppresses tag creation.
#
get_git_tag() {
tag="${1:?}"
}
# @noreturn die ( [message], [exit_code] ), raises exit($exit_code)
#
# Lets the script die.
#
die() {
echo "${1:+died: }${1:-died.}" 1>&2; exit ${2:-2};
}
# autodie ( *cmdv )
#
# Runs *cmdv and dies on non-zero return.
# ("set -ex" is too verbose)
#
autodie() {
"$@" || die "command '${*}' returned ${?}." ${?}
}
# parse_version ( version_str, **major!, **minor!, **plvl!, **suffix! )
#
# Splits a version_str into its components.
#
# Example: 0.2.6-pre1 => major=0 minor=2 plvl=6 suffix=-pre1
#
parse_version() {
unset -v major minor plvl suffix
local IFS="."
set -- ${1}
IFS="${IFS_DEFAULT}"
[ -n "${1-}" ] && [ -n "${2-}" ] && [ -n "${3-}" ] || return 2
major="${1:?}"
minor="${2:?}"
plvl="${3:?}"
shift 3 || die "error in parse_version()"
suffix=
while [ ${#} -gt 0 ]; do suffix="${suffix}.${1}"; shift; done
suffix="${suffix#.}"
}
# inc ( k, **v0! )
#
# Increments $k by one and stores the result in $v0.
#
inc() {
v0=$(( ${1:?} + 1 ))
# unlikely:
[ ${v0} -gt ${1} ] || die "overflow"
}
# do_git_add ( *files, **want_gitadd, **X_GIT )
#
do_git_add() {
${want_gitadd} || return 0
printf "git-add: %s\n" "${*}"
${want_pretend} || autodie "${X_GIT}" add -- "$@"
}
# do_git_commit_and_tag (
# version_str, numfiles_changed,
# **want_gitcommit, **want_forcecommit, **want_gittag, **X_GIT
# )
#
do_git_commit_and_tag() {
${want_gitcommit} || return 0
local commit_msg commit_type tag
autodie get_git_commit_message "${1}"
autodie get_git_tag "${1}"
if [ -z "${commit_msg}" ]; then
return 0
elif ${want_pretend}; then
commit_type=maybe
elif \
[ $(git status --porcelain -- . | grep -c -- ^[MADRCU]) -eq ${2} ]
then
commit_type=clean
elif ${want_forcecommit}; then
commit_type=forced
else
die "cannot commit changes (try --force-commit)."
fi
printf "git-commit [%s]: %s\n" "${commit_type}" "${commit_msg}"
${want_pretend} || autodie "${X_GIT}" commit -m "${commit_msg}"
${want_gittag} && [ -n "${tag}" ] || return 0
printf "git-tag: %s\n" "${tag}"
${want_pretend} || autodie "${X_GIT}" tag "${tag}"
}
# set defaults / parse args
autodie hash "${X_GIT}"
S="${PWD}"
unset -v V
unset -v ACTION
unset -v new_suffix
readonly _boolvars="pretend gitadd gitcommit forcecommit gittag reset"
for iter in ${_boolvars}; do eval "want_${iter}=false"; done
doshift=
while [ ${#} -gt 0 ]; do
doshift=1
case "${1}" in
'') : ;;
-S|--src)
[ -n "${2-}" ] || die "one non-empty arg required after '${1}'."
doshift=2
S="${2:?}"
;;
--pretend) want_pretend=true ;;
--reset) want_reset=true ;;
--git-add) want_gitadd=true ;;
--git-commit) want_gitcommit=true ;;
--force-commit) want_forcecommit=true ;;
--git-tag) want_gittag=true ;;
[Mmp]bump) ACTION="${1}" ;;
'+') ACTION=pbump ;;
'++') ACTION=mbump ;;
*.*.*) ACTION=setver; V="${1}" ;;
setver)
[ -n "${2-}" ] || die "one non-empty arg required after '${1}'."
doshift=2
ACTION=setver
V="${2:?}"
;;
--suffix)
[ -n "${2+SET}" ] || die "one arg required after '${1}'."
doshift=2
new_suffix="${2?}"
;;
-l|--list-files)
for fname in ${PY_FILES_TO_EDIT}; do echo "${fname}"; done
echo "VERSION"
exit 0
;;
*)
die "unknown arg: ${1}" 64
;;
esac
[ ${doshift} -eq 0 ] || shift ${doshift} || die "argparse: shift failed"
done
! { ${want_gittag} || ${want_forcecommit}; } || want_gitcommit=true
! ${want_gitcommit} || want_gitadd=true
readonly S ACTION
for iter in ${_boolvars}; do readonly "want_${iter}"; done
cd "${S}" || die "chdir ${S}"
! ${want_reset} || autodie "${X_GIT}" checkout HEAD -- VERSION
readonly OLDVER="$(cat "${S}/VERSION")" || die "failed to read ${S}/VERSION"
autodie parse_version "${OLDVER}"
: ${new_suffix="${suffix}"}
# get new version
case "${ACTION-}" in
pbump)
inc "${plvl}"
V="${major}.${minor}.${v0}${new_suffix}"
;;
mbump)
inc "${minor}"
V="${major}.${v0}.0${new_suffix}"
;;
Mbump)
inc "${major}"
V="${v0}.0.0${new_suffix}"
;;
setver)
true
;;
*)
${want_reset} || die "unknown or no action specified."
exit 0
;;
esac
# edit files
q="\"\'"
re_pyfile_ver="^(\s*version\s*=\s*)[${q}]?([^\s;,${q}]*)[${q}]?(\s*[;,]?\s*)\$"
v0=0
_fmt="edit %-18s: %8s -> %s\n"
for fname in ${PY_FILES_TO_EDIT}; do
inc "${v0}"
f="${S}/${fname}"
fver="$(sed -rn -e "s@${re_pyfile_ver}@\2@p" < "${f}")"
printf "${_fmt}" "${fname}" "${fver}" "${V}"
${want_pretend} || \
autodie sed -r -e "s@${re_pyfile_ver}@\1\"${V}\"\3@" -i "${f}"
do_git_add "${f}"
done
inc "${v0}"
printf "${_fmt}" "VERSION" "${OLDVER}" "${V}"
${want_pretend} || echo "${V}" > "${S}/VERSION" || die "failed to write VERSION"
do_git_add "${S}/VERSION"
[ ${v0} -gt 0 ] || die "numfiles?"
printf "edited %d files in total.\n" "${v0}"
autodie do_git_commit_and_tag "${V}" "${v0}"
|