Skip to content

Commit

Permalink
fix: add general action for building both commands and do actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 1, 2024
1 parent 3b478c7 commit 74f7268
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 238 deletions.
8 changes: 4 additions & 4 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/evilmartians/lefthook/internal/system"
)

var errFilesIncompatible = errors.New("One of your runners contains incompatible file types")
var ErrFilesIncompatible = errors.New("One of your runners contains incompatible file types")

type Command struct {
Run string `json:"run" mapstructure:"run" toml:"run" yaml:"run"`
Expand Down Expand Up @@ -39,16 +39,16 @@ type commandRunReplace struct {
}

func (c Command) Validate() error {
if !isRunnerFilesCompatible(c.Run) {
return errFilesIncompatible
if !IsRunnerFilesCompatible(c.Run) {
return ErrFilesIncompatible
}

return nil
}

func (c Command) DoSkip(state func() git.State) bool {
skipChecker := NewSkipChecker(system.Cmd)
return skipChecker.check(state, c.Skip, c.Only)
return skipChecker.Check(state, c.Skip, c.Only)
}

func (c Command) ExecutionPriority() int {
Expand Down
14 changes: 14 additions & 0 deletions internal/config/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ type Group struct {
Do []*Do `json:"do,omitempty" mapstructure:"do" toml:"do,omitempty" yaml:",omitempty"`
}

func (do *Do) PrintableName(id string) string {
if len(do.Name) != 0 {
return do.Name
}
if len(do.Run) != 0 {
return do.Run
}
if len(do.Script) != 0 {
return do.Script
}

return "(" + id + ")"
}

func (do *Do) Validate() error {
if len(do.Run) > 0 && len(do.Script) > 0 {
return errAmbiguousDo
Expand Down
2 changes: 1 addition & 1 deletion internal/config/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
SubPushFiles string = "{push_files}"
)

func isRunnerFilesCompatible(runner string) bool {
func IsRunnerFilesCompatible(runner string) bool {
if strings.Contains(runner, SubStagedFiles) && strings.Contains(runner, SubPushFiles) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h *Hook) Validate() error {

func (h *Hook) DoSkip(state func() git.State) bool {
skipChecker := NewSkipChecker(system.Cmd)
return skipChecker.check(state, h.Skip, h.Only)
return skipChecker.Check(state, h.Skip, h.Only)
}

func unmarshalHooks(base, extra *viper.Viper) (*Hook, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type scriptRunnerReplace struct {

func (s Script) DoSkip(state func() git.State) bool {
skipChecker := NewSkipChecker(system.Cmd)
return skipChecker.check(state, s.Skip, s.Only)
return skipChecker.Check(state, s.Skip, s.Only)
}

func (s Script) ExecutionPriority() int {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/skip_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewSkipChecker(cmd system.Command) *skipChecker {
}

// check returns the result of applying a skip/only setting which can be a branch, git state, shell command, etc.
func (sc *skipChecker) check(state func() git.State, skip interface{}, only interface{}) bool {
func (sc *skipChecker) Check(state func() git.State, skip interface{}, only interface{}) bool {
if skip == nil && only == nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/skip_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestDoSkip(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
if skipChecker.check(tt.state, tt.skip, tt.only) != tt.skipped {
if skipChecker.Check(tt.state, tt.skip, tt.only) != tt.skipped {
t.Errorf("Expected: %v, Was %v", tt.skipped, !tt.skipped)
}
})
Expand Down
85 changes: 85 additions & 0 deletions internal/lefthook/runner/action/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package action

import (
"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/system"
)

type Params struct {
Repo *git.Repository
Hook *config.Hook
HookName string
GitArgs []string
Force bool
ForceFiles []string

Run string
Root string
Glob string
Files string
FileTypes []string
Tags []string
Exclude interface{}
Only interface{}
Skip interface{}
}

type Action struct {
Execs []string
Files []string
}

func New(name string, params *Params) (*Action, error) {
if params.skip() {
return nil, SkipError{"settings"}
}

if intersect(params.Hook.ExcludeTags, params.Tags) {
return nil, SkipError{"tags"}
}

if intersect(params.Hook.ExcludeTags, []string{name}) {
return nil, SkipError{"name"}
}

if err := params.validate(); err != nil {
return nil, err
}

action, err := build(params)
if err != nil {
return nil, err
}

return action, nil
}

func (p *Params) skip() bool {
skipChecker := config.NewSkipChecker(system.Cmd)
return skipChecker.Check(p.Repo.State, p.Skip, p.Only)
}

func (p *Params) validate() error {
if !config.IsRunnerFilesCompatible(p.Run) {
return config.ErrFilesIncompatible
}

return nil
}

func intersect(a, b []string) bool {
intersections := make(map[string]struct{}, len(a))

for _, v := range a {
intersections[v] = struct{}{}
}

for _, v := range b {
if _, ok := intersections[v]; ok {
return true
}
}

return false
}
Loading

0 comments on commit 74f7268

Please sign in to comment.