| 1 | |
| 2 | |
| 3 | cd "$ZSH" |
| 4 | setopt extendedglob |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | local -A TYPES |
| 13 | TYPES=( |
| 14 | build "Build system" |
| 15 | chore "Chore" |
| 16 | ci "CI" |
| 17 | docs "Documentation" |
| 18 | feat "Features" |
| 19 | fix "Bug fixes" |
| 20 | perf "Performance" |
| 21 | refactor "Refactor" |
| 22 | style "Style" |
| 23 | test "Testing" |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | local -a MAIN_TYPES |
| 28 | MAIN_TYPES=(feat fix perf docs) |
| 29 | |
| 30 | |
| 31 | local -a OTHER_TYPES |
| 32 | OTHER_TYPES=(refactor style other) |
| 33 | |
| 34 | |
| 35 | |
| 36 | local -a IGNORED_TYPES |
| 37 | IGNORED_TYPES=(${${${(@k)TYPES}:|MAIN_TYPES}:|OTHER_TYPES}) |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | function parse-commit { |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | function commit:type { |
| 55 | local type |
| 56 | |
| 57 | |
| 58 | if [[ "$1" =~ '^([a-zA-Z_\-]+)(\(.+\))?!?: .+$' ]]; then |
| 59 | type="${match[1]}" |
| 60 | fi |
| 61 | |
| 62 | |
| 63 | if [[ -n "$type" && -n "${(k)TYPES[(i)$type]}" ]]; then |
| 64 | echo $type |
| 65 | else |
| 66 | echo other |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | function commit:scope { |
| 71 | local scope |
| 72 | |
| 73 | |
| 74 | if [[ "$1" =~ '^[a-zA-Z_\-]+\((.+)\)!?: .+$' ]]; then |
| 75 | echo "${match[1]}" |
| 76 | return |
| 77 | fi |
| 78 | |
| 79 | |
| 80 | if [[ "$1" =~ '^([a-zA-Z_\-]+): .+$' ]]; then |
| 81 | scope="${match[1]}" |
| 82 | |
| 83 | if [[ -z "${(k)TYPES[(i)$scope]}" ]]; then |
| 84 | echo "$scope" |
| 85 | fi |
| 86 | fi |
| 87 | } |
| 88 | |
| 89 | function commit:subject { |
| 90 | |
| 91 | |
| 92 | |
| 93 | if [[ "$1" =~ '^[a-zA-Z_\-]+(\(.+\))?!?: (.+)$' ]]; then |
| 94 | echo "${match[2]}" |
| 95 | else |
| 96 | echo "$1" |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | |
| 101 | function commit:is-breaking { |
| 102 | local subject="$1" body="$2" message |
| 103 | |
| 104 | if [[ "$body" =~ "BREAKING CHANGE: (.*)" || \ |
| 105 | "$subject" =~ '^[^ :\)]+\)?!: (.*)$' ]]; then |
| 106 | message="${match[1]}" |
| 107 | |
| 108 | message="${message//$'\r'/}" |
| 109 | |
| 110 | local nlnl=$'\n\n' |
| 111 | message="${message//$'\n'[[:space:]]##$'\n'/$nlnl}" |
| 112 | |
| 113 | message="${message%%$'\n\n'*}" |
| 114 | |
| 115 | echo "${message//$'\n'/ }" |
| 116 | else |
| 117 | return 1 |
| 118 | fi |
| 119 | } |
| 120 | |
| 121 | |
| 122 | function commit:is-revert { |
| 123 | local subject="$1" body="$2" |
| 124 | |
| 125 | if [[ "$subject" = Revert* && \ |
| 126 | "$body" =~ "This reverts commit ([^.]+)\." ]]; then |
| 127 | echo "${match[1]:0:7}" |
| 128 | else |
| 129 | return 1 |
| 130 | fi |
| 131 | } |
| 132 | |
| 133 | |
| 134 | local hash="$1" subject="$2" body="$3" warning rhash |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | types[$hash]="$(commit:type "$subject")" |
| 146 | |
| 147 | scopes[$hash]="$(commit:scope "$subject")" |
| 148 | |
| 149 | subjects[$hash]="$(commit:subject "$subject")" |
| 150 | |
| 151 | |
| 152 | |
| 153 | if warning=$(commit:is-breaking "$subject" "$body"); then |
| 154 | breaking[$hash]="$warning" |
| 155 | fi |
| 156 | |
| 157 | |
| 158 | if rhash=$(commit:is-revert "$subject" "$body"); then |
| 159 | reverts[$hash]=$rhash |
| 160 | fi |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | if [ -t 1 ]; then |
| 174 | is_tty() { |
| 175 | true |
| 176 | } |
| 177 | else |
| 178 | is_tty() { |
| 179 | false |
| 180 | } |
| 181 | fi |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | supports_hyperlinks() { |
| 202 | |
| 203 | if [ -n "$FORCE_HYPERLINK" ]; then |
| 204 | [ "$FORCE_HYPERLINK" != 0 ] |
| 205 | return $? |
| 206 | fi |
| 207 | |
| 208 | |
| 209 | is_tty || return 1 |
| 210 | |
| 211 | |
| 212 | if [ -n "$DOMTERM" ]; then |
| 213 | return 0 |
| 214 | fi |
| 215 | |
| 216 | |
| 217 | if [ -n "$VTE_VERSION" ]; then |
| 218 | [ $VTE_VERSION -ge 5000 ] |
| 219 | return $? |
| 220 | fi |
| 221 | |
| 222 | |
| 223 | case "$TERM_PROGRAM" in |
| 224 | Hyper|iTerm.app|terminology|WezTerm|vscode) return 0 ;; |
| 225 | esac |
| 226 | |
| 227 | |
| 228 | case "$TERM" in |
| 229 | xterm-kitty|alacritty|alacritty-direct) return 0 ;; |
| 230 | esac |
| 231 | |
| 232 | |
| 233 | if [ "$COLORTERM" = "xfce4-terminal" ]; then |
| 234 | return 0 |
| 235 | fi |
| 236 | |
| 237 | |
| 238 | if [ -n "$WT_SESSION" ]; then |
| 239 | return 0 |
| 240 | fi |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | return 1 |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | function display-release { |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | local hash rhash |
| 267 | for hash rhash in ${(kv)reverts}; do |
| 268 | if (( ${+types[$rhash]} )); then |
| 269 | |
| 270 | unset "types[$hash]" "subjects[$hash]" "scopes[$hash]" "breaking[$hash]" |
| 271 | |
| 272 | unset "types[$rhash]" "subjects[$rhash]" "scopes[$rhash]" "breaking[$rhash]" |
| 273 | fi |
| 274 | done |
| 275 | |
| 276 | |
| 277 | for hash in ${(k)types[(R)${(j:|:)IGNORED_TYPES}]}; do |
| 278 | (( ! ${+breaking[$hash]} )) || continue |
| 279 | unset "types[$hash]" "subjects[$hash]" "scopes[$hash]" |
| 280 | done |
| 281 | |
| 282 | |
| 283 | if (( $#types == 0 )); then |
| 284 | return |
| 285 | fi |
| 286 | |
| 287 | |
| 288 | local max_scope=0 |
| 289 | for hash in ${(k)scopes}; do |
| 290 | max_scope=$(( max_scope < ${#scopes[$hash]} ? ${#scopes[$hash]} : max_scope )) |
| 291 | done |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | function fmt:hash { |
| 298 | |
| 299 | local hash="${1:-$hash}" |
| 300 | local short_hash="${hash:0:7}" |
| 301 | case "$output" in |
| 302 | raw) printf '%s' "$short_hash" ;; |
| 303 | text) |
| 304 | local text="\e[33m$short_hash\e[0m"; |
| 305 | if supports_hyperlinks; then |
| 306 | printf "\e]8;;%s\a%s\e]8;;\a" "https://github.com/ohmyzsh/ohmyzsh/commit/$hash" $text; |
| 307 | else |
| 308 | echo $text; |
| 309 | fi ;; |
| 310 | md) printf '[`%s`](https://github.com/ohmyzsh/ohmyzsh/commit/%s)' "$short_hash" "$hash" ;; |
| 311 | esac |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | function fmt:header { |
| 318 | local header="$1" level="$2" |
| 319 | case "$output" in |
| 320 | raw) |
| 321 | case "$level" in |
| 322 | 1) printf '%s\n%s\n\n' "$header" "$(printf '%.0s=' {1..${#header}})" ;; |
| 323 | 2) printf '%s\n%s\n\n' "$header" "$(printf '%.0s-' {1..${#header}})" ;; |
| 324 | *) printf '%s:\n\n' "$header" ;; |
| 325 | esac ;; |
| 326 | text) |
| 327 | case "$level" in |
| 328 | 1|2) printf '\e[1;4m%s\e[0m\n\n' "$header" ;; |
| 329 | *) printf '\e[1m%s:\e[0m\n\n' "$header" ;; |
| 330 | esac ;; |
| 331 | md) printf '%s %s\n\n' "$(printf '%.0s#' {1..${level}})" "$header" ;; |
| 332 | esac |
| 333 | } |
| 334 | |
| 335 | function fmt:scope { |
| 336 | |
| 337 | local scope="${1:-${scopes[$hash]}}" |
| 338 | |
| 339 | |
| 340 | if [[ $max_scope -eq 0 ]]; then |
| 341 | return |
| 342 | fi |
| 343 | |
| 344 | |
| 345 | local padding=0 |
| 346 | padding=$(( max_scope < ${#scope} ? 0 : max_scope - ${#scope} )) |
| 347 | padding="${(r:$padding:: :):-}" |
| 348 | |
| 349 | |
| 350 | if [[ -z "$scope" ]]; then |
| 351 | printf "${padding} " |
| 352 | return |
| 353 | fi |
| 354 | |
| 355 | |
| 356 | case "$output" in |
| 357 | raw|md) printf '[%s]%s ' "$scope" "$padding";; |
| 358 | text) printf '[\e[38;5;9m%s\e[0m]%s ' "$scope" "$padding";; |
| 359 | esac |
| 360 | } |
| 361 | |
| 362 | |
| 363 | function fmt:subject { |
| 364 | |
| 365 | local subject="${1:-${subjects[$hash]}}" |
| 366 | |
| 367 | |
| 368 | subject="${(U)subject:0:1}${subject:1}" |
| 369 | |
| 370 | case "$output" in |
| 371 | raw) printf '%s' "$subject" ;; |
| 372 | |
| 373 | text) |
| 374 | if supports_hyperlinks; then |
| 375 | sed -E $'s|#([0-9]+)|\e]8;;https://github.com/ohmyzsh/ohmyzsh/issues/\\1\a\e[32m#\\1\e[0m\e]8;;\a|g;s|`([^`]+)`|`\e[2m\\1\e[0m`|g' <<< "$subject" |
| 376 | else |
| 377 | sed -E $'s|#([0-9]+)|\e[32m#\\1\e[0m|g;s|`([^`]+)`|`\e[2m\\1\e[0m`|g' <<< "$subject" |
| 378 | fi ;; |
| 379 | |
| 380 | md) sed -E 's|#([0-9]+)|[#\1](https://github.com/ohmyzsh/ohmyzsh/issues/\1)|g' <<< "$subject" ;; |
| 381 | esac |
| 382 | } |
| 383 | |
| 384 | function fmt:type { |
| 385 | |
| 386 | local type="${1:-${TYPES[$type]:-${(C)type}}}" |
| 387 | [[ -z "$type" ]] && return 0 |
| 388 | case "$output" in |
| 389 | raw|md) printf '%s: ' "$type" ;; |
| 390 | text) printf '\e[4m%s\e[24m: ' "$type" ;; |
| 391 | esac |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | function display:version { |
| 397 | fmt:header "$version" 2 |
| 398 | } |
| 399 | |
| 400 | function display:breaking { |
| 401 | (( $#breaking != 0 )) || return 0 |
| 402 | |
| 403 | |
| 404 | shown_commits=1 |
| 405 | |
| 406 | case "$output" in |
| 407 | text) printf '\e[31m'; fmt:header "BREAKING CHANGES" 3 ;; |
| 408 | raw) fmt:header "BREAKING CHANGES" 3 ;; |
| 409 | md) fmt:header "BREAKING CHANGES ⚠" 3 ;; |
| 410 | esac |
| 411 | |
| 412 | local hash message |
| 413 | local wrap_width=$(( (COLUMNS < 100 ? COLUMNS : 100) - 3 )) |
| 414 | for hash message in ${(kv)breaking}; do |
| 415 | |
| 416 | |
| 417 | message="$(fmt -w $wrap_width <<< "$message")" |
| 418 | |
| 419 | |
| 420 | echo " - $(fmt:hash) $(fmt:scope)\n\n$(fmt:subject "$message" | sed 's/^/ /')\n" |
| 421 | done |
| 422 | } |
| 423 | |
| 424 | function display:type { |
| 425 | local hash type="$1" |
| 426 | |
| 427 | local -a hashes |
| 428 | hashes=(${(k)types[(R)$type]}) |
| 429 | |
| 430 | |
| 431 | (( $#hashes != 0 )) || return 0 |
| 432 | |
| 433 | |
| 434 | shown_commits=1 |
| 435 | |
| 436 | fmt:header "${TYPES[$type]}" 3 |
| 437 | for hash in $hashes; do |
| 438 | echo " - $(fmt:hash) $(fmt:scope)$(fmt:subject)" |
| 439 | done | sort -k3 |
| 440 | echo |
| 441 | } |
| 442 | |
| 443 | function display:others { |
| 444 | local hash type |
| 445 | |
| 446 | |
| 447 | local -A changes |
| 448 | changes=(${(kv)types[(R)${(j:|:)OTHER_TYPES}]}) |
| 449 | |
| 450 | |
| 451 | (( $#changes != 0 )) || return 0 |
| 452 | |
| 453 | |
| 454 | shown_commits=1 |
| 455 | |
| 456 | fmt:header "Other changes" 3 |
| 457 | for hash type in ${(kv)changes}; do |
| 458 | case "$type" in |
| 459 | other) echo " - $(fmt:hash) $(fmt:scope)$(fmt:subject)" ;; |
| 460 | *) echo " - $(fmt:hash) $(fmt:scope)$(fmt:type)$(fmt:subject)" ;; |
| 461 | esac |
| 462 | done | sort -k3 |
| 463 | echo |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | display:version |
| 470 | |
| 471 | |
| 472 | display:breaking |
| 473 | |
| 474 | |
| 475 | for type in $MAIN_TYPES; do |
| 476 | display:type "$type" |
| 477 | done |
| 478 | |
| 479 | |
| 480 | display:others |
| 481 | } |
| 482 | |
| 483 | function main { |
| 484 | |
| 485 | local until="$1" since="$2" |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | local output=${${3:-"--text"}#--*} |
| 492 | |
| 493 | if [[ -z "$until" ]]; then |
| 494 | until=HEAD |
| 495 | fi |
| 496 | |
| 497 | if [[ -z "$since" ]]; then |
| 498 | |
| 499 | |
| 500 | |
| 501 | since=$(command git config --get oh-my-zsh.lastVersion 2>/dev/null) || \ |
| 502 | since=$(command git describe --abbrev=0 --tags "$until^" 2>/dev/null) || \ |
| 503 | unset since |
| 504 | elif [[ "$since" = --all ]]; then |
| 505 | unset since |
| 506 | fi |
| 507 | |
| 508 | |
| 509 | local -A types subjects scopes breaking reverts |
| 510 | local truncate=0 read_commits=0 shown_commits=0 |
| 511 | local version tag |
| 512 | local hash refs subject body |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | version=$(command git describe --tags $until 2>/dev/null) \ |
| 520 | || version=$(command git symbolic-ref --quiet --short $until 2>/dev/null) \ |
| 521 | || version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" $until 2>/dev/null) \ |
| 522 | || version=$(command git rev-parse --short $until 2>/dev/null) |
| 523 | |
| 524 | |
| 525 | local range=${since:+$since..}$until |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | local SEP="0mZmAgIcSeP" |
| 534 | local -a raw_commits |
| 535 | raw_commits=(${(0)"$(command git -c log.showSignature=false log -z \ |
| 536 | --format="%h${SEP}%D${SEP}%s${SEP}%b" --abbrev=12 \ |
| 537 | --no-merges --first-parent $range)"}) |
| 538 | |
| 539 | local raw_commit |
| 540 | local -a raw_fields |
| 541 | for raw_commit in $raw_commits; do |
| 542 | |
| 543 | if [[ -z "$since" ]] && (( ++read_commits > 35 )); then |
| 544 | truncate=1 |
| 545 | break |
| 546 | fi |
| 547 | |
| 548 | |
| 549 | eval "raw_fields=(\"\${(@ps:$SEP:)raw_commit}\")" |
| 550 | hash="${raw_fields[1]}" |
| 551 | refs="${raw_fields[2]}" |
| 552 | subject="${raw_fields[3]}" |
| 553 | body="${raw_fields[4]}" |
| 554 | |
| 555 | |
| 556 | if [[ "$refs" = *tag:\ * ]]; then |
| 557 | |
| 558 | tag="${${refs##*tag: }%%,# *}" |
| 559 | |
| 560 | display-release |
| 561 | |
| 562 | types=() |
| 563 | subjects=() |
| 564 | scopes=() |
| 565 | breaking=() |
| 566 | reverts=() |
| 567 | |
| 568 | version="$tag" |
| 569 | read_commits=1 |
| 570 | fi |
| 571 | |
| 572 | parse-commit "$hash" "$subject" "$body" |
| 573 | done |
| 574 | |
| 575 | display-release |
| 576 | |
| 577 | if (( truncate )); then |
| 578 | echo " ...more commits omitted" |
| 579 | echo |
| 580 | fi |
| 581 | |
| 582 | if (( ! shown_commits )); then |
| 583 | echo "No changes to mention." |
| 584 | fi |
| 585 | } |
| 586 | |
| 587 | |
| 588 | if [[ ! -t 1 && -z "$3" ]]; then |
| 589 | main "$1" "$2" --raw |
| 590 | else |
| 591 | main "$@" |
| 592 | fi |