Skip to content

Commit

Permalink
Path around proper Executable struct rather than just a string path
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk committed Aug 28, 2016
1 parent 3157593 commit 266638f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
18 changes: 15 additions & 3 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ func (c *Context) FilesToBeCommitted() ([]string, error) {
return files, nil
}

func (c *Context) ExecutablesForHook(hook string) ([]string, error) {
type Executable struct {
Name string
RelativePath string
AbsolutePath string
}

func (c *Context) ExecutablesForHook(hook string) ([]*Executable, error) {
shortPath := path.Join(".quickhook", hook)
absolutePath := path.Join(c.path, shortPath)

Expand All @@ -64,14 +70,20 @@ func (c *Context) ExecutablesForHook(hook string) ([]string, error) {
}
}

var executables []string
var executables []*Executable
for _, fileInfo := range allFiles {
if fileInfo.IsDir() { continue }

name := fileInfo.Name()

if (fileInfo.Mode() & 0111) > 0 {
executables = append(executables, path.Join(shortPath, name))
relativePath := path.Join(shortPath, name)

executables = append(executables, &Executable{
Name: name,
RelativePath: relativePath,
AbsolutePath: path.Join(c.path, relativePath),
})
} else {
fmt.Printf("Warning: Non-executable file found in %v: %v\n", shortPath, name)
}
Expand Down
14 changes: 7 additions & 7 deletions hooks/pre_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func PreCommit(c *context.Context) error {
if result.commandError != nil {
hasErrors = true

fmt.Printf("%v:\n", result.executablePath)
fmt.Printf("%v:\n", result.executable.Name)

output := strings.TrimSpace(result.combinedOutput)
for _, line := range strings.Split(output, "\n") {
Expand All @@ -53,18 +53,18 @@ func PreCommit(c *context.Context) error {
}

type Result struct {
executablePath string
executable *context.Executable
commandError error
combinedOutput string
}

// Uses a pool sized to the number of CPUs to run all the executables. It's
// sized to the CPU count so that we fully utilized the hardwire but don't
// context switch in the OS too much.
func runExecutablesInParallel(executables []string, files[]string) ([]*Result) {
func runExecutablesInParallel(executables []*context.Executable, files[]string) ([]*Result) {
bufferSize := len(executables)

in := make(chan string, bufferSize)
in := make(chan *context.Executable, bufferSize)
out := make(chan *Result, bufferSize)

pool, err := tunny.CreatePoolGeneric(runtime.NumCPU()).Open()
Expand Down Expand Up @@ -95,14 +95,14 @@ func runExecutablesInParallel(executables []string, files[]string) ([]*Result) {
return results
}

func runExecutable(path string, files []string) *Result {
cmd := exec.Command(path)
func runExecutable(executable *context.Executable, files []string) *Result {
cmd := exec.Command(executable.AbsolutePath)
cmd.Stdin = strings.NewReader(strings.Join(files, "\n"))

combinedOutputBytes, exitError := cmd.CombinedOutput()

return &Result{
executablePath: path,
executable: executable,
commandError: exitError,
combinedOutput: string(combinedOutputBytes),
}
Expand Down

0 comments on commit 266638f

Please sign in to comment.