Skip to content

Commit

Permalink
feat: allow provigind arrays to glob option
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jan 20, 2025
1 parent 40e97fc commit 5fd0169
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 36 deletions.
2 changes: 1 addition & 1 deletion internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Command struct {

FileTypes []string `json:"file_types,omitempty" koanf:"file_types" mapstructure:"file_types" toml:"file_types,omitempty" yaml:"file_types,omitempty"`

Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Glob []string `json:"glob,omitempty" jsonschema:"oneof_type=string" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Exclude interface{} `json:"exclude,omitempty" jsonschema:"oneof_type=string;array" mapstructure:"exclude" toml:"exclude,omitempty" yaml:",omitempty"`

Expand Down
8 changes: 4 additions & 4 deletions internal/config/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type Job struct {
Script string `json:"script,omitempty" jsonschema:"oneof_required=Run a script" mapstructure:"script" toml:"script,omitempty" yaml:",omitempty"`
Runner string `json:"runner,omitempty" mapstructure:"runner" toml:"runner,omitempty" yaml:",omitempty"`

Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"`
FailText string `json:"fail_text,omitempty" koanf:"fail_text" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`
Glob []string `json:"glob,omitempty" jsonschema:"oneof_type=string;array" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"`
FailText string `json:"fail_text,omitempty" koanf:"fail_text" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`

Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
FileTypes []string `json:"file_types,omitempty" koanf:"file_types" mapstructure:"file_types" toml:"file_types,omitempty" yaml:"file_types,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pre-push:
Tags: []string{"backend", "test"},
},
"lint": {
Glob: "*.rb",
Glob: []string{"*.rb"},
Run: "docker exec -it ruby:2.7 bundle exec rubocop",
Tags: []string{"backend", "linter"},
},
Expand Down Expand Up @@ -649,7 +649,7 @@ pre-commit:
"global-lint": {
Run: "bundle exec rubocop",
Tags: []string{"backend", "linter"},
Glob: "*.rb",
Glob: []string{"*.rb"},
},
"global-other": {
Run: "bundle exec rubocop",
Expand Down Expand Up @@ -783,7 +783,7 @@ pre-commit:
Jobs: []*Job{
{
Name: "group 1",
Glob: "*.rb",
Glob: []string{"*.rb"},
Group: &Group{
Parallel: true,
Jobs: []*Job{
Expand Down
30 changes: 22 additions & 8 deletions internal/lefthook/runner/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

type Params struct {
Glob string
Glob []string
Root string
FileTypes []string
Exclude interface{}
Expand All @@ -52,19 +52,33 @@ func Apply(fs afero.Fs, files []string, params Params) []string {
return files
}

func byGlob(vs []string, matcher string) []string {
if matcher == "" {
func byGlob(vs []string, matchers []string) []string {
if len(matchers) == 0 {
return vs
}

g := glob.MustCompile(strings.ToLower(matcher))

var hasNonEmpty bool
vsf := make([]string, 0)
for _, v := range vs {
if res := g.Match(strings.ToLower(v)); res {
vsf = append(vsf, v)
for _, matcher := range matchers {
if len(matcher) == 0 {
continue
}

hasNonEmpty = true

g := glob.MustCompile(strings.ToLower(matcher))

for _, v := range vs {
if res := g.Match(strings.ToLower(v)); res {
vsf = append(vsf, v)
}
}
}

if !hasNonEmpty {
return vs
}

return vsf
}

Expand Down
19 changes: 12 additions & 7 deletions internal/lefthook/runner/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ func slicesEqual(a, b []string) bool {
func TestByGlob(t *testing.T) {
for i, tt := range [...]struct {
source, result []string
glob string
glob []string
}{
{
source: []string{"folder/subfolder/0.rb", "1.txt", "2.RB", "3.rbs"},
glob: "",
glob: []string{},
result: []string{"folder/subfolder/0.rb", "1.txt", "2.RB", "3.rbs"},
},
{
source: []string{"folder/subfolder/0.rb", "1.txt", "2.RB", "3.rbs"},
glob: "*.rb",
glob: []string{"*.rb"},
result: []string{"folder/subfolder/0.rb", "2.RB"},
},
{
source: []string{"folder/subfolder/0.rb", "1.rbs"},
glob: "**/*.rb",
glob: []string{"**/*.rb"},
result: []string{"folder/subfolder/0.rb"},
},
{
source: []string{"folder/0.rb", "1.rBs", "2.rbv"},
glob: "*.rb?",
glob: []string{"*.rb?"},
result: []string{"1.rBs", "2.rbv"},
},
{
source: []string{"f.a", "f.b", "f.c", "f.cn"},
glob: "*.{a,b,cn}",
glob: []string{"*.{a,b,cn}"},
result: []string{"f.a", "f.b", "f.cn"},
},
} {
Expand All @@ -68,7 +68,7 @@ func TestByGlob(t *testing.T) {
func TestByExclude(t *testing.T) {
for i, tt := range [...]struct {
source, result []string
exclude string
exclude interface{}
}{
{
source: []string{"folder/subfolder/0.rb", "1.txt", "2.RB", "3.rb"},
Expand All @@ -95,6 +95,11 @@ func TestByExclude(t *testing.T) {
exclude: ".*\\.(a|b|cn)$",
result: []string{"f.c"},
},
{
source: []string{"f.a", "f.b", "f.c", "f.cn"},
exclude: []interface{}{"*.a", "*.b", "*.cn"},
result: []string{"f.c"},
},
} {
t.Run(fmt.Sprintf("%d:", i), func(t *testing.T) {
res := byExclude(tt.source, tt.exclude)
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type Params struct {
Root string
Runner string
Script string
Glob string
Files string
FileTypes []string
Tags []string
Glob []string
Templates map[string]string
Exclude interface{}
Only interface{}
Expand Down
6 changes: 3 additions & 3 deletions internal/lefthook/runner/run_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
type domain struct {
failed *atomic.Bool

glob string
glob []string
root string
exclude interface{}
onlyJobs []string
Expand Down Expand Up @@ -95,7 +95,7 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con

if job.Group != nil {
inheritedDomain := *domain
inheritedDomain.glob = first(job.Glob, domain.glob)
inheritedDomain.glob = append(inheritedDomain.glob, job.Glob...)
inheritedDomain.root = first(job.Root, domain.root)
switch list := job.Exclude.(type) {
case []interface{}:
Expand Down Expand Up @@ -126,7 +126,7 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, jo
name := job.PrintableName(id)

root := first(job.Root, domain.root)
glob := first(job.Glob, domain.glob)
glob := slices.Concat(domain.glob, job.Glob)
exclude := join(job.Exclude, domain.exclude)
executionJob, err := jobs.New(name, &jobs.Params{
Repo: r.Repo,
Expand Down
12 changes: 6 additions & 6 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,12 @@ func TestRunAll(t *testing.T) {
"ok": {
Run: "success",
StageFixed: true,
Glob: "*.md",
Glob: []string{"*.md"},
},
"fail": {
Run: "fail",
StageFixed: true,
Glob: "*.txt",
Glob: []string{"*.txt"},
},
},
},
Expand All @@ -648,12 +648,12 @@ func TestRunAll(t *testing.T) {
"ok": {
Run: "success",
StageFixed: true,
Glob: "*.md",
Glob: []string{"*.md"},
},
"fail": {
Run: "fail",
StageFixed: true,
Glob: "*.sh",
Glob: []string{"*.sh"},
},
},
},
Expand Down Expand Up @@ -703,12 +703,12 @@ func TestRunAll(t *testing.T) {
"ok": {
Run: "success",
StageFixed: true,
Glob: "*.md",
Glob: []string{"*.md"},
},
"fail": {
Run: "fail",
StageFixed: true,
Glob: "*.sh",
Glob: []string{"*.sh"},
},
},
},
Expand Down
23 changes: 20 additions & 3 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@
"type": "array"
},
"glob": {
"type": "string"
"oneOf": [
{
"type": "string"
}
],
"items": {
"type": "string"
}
},
"root": {
"type": "string"
Expand Down Expand Up @@ -214,7 +221,17 @@
"type": "string"
},
"glob": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "array"
}
],
"items": {
"type": "string"
}
},
"root": {
"type": "string"
Expand Down Expand Up @@ -392,7 +409,7 @@
"type": "object"
}
},
"$comment": "Last updated on 2025.01.16.",
"$comment": "Last updated on 2025.01.20.",
"properties": {
"min_version": {
"type": "string",
Expand Down

0 comments on commit 5fd0169

Please sign in to comment.