Skip to content

Commit 56d2b9e

Browse files
committed
fix: enable avalanche_golangci_lint hook
1 parent 45b9885 commit 56d2b9e

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ repos:
3333
# Local utility scripts and full repo checks
3434
- repo: local
3535
hooks:
36-
# TODO: Re-enable this once we have a way to run the extra lints in CI.
37-
# See https://github.com/ava-labs/coreth/pull/1117
38-
# # Stricter extra lints (golangci-lint v2) on staged changes at pre-commit
39-
# - id: extra-golangci-lint
40-
# name: golangci-lint (extra via scripts/lint.sh)
41-
# entry: bash -lc 'TESTS="extra_golangci_lint" scripts/lint.sh'
42-
# language: system
43-
# stages: [pre-commit]
44-
# pass_filenames: false
36+
# Stricter extra lints (golangci-lint v2) on staged changes at pre-commit
37+
- id: extra-golangci-lint
38+
name: golangci-lint (extra via scripts/lint.sh)
39+
entry: bash -lc 'TESTS="avalanche_golangci_lint" scripts/lint.sh'
40+
language: system
41+
stages: [pre-commit]
42+
pass_filenames: false
4543

4644
- id: actionlint
4745
name: actionlint

scripts/lint.sh

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,62 @@ if ! [[ "$0" =~ scripts/lint.sh ]]; then
77
exit 255
88
fi
99

10-
# The -P option is not supported by the grep version installed by
11-
# default on macos. Since `-o errexit` is ignored in an if
12-
# conditional, triggering the problem here ensures script failure when
13-
# using an unsupported version of grep.
14-
grep -P 'lint.sh' scripts/lint.sh &>/dev/null || (
10+
# Darwin-specific environment handling: ensure Bash 4+ and GNU grep availability.
11+
function handle_darwin_environment_specifics {
12+
# Re-exec with Homebrew bash when system bash is < 4 (macOS default 3.2)
13+
if ! { test -n "${BASH_VERSINFO:-}" && [ "${BASH_VERSINFO[0]}" -ge 4 ]; }; then
14+
for _brew_bash in "/opt/homebrew/bin/bash" "/usr/local/bin/bash"; do
15+
if [[ -x "${_brew_bash}" ]]; then
16+
exec "${_brew_bash}" "$0" "$@"
17+
fi
18+
done
19+
echo >&2 "error: This script requires bash >= 4. On macOS, install with: brew install bash"
20+
echo >&2 " Then ensure Homebrew bash is used first in PATH or re-run via: /opt/homebrew/bin/bash scripts/lint.sh"
21+
exit 255
22+
fi
23+
24+
# Prefer ggrep when available; make a shim so plain `grep` resolves to it
25+
if command -v ggrep >/dev/null 2>&1; then
26+
if ggrep -P 'lint.sh' scripts/lint.sh &>/dev/null; then
27+
_gnu_grep_shim_dir="$(mktemp -d)"
28+
ln -s "$(command -v ggrep)" "${_gnu_grep_shim_dir}/grep" 2>/dev/null || true
29+
export PATH="${_gnu_grep_shim_dir}:$PATH"
30+
fi
31+
fi
32+
33+
# Try Homebrew gnubin via brew and well-known locations
34+
if command -v brew >/dev/null 2>&1; then
35+
local gnubin_dir
36+
gnubin_dir="$(brew --prefix grep 2>/dev/null)/libexec/gnubin"
37+
if [[ -d "$gnubin_dir" ]]; then
38+
export PATH="$gnubin_dir:$PATH"
39+
fi
40+
fi
41+
for d in \
42+
"/opt/homebrew/opt/grep/libexec/gnubin" \
43+
"/usr/local/opt/grep/libexec/gnubin"; do
44+
if [[ -d "$d" ]]; then
45+
export PATH="$d:$PATH"
46+
fi
47+
done
48+
}
49+
50+
# Run Darwin adjustments early if applicable
51+
if [[ "$(uname -s)" == "Darwin" ]]; then
52+
handle_darwin_environment_specifics "$@"
53+
fi
54+
55+
# Final probe; fail hard if still unsupported
56+
if ! grep -P 'lint.sh' scripts/lint.sh &>/dev/null; then
1557
echo >&2 "error: This script requires a recent version of gnu grep."
16-
echo >&2 " On macos, gnu grep can be installed with 'brew install grep'."
17-
echo >&2 " It will also be necessary to ensure that gnu grep is available in the path."
58+
if [[ "$(uname -s)" == "Darwin" ]]; then
59+
echo >&2 " On macOS, install with: brew install grep"
60+
echo >&2 " Ensure gnubin is on PATH, e.g.:"
61+
echo >&2 " export PATH=\"/opt/homebrew/opt/grep/libexec/gnubin:\$PATH\" # Apple Silicon"
62+
echo >&2 " export PATH=\"/usr/local/opt/grep/libexec/gnubin:\$PATH\" # Intel"
63+
fi
1864
exit 255
19-
)
65+
fi
2066

2167
# Read excluded directories into arrays
2268
DEFAULT_FILES=()

0 commit comments

Comments
 (0)