Skip to content

Commit

Permalink
[skip travis] add files option as git command
Browse files Browse the repository at this point in the history
  • Loading branch information
A.A.Abroskin committed Feb 21, 2019
1 parent dea9dc1 commit 8e9a997
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,11 @@ pre-commit:
glob: "*.{rb}"
exclude: "application.rb|routes.rb" # simple regexp for more flexibility
runner: bundle exec rubocop {all_files}

pre-push:
# Describe what files will be placed in runner command
# Default: staged
# Available: all, staged, none
files: all

audit:
runner: bundle audit
govet:
tags: backend style
files: git ls-files -m # we can explicity define scope of files
glob: "*.{go}"
runner: go vet {files}
```
If your team have backend and frontend developers, you can skip unnsecesary hooks this way:
`hookah-local.yml`
Expand All @@ -134,9 +130,9 @@ pre-commit:
scripts:
"any.go":
runner: docker exec -it <container_id_or_name> {cmd} # Wrap command from hookah.yml in docker
runner: docker exec -it --rm <container_id_or_name> {cmd} # Wrap command from hookah.yml in docker
commands:
audit:
govet:
skip: true # You can also skip command with this option
```

Expand Down
31 changes: 13 additions & 18 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
skipConfigKey string = "skip"
skipEmptyConfigKey string = "skip_empty"
filesConfigKey string = "files"
subFiles string = "{files}"
subAllFiles string = "{all_files}"
subStagedFiles string = "{staged_files}"
runnerWrapPattern string = "{cmd}"
Expand Down Expand Up @@ -111,29 +112,23 @@ func RunCmdExecutor(args []string, fs afero.Fs) error {
func executeCommand(commandName string) {
setExecutableName(commandName)

var files []string
switch getCommandFiles() {
case "staged":
files, _ = context.StagedFiles()
case "all":
files, _ = context.AllFiles()
case "none":
files = []string{}
}
if strings.Contains(getRunner(commandsConfigKey), subStagedFiles) {
files, _ := context.AllFiles()
runner := getRunner(commandsConfigKey)

if strings.Contains(runner, subStagedFiles) {
files, _ = context.StagedFiles()
} else if strings.Contains(runner, subFiles) {
files, _ = context.ExecGitCommand(getCommandFiles())
}
if strings.Contains(getRunner(commandsConfigKey), subAllFiles) {
files, _ = context.AllFiles()
}

files = FilterGlob(files, getCommandGlobRegexp())
files = FilterInclude(files, getCommandIncludeRegexp()) // NOTE: confusing option, suppose delete it
files = FilterExclude(files, getCommandExcludeRegexp())

runner := strings.Replace(getRunner(commandsConfigKey), subAllFiles, strings.Join(files, " "), -1)
if strings.Contains(getRunner(commandsConfigKey), subStagedFiles) {
runner = strings.Replace(getRunner(commandsConfigKey), subStagedFiles, strings.Join(files, " "), -1)
}
runner = strings.Replace(runner, subStagedFiles, strings.Join(files, " "), -1)
runner = strings.Replace(runner, subAllFiles, strings.Join(files, " "), -1)
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

runnerArg := strings.Split(runner, " ")

command := exec.Command(runnerArg[0], runnerArg[1:]...)
Expand Down Expand Up @@ -333,7 +328,7 @@ func getCommandFiles() string {
return viper.GetString(key)
}

return "staged"
return ""
}

func getTags(source string) []string {
Expand Down
25 changes: 13 additions & 12 deletions context/context.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package context

import (
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
)

func StagedFiles() ([]string, error) {
cmd := exec.Command("git", "diff", "--name-only", "--cached")

outputBytes, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}

lines := strings.Split(string(outputBytes), "\n")

return ExtractFiles(lines)
return ExecGitCommand("git diff --name-only --cached")
}

func AllFiles() ([]string, error) {
cmd := exec.Command("git", "ls-files", "--cached")
return ExecGitCommand("git ls-files --cached")
}

func ExecGitCommand(command string) ([]string, error) {
if !strings.Contains(command, "git") {
return []string{}, errors.New("Wrong git command")
}

commandArg := strings.Split(command, " ")
cmd := exec.Command(commandArg[0], commandArg[1:]...)

outputBytes, err := cmd.CombinedOutput()
if err != nil {
return nil, err
return []string{}, err
}

lines := strings.Split(string(outputBytes), "\n")
Expand Down

0 comments on commit 8e9a997

Please sign in to comment.