Skip to content

Commit 266638f

Browse files
committed
Path around proper Executable struct rather than just a string path
1 parent 3157593 commit 266638f

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

context/context.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ func (c *Context) FilesToBeCommitted() ([]string, error) {
5050
return files, nil
5151
}
5252

53-
func (c *Context) ExecutablesForHook(hook string) ([]string, error) {
53+
type Executable struct {
54+
Name string
55+
RelativePath string
56+
AbsolutePath string
57+
}
58+
59+
func (c *Context) ExecutablesForHook(hook string) ([]*Executable, error) {
5460
shortPath := path.Join(".quickhook", hook)
5561
absolutePath := path.Join(c.path, shortPath)
5662

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

67-
var executables []string
73+
var executables []*Executable
6874
for _, fileInfo := range allFiles {
6975
if fileInfo.IsDir() { continue }
7076

7177
name := fileInfo.Name()
7278

7379
if (fileInfo.Mode() & 0111) > 0 {
74-
executables = append(executables, path.Join(shortPath, name))
80+
relativePath := path.Join(shortPath, name)
81+
82+
executables = append(executables, &Executable{
83+
Name: name,
84+
RelativePath: relativePath,
85+
AbsolutePath: path.Join(c.path, relativePath),
86+
})
7587
} else {
7688
fmt.Printf("Warning: Non-executable file found in %v: %v\n", shortPath, name)
7789
}

hooks/pre_commit.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func PreCommit(c *context.Context) error {
3636
if result.commandError != nil {
3737
hasErrors = true
3838

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

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

5555
type Result struct {
56-
executablePath string
56+
executable *context.Executable
5757
commandError error
5858
combinedOutput string
5959
}
6060

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

67-
in := make(chan string, bufferSize)
67+
in := make(chan *context.Executable, bufferSize)
6868
out := make(chan *Result, bufferSize)
6969

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

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

102102
combinedOutputBytes, exitError := cmd.CombinedOutput()
103103

104104
return &Result{
105-
executablePath: path,
105+
executable: executable,
106106
commandError: exitError,
107107
combinedOutput: string(combinedOutputBytes),
108108
}

0 commit comments

Comments
 (0)