|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +# Copyright 2020, Mark Callow |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# Generate release notes from git log. |
| 7 | +# |
| 8 | +# mkrelnotes [-b] [-c] [-d] [-p <preface> [-t] <previous release tag> <this release> |
| 9 | +# |
| 10 | +# <previous release tag> is the name of the tag identifying the previous |
| 11 | +# release. <this release> is the name to be given to the tag for the upcoming |
| 12 | +# release. N.B. this tag cannot be created until after ther release notes |
| 13 | +# have been generated and checked in. The release notes will include changes |
| 14 | +# in the two-dot commit range <previous release tag>..HEAD. |
| 15 | +# |
| 16 | +# <preface> is inserted between the title and the detailed change list |
| 17 | +# that is extracted with git log. It should be used to summarize |
| 18 | +# new features and known issues using level 3 (###) headings for those |
| 19 | +# sections. |
| 20 | +# |
| 21 | +# If -b is specified build system changes will be included in the |
| 22 | +# release # notes. |
| 23 | +# |
| 24 | +# If -c is specified any previous $RELNOTES_FILE will be appended, with the |
| 25 | +# title lines stripped, to the release notes generated here, thus enabling |
| 26 | +# cumulative release notes. Otherwise any previous $RELNOTES_FILE file is |
| 27 | +# replaced. |
| 28 | +# |
| 29 | +# If -d is specified details of the changes will be included, otherwise only |
| 30 | +# the summary is shown. |
| 31 | +# |
| 32 | +# If -i is specified the release notes will be built interactively. That is, |
| 33 | +# for each change the user will be asked if it should be included. |
| 34 | +# |
| 35 | +# If -t is specified changes to the tests will be included in the release notes |
| 36 | +# otherwise they are omitted. |
| 37 | + |
| 38 | +# Depth of this script relative to the project root |
| 39 | +depth=. |
| 40 | + |
| 41 | +RELNOTES_FILE="RELEASE_NOTES.md" |
| 42 | + |
| 43 | +# The author name in a commit message comes from whatever the user |
| 44 | +# has set as user.name in their git config. It usually has no relation |
| 45 | +# to their GitHub username so putting an @ before it has no meaning. |
| 46 | +# |
| 47 | +SUMMARY_FORMAT_W_AUTH="* %s (%h) (%an)" |
| 48 | +SUMMARY_FORMAT="* %s (%h)" |
| 49 | + |
| 50 | +function commit_summary() { |
| 51 | + local hash=$1 |
| 52 | + local summary |
| 53 | + local prjson |
| 54 | + pr=$(git log --oneline -n 1 $hash | grep -o -E "\(#[0-9]+\)" | grep -o -E "[0-9]+") |
| 55 | + # Even in PR's the commit has the full name of the user as known to GitHub, |
| 56 | + # not their GitHub username. We want to use the GitHub username so there will |
| 57 | + # be a link back to the person. We can retrieve this by requesting detailed info |
| 58 | + # from GitHub via HTTPS. |
| 59 | + summary=$(git log --pretty="$SUMMARY_FORMAT" -n 1 $hash) |
| 60 | + if [ -z "$pr" ]; then |
| 61 | + cmtjson="$(curl -n https://api.github.com/repos/KhronosGroup/KTX-Software/commits/$hash 2>/dev/null)" |
| 62 | + author="$(echo $cmtjson | jq -r -e '.author.login')" |
| 63 | + else |
| 64 | + prjson="$(curl -n https://api.github.com/repos/KhronosGroup/KTX-Software/pulls/$pr 2>/dev/null)" |
| 65 | + author="$(echo $prjson | jq -r -e '.user.login')" |
| 66 | + fi |
| 67 | + summary+=" (@$author)" |
| 68 | + echo "$summary" |
| 69 | + if [ -n "$includeDetails" ]; then |
| 70 | + body="$(git log --pretty="%b" -n 1 $hash | sed -E 's/^(.+)/ \1/')" |
| 71 | + if [ -n "$body" ]; then |
| 72 | + echo "" |
| 73 | + # Remove CR which some multi-line commit bodies contain for unknown reasons. |
| 74 | + echo "$body" | tr -d \\r |
| 75 | + fi |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +function revisions_in () { |
| 80 | + local range=$1; shift |
| 81 | + git rev-list $range $* |
| 82 | +} |
| 83 | + |
| 84 | +function log() { |
| 85 | + local part=$1; shift |
| 86 | + local log |
| 87 | + for rev in $(revisions_in "$range" $*); do |
| 88 | + if [ -n "$interactive" ]; then |
| 89 | + git log -1 $rev > /dev/tty |
| 90 | + |
| 91 | + processed=0 |
| 92 | + while [ $processed -eq 0 ]; do |
| 93 | + echo "Include this change? [d,i,s,?] ?" > /dev/tty |
| 94 | + read -n 1 opt |
| 95 | + echo "" > /dev/tty |
| 96 | + |
| 97 | + case $opt in |
| 98 | + d) |
| 99 | + git show --pretty=oneline $rev > /dev/tty |
| 100 | + ;; |
| 101 | + [is]) |
| 102 | + processed=1 |
| 103 | + ;; |
| 104 | + ?) |
| 105 | + echo "d - show diff of this commit" > /dev/tty |
| 106 | + echo "i - include this commit." > /dev/tty |
| 107 | + echo "s - skip this commit." > /dev/tty |
| 108 | + echo "? - display this help message." > /dev/tty |
| 109 | + ;; |
| 110 | + *) |
| 111 | + echo "Unknown option: $opt. Try again." > /dev/tty |
| 112 | + ;; |
| 113 | + esac |
| 114 | + done |
| 115 | + |
| 116 | + case $opt in |
| 117 | + i) |
| 118 | + log+="$(commit_summary $rev)" |
| 119 | + ;; |
| 120 | + esac |
| 121 | + |
| 122 | + else |
| 123 | + log+="$(commit_summary $rev)" |
| 124 | + fi |
| 125 | + # For reasons I do not understand I have been completely unable to prevent |
| 126 | + # trailing newlines from being removed from the output of commit_summary |
| 127 | + # so resorting to ANSI-C quoting to insert some new lines between summaries. |
| 128 | + log+=$'\n\n' |
| 129 | + done |
| 130 | + if [ -n "$log" ]; then |
| 131 | + echo "### $part" |
| 132 | + echo |
| 133 | + echo "$log" |
| 134 | + echo |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | +function usage() { |
| 139 | +cat << EOU |
| 140 | +Usage: $0 [-b] [-c] [-d] [-i] [-p <preface>] [-t] <commit range>" |
| 141 | +Options: |
| 142 | + -b Include build system changes |
| 143 | + -c Make cumulative release notes |
| 144 | + -d Include details of changes. If absent only the summary is shown. |
| 145 | + -i Interactively select the changes to include. |
| 146 | + -p <preface> |
| 147 | + Include the file <preface> before the list of changes. |
| 148 | + -t Include test changes. |
| 149 | +EOU |
| 150 | +} |
| 151 | + |
| 152 | +while true; do |
| 153 | + case $1 in |
| 154 | + -b) includeBuildSystem="true"; shift ;; |
| 155 | + -c) cumulative="true"; shift;; |
| 156 | + -d) includeDetails="true"; shift ;; |
| 157 | + -i) interactive="true"; shift;; |
| 158 | + -p) preface=$2; shift 2 ;; |
| 159 | + -t) includeTests="true"; shift ;; |
| 160 | + -*) usage; exit 1 ;; |
| 161 | + *) break ;; |
| 162 | + esac |
| 163 | +done |
| 164 | + |
| 165 | +if [ $# -ne 2 ]; then |
| 166 | + echo "$0: Need previous release tag and this release name, e.g. 'v4.0.0 v4.0.1'." |
| 167 | + exit 1 |
| 168 | +else |
| 169 | + range=$1.. |
| 170 | + lastrel=$1 |
| 171 | + thisrel=$2 |
| 172 | +fi |
| 173 | + |
| 174 | +#if [ $# -ne 1 ]; then |
| 175 | +# echo '$0: Need a two-dot revision range, e.g, `v4.0.0..v4.0.1`.' |
| 176 | +# exit 1 |
| 177 | +#else |
| 178 | +# range=$1 |
| 179 | +# if ! [[ $range =~ ([[:alnum:][:punct:]]+)\.\.([[:alnum:]][[:alnum:][:punct:]]*)? ]]; then |
| 180 | +# echo "$0: <revision range> is not a two-dot range." |
| 181 | +# exit 1 |
| 182 | +# else |
| 183 | +# lastrel=${BASH_REMATCH[1]} |
| 184 | +# thisrel=${BASH_REMATCH[2]} |
| 185 | +# if [ -z "$lastrel" ]; then |
| 186 | +# lastrel=HEAD |
| 187 | +# fi |
| 188 | +# fi |
| 189 | +#fi |
| 190 | + |
| 191 | +SAVED_RELNOTES_FILE="${RELNOTES_FILE%.md}-$lastrel.md" |
| 192 | + |
| 193 | +# Save or remove old relnotes. |
| 194 | +if [ -f $RELNOTES_FILE ]; then |
| 195 | + if [ -z "$cumulative" ]; then |
| 196 | + rm $RELNOTES_FILE; |
| 197 | + else |
| 198 | + mv $RELNOTES_FILE $SAVED_RELNOTES_FILE |
| 199 | + fi |
| 200 | +fi |
| 201 | + |
| 202 | +# Read preface file before cd. |
| 203 | +if [ -n "$preface" ]; then |
| 204 | + PREFACE=$(cat $preface) |
| 205 | +fi |
| 206 | + |
| 207 | +# Change to the directory where the script is. |
| 208 | +cd $(dirname $0) |
| 209 | + |
| 210 | +# Change to project root. |
| 211 | +pushd $depth > /dev/null |
| 212 | + |
| 213 | +lib=$(log libktx lib) |
| 214 | +tools=$(log Tools tools) |
| 215 | +if [ -n "$includeTests" ]; then |
| 216 | + tests=$(log "Tests" tests) |
| 217 | +fi |
| 218 | +js_binding=$(log "JS Wrappers" interface/js_binding) |
| 219 | + |
| 220 | +if [ -n "$includeBuildSystem" ]; then |
| 221 | + cmake=$(log "Build System" $(find . \( -path ./build -o -path ./.git -o -path ./other_include \) -prune -false -o -name CMakeLists.txt -o -name '*.cmake')) |
| 222 | +fi |
| 223 | + |
| 224 | + |
| 225 | +cat > $RELNOTES_FILE <<- EOF |
| 226 | +$(date '+<!-- Copyright %Y, The Khronos Group Inc. -->') |
| 227 | +<!-- SPDX-License-Identifier: Apache-2.0 --> |
| 228 | +Release Notes |
| 229 | +============= |
| 230 | +## Version ${thisrel#v} |
| 231 | +$PREFACE |
| 232 | +
|
| 233 | +### Changes since $lastrel (by part) |
| 234 | +$lib |
| 235 | +
|
| 236 | +$tools |
| 237 | +
|
| 238 | +$tests |
| 239 | +
|
| 240 | +$js_binding |
| 241 | +
|
| 242 | +$cmake |
| 243 | +EOF |
| 244 | + |
| 245 | + |
| 246 | +popd > /dev/null |
| 247 | + |
| 248 | +if [ -f $SAVED_RELNOTES_FILE ]; then |
| 249 | + awk '! /Release Notes/ && ! /======/ && ! /<!-- Copyright/ && ! /<!-- SPDX/ {print}' $SAVED_RELNOTES_FILE >> $RELNOTES_FILE |
| 250 | + rm $SAVED_RELNOTES_FILE |
| 251 | +fi |
0 commit comments