From bf3997c77726e970f97ec8f4d1a71ce5c45bde60 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Tue, 29 Aug 2023 16:47:13 +0300 Subject: [PATCH] chore: use lefthook in lefthook project --- .gitignore | 3 --- .lefthook.toml | 15 +++++++++++++++ Makefile | 2 +- docs/usage.md | 2 +- internal/lefthook/run.go | 2 +- internal/lefthook/run/prepare_command.go | 19 ++++++++++--------- internal/lefthook/run/runner.go | 8 ++++---- internal/lefthook/run/runner_test.go | 2 +- 8 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 .lefthook.toml diff --git a/.gitignore b/.gitignore index bfd1d16a..3fb90110 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ .vscode/ .idea/ -.lefthook-local/ -.lefthook/ /lefthook-local.yml -/lefthook.yml /lefthook tmp/ diff --git a/.lefthook.toml b/.lefthook.toml new file mode 100644 index 00000000..f0194a6f --- /dev/null +++ b/.lefthook.toml @@ -0,0 +1,15 @@ +[pre-commit] +parallel = true + +[pre-commit.commands.lint] +run = "make lint" +glob = "*.go" +stage_fixed = true + +[pre-commit.commands.test] +run = "make test" +glob = "*.go" + +[pre-commit.commands.lychee] +glob = "*.md" +run = "lychee {staged_files}" diff --git a/Makefile b/Makefile index 6c8fc21e..cd044ada 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ bin/golangci-lint: curl -sSfL https://github.com/raw/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.54.1 lint: bin/golangci-lint - $$(go env GOPATH)/bin/golangci-lint run + $$(go env GOPATH)/bin/golangci-lint run --fix .ONESHELL: version: diff --git a/docs/usage.md b/docs/usage.md index 7e3ec6d9..ec251d51 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -219,7 +219,7 @@ When you try to commit `git commit -m "haha bad commit text"` script `commitlint ### Parallel execution You can enable parallel execution if you want to speed up your checks. -Lets get example from [discourse](https://github.com/discourse/discourse/blob/master/.travis.yml#L77-L83) project. +Lets imagine we have the following rules to lint the whole project: ``` bundle exec rubocop --parallel && \ diff --git a/internal/lefthook/run.go b/internal/lefthook/run.go index 8f3fa8e7..bd3770c1 100644 --- a/internal/lefthook/run.go +++ b/internal/lefthook/run.go @@ -108,7 +108,7 @@ Run 'lefthook install' manually.`, resultChan := make(chan run.Result, len(hook.Commands)+len(hook.Scripts)) runner := run.NewRunner( - run.Opts{ + run.Options{ Repo: l.repo, Hook: hook, HookName: hookName, diff --git a/internal/lefthook/run/prepare_command.go b/internal/lefthook/run/prepare_command.go index b4a210ec..a6c3caec 100644 --- a/internal/lefthook/run/prepare_command.go +++ b/internal/lefthook/run/prepare_command.go @@ -67,6 +67,9 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { if len(command.Files) > 0 { filesCmd = command.Files } + if len(filesCmd) > 0 { + filesCmd = replacePositionalArguments(filesCmd, r.GitArgs) + } var stagedFiles func() ([]string, error) switch { @@ -83,7 +86,6 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { config.PushFiles: r.Repo.PushFiles, config.SubAllFiles: r.Repo.AllFiles, config.SubFiles: func() ([]string, error) { - filesCmd = r.replacePositionalArguments(filesCmd) return r.Repo.FilesByCommand(filesCmd) }, } @@ -114,8 +116,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { // Checking substitutions and skipping execution if it is empty. // - // Special case - `files` option: return if the result of files - // command is empty. + // Special case for `files` option: return if the result of files command is empty. if len(filesCmd) > 0 && templates[config.SubFiles] == nil { files, err := filesFns[config.SubFiles]() if err != nil { @@ -130,8 +131,8 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { } runString := command.Run - runString = r.replacePositionalArguments(runString) - log.Debugf("[lefthook] found templates: %+v", templates) + runString = replacePositionalArguments(runString, r.GitArgs) + var maxlen int switch runtime.GOOS { case "windows": @@ -172,10 +173,10 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) { return result, nil, nil } -func (r *Runner) replacePositionalArguments(str string) string { - str = strings.ReplaceAll(str, "{0}", strings.Join(r.GitArgs, " ")) - for i, gitArg := range r.GitArgs { - str = strings.ReplaceAll(str, fmt.Sprintf("{%d}", i+1), gitArg) +func replacePositionalArguments(str string, args []string) string { + str = strings.ReplaceAll(str, "{0}", strings.Join(args, " ")) + for i, arg := range args { + str = strings.ReplaceAll(str, fmt.Sprintf("{%d}", i+1), arg) } return str } diff --git a/internal/lefthook/run/runner.go b/internal/lefthook/run/runner.go index f2aa90f8..380471ae 100644 --- a/internal/lefthook/run/runner.go +++ b/internal/lefthook/run/runner.go @@ -31,7 +31,7 @@ const ( var surroundingQuotesRegexp = regexp.MustCompile(`^'(.*)'$`) -type Opts struct { +type Options struct { Repo *git.Repository Hook *config.Hook HookName string @@ -46,16 +46,16 @@ type Opts struct { // Runner responds for actual execution and handling the results. type Runner struct { - Opts + Options partiallyStagedFiles []string failed atomic.Bool executor exec.Executor } -func NewRunner(opts Opts) *Runner { +func NewRunner(opts Options) *Runner { return &Runner{ - Opts: opts, + Options: opts, executor: exec.CommandExecutor{}, } } diff --git a/internal/lefthook/run/runner_test.go b/internal/lefthook/run/runner_test.go index 035b1def..e16e24fc 100644 --- a/internal/lefthook/run/runner_test.go +++ b/internal/lefthook/run/runner_test.go @@ -728,7 +728,7 @@ func TestRunAll(t *testing.T) { resultChan := make(chan Result, len(tt.hook.Commands)+len(tt.hook.Scripts)) executor := TestExecutor{} runner := &Runner{ - Opts: Opts{ + Options: Options{ Repo: repo, Hook: tt.hook, HookName: tt.hookName,