#!/bin/sh set -eu # Check if 'go generate' needs to be run. # TODO: see if we can only run this against the Unicode version native to # the installed version of Go since that takes around ~40ms compared to # ~450ms when we have to generate all the tables for the non-native version. FILES='^(tables_\w+\.go|internal/gen/gentables/main\.go|)$' ERROR=Error if [ -t 1 ]; then ERROR='\033[0;31mError\033[0m' # red fi if git rev-parse --verify HEAD >/dev/null 2>&1; then against=HEAD else # Initial commit: diff against an empty tree object against=$(git hash-object -t tree /dev/null) fi # Redirect output to stderr. exec 1>&2 if git diff --cached --name-only --diff-filter=M -z "${against}" | \grep --null --quiet --extended-regexp "${FILES}"; then # Project root root="$(git rev-parse --show-toplevel)" exit_code=0 [ -d "${root}/bin" ] || mkdir -p "${root}/bin" GOBIN="${root}/bin" go install "${root}/gen.go" if ! "${root}/bin/gen" -dry-run >/dev/null 2>&1; then echo "${ERROR}: Cannot commit changes without re-running \`go generate\`." echo '' echo 'This will cause failures in CI.' echo '' echo "Output of \`go run ${root}/bin/gen -dry-run\`:" echo '' echo '################################################################################' "${root}/bin/gen" -dry-run || true echo '################################################################################' exit_code=1 fi # Make sure changes to generation code and the .tables.json are coordinated. if ! git diff --cached --name-only --diff-filter=M -z "${against}" | \grep --null --quiet --extended-regexp '^\.tables\.json$'; then if [ $exit_code -ne 0 ]; then echo '' # Add a new line to separate this error from the prior one fi echo "${ERROR}: please stage your changes to \".tables.json\"" echo '' echo 'The ".tables.json" file is used by "gen.go" to determine' echo 'when the Unicode tables need to be updated and they should' echo 'always be committed together.' echo '' exit_code=1 fi exit $exit_code fi