-
Notifications
You must be signed in to change notification settings - Fork 0
Add Go pre-commit hook #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| repo_root=$(git rev-parse --show-toplevel) | ||
| cd "$repo_root" | ||
|
|
||
| mapfile -t staged_go_files < <(git diff --cached --name-only --diff-filter=ACM -- "*.go") | ||
| if [ ${#staged_go_files[@]} -eq 0 ]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| stashed=0 | ||
| cleanup() { | ||
| if [ "$stashed" -eq 1 ]; then | ||
| git stash pop -q >/dev/null 2>&1 || true | ||
| fi | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| if ! git diff --quiet -- . || [ -n "$(git ls-files --others --exclude-standard)" ]; then | ||
| git stash push -q --keep-index --include-untracked -m "pre-commit-$(date +%s)" | ||
| stashed=1 | ||
| fi | ||
|
|
||
| echo "Running gofmt on staged files..." | ||
| gofmt -w "${staged_go_files[@]}" | ||
| git add -- "${staged_go_files[@]}" | ||
|
|
||
| if ! command -v golangci-lint >/dev/null 2>&1; then | ||
| echo "golangci-lint is required for this repo. Install it and retry." >&2 | ||
| exit 1 | ||
| fi | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tool check runs after index is already modifiedLow Severity The Additional Locations (1)Reviewed by Cursor Bugbot for commit c81d805. Configure here. |
||
|
|
||
| echo "Running golangci-lint..." | ||
| golangci-lint run ./... | ||
|
|
||
| echo "Running go test..." | ||
| go test ./... | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hook fails on macOS default bash lacking
mapfileMedium Severity
mapfileis a bash 4.0+ built-in, but macOS ships bash 3.2 (Apple won't update past GPLv2). Running this hook with/usr/bin/env bashon a stock Mac resolves to/bin/bash(3.2), causing an immediatemapfile: command not founderror that blocks every Go commit. This is a frequently reported issue in other projects. Awhile IFS= read -rloop achieves the same result portably.Reviewed by Cursor Bugbot for commit c81d805. Configure here.