Skip to content

Add Go pre-commit hook#55

Merged
haasonsaas merged 1 commit intomainfrom
codex/keep-go-precommit
Apr 15, 2026
Merged

Add Go pre-commit hook#55
haasonsaas merged 1 commit intomainfrom
codex/keep-go-precommit

Conversation

@haasonsaas
Copy link
Copy Markdown
Contributor

Summary

  • add a repo-committed Go pre-commit hook for staged Go changes
  • add make install-hooks to install the hook into .git/hooks
  • run gofmt, golangci-lint, and go test ./... before Go commits land

Testing

  • bash -n scripts/pre-commit
  • make install-hooks
  • GOTOOLCHAIN=go1.26.1 go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.8
  • PATH="/tmp/keep-gobin:$PATH" golangci-lint run ./...
  • go test ./...

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 15, 2026

PR Summary

Low Risk
Low risk: adds local developer tooling only, but could slow/interrupt commits due to running golangci-lint and go test on each Go commit.

Overview
Introduces a repo-committed scripts/pre-commit hook that, when staged .go files are present, stashes unrelated working-tree changes, runs gofmt on staged Go files (and re-adds them), then enforces golangci-lint run ./... and go test ./... before allowing the commit.

Adds make install-hooks to install this hook into .git/hooks/pre-commit.

Reviewed by Cursor Bugbot for commit c81d805. Bugbot is set up for automated code reviews on this repo. Configure here.

@haasonsaas haasonsaas merged commit 81a775b into main Apr 15, 2026
11 checks passed
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix prepared fixes for both issues found in the latest run.

  • ✅ Fixed: Tool check runs after index is already modified
    • Moved the golangci-lint availability check ahead of any stashing, formatting, or git add work so the hook fails before mutating state.
  • ✅ Fixed: Hook fails on macOS default bash lacking mapfile
    • Replaced mapfile with a portable while read loop so the hook works with macOS's default Bash 3.2.

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit c81d805. Configure here.

Comment thread scripts/pre-commit
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tool check runs after index is already modified

Low Severity

The golangci-lint availability check on line 30 runs after gofmt -w and git add have already mutated the working tree and index on lines 27–28. If the tool is missing, the hook exits with an error, but the developer's staged content has been silently reformatted. The pre-flight tool check needs to happen before any index/worktree modifications (i.e., before the gofmt -w / git add block).

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c81d805. Configure here.

Comment thread scripts/pre-commit
repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"

mapfile -t staged_go_files < <(git diff --cached --name-only --diff-filter=ACM -- "*.go")
Copy link
Copy Markdown

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 mapfile

Medium Severity

mapfile is 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 bash on a stock Mac resolves to /bin/bash (3.2), causing an immediate mapfile: command not found error that blocks every Go commit. This is a frequently reported issue in other projects. A while IFS= read -r loop achieves the same result portably.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c81d805. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant