add cilint git hook

This commit is contained in:
Martin Thielecke 2023-08-24 19:18:46 +02:00
parent cc9ca58a85
commit 9c40173268
Signed by: mthie
GPG Key ID: D1D25A85C8604DFB
2 changed files with 38 additions and 10 deletions

View File

@ -9,24 +9,27 @@ usage
* to install the hooks use:
go get github.com/mthie/git-gohooks
go get github.com/mthie/git-gohooks/githook-gobuild
go get github.com/mthie/git-gohooks/githook-gofmt
go get github.com/mthie/git-gohooks/githook-gotest
go get git.mthie.com/mthie/git-gohooks
go get git.mthie.com/mthie/git-gohooks/githook-golangci-lint
go get git.mthie.com/mthie/git-gohooks/githook-gobuild
go get git.mthie.com/mthie/git-gohooks/githook-gofmt
go get git.mthie.com/mthie/git-gohooks/githook-gotest
* on a Unix based system symlink it with
( cd .git/hooks && \
ln -s $GOPATH/bin/git-gohooks pre-commit && \
ln -s $GOPATH/bin/githook-gofmt pre-commit_01_gofmt && \
ln -s $GOPATH/bin/githook-gobuild pre-commit_02_gobuild && \
ln -s $GOPATH/bin/githook-gotest pre-commit_03_gotest )
ln -s $GOPATH/bin/githook-golangci-lint pre-commit_01_golangci_lint && \
ln -s $GOPATH/bin/githook-gofmt pre-commit_02_gofmt && \
ln -s $GOPATH/bin/githook-gobuild pre-commit_03_gobuild && \
ln -s $GOPATH/bin/githook-gotest pre-commit_04_gotest )
* on a Windows system in a command shell with **Administrator privileges**
cd .git\hooks
mklink /H pre-commit <YourGoPath>\bin\git-gohooks.exe
mklink /H pre-commit_01_gofmt.exe <YourGoPath>\bin\githook-gofmt.exe
mklink /H pre-commit_02_gobuild.exe <YourGoPath>\bin\githook-gobuild.exe
mklink /H pre-commit_03_gotest.exe <YourGoPath>\bin\githook-gotest.exe
mklink /H pre-commit_01_golangci_lint.exe <YourGoPath>\bin\githook-golangci-lint.exe
mklink /H pre-commit_02_gofmt.exe <YourGoPath>\bin\githook-gofmt.exe
mklink /H pre-commit_03_gobuild.exe <YourGoPath>\bin\githook-gobuild.exe
mklink /H pre-commit_04_gotest.exe <YourGoPath>\bin\githook-gotest.exe

View File

@ -0,0 +1,25 @@
package main
import (
"fmt"
"os"
"go.mthie.com/git-gohooks/general"
)
func main() {
files := general.GetChangedGoFiles()
if len(files) == 0 {
os.Exit(0)
return
}
os.Chdir(general.GetGitRoot())
result, status := general.RunCommand("golangci-lint", "run", "./...")
if status != 0 {
fmt.Fprintf(os.Stderr, "Lint failed, please commit only stuff that is linted.\n%s", result)
os.Exit(1)
return
}
}