-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlog2dch
executable file
·312 lines (279 loc) · 9.95 KB
/
log2dch
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
VERBOSITY=0
TEMP_D=""
COMMIT_URL=${COMMIT_URL:-https://github.com/canonical/cloud-init/commit/}
BUG_URL=${BUG_URL:-https://pad.lv/}
FILTER_LIST=( "lp-to-git-user" "Merge pull request" "Merge branch" )
error() { echo "$@" 1>&2; }
fail() { local r=$?; [ $r -eq 0 ] && r=1; failrc "$r" "$@"; }
failrc() { local r=$1; shift; [ $# -eq 0 ] || error "$@"; exit "$r"; }
Usage() {
cat <<EOF
Usage: ${0##*/} [ options ] [input-file]
Read input-file which is a git or bzr log and create a
debian/changelog formatted file from the result.
options:
--vcs VCS which log format is this ('git', 'auto', 'bzr')
default: auto
--skip-bugs Do not print bug info
--trello output in markdown for checklist items in trello
--hackmd output markdown formated for hackmd.io
--commit-url=URL use a different commit url.
default=$COMMIT_URL
--bug-url=URL use a different bug url.
default=$BUG_URL
Assuming git style log commit messages, this reads the makes the
summary line into a debian/changelog style entry with author
in [] and mentions bugs fixed (bzr via '--fixes', git via LP: #
in commit messages.)
Examples:
* bzr based simplestreams (bzr branch lp:simplestreams)
$ bzr log -r-5.. | log2dch
- Keystone v3 Support (LP: #1686437)
- tools/ubuntu_versions.py: Exclude old versions by version not name
- fix flake8
- tests: change to having http server select its own port
- flake8: fix style errors reported by pycodestyle 2.1.0
* git based cloud-init (git clone http://git.launchpad.net/cloud-init)
$ git log HEAD~5.. | log2dch
- tests: execute: support command as string [Joshua Powers]
- schema and docs: Add jsonschema to resizefs and bootcmd modules
[Chad Smith]
- tools: Add xkvm script, wrapper around qemu-system [Joshua Powers]
- vmware customization: return network config format
[Sankar Tanguturi] (LP: #1675063)
- Ec2: only attempt to operate at local mode on known platforms.
(LP: #1715128)
EOF
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
cleanup() {
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}
debug() {
local level=${1}; shift;
[ "${level}" -gt "${VERBOSITY}" ] && return
error "${@}"
}
bzr_log_to_dch() {
local line="" commit="" bugs="" bug=""
local func="${1:-print_dch}"
local skip_bugs="${2:-false}"
while :; do
read line || break
case "$line" in
revno:\ *)
if [ -n "$commit" ]; then
$skip_bugs && bugs=""
$func "$commit" "$subject" "$author" "$bugs"
fi
commit=${line#*: }
bugs=""
author=""
subject=""
;;
author:*) author="${line#author: }";;
fixes\ bug:*)
# fixes bug: https://launchpad.net/bugs/1618429
bug="#${line##*/}"
bugs="${bugs:+${bugs}, }${bug}";;
message:)
read subject;;
esac
done
if [ -n "$commit" ]; then
$skip_bugs && bugs=""
$func "$commit" "$subject" "$author" "$bugs"
fi
}
print_hackmd() {
local commit="$1" subject="$2" author="$3" bugs="$4" bug_tracker="$5"
local aname="" abugs="" buf="" bug="" t=""
local url="${COMMIT_URL}"
t=${commit#????????}
local bugmd="" pre=""
for bug in ${bugs}; do
bug=${bug%,}
[ -z "$bugmd" ] && pre="LP: "
bugmd="${bugmd}[$pre${bug}](${BUG_URL}$bug),"
done
bugmd=${bugmd%,}
t=${commit#????????}
buf="${bugmd:+${bugmd} }${subject}"
buf="${buf} \[[${commit%$t}]($url${commit%$t})\]"
if [[ $buf == 'tests:'* ]]; then
buf="~~NOTEST ${buf}~~"
fi
for filter in "${FILTER_LIST[@]}"; do
if [[ "$subject" != "${subject/$filter//}" ]]; then
return # skip echo because our filter string matched
fi
done
echo "- $buf"
}
print_trello() {
local commit="$1" subject="$2" author="$3" bugs="$4" bug_tracker="$5"
local aname="" abugs="" buf="" bug="" t=""
local url="${COMMIT_URL}"
t=${commit#????????}
buf="[${commit%$t}](${url}${commit%$t})"
for bug in ${bugs}; do
bug=${bug%,}
buf="${buf:+${buf} }[${bug}](${BUG_URL}$bug)"
done
if [ "${subject#*\(#}" = "$subject" ]; then
pr_num=""
else
# replace PR # with a markdown link
pr_num=${subject#*\(#} # redact leading prefix before.*(#
pr_num=${pr_num%)} # truncate trailing ) if present
subject=${subject/$pr_num/[${pr_num}](http://github.com/canonical/cloud-init/pull/${pr_num})}
fi
buf="${buf:+${buf} }$subject"
for filter in "${FILTER_LIST[@]}"; do
if [[ "$subject" != "${subject/$filter//}" ]]; then
return # skip echo because our filter string matched
fi
done
echo "$buf"
}
print_dch() {
local commit="$1" subject="$2" author="$3" bugs="$4" bug_tracker="$5"
local aname="" abugs="" indent=" - " indent2=" " ll=79
local i=""
aname=${author% <*}
case "$aname" in
Chad\ Smith|Lucas\ Moura|James\ Falcon|Brett\ Holman|Alberto\ Contreras) aname="";;
esac
abugs="${aname:+ [${aname}]}${bugs:+ (${bug_tracker}: ${bugs})}"
for filter in "${FILTER_LIST[@]}"; do
if [[ "$subject" != "${subject/$filter//}" ]]; then
return # skip echo because our filter string matched
fi
done
if [ $((${#subject}+${#abugs})) -le $((ll-${#indent})) ]; then
echo "${indent}${subject}${abugs}"
elif [ ${#subject} -ge $((ll-${#indent})) ]; then
echo "${subject}${abugs}" |
fmt --width=$((ll-${#indent})) |
sed -e "1s/^/${indent}/; 1n;" \
-e 's/^[ ]*//' \
-e '/^[ ]*$/d' -e "s/^/$indent2/" -e 's/[ ]\+$//'
else
( echo "${subject}"; echo "${abugs}" ) |
fmt --width=$((ll-${#indent})) |
sed -e "1s/^/${indent}/; 1n;" \
-e 's/^[ ]*//' \
-e '/^[ ]*$/d' -e "s/^/$indent2/" -e 's/[ ]\+$//'
fi
}
git_log_to_dch() {
local line="" commit="" bugs=""
local func=${1:-print_dch}
local skip_bugs=${2:-false}
local bug_tracker="LP"
while :; do
read line || break
case "$line" in
commit\ *)
if [ -n "$commit" ]; then
if [ "${skip_bugs}" = "true" ]; then
bugs="" # Don't append bugs referenced in commit body
# Replace "LP: #" with "LP:" if present in subject
# to avoid trigging SRU validation of bugs
subject=${subject/LP: #/LP:}
fi
"$func" "$commit" "$subject" "$author" "$bugs" "$bug_tracker"
fi
commit=${line#commit }
bugs=""
author=""
subject=""
;;
Author:*) author="${line#Author: }";;
LP:*) bug_tracker="LP"; bugs="${bugs:+${bugs}, }${line#*: }";;
Fixes\ GH*|Closes\ GH*)
bug_tracker="GH";
bug="${line#*\ GH-}";
[ ! -z "${bug##*[!0-9]*}" ] && bugs="${bugs:+${bugs}, }$bug";;
"") [ -z "$subject" ] && read -r subject;;
esac
done
if [ -n "$commit" ]; then
if [ "${skip_bugs}" = "true" ]; then
bugs="" # Don't append bugs referenced in commit body
# Replace "LP: #" with "LP:" if present in subject
# to avoid trigging SRU validation of bugs
subject=${subject/LP: #/LP:}
fi
$func "$commit" "$subject" "$author" "$bugs" "$bug_tracker"
fi
}
assert_temp_d() {
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
fail "failed creating temp directory";
}
main() {
local short_opts="ht:v"
local long_opts="bug-url:,commit-url:,hackmd,help,skip-bugs,trello,vcs:,verbose"
local getopt_out=""
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
{ bad_Usage; return; }
local cur="" next="" vcs="auto" input="-" omode="dch" skip_bugs=false
while [ $# -ne 0 ]; do
cur="$1"; next="$2";
case "$cur" in
-h|--help) Usage ; exit 0;;
--skip-bugs) skip_bugs=true;;
--trello) omode="trello";;
--hackmd) omode="hackmd";;
--commit-url) COMMIT_URL="$next";;
--bug-url) BUG_URL="$next";;
-t|--vcs) vcs="$next";
case "$vcs" in
auto|git|bzr)
bad_Usage "vcs must be one of auto, git, bzr.";
return;;
esac
;;
-v|--verbose) VERBOSITY=$((VERBOSITY+1));;
--) shift; break;;
esac
shift;
done
[ $# -le 1 ] || { bad_Usage "got $# arguments, expected 0 or 1"; return; }
[ $# -eq 1 ] && { input="$1"; shift; }
trap cleanup EXIT
local oinput="$input"
if [ "$vcs" = "auto" ]; then
if [ "$input" = "-" ]; then
assert_temp_d
input="${TEMP_D}/input"
cat > "$input"
fi
if grep -q '^revno:' "$input"; then
vcs="bzr"
elif grep -q '^commit ' "$input"; then
vcs="git"
else
error "failed to figure out vcs type in $oinput"
return
fi
fi
if [ "$input" != "-" ]; then
exec < "$input" ||
{ error "failed to open $input for reading"; return 1; }
fi
case "$omode" in
trello) printer="print_trello";;
hackmd) printer="print_hackmd";;
*) printer="print_dch";;
esac
local func="${vcs}_log_to_dch"
"$func" "$printer" $skip_bugs
return
}
main "$@"
# vi: ts=4 expandtab