Pre-commit git hook to require Golang formatting and linting
- Add this file in a local repo at
.githooks/pre-commit
. git config core.hooksPath .githooks
sets up the hook
.githooks/pre-commit
.git config core.hooksPath .githooks
sets up the hook#!/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 |