49 lines
1.5 KiB
Bash
49 lines
1.5 KiB
Bash
#!/bin/sh
|
|
# Pre-commit hook for ULFlow Golang Starter Kit
|
|
# This hook runs linters and checks before allowing a commit
|
|
|
|
echo "Running pre-commit checks..."
|
|
|
|
# Check for staged Go files
|
|
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.go$")
|
|
|
|
if [[ "$STAGED_GO_FILES" = "" ]]; then
|
|
echo "No Go files staged for commit. Skipping Go-specific checks."
|
|
else
|
|
# Run golangci-lint on staged files
|
|
echo "Running golangci-lint..."
|
|
golangci-lint run ./...
|
|
if [ $? -ne 0 ]; then
|
|
echo "golangci-lint failed. Please fix the issues before committing."
|
|
exit 1
|
|
fi
|
|
|
|
# Check for formatting issues
|
|
echo "Checking Go formatting..."
|
|
gofmt -l $STAGED_GO_FILES
|
|
if [ $? -ne 0 ]; then
|
|
echo "gofmt failed. Please run 'gofmt -w' on your files before committing."
|
|
exit 1
|
|
fi
|
|
|
|
# Run tests related to staged files
|
|
echo "Running relevant tests..."
|
|
go test $(go list ./... | grep -v vendor) -short
|
|
if [ $? -ne 0 ]; then
|
|
echo "Tests failed. Please fix the tests before committing."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Check for sensitive information in staged files
|
|
echo "Checking for sensitive information..."
|
|
if git diff --cached | grep -E "(API_KEY|SECRET|PASSWORD|TOKEN).*[A-Za-z0-9_/-]{8,}" > /dev/null; then
|
|
echo "WARNING: Possible sensitive information detected in commit."
|
|
echo "Please verify you're not committing secrets, credentials or personal data."
|
|
echo "Press Enter to continue or Ctrl+C to abort the commit."
|
|
read
|
|
fi
|
|
|
|
echo "Pre-commit checks passed!"
|
|
exit 0
|