Skip to content

Commit

Permalink
chore: use lefthook in lefthook project
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Aug 29, 2023
1 parent a37020c commit bf3997c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
.vscode/
.idea/
.lefthook-local/
.lefthook/
/lefthook-local.yml
/lefthook.yml
/lefthook

tmp/
Expand Down
15 changes: 15 additions & 0 deletions .lefthook.toml
Original file line number Diff line number Diff line change
@@ -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}"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions internal/lefthook/run/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
},
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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":
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (

var surroundingQuotesRegexp = regexp.MustCompile(`^'(.*)'$`)

type Opts struct {
type Options struct {
Repo *git.Repository
Hook *config.Hook
HookName string
Expand All @@ -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{},
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bf3997c

Please sign in to comment.