Skip to content

Commit

Permalink
fix: move filters into a separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Aug 30, 2023
1 parent 28c9779 commit 9d46a82
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package run
package filter

import (
"regexp"
"strings"

"github.com/gobwas/glob"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/log"
)

func filterGlob(vs []string, matcher string) []string {
func Apply(command *config.Command, files []string) []string {
if len(files) == 0 {
return nil
}

log.Debug("[lefthook] files before filters:\n", files)

files = byGlob(files, command.Glob)
files = byExclude(files, command.Exclude)
files = byRoot(files, command.Root)

log.Debug("[lefthook] files after filters:\n", files)

return files
}

func byGlob(vs []string, matcher string) []string {
if matcher == "" {
return vs
}
Expand All @@ -23,7 +42,7 @@ func filterGlob(vs []string, matcher string) []string {
return vsf
}

func filterExclude(vs []string, matcher string) []string {
func byExclude(vs []string, matcher string) []string {
if matcher == "" {
return vs
}
Expand All @@ -37,7 +56,7 @@ func filterExclude(vs []string, matcher string) []string {
return vsf
}

func filterRelative(vs []string, matcher string) []string {
func byRoot(vs []string, matcher string) []string {
if matcher == "" {
return vs
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run
package filter

import (
"fmt"
Expand All @@ -10,16 +10,22 @@ func slicesEqual(a, b []string) bool {
return false
}

for i, item := range a {
if item != b[i] {
r := make(map[string]struct{})

for _, item := range a {
r[item] = struct{}{}
}

for _, item := range b {
if _, ok := r[item]; !ok {
return false
}
}

return true
}

func TestFilterGlob(t *testing.T) {
func TestByGlob(t *testing.T) {
for i, tt := range [...]struct {
source, result []string
glob string
Expand Down Expand Up @@ -51,15 +57,15 @@ func TestFilterGlob(t *testing.T) {
},
} {
t.Run(fmt.Sprintf("%d:", i), func(t *testing.T) {
res := filterGlob(tt.source, tt.glob)
res := byGlob(tt.source, tt.glob)
if !slicesEqual(res, tt.result) {
t.Errorf("expected %v to be equal to %v", res, tt.result)
}
})
}
}

func TestFilterExclude(t *testing.T) {
func TestByExclude(t *testing.T) {
for i, tt := range [...]struct {
source, result []string
exclude string
Expand Down Expand Up @@ -91,15 +97,15 @@ func TestFilterExclude(t *testing.T) {
},
} {
t.Run(fmt.Sprintf("%d:", i), func(t *testing.T) {
res := filterExclude(tt.source, tt.exclude)
res := byExclude(tt.source, tt.exclude)
if !slicesEqual(res, tt.result) {
t.Errorf("expected %v to be equal to %v", res, tt.result)
}
})
}
}

func TestFilterRelative(t *testing.T) {
func TestByRoot(t *testing.T) {
for i, tt := range [...]struct {
source, result []string
path string
Expand All @@ -126,7 +132,7 @@ func TestFilterRelative(t *testing.T) {
},
} {
t.Run(fmt.Sprintf("%d:", i), func(t *testing.T) {
res := filterRelative(tt.source, tt.path)
res := byRoot(tt.source, tt.path)
if !slicesEqual(res, tt.result) {
t.Errorf("expected %v to be equal to %v", res, tt.result)
}
Expand Down
25 changes: 5 additions & 20 deletions internal/lefthook/run/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gopkg.in/alessio/shellescape.v1"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/lefthook/run/filter"
"github.com/evilmartians/lefthook/internal/log"
)

Expand Down Expand Up @@ -106,7 +107,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) {
return nil, fmt.Errorf("error replacing %s: %w", filesType, err), nil
}

files = filterFiles(command, files)
files = filter.Apply(command, files)
if len(files) == 0 {
return nil, nil, errors.New("no files for inspection")
}
Expand All @@ -123,7 +124,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) {
return nil, fmt.Errorf("error calling replace command for %s: %w", config.SubFiles, err), nil
}

files = filterFiles(command, files)
files = filter.Apply(command, files)

if len(files) == 0 {
return nil, nil, errors.New("no files for inspection")
Expand Down Expand Up @@ -151,7 +152,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) {

files, err := r.Repo.StagedFiles()
if err == nil {
if len(filterFiles(command, files)) == 0 {
if len(filter.Apply(command, files)) == 0 {
return nil, nil, errors.New("no matching staged files")
}
}
Expand All @@ -164,7 +165,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) {

files, err := r.Repo.PushFiles()
if err == nil {
if len(filterFiles(command, files)) == 0 {
if len(filter.Apply(command, files)) == 0 {
return nil, nil, errors.New("no matching push files")
}
}
Expand All @@ -181,22 +182,6 @@ func replacePositionalArguments(str string, args []string) string {
return str
}

func filterFiles(command *config.Command, files []string) []string {
if files == nil {
return []string{}
}

log.Debug("[lefthook] files before filters:\n", files)

files = filterGlob(files, command.Glob)
files = filterExclude(files, command.Exclude)
files = filterRelative(files, command.Root)

log.Debug("[lefthook] files after filters:\n", files)

return files
}

// Escape file names to prevent unexpected bugs.
func escapeFiles(files []string) []string {
var filesEsc []string
Expand Down
20 changes: 20 additions & 0 deletions internal/lefthook/run/prepare_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ import (
"testing"
)

func slicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}

r := make(map[string]struct{})

for _, item := range a {
r[item] = struct{}{}
}

for _, item := range b {
if _, ok := r[item]; !ok {
return false
}
}

return true
}

func TestGetNChars(t *testing.T) {
for i, tt := range [...]struct {
source, cut, rest []string
Expand Down
3 changes: 2 additions & 1 deletion internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/lefthook/run/exec"
"github.com/evilmartians/lefthook/internal/lefthook/run/filter"
"github.com/evilmartians/lefthook/internal/log"
)

Expand Down Expand Up @@ -387,7 +388,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
return
}

files = filterFiles(command, files)
files = filter.Apply(command, files)
}

if len(command.Root) > 0 {
Expand Down

0 comments on commit 9d46a82

Please sign in to comment.