diff --git a/release-notes.sh b/release-notes.sh index d6712d34..7ad736c8 100755 --- a/release-notes.sh +++ b/release-notes.sh @@ -66,14 +66,19 @@ fi : "${REPO_SLUG:=nvm-sh/nvm}" TAB="$(printf '\t')" +# U+0080 through U+009F, as UTF-8 bytes +C1_CONTROLS="$(printf '\302[\200-\237]')" COMMITS_FILE="$(mktemp)" || exit 1 GH_ERR_FILE="$(mktemp)" || exit 1 trap 'rm -f "${COMMITS_FILE}" "${GH_ERR_FILE}"' EXIT # each line:
TAB TAB , in git log order -git log --no-merges --format='%H %s' "${PREV_REF}..${END_REF}" | while IFS=' ' read -r SHA SUBJECT; do - LOWERED="$(printf '%s' "${SUBJECT}" | tr '[:upper:]' '[:lower:]')" +# control characters in a subject would corrupt the terminal and the tab-separated output; +# replacing them (rather than removing them) keeps the bytes around them from joining into another one +git log --no-merges --format='%H %s' "${PREV_REF}..${END_REF}" | LC_ALL=C sed "s/[[:cntrl:]]/?/g;s/${C1_CONTROLS}/?/g" | while IFS=' ' read -r SHA SUBJECT; do + # match bytes: in a UTF-8 locale, BSD tr stops at a byte that is not valid UTF-8 + LOWERED="$(printf '%s' "${SUBJECT}" | LC_ALL=C tr '[:upper:]' '[:lower:]')" STRIPPED="${SUBJECT#\[*\] }" case "${LOWERED}" in v[0-9]*.[0-9]*.[0-9]*) @@ -115,7 +120,8 @@ print_section() { return 0 fi printf '\n%s\n\n' "$2" - grep "^$1${TAB}" "${COMMITS_FILE}" | while IFS="${TAB}" read -r _KEY SHA TEXT; do + # match bytes: in a UTF-8 locale, GNU grep stops printing matches at a line that is not valid UTF-8 + LC_ALL=C grep "^$1${TAB}" "${COMMITS_FILE}" | while IFS="${TAB}" read -r _KEY SHA TEXT; do REF='' case "${TEXT}" in *'(#'[0-9]*')') ;; # the subject already carries a reference diff --git a/test/fast/Unit tests/release-notes.sh replaces control characters in commit subjects b/test/fast/Unit tests/release-notes.sh replaces control characters in commit subjects new file mode 100755 index 00000000..c6739dd8 --- /dev/null +++ b/test/fast/Unit tests/release-notes.sh replaces control characters in commit subjects @@ -0,0 +1,87 @@ +#!/bin/sh + +RELEASE_NOTES="${PWD}/../../../release-notes.sh" +WORK="$(mktemp -d)" || exit 1 + +cleanup() { + cd / && rm -rf "${WORK}" +} +die() { echo "$@" ; cleanup ; exit 1; } + +export HOME="${WORK}/home" +export GIT_CONFIG_NOSYSTEM=1 +unset XDG_CONFIG_HOME GIT_DIR GIT_WORK_TREE +export NVM_RELEASE_NOTES_SKIP_PRS=1 +export GIT_COMMITTER_NAME=nvm GIT_COMMITTER_EMAIL=nvm@example.com + +IDENT='contributor 0 +0000' + +# writes the commit object byte for byte; `git commit` would repair a subject that is not valid UTF-8 +commit() { + PARENT='' + if git rev-parse --quiet --verify HEAD >/dev/null; then + PARENT="parent $(git rev-parse HEAD) +" + fi + COMMIT="$( + { + printf 'tree %s\n%sauthor %s\ncommitter %s\n\n' "${EMPTY_TREE}" "${PARENT}" "${IDENT}" "${IDENT}" + # shellcheck disable=SC2059 + printf "$1\n" + } | git hash-object -t commit -w --stdin + )" || die 'failed to write a commit' + git update-ref HEAD "${COMMIT}" || die 'failed to update HEAD' +} + +mkdir -p "${HOME}" "${WORK}/repo" || die 'failed to create the scratch dirs' +cd "${WORK}/repo" || die 'failed to enter the scratch repo' +git init --quiet . || die 'failed to create the scratch repo' +EMPTY_TREE="$(git hash-object -t tree -w /dev/null)" || die 'failed to write the empty tree' + +commit 'v0.0.1' +PREV_REF="$(git rev-parse HEAD)" +commit '[Fix] older bullet' +commit '[New] \342\200\234quoted\342\200\235 bullet' +commit '[Fix] stray \233 byte' +commit '[Fix] U+009B once the escape is gone: \302\033\2332K' +commit '[Fix] U+009B: \302\2332K' +commit '[Fix] tab\tand delete\177' +commit '[Fix] escapes\033]0;title\007\r\033[2K\033[1A' +commit 'no prefix\033[2K' + +EXPECTED_OUTPUT="$(printf '%b\n' \ + 'v0.0.2' \ + '' \ + 'New Stuff' \ + '' \ + ' - \0342\0200\0234quoted\0342\0200\0235 bullet' \ + '' \ + 'Bug Fixes' \ + '' \ + ' - escapes?]0;title???[2K?[1A' \ + ' - tab?and delete?' \ + ' - U+009B: ?2K' \ + ' - U+009B once the escape is gone: \0302?\02332K' \ + ' - stray \0233 byte' \ + ' - older bullet' \ + '' \ + 'Misc' \ + '' \ + ' - no prefix?[2K')" +EXPECTED_STDERR='release-notes: no category prefix, filing under Misc: no prefix?[2K' + +for LOCALE in C C.UTF-8 en_US.UTF-8; do + case "${LOCALE}" in + *.UTF-8) [ "$(LC_ALL="${LOCALE}" locale charmap 2>/dev/null)" = 'UTF-8' ] || continue ;; + esac + + OUTPUT="$(LC_ALL="${LOCALE}" "${RELEASE_NOTES}" v0.0.2 "${PREV_REF}" 2>/dev/null)" + [ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \ + || die "${LOCALE}: expected >${EXPECTED_OUTPUT}<, got >$(printf '%s' "${OUTPUT}" | od -c)<" + + STDERR="$(LC_ALL="${LOCALE}" "${RELEASE_NOTES}" v0.0.2 "${PREV_REF}" 2>&1 >/dev/null)" + [ "_${STDERR}" = "_${EXPECTED_STDERR}" ] \ + || die "${LOCALE}: expected stderr >${EXPECTED_STDERR}<, got >$(printf '%s' "${STDERR}" | od -c)<" +done + +cleanup