|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Prints a Markdown-formatted summary of git commits ready to include in |
| 4 | +# a CHANGELOG.md file |
| 5 | +# |
| 6 | +# Usage: gen-changelog.sh [from] [to] |
| 7 | +# If no [from] argument is supplied, uses the most recent git tag. |
| 8 | +# if no [to] argument is supplied, uses git HEAD. |
| 9 | +# |
| 10 | +# The REPOROOT and TYPES variables may be used to customize what links are |
| 11 | +# generated and what is scraped from the commit logs. |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +PACKAGE=${PACKAGE:=$(go list .)} |
| 16 | +REPOROOT=${REPOROOT:=https://$PACKAGE} |
| 17 | +TYPES=${TYPES:=feat(;Features fix(;Fixes docs(;Documentation chore(;Maintenance} |
| 18 | + |
| 19 | +# Print the information scraped from git |
| 20 | +retrieve() { |
| 21 | + while read -r commit hash message; do |
| 22 | + # Extract the subsystem where type(subsystem): message |
| 23 | + SUBSYSTEM=$(echo "$message" | cut -d'(' -f2 | cut -d')' -f1 | sed 's/\*/\(all\)/g') |
| 24 | + # Extract the message where type(subsystem): message |
| 25 | + MESSAGE=$(echo "$message" | awk -F ")" '{ print $2}' | sed 's/://' | cut -f2- -d' ') |
| 26 | + # Generate a link to the full legal commit on GitHub |
| 27 | + LINK="$REPOROOT/commit/$hash" |
| 28 | + # Echo all this in a way that makes the commit hash and message a link |
| 29 | + # to the commit in markdown |
| 30 | + echo ' -' "[\`$commit\`]($LINK)" "$SUBSYSTEM": "$MESSAGE" |
| 31 | + done < <(git --no-pager log --oneline --merges --oneline --format="%h %H %b" --grep="$1" "$FROM".."$TO") |
| 32 | + # Scrape the information from git |
| 33 | +} |
| 34 | + |
| 35 | +# Wrap feature type and show its relevant commits |
| 36 | +subheading() { |
| 37 | + echo "#### $1" |
| 38 | + echo |
| 39 | + retrieve "$2" |
| 40 | + echo |
| 41 | +} |
| 42 | + |
| 43 | +main() { |
| 44 | + FROM=${1:-$(git describe --abbrev=0 --tags)} |
| 45 | + TO=${2:-"HEAD"} |
| 46 | + RELEASE=${RELEASE:=$TO} |
| 47 | + |
| 48 | + printf "### %s -> %s\n\n" "$FROM" "$RELEASE" |
| 49 | + |
| 50 | + # Iterate over the types of messages specified |
| 51 | + for LEGALTYPE in $TYPES; do |
| 52 | + SHORT=$(echo "$LEGALTYPE" | cut -f1 -d';') |
| 53 | + LONG=$(echo "$LEGALTYPE" | cut -f2 -d';') |
| 54 | + subheading "$LONG" "$SHORT" |
| 55 | + done |
| 56 | +} |
| 57 | + |
| 58 | +if [ $SHLVL -eq 1 ] || [ $SHLVL -eq 2 ]; then |
| 59 | + # If this is being run as a command |
| 60 | + main "$@" |
| 61 | + >&2 echo -e "\033[0;33mBe sure to change headers, remove empty sections," |
| 62 | + >&2 echo -e "and proofread before committing to CHANGELOG.md.\033[0m" |
| 63 | +else |
| 64 | + # Otherwise this is being sourced |
| 65 | + unset -f main |
| 66 | + unset -f usage |
| 67 | +fi |
0 commit comments