-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chore/more-linting
- Loading branch information
Showing
28 changed files
with
788 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package config | ||
|
||
import ( | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
type Exec interface { | ||
Cmd(commandLine string) bool | ||
} | ||
|
||
type osExec struct{} | ||
|
||
// NewOsExec returns an object that executes given commands in the OS. | ||
func NewOsExec() Exec { | ||
return &osExec{} | ||
} | ||
|
||
// Cmd runs plain string command. It checks only exit code and returns bool value. | ||
func (o *osExec) Cmd(commandLine string) bool { | ||
if commandLine == "" { | ||
return false | ||
} | ||
|
||
var cmd *exec.Cmd | ||
if runtime.GOOS == "windows" { | ||
cmd = exec.Command("powershell", "-Command", commandLine) | ||
} else { | ||
cmd = exec.Command("sh", "-c", commandLine) | ||
} | ||
|
||
err := cmd.Run() | ||
|
||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/gobwas/glob" | ||
|
||
"github.com/evilmartians/lefthook/internal/git" | ||
"github.com/evilmartians/lefthook/internal/log" | ||
) | ||
|
||
type SkipChecker struct { | ||
Executor Exec | ||
} | ||
|
||
func NewSkipChecker(executor Exec) *SkipChecker { | ||
if executor == nil { | ||
executor = NewOsExec() | ||
} | ||
|
||
return &SkipChecker{Executor: executor} | ||
} | ||
|
||
func (sc *SkipChecker) Check(gitState git.State, skip interface{}, only interface{}) bool { | ||
if skip != nil { | ||
if sc.matches(gitState, skip) { | ||
return true | ||
} | ||
} | ||
|
||
if only != nil { | ||
return !sc.matches(gitState, only) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (sc *SkipChecker) matches(gitState git.State, value interface{}) bool { | ||
switch typedValue := value.(type) { | ||
case bool: | ||
return typedValue | ||
case string: | ||
return typedValue == gitState.Step | ||
case []interface{}: | ||
return sc.matchesSlices(gitState, typedValue) | ||
} | ||
return false | ||
} | ||
|
||
func (sc *SkipChecker) matchesSlices(gitState git.State, slice []interface{}) bool { | ||
for _, state := range slice { | ||
switch typedState := state.(type) { | ||
case string: | ||
if typedState == gitState.Step { | ||
return true | ||
} | ||
case map[string]interface{}: | ||
if sc.matchesRef(gitState, typedState) { | ||
return true | ||
} | ||
|
||
if sc.matchesCommands(typedState) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (sc *SkipChecker) matchesRef(gitState git.State, typedState map[string]interface{}) bool { | ||
ref, ok := typedState["ref"].(string) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if ref == gitState.Branch { | ||
return true | ||
} | ||
|
||
g := glob.MustCompile(ref) | ||
|
||
return g.Match(gitState.Branch) | ||
} | ||
|
||
func (sc *SkipChecker) matchesCommands(typedState map[string]interface{}) bool { | ||
commandLine, ok := typedState["run"].(string) | ||
if !ok { | ||
return false | ||
} | ||
|
||
result := sc.Executor.Cmd(commandLine) | ||
|
||
log.Debugf("[lefthook] skip/only cmd: %s, result: %t", commandLine, result) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.