- Add this file in a local repo at
.githooks/pre-commit
. git config core.hooksPath .githooks
sets up the hook
Last active
April 4, 2023 19:05
-
-
Save rclark/316546e8939faca5f1a827f0feb74ca8 to your computer and use it in GitHub Desktop.
Pre-commit hook for Golang formatting and linting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter ACMR | grep ".go$" | grep -v '^vendor/') | |
if [ "$STAGED_GO_FILES" = "" ] | |
then exit 0 | |
fi | |
directories="" | |
for filename in $STAGED_GO_FILES | |
do directories="$directories $(dirname $filename)" | |
done | |
STAGED_GO_PACKAGES=$(echo "$directories" | tr " " "\n" | sort | uniq) | |
PASS=true | |
if ! gofmt -s -w $STAGED_GO_FILES | |
then PASS=false | |
fi | |
if ! golangci-lint run --fix $STAGED_GO_PACKAGES | |
then PASS=false | |
fi | |
if ! git add $STAGED_GO_FILES | |
then PASS=false | |
fi | |
if [ "$PASS" = "false" ] | |
then exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment