File: pre-commit

package info (click to toggle)
golang-github-charlievieth-strcase 0.0.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,360 kB
  • sloc: asm: 1,836; makefile: 203; sh: 146
file content (70 lines) | stat: -rwxr-xr-x 2,222 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/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