From 6469447d52f8a4e48fbba6231bbe60f24cccfb75 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 28 Apr 2020 15:51:42 +0100 Subject: [PATCH] add pre-commit hook (#6041) * add pre-commit hook Co-authored-by: Gianguido Sora * 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 Co-authored-by: Alexander Bezobchuk Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- CONTRIBUTING.md | 8 +++++--- contrib/githooks/README.md | 21 +++++++++++++++++++ contrib/githooks/pre-commit | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 contrib/githooks/README.md create mode 100755 contrib/githooks/pre-commit diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 27804172b5..ce18156709 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -56,9 +56,11 @@ Other notes: - Looking for a good place to start contributing? How about checking out some [good first issues](https://github.com/cosmos/cosmos-sdk/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22) -- Please make sure to use `gofmt` before every commit - the easiest way to do - this is have your editor run it for you upon saving a file. Additionally - please ensure that your code is lint compliant by running `make lint` +- Please make sure to run `make format` before every commit - the easiest way + to do this is have your editor run it for you upon saving a file. Additionally + please ensure that your code is lint compliant by running `golangci-lint run`. + A convenience git `pre-commit` hook that runs the formatters automatically + before each commit is available in the `contrib/githooks/` directory. ## Architecture Decision Records (ADR) diff --git a/contrib/githooks/README.md b/contrib/githooks/README.md new file mode 100644 index 0000000000..83057835d4 --- /dev/null +++ b/contrib/githooks/README.md @@ -0,0 +1,21 @@ +# Git hooks + +Installation: + +``` +$ git config core.hooksPath contrib/githooks +``` + +## pre-commit + +The hook automatically runs `gofmt`, `goimports`, and `misspell` +to correctly format the `.go` files included in the commit, provided +that all the aforementioned commands are installed and available +in the user's search `$PATH` environment variable: + +``` +$ go get golang.org/x/tools/cmd/goimports +$ go get github.com/golangci/misspell/cmd/misspell@master +``` + +It also runs `go mod tidy` and `golangci-lint` if available. diff --git a/contrib/githooks/pre-commit b/contrib/githooks/pre-commit new file mode 100755 index 0000000000..b80fd291f1 --- /dev/null +++ b/contrib/githooks/pre-commit @@ -0,0 +1,41 @@ +#!/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