cosmos-sdk/contrib/githooks/pre-commit
Alessio Treglia 6469447d52
add pre-commit hook (#6041)
* add pre-commit hook

Co-authored-by: Gianguido Sora <gsora@users.noreply.github.com>

* add go tools installation notes

* Update CONTRIBUTING.md

* run both go mod and golangci-lint

* silence which

* add go.mod go.sum after go mod tidy

* update README

* look up golangci-lint specifically in GOPATH/bin

* don't run golangci-lint

* exclude proto files

Co-authored-by: Gianguido Sora <gsora@users.noreply.github.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-04-28 14:51:42 +00:00

42 lines
867 B
Bash
Executable File

#!/bin/bash
set -e
CMDS='git go gofmt goimports misspell'
STAGED_GO_FILES=$(git diff --cached --name-only -- '*.go')
f_echo_stderr() {
echo $@ >&2
}
f_exit_success() {
[ x"$@" != "x" ] && f_echo_stderr $@ || exit 0
}
trap f_exit_success EXIT
f_check_cmds() {
for cmd in ${CMDS}; do
which ${cmd} &>/dev/null || f_exit_success "couldn't find ${cmd}, skipping"
done
}
f_check_cmds
if [[ $STAGED_GO_FILES != "" ]]; then
f_echo_stderr "[pre-commit] fmt'ing staged files..."
for file in $STAGED_GO_FILES; do
if [[ $file =~ vendor/ ]] || [[ $file =~ client/lcd/statik/ ]] || [[ $file =~ tests/mocks/ ]] || [[ $file =~ \.pb\.go ]]; then
continue
fi
gofmt -w -s $file
misspell -w $file
goimports -w -local github.com/cosmos/cosmos-sdk $file
git add $file
done
fi
# Run go mod tidy
go mod tidy && git add go.mod go.sum