From 006c6f5647c5e1e0890f48e1fef6e57e83afda82 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Wed, 23 Aug 2023 17:18:04 +0100 Subject: [PATCH 1/4] pre-commit hook config added `.pre-commit-config.yaml` to the repo. To use it: - `pip install pre-commit` - Install the following Go packages: * go install github.com/go-critic/go-critic/cmd/gocritic@latest * go install https://pkg.go.dev/golang.org/x/tools/cmd/goimports@latest * go install golang.org/x/lint/golint@latest - In your clone, run: `pre-commit install` This will create .git/hooks/pre-commit and following that, these tests will run every time you invoke `git commit`: ```sh go fmt...................................................................Passed go imports...............................................................Passed go vet...................................................................Passed go lint..................................................................Passed go-critic................................................................Passed ``` --- .pre-commit-config.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..1810495 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +repos: + - repo: https://github.com/jessp01/pre-commit-golang.git + rev: v0.5.7 + hooks: + - id: go-fmt + - id: go-imports + - id: go-vet + - id: go-lint + - id: go-critic + - id: go-ineffassign + - id: shellcheck From 6286b107d87195af6568b034d9c44caeeb523062 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 25 Feb 2024 15:55:14 +0000 Subject: [PATCH 2/4] Invoke pre-commit checks during CI --- .github/workflows/test.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0e2b2fa..3d186d4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,5 +24,13 @@ jobs: - uses: actions/setup-go@v4 - uses: actions/checkout@v3 - run: | + set -e + sudo apt-get -qq update && sudo apt-get install -y shellcheck go install github.com/go-critic/go-critic/cmd/gocritic@latest - gocritic check . + go install golang.org/x/tools/cmd/goimports@latest + go install golang.org/x/lint/golint@latest + go install github.com/gordonklaus/ineffassign@latest + pip install pre-commit + pre-commit install + pre-commit run --all-files + go test -v From 1c446af831e01ad0ffefcc2b3590e94726645d98 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 25 Feb 2024 15:59:27 +0000 Subject: [PATCH 3/4] Added doc on pre-commit hooks usage. --- CONTRIBUTING.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 930a603..96a5cc9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -95,6 +95,42 @@ This is easy in Go. Just use `gofmt`. End of. Your code should also pass `golint` and `go vet` without errors (and if you want to run other linters too, that would be excellent). Very, very occasionally there are situations where `golint` incorrectly detects a problem, and the workaround is awkward or annoying. In that situation, comment on the PR and we'll work out how best to handle it. +### pre-commit hooks + +Install the needed GO packages: +```sh +go install github.com/go-critic/go-critic/cmd/gocritic@latest +go install golang.org/x/tools/cmd/goimports@latest +go install golang.org/x/lint/golint@latest +go install github.com/gordonklaus/ineffassign@latest +``` + +Install the `pre-commit` util: +```sh +pip install pre-commit +``` + +Generate `.git/hooks/pre-commit`: +```sh +pre-commit install +``` + +Following that, these tests will run every time you invoke `git commit`: +```sh +go fmt...................................................................Passed +go imports...............................................................Passed +go vet...................................................................Passed +go lint..................................................................Passed +go-critic................................................................Passed +shellcheck...............................................................Passed +``` + +To manually run all tests on all repo files, invoke: + +```sh +pre-commit run --all-files +``` + # Documentation It doesn't matter if you write the greatest piece of code in the history of the world, if no one knows it exists, or how to use it. From a42fe4287ee9aa50efdcdf2dc864c8b03a348e48 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 25 Feb 2024 16:25:48 +0000 Subject: [PATCH 4/4] rename stage --- .github/workflows/test.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3d186d4..14338d7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: golang/govulncheck-action@v1 - gocritic: + gocritic_lint_ineffassign_checks: runs-on: ubuntu-latest steps: - uses: actions/setup-go@v4 @@ -33,4 +33,3 @@ jobs: pip install pre-commit pre-commit install pre-commit run --all-files - go test -v