Skip to content

Commit

Permalink
feat: add priorities to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Mar 21, 2024
1 parent 599459e commit e85694c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 24 deletions.
4 changes: 4 additions & 0 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (c Command) DoSkip(gitState git.State) bool {
return skipChecker.Check(gitState, c.Skip, c.Only)
}

func (c Command) GetPriority() int {
return c.Priority
}

type commandRunReplace struct {
Run string `mapstructure:"run"`
}
Expand Down
13 changes: 9 additions & 4 deletions internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
type Script struct {
Runner string `json:"runner" mapstructure:"runner" toml:"runner" yaml:"runner"`

Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`
Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"`
Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`
Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"`
Priority int `json:"priority,omitempty" mapstructure:"priority" toml:"priority,omitempty" yaml:",omitempty"`

FailText string `json:"fail_text,omitempty" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`
Interactive bool `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"`
Expand All @@ -32,6 +33,10 @@ type scriptRunnerReplace struct {
Runner string `mapstructure:"runner"`
}

func (s Script) GetPriority() int {
return s.Priority
}

func mergeScripts(base, extra *viper.Viper) (map[string]*Script, error) {
if base == nil && extra == nil {
return nil, nil
Expand Down
52 changes: 34 additions & 18 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func NewRunner(opts Options) *Runner {
}
}

type executable interface {
*config.Command | *config.Script
GetPriority() int
}

// RunAll runs scripts and commands.
// LFS hook is executed at first if needed.
func (r *Runner) RunAll(ctx context.Context, sourceDirs []string) {
Expand Down Expand Up @@ -235,10 +240,20 @@ func (r *Runner) runScripts(ctx context.Context, dir string) {
return
}

scripts := make([]string, 0, len(files))
filesMap := make(map[string]os.FileInfo)
for _, file := range files {
filesMap[file.Name()] = file
scripts = append(scripts, file.Name())
}
sortByPriority(scripts, r.Hook.Scripts)

interactiveScripts := make([]os.FileInfo, 0)
var wg sync.WaitGroup

for _, file := range files {
for _, name := range scripts {
file := filesMap[name]

if ctx.Err() != nil {
return
}
Expand Down Expand Up @@ -331,7 +346,7 @@ func (r *Runner) runCommands(ctx context.Context) {
}
}

sortCommands(commands, r.Hook.Commands)
sortByPriority(commands, r.Hook.Commands)

interactiveCommands := make([]string, 0)
var wg sync.WaitGroup
Expand Down Expand Up @@ -531,44 +546,45 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
}
}

// sortCommands sorts the command names by preceding numbers if they occur and special priority if it is set.
// If the command names starts with letter the command name will be sorted alphabetically.
// sortByPriority sorts the names by preceding numbers if they occur and special priority if it is set.
// If the names starts with letter the command name will be sorted alphabetically.
// If there's a `priority` field defined for a command or script it will be used instead of alphanumeric sorting.
//
// []string{"1_command", "10command", "3 command", "command5"} // -> 1_command, 3 command, 10command, command5
func sortCommands(strs []string, commands map[string]*config.Command) {
sort.SliceStable(strs, func(i, j int) bool {
commandI, iOk := commands[strs[i]]
commandJ, jOk := commands[strs[j]]
func sortByPriority[E executable](array []string, exe map[string]E) {
sort.SliceStable(array, func(i, j int) bool {
exeI, iOk := exe[array[i]]
exeJ, jOk := exe[array[j]]

if iOk && commandI.Priority != 0 || jOk && commandJ.Priority != 0 {
if !iOk || commandI.Priority == 0 {
if iOk && exeI.GetPriority() != 0 || jOk && exeJ.GetPriority() != 0 {
if !iOk || exeI.GetPriority() == 0 {
return false
}
if !jOk || commandJ.Priority == 0 {
if !jOk || exeJ.GetPriority() == 0 {
return true
}

return commandI.Priority < commandJ.Priority
return exeI.GetPriority() < exeJ.GetPriority()
}

numEnds := -1
for idx, ch := range strs[i] {
for idx, ch := range array[i] {
if unicode.IsDigit(ch) {
numEnds = idx
} else {
break
}
}
if numEnds == -1 {
return strs[i] < strs[j]
return array[i] < array[j]
}
numI, err := strconv.Atoi(strs[i][:numEnds+1])
numI, err := strconv.Atoi(array[i][:numEnds+1])
if err != nil {
return strs[i] < strs[j]
return array[i] < array[j]
}

numEnds = -1
for idx, ch := range strs[j] {
for idx, ch := range array[j] {
if unicode.IsDigit(ch) {
numEnds = idx
} else {
Expand All @@ -578,7 +594,7 @@ func sortCommands(strs []string, commands map[string]*config.Command) {
if numEnds == -1 {
return true
}
numJ, err := strconv.Atoi(strs[j][:numEnds+1])
numJ, err := strconv.Atoi(array[j][:numEnds+1])
if err != nil {
return true
}
Expand Down
41 changes: 39 additions & 2 deletions internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,8 @@ func TestReplaceQuoted(t *testing.T) {
}
}

func TestSortCommands(t *testing.T) {
//nolint:dupl
func TestSortByPriorityCommands(t *testing.T) {
for i, tt := range [...]struct {
name string
names []string
Expand All @@ -931,7 +932,43 @@ func TestSortCommands(t *testing.T) {
},
} {
t.Run(fmt.Sprintf("%d: %s", i+1, tt.name), func(t *testing.T) {
sortCommands(tt.names, tt.commands)
sortByPriority(tt.names, tt.commands)
for i, name := range tt.result {
if tt.names[i] != name {
t.Errorf("Not matching on index %d: %s != %s", i, name, tt.names[i])
}
}
})
}
}

//nolint:dupl
func TestSortByPriorityScripts(t *testing.T) {
for i, tt := range [...]struct {
name string
names []string
scripts map[string]*config.Script
result []string
}{
{
name: "alphanumeric sort",
names: []string{"10_a.sh", "1_a.sh", "2_a.sh", "5_b.sh"},
scripts: map[string]*config.Script{},
result: []string{"1_a.sh", "2_a.sh", "5_b.sh", "10_a.sh"},
},
{
name: "partial priority",
names: []string{"10.rb", "file.sh", "script.go", "5_a.sh"},
scripts: map[string]*config.Script{
"5_a.sh": {Priority: 10},
"script.go": {Priority: 1},
"10.rb": {},
},
result: []string{"script.go", "5_a.sh", "10.rb", "file.sh"},
},
} {
t.Run(fmt.Sprintf("%d: %s", i+1, tt.name), func(t *testing.T) {
sortByPriority(tt.names, tt.scripts)
for i, name := range tt.result {
if tt.names[i] != name {
t.Errorf("Not matching on index %d: %s != %s", i, name, tt.names[i])
Expand Down

0 comments on commit e85694c

Please sign in to comment.