Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relative roots for monorepos (#72) #98

Merged
merged 5 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ pre-commit:
run: bundle exec rubocop --force-exclusion {all_files}
```
* ### **Execute in sub-directory**
If you want to execute the commands in a relative path
```yml
pre-commit:
commands:
backend-linter:
root: "api/" # Careful to have only trailing slash
glob: "*.rb" # glob filter
run: bundle exec rubocop {all_files}
```
* ### **Run scripts**
If oneline commands are not enough, you can execute files. [Example](./docs/full_guide.md#bash-script-example).
Expand Down
27 changes: 27 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
)

const (
rootConfigKey string = "root"
runnerConfigKey string = "runner"
runConfigKey string = "run" // alias for runner
runnerArgsConfigKey string = "runner_args"
Expand Down Expand Up @@ -195,6 +196,9 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
files = FilterInclude(files, getCommandIncludeRegexp(hooksGroup, commandName)) // NOTE: confusing option, suppose delete it
files = FilterExclude(files, getCommandExcludeRegexp(hooksGroup, commandName))

cmdRoot := getRoot(hooksGroup, commandName)
files = FilterRelative(files, cmdRoot)

VerbosePrint("Files after filters: \n", files)

files_esc := []string{}
Expand All @@ -212,6 +216,10 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

command := exec.Command("sh", "-c", runner)
if cmdRoot != "" {
fullPath, _ := filepath.Abs(cmdRoot)
command.Dir = fullPath
}
command.Stdin = os.Stdin

if isSkipCommmand(hooksGroup, commandName) {
Expand Down Expand Up @@ -425,6 +433,11 @@ func getCommands(hooksGroup string) []string {
return keys
}

func getRoot(hooksGroup string, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, rootConfigKey}, ".")
return viper.GetString(key)
}

func getCommandIncludeRegexp(hooksGroup, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, includeConfigKey}, ".")
return viper.GetString(key)
Expand Down Expand Up @@ -515,6 +528,20 @@ func FilterGlob(vs []string, matcher string) []string {
return vsf
}

func FilterRelative(vs []string, matcher string) []string {
if matcher == "" {
return vs
}

vsf := make([]string, 0)
for _, v := range vs {
if strings.HasPrefix(v, matcher) {
vsf = append(vsf, strings.Replace(v, matcher, "./", 1))
}
}
return vsf
}

func FilterInclude(vs []string, matcher string) []string {
if matcher == "" {
return vs
Expand Down
27 changes: 27 additions & 0 deletions cmd/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
)

const (
rootConfigKey string = "root"
runnerConfigKey string = "runner"
runConfigKey string = "run" // alias for runner
runnerArgsConfigKey string = "runner_args"
Expand Down Expand Up @@ -190,6 +191,9 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
files = FilterInclude(files, getCommandIncludeRegexp(hooksGroup, commandName)) // NOTE: confusing option, suppose delete it
files = FilterExclude(files, getCommandExcludeRegexp(hooksGroup, commandName))

cmdRoot := getRoot(hooksGroup, commandName)
files = FilterRelative(files, cmdRoot)

VerbosePrint("Files after filters: \n", files)

files_esc := []string{}
Expand Down Expand Up @@ -227,6 +231,10 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {

runnerArg := strings.Split(runner, " ")
command := exec.Command(runnerArg[0], runnerArg[1:]...)
if cmdRoot != "" {
fullPath, _ := filepath.Abs(cmdRoot)
command.Dir = fullPath
}
command.Stdin = os.Stdin
command.Stdout = os.Stdout // win specific
command.Stderr = os.Stderr // win specific
Expand Down Expand Up @@ -414,6 +422,11 @@ func getCommands(hooksGroup string) []string {
return keys
}

func getRoot(hooksGroup string, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, rootConfigKey}, ".")
return viper.GetString(key)
}

func getCommandIncludeRegexp(hooksGroup, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, includeConfigKey}, ".")
return viper.GetString(key)
Expand Down Expand Up @@ -504,6 +517,20 @@ func FilterGlob(vs []string, matcher string) []string {
return vsf
}

func FilterRelative(vs []string, matcher string) []string {
if matcher == "" {
return vs
}

vsf := make([]string, 0)
for _, v := range vs {
if strings.HasPrefix(v, matcher) {
vsf = append(vsf, strings.Replace(v, matcher, "./", 1))
}
}
return vsf
}

func FilterInclude(vs []string, matcher string) []string {
if matcher == "" {
return vs
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
version string = "0.6.7"
version string = "0.7.0"
)

// versionCmd represents the version command
Expand Down