From fa4ffb54f82c88bbea1f1a690732c76e5cbac1ea Mon Sep 17 00:00:00 2001 From: Felix Wiedmann Date: Sun, 9 May 2021 00:28:17 +0200 Subject: [PATCH] build(githooks): add githooks --- .githooks/pre-commit | 23 +++++++++++++++++++++++ README.md | 10 ++++++++++ 2 files changed, 33 insertions(+) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..ed4ad84 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,23 @@ +#!/bin/bash +## this will retrieve all of the .go files that have been +## changed since the last commit +STAGED_GO_FILES=$(git diff --cached --name-only -- '*.go') + +if [[ $STAGED_GO_FILES == "" ]]; then + echo "No Go Files to Update" +## otherwise we can do stuff with these changed go files +else + for file in $STAGED_GO_FILES; do + ## format our file + go fmt $file + ## add any potential changes from our formatting to the + ## commit + git add $file + done +fi + +echo "go test" +go test .././... + +echo "golangci-lint run" +golangci-lint run diff --git a/README.md b/README.md index af45ad4..40db6fd 100644 --- a/README.md +++ b/README.md @@ -341,3 +341,13 @@ go test ./... curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0 golangci-lint run ./... ``` + +### Git Hooks + +The `.githooks` folder contains a pre-commit script which has to be run before each commit. +To enable the hooks please add the `.githooks` folder to your core hooksPath via: `git config core.hooksPath .githooks`. +The following will be run in the pre-commit script: + +- Formats all `*.go` files which are staged with `go fmt` +- Runs `go test` for the whole project +- Runs `golangci-lint` to check for linting errors \ No newline at end of file