-
Notifications
You must be signed in to change notification settings - Fork 113
/
mkChangelog
executable file
·233 lines (205 loc) · 5.1 KB
/
mkChangelog
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
#! /bin/bash
function Recho() { echo -e "\e[0;31m""$@""\e[0m"; }
function Gecho() { echo -e "\e[0;32m""$@""\e[0m"; }
function Becho() { echo -e "\e[0;34m""$@""\e[0m"; }
function errexit() {
exec >&2
Recho "Error: $@"
exit 1
}
function usage() {
exec >&2
cat <<EOF
Usage:
$(basename $0) [OPTIONS]
Options:
-h,-?,--help This page.
-n Dryrun.
Prepare a new changes file entry preloaded with all commits since the last
changes tag and load it into \$EDITOR (vi). If the version file was changed,
optionally submitt and tag the new changes. Otherwise simply leave the changes
in place.
Don't forgett to push created tags as well: git push --tags
EOF
exit 1
}
DRYRUN=0
case "$1" in
-[n]*)
DRYRUN=1
;;
-[hH?]*)
usage
;;
--help)
usage
;;
esac
export LC_ALL=""
export LANG="en"
function Edit()
{
local FILE="$1"
vi "$FILE"
sed -i 's/ \+$//' "$FILE"
}
EMAIL="$(git config --get user.email)"
CHANGESFILE=$(ls package/*.changes)
test -f "$CHANGESFILE" || errexit "No changes file '$CHANGESFILE'"
VERSIONFILE="VERSION.cmake"
test -f "$VERSIONFILE" || errexit "No version file '$VERSIONFILE'"
function getversion() {
cat "$VERSIONFILE" \
| awk '
function getnum() {
gsub("^[^\"]*\"","")
gsub("\".*$","")
}
/^ *SET *\( *VERSION_MAJOR *"[0-9]+" *\)/ {getnum();major=$0}
/^ *SET *\( *VERSION_MINOR *"[0-9]+" *\)/ {getnum();minor=$0}
/^ *SET *\( *VERSION_PATCH *"[0-9]+" *\)/ {getnum();patch=$0}
/^# LAST RELEASED:/ {
gsub("^.*RELEASED: *","");
gsub(" +$","");
lastrelease=$0
}
END {
thisrelease = major"."minor"."patch
printf "LAST_RELEASE='%s'\n", lastrelease
printf "THIS_RELEASE='%s'\n", major"."minor"."patch
printf "THIS_MINOR='%s'\n", minor
printf "THIS_PATCH='%s'\n", patch
}
'
}
function setversion() {
local KEY="$1"
local VAL="$2"
sed -i "s/^ *SET *( *${KEY} .*/SET(${KEY} \"${VAL}\")/" "$VERSIONFILE"
}
function sameVersion() {
test "$LAST_RELEASE" == "$THIS_RELEASE"
}
function getchanges() {
git log --no-merges --pretty=format:'@@%B' "$LAST_RELEASE"..HEAD \
| awk '/^@@/{p=1}/^@@Translated using Weblate/{p=0}(p){print}' \
| sed '/./{H;$!d};x;/./{s/ *\n */ /g;s/^ *//;s/ *$//;/[^]})!?:.]$/s/$/./;p};d' \
| fold -s -w 66 | sed '/^@@/{s/^../- /;p;d};s/^/ /'
}
function newchangesentry() {
echo "-------------------------------------------------------------------"
echo "$(date) - $EMAIL"
echo ""
echo "$(getchanges)"
sameVersion || {
echo "- version $THIS_RELEASE"
}
echo ""
}
function is_fast_forward() {
git fetch
test "$(git rev-list HEAD..origin/$(git name-rev --name-only HEAD) --count)" == "0"
}
is_fast_forward || {
Recho "!!!"
Recho "!!! Branch is not fast-forward. Pull changes first."
Recho "!!!"
exit 7
}
git status --porcelain | grep '^[^ ?]' | grep -v "$VERSIONFILE\|$CHANGESFILE" && {
Becho "!!! Files other than version and changes are added to the index."
Becho "!!! Doing dryrun..."
DRYRUN=1
}
if [ "$DRYRUN" == "1" ]; then
eval $(getversion)
newchangesentry
sameVersion && {
Becho "!!! Version is unchanged at $LAST_RELEASE."
}
exit 0
fi
# check version file
#
while true; do
# $LAST_RELEASE
# $THIS_RELEASE
eval $(getversion)
sameVersion && {
newchangesentry
Becho "!!! Version is unchanged at $LAST_RELEASE."
read -n 1 -p "$(Gecho "(a)bort, (c)ontinue, (P) patch, (M) minor, (e)dit version [e]: ")" RES
echo
case "${RES:-e}" in
[eE]*)
Edit $VERSIONFILE
continue
;;
[P])
setversion VERSION_PATCH $(($THIS_PATCH + 1))
eval $(getversion)
continue
;;
[M])
setversion VERSION_MINOR $(($THIS_MINOR + 1))
setversion VERSION_PATCH 0
eval $(getversion)
continue
;;
[cC])
Becho "!!! Leave $VERSIONFILE untouched"
break
;;
*)
errexit "aborted"
;;
esac
}
break
done
# prepare changes file
#
TMPFILE=$(mktemp)
trap " [ -f \"$TMPFILE\" ] && /bin/rm -f -- \"$TMPFILE\" " 0 1 2 3 13 15
{ newchangesentry; cat $CHANGESFILE; } >$TMPFILE
RES=e
while [ "$RES" == "e" ]; do
Edit $TMPFILE
echo
awk '{print}/^----------/{n=n+1; if ( n == 2 ) exit 0; }' $TMPFILE
read -n 1 -p "$(Gecho "(a)bort, (c)ontinue, (s)ubmitt, (e)dit : ")" RES
echo
case "$RES" in
[eE]*)
RES=e
;;
[cCsS])
Becho "!!! Store new $CHANGESFILE"
mv $TMPFILE $CHANGESFILE
chmod 644 $CHANGESFILE
test "$RES" == "s" && {
if [ "$LAST_RELEASE" == "$THIS_RELEASE" ]; then
git add "$CHANGESFILE" && git commit -m "changes"
else
Becho "!!! Remember new version $THIS_RELEASE in $VERSIONFILE"
sed -i "s/^# LAST RELEASED:.*$/# LAST RELEASED: $THIS_RELEASE/" $VERSIONFILE
if git add "$CHANGESFILE" "$VERSIONFILE" \
&& git commit -m "changes $THIS_RELEASE" \
&& git tag -m "tagging $THIS_RELEASE" "$THIS_RELEASE" HEAD; then
Becho "!!!"
Becho "!!! Do not forget to push the commit and the tag: $(Gecho git push --tags origin HEAD)"
Becho "!!!"
else
Recho "!!!"
Recho "!!! Commit failed. Check manually. (git reset HEAD~)"
Recho "!!!"
exit 9
fi
fi
}
;;
*)
Becho "!!! Leave $CHANGESFILE untouched"
;;
esac
done