Debian Codegraph @

.github/changelog.sh

raw symbols SCIP
1 #!/usr/bin/env zsh
2
3 ##############################
4 # CHANGELOG SCRIPT CONSTANTS #
5 ##############################
6
7 #* Holds the list of valid types recognized in a commit subject
8 #* and the display string of such type
9 local -A TYPES
10 TYPES=(
11 BUILD "Build system"
12 CHORE "Chore"
13 CI "CI"
14 CUT "Features removed"
15 DOC "Documentation"
16 FEAT "Features"
17 FIX "Bug fixes"
18 LICENSE "License update"
19 MAKE "Build system"
20 OPTIMIZE "Code optimization"
21 PERF "Performance"
22 REFACTOR "Code Refactoring"
23 REFORMAT "Code Reformatting"
24 REVERT "Revert"
25 TEST "Testing"
26 )
27
28 #* Types that will be displayed in their own section,
29 #* in the order specified here.
30 local -a MAIN_TYPES
31 MAIN_TYPES=(FEAT FIX PERF REFACTOR DOCS DOC)
32
33 #* Types that will be displayed under the category of other changes
34 local -a OTHER_TYPES
35 OTHER_TYPES=(MAKE TEST STYLE CI OTHER)
36
37 #* Commit types that don't appear in $MAIN_TYPES nor $OTHER_TYPES
38 #* will not be displayed and will simply be ignored.
39
40
41 ############################
42 # COMMIT PARSING UTILITIES #
43 ############################
44
45 function parse-commit {
46
47 # This function uses the following globals as output: commits (A),
48 # subjects (A), scopes (A) and breaking (A). All associative arrays (A)
49 # have $hash as the key.
50 # - commits holds the commit type
51 # - subjects holds the commit subject
52 # - scopes holds the scope of a commit
53 # - breaking holds the breaking change warning if a commit does
54 # make a breaking change
55
56 function commit:type {
57 local commit_message="$1"
58 local type="$(sed -E 's/^([a-zA-Z_\-]+)(\(.+\))?!?: .+$/\1/' <<< "$commit_message"| tr '[:lower:]' '[:upper:]')"
59 # If $type doesn't appear in $TYPES array mark it as 'other'
60 if [[ -n "${(k)TYPES[(i)${type}]}" ]]; then
61 echo $type
62 else
63 echo other
64 fi
65 }
66
67 function commit:scope {
68 local scope
69
70 # Try to find scope in "type(<scope>):" format
71 # Scope will be formatted in lower cases
72 scope=$(sed -nE 's/^[a-zA-Z_\-]+\((.+)\)!?: .+$/\1/p' <<< "$1")
73 if [[ -n "$scope" ]]; then
74 echo "$scope" | tr '[:upper:]' '[:lower:]'
75 return
76 fi
77
78 # If no scope found, try to find it in "<scope>:" format
79 # Make sure it's not a type before printing it
80 scope=$(sed -nE 's/^([a-zA-Z_\-]+): .+$/\1/p' <<< "$1")
81 if [[ -z "${(k)TYPES[(i)$scope]}" ]]; then
82 echo "$scope"
83 fi
84 }
85
86 function commit:subject {
87 # Only display the relevant part of the commit, i.e. if it has the format
88 # type[(scope)!]: subject, where the part between [] is optional, only
89 # displays subject. If it doesn't match the format, returns the whole string.
90 sed -E 's/^[a-zA-Z_\-]+(\(.+\))?!?: (.+)$/\2/' <<< "$1"
91 }
92
93 # Return subject if the body or subject match the breaking change format
94 function commit:is-breaking {
95 local subject="$1" body="$2" message
96
97 if [[ "$body" =~ "BREAKING CHANGE: (.*)" || \
98 "$subject" =~ '^[^ :\)]+\)?!: (.*)$' ]]; then
99 message="${match[1]}"
100 # remove CR characters (might be inserted in GitHub UI commit description form)
101 message="${message//$'\r'/}"
102 # skip next paragraphs (separated by two newlines or more)
103 message="${message%%$'\n\n'*}"
104 # ... and replace newlines with spaces
105 echo "${message//$'\n'/ }"
106 else
107 return 1
108 fi
109 }
110
111 # Return truncated hash of the reverted commit
112 function commit:is-revert {
113 local subject="$1" body="$2"
114
115 if [[ "$subject" = Revert* && \
116 "$body" =~ "This reverts commit ([^.]+)\." ]]; then
117 echo "${match[1]:0:7}"
118 else
119 return 1
120 fi
121 }
122
123 # Parse commit with hash $1
124 local hash="$1" subject body warning rhash
125 subject="$(command git show -s --format=%s $hash)"
126 body="$(command git show -s --format=%b $hash)"
127
128 # Commits following Conventional Commits (https://www.conventionalcommits.org/)
129 # have the following format, where parts between [] are optional:
130 #
131 # type[(scope)][!]: subject
132 #
133 # commit body
134 # [BREAKING CHANGE: warning]
135
136 # commits holds the commit type
137 commits[$hash]="$(commit:type "$subject")"
138 # scopes holds the commit scope
139 scopes[$hash]="$(commit:scope "$subject")"
140 # subjects holds the commit subject
141 subjects[$hash]="$(commit:subject "$subject")"
142
143 # breaking holds whether a commit has breaking changes
144 # and its warning message if it does
145 if warning=$(commit:is-breaking "$subject" "$body"); then
146 breaking[$hash]="$warning"
147 fi
148
149 # reverts holds commits reverted in the same release
150 if rhash=$(commit:is-revert "$subject" "$body"); then
151 reverts[$hash]=$rhash
152 fi
153 }
154
155 #############################
156 # RELEASE CHANGELOG DISPLAY #
157 #############################
158
159 function display-release {
160
161 # This function uses the following globals: output, version,
162 # commits (A), subjects (A), scopes (A), breaking (A) and reverts (A).
163 #
164 # - output is the output format to use when formatting (raw|text|md)
165 # - version is the version in which the commits are made
166 # - commits, subjects, scopes, breaking, and reverts are associative arrays
167 # with commit hashes as keys
168
169 # Remove commits that were reverted
170 local hash rhash
171 for hash rhash in ${(kv)reverts}; do
172 if (( ${+commits[$rhash]} )); then
173 # Remove revert commit
174 unset "commits[$hash]" "subjects[$hash]" "scopes[$hash]" "breaking[$hash]"
175 # Remove reverted commit
176 unset "commits[$rhash]" "subjects[$rhash]" "scopes[$rhash]" "breaking[$rhash]"
177 fi
178 done
179
180 # If no commits left skip displaying the release
181 if (( $#commits == 0 )); then
182 return
183 fi
184
185 ##* Formatting functions
186
187 # Format the hash according to output format
188 # If no parameter is passed, assume it comes from `$hash`
189 function fmt:hash {
190 #* Uses $hash from outer scope
191 local hash="${1:-$hash}"
192 case "$output" in
193 raw) printf "$hash" ;;
194 text) printf "\e[33m$hash\e[0m" ;; # red
195 md) printf "[\`$hash\`](https://github.com/aristanetworks/ansible-avd/commit/$hash)" ;;
196 esac
197 }
198
199 # Format headers according to output format
200 # Levels 1 to 2 are considered special, the rest are formatted
201 # the same, except in md output format.
202 function fmt:header {
203 local header="$1" level="$2"
204 case "$output" in
205 raw)
206 case "$level" in
207 1) printf "$header\n$(printf '%.0s=' {1..${#header}})\n\n" ;;
208 2) printf "$header\n$(printf '%.0s-' {1..${#header}})\n\n" ;;
209 *) printf "$header:\n\n" ;;
210 esac ;;
211 text)
212 case "$level" in
213 1|2) printf "\e[1;4m$header\e[0m\n\n" ;; # bold, underlined
214 *) printf "\e[1m$header:\e[0m\n\n" ;; # bold
215 esac ;;
216 md) printf "$(printf '%.0s#' {1..${level}}) $header\n\n" ;;
217 esac
218 }
219
220 function fmt:scope {
221 #* Uses $scopes (A) and $hash from outer scope
222 local scope="${1:-${scopes[$hash]}}"
223
224 # Get length of longest scope for padding
225 local max_scope=0 padding=0
226 for hash in ${(k)scopes}; do
227 max_scope=$(( max_scope < ${#scopes[$hash]} ? ${#scopes[$hash]} : max_scope ))
228 done
229
230 # If no scopes, exit the function
231 if [[ $max_scope -eq 0 ]]; then
232 return
233 fi
234
235 # Get how much padding is required for this scope
236 padding=$(( max_scope < ${#scope} ? 0 : max_scope - ${#scope} ))
237 padding="${(r:$padding:: :):-}"
238
239 # If no scope, print padding and 3 spaces (equivalent to "[] ")
240 if [[ -z "$scope" ]]; then
241 printf "${padding} "
242 return
243 fi
244
245 # Print [scope]
246 case "$output" in
247 raw|md) printf "[$scope]${padding} " ;;
248 text) printf "[\e[38;5;9m$scope\e[0m]${padding} " ;; # red 9
249 esac
250 }
251
252 # If no parameter is passed, assume it comes from `$subjects[$hash]`
253 function fmt:subject {
254 #* Uses $subjects (A) and $hash from outer scope
255 local subject="${1:-${subjects[$hash]}}"
256
257 # Capitalize first letter of the subject
258 subject="${(U)subject:0:1}${subject:1}"
259
260 case "$output" in
261 raw) printf "$subject" ;;
262 # In text mode, highlight (#<issue>) and dim text between `backticks`
263 text) sed -E $'s|#([0-9]+)|\e[32m#\\1\e[0m|g;s|`([^`]+)`|`\e[2m\\1\e[0m`|g' <<< "$subject" ;;
264 # In markdown mode, link to (#<issue>) issues
265 md) sed -E 's|#([0-9]+)|[#\1](https://github.com/aristanetworks/ansible-avd/issues/\1)|g' <<< "$subject" ;;
266 esac
267 }
268
269 function fmt:type {
270 #* Uses $type from outer scope
271 local type="${1:-${TYPES[$type]:-${(C)type}}}"
272 [[ -z "$type" ]] && return 0
273 case "$output" in
274 raw|md) printf "$type: " ;;
275 text) printf "\e[4m$type\e[24m: " ;; # underlined
276 esac
277 }
278
279 ##* Section functions
280
281 function display:version {
282 fmt:header "$version" 2
283 }
284
285 function display:breaking {
286 (( $#breaking != 0 )) || return 0
287
288 case "$output" in
289 raw) fmt:header "BREAKING CHANGES" 3 ;;
290 text|md) fmt:header "⚠ BREAKING CHANGES" 3 ;;
291 esac
292
293 local hash subject
294 for hash message in ${(kv)breaking}; do
295 echo " - $(fmt:hash) $(fmt:scope)$(fmt:subject "${message}")"
296 done | sort
297 echo
298 }
299
300 function display:type {
301 local hash type="$1"
302
303 local -a hashes
304 hashes=(${(k)commits[(R)$type]})
305
306 # If no commits found of type $type, go to next type
307 (( $#hashes != 0 )) || return 0
308
309 fmt:header "${TYPES[$type]}" 3
310 for hash in $hashes; do
311 echo " - $(fmt:hash) $(fmt:scope)$(fmt:subject)"
312 done | sort -k3 # sort by scope
313 echo
314 }
315
316 function display:others {
317 local hash type
318
319 # Commits made under types considered other changes
320 local -A changes
321 changes=(${(kv)commits[(R)${(j:|:)OTHER_TYPES}]})
322
323 # If no commits found under "other" types, don't display anything
324 (( $#changes != 0 )) || return 0
325
326 fmt:header "Other changes" 3
327 for hash type in ${(kv)changes}; do
328 case "$type" in
329 other) echo " - $(fmt:hash) $(fmt:scope)$(fmt:subject)" ;;
330 *) echo " - $(fmt:hash) $(fmt:scope)$(fmt:type)$(fmt:subject)" ;;
331 esac
332 done | sort -k3 # sort by scope
333 echo
334 }
335
336 ##* Release sections order
337
338 # Display version header
339 display:version
340
341 # Display breaking changes first
342 display:breaking
343
344 # Display changes for commit types in the order specified
345 for type in $MAIN_TYPES; do
346 display:type "$type"
347 done
348
349 # Display other changes
350 display:others
351 }
352
353 function main {
354 # $1 = until commit, $2 = since commit
355 local until="$1" since="$2"
356
357 # $3 = output format (--text|--raw|--md)
358 # --md: uses markdown formatting
359 # --raw: outputs without style
360 # --text: uses ANSI escape codes to style the output
361 local output=${${3:-"--text"}#--*}
362
363 if [[ -z "$until" ]]; then
364 until=HEAD
365 fi
366
367 if [[ -z "$since" ]]; then
368 # If $since is not specified:
369 # 1) try to find the version used before updating
370 # 2) try to find the first version tag before $until
371 since=$(command git config --get ansible-avd.lastVersion 2>/dev/null) || \
372 since=$(command git describe --abbrev=0 --tags "$until^" 2>/dev/null) || \
373 unset since
374 elif [[ "$since" = --all ]]; then
375 unset since
376 fi
377
378 # Commit classification arrays
379 local -A commits subjects scopes breaking reverts
380 local truncate=0 read_commits=0
381 local hash version tag
382
383 # Get the first version name:
384 # 1) try tag-like version, or
385 # 2) try name-rev, or
386 # 3) try branch name, or
387 # 4) try short hash
388 version=$(command git describe --tags $until 2>/dev/null) \
389 || version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" $until 2>/dev/null) \
390 || version=$(command git symbolic-ref --quiet --short $until 2>/dev/null) \
391 || version=$(command git rev-parse --short $until 2>/dev/null)
392
393 # Get commit list from $until commit until $since commit, or until root
394 # commit if $since is unset, in short hash form.
395 # --first-parent is used when dealing with merges: it only prints the
396 # merge commit, not the commits of the merged branch.
397 command git rev-list --first-parent --abbrev-commit --abbrev=7 ${since:+$since..}$until | while read hash; do
398 # Truncate list on versions with a lot of commits
399 if [[ -z "$since" ]] && (( ++read_commits > 35 )); then
400 truncate=1
401 break
402 fi
403
404 # If we find a new release (exact tag)
405 if tag=$(command git describe --exact-match --tags $hash 2>/dev/null); then
406 # Output previous release
407 display-release
408 # Reinitialize commit storage
409 commits=()
410 subjects=()
411 scopes=()
412 breaking=()
413 reverts=()
414 # Start work on next release
415 version="$tag"
416 read_commits=1
417 fi
418
419 parse-commit "$hash"
420 done
421
422 display-release
423
424 if (( truncate )); then
425 echo " ...more commits omitted"
426 echo
427 fi
428 }
429
430 # Use raw output if stdout is not a tty
431 if [[ ! -t 1 && -z "$3" ]]; then
432 main "$1" "$2" --raw
433 else
434 main "$@"
435 fi