Debian Codegraph @

scripts/pre-commit

raw
1 #!/bin/sh
2
3 set -eu
4
5 # Check if 'go generate' needs to be run.
6
7 # TODO: see if we can only run this against the Unicode version native to
8 # the installed version of Go since that takes around ~40ms compared to
9 # ~450ms when we have to generate all the tables for the non-native version.
10
11 FILES='^(tables_\w+\.go|internal/gen/gentables/main\.go|)$'
12
13 ERROR=Error
14 if [ -t 1 ]; then
15 ERROR='\033[0;31mError\033[0m' # red
16 fi
17
18 if git rev-parse --verify HEAD >/dev/null 2>&1; then
19 against=HEAD
20 else
21 # Initial commit: diff against an empty tree object
22 against=$(git hash-object -t tree /dev/null)
23 fi
24
25 # Redirect output to stderr.
26 exec 1>&2
27
28 if git diff --cached --name-only --diff-filter=M -z "${against}" |
29 \grep --null --quiet --extended-regexp "${FILES}"; then
30
31 # Project root
32 root="$(git rev-parse --show-toplevel)"
33 exit_code=0
34
35 [ -d "${root}/bin" ] || mkdir -p "${root}/bin"
36 GOBIN="${root}/bin" go install "${root}/gen.go"
37
38 if ! "${root}/bin/gen" -dry-run >/dev/null 2>&1; then
39 echo "${ERROR}: Cannot commit changes without re-running \`go generate\`."
40 echo ''
41 echo 'This will cause failures in CI.'
42 echo ''
43 echo "Output of \`go run ${root}/bin/gen -dry-run\`:"
44 echo ''
45 echo '################################################################################'
46 "${root}/bin/gen" -dry-run || true
47 echo '################################################################################'
48
49 exit_code=1
50 fi
51
52 # Make sure changes to generation code and the .tables.json are coordinated.
53 if ! git diff --cached --name-only --diff-filter=M -z "${against}" |
54 \grep --null --quiet --extended-regexp '^\.tables\.json$'; then
55
56 if [ $exit_code -ne 0 ]; then
57 echo '' # Add a new line to separate this error from the prior one
58 fi
59 echo "${ERROR}: please stage your changes to \".tables.json\""
60 echo ''
61 echo 'The ".tables.json" file is used by "gen.go" to determine'
62 echo 'when the Unicode tables need to be updated and they should'
63 echo 'always be committed together.'
64 echo ''
65
66 exit_code=1
67 fi
68
69 exit $exit_code
70 fi