diff --git a/general/gochanges.go b/general/gochanges.go index d3e4f3d..0a5fb0d 100644 --- a/general/gochanges.go +++ b/general/gochanges.go @@ -2,7 +2,6 @@ package general import ( "bytes" - "io/ioutil" "log" "os/exec" "path/filepath" @@ -47,26 +46,14 @@ func GetChangedGoFiles() (result []string) { absolutePath := GetGitRoot() resultLines := strings.Split(gitDiff, "\n") - if resultLines == nil { - return - } for _, filename := range resultLines { - if filename != "" && strings.HasSuffix(filename, ".go") { - result = append(result, absolutePath+"/"+filename) + if strings.HasSuffix(filename, ".go") { + result = append(result, filepath.Join(absolutePath, filename)) } } return result } -// GetFilesList returns a list of all files in the current directory -func GetFilesList() (result []string) { - files, _ := ioutil.ReadDir("./") - for _, f := range files { - result = append(result, f.Name()) - } - return result -} - // GetGitRoot returns the path with the .git directory func GetGitRoot() string { gitDir, _ := RunCommand("git", "rev-parse", "--git-dir") diff --git a/githook-gobuild/hook.go b/githook-gobuild/hook.go index 2545e7d..cdc52f3 100644 --- a/githook-gobuild/hook.go +++ b/githook-gobuild/hook.go @@ -9,7 +9,7 @@ import ( func main() { files := general.GetChangedGoFiles() - if files == nil { + if len(files) == 0 { os.Exit(0) return } diff --git a/githook-gofmt/hook.go b/githook-gofmt/hook.go index b29cfe6..d9e3fd2 100644 --- a/githook-gofmt/hook.go +++ b/githook-gofmt/hook.go @@ -10,7 +10,7 @@ import ( func main() { files := general.GetChangedGoFiles() - if files == nil { + if len(files) == 0 { os.Exit(0) return } diff --git a/githook-gotest/hook.go b/githook-gotest/hook.go index b070815..ee41eb7 100644 --- a/githook-gotest/hook.go +++ b/githook-gotest/hook.go @@ -9,7 +9,7 @@ import ( func main() { files := general.GetChangedGoFiles() - if files == nil { + if len(files) == 0 { os.Exit(0) return } diff --git a/main.go b/main.go index e7c64d2..2ee4401 100644 --- a/main.go +++ b/main.go @@ -11,20 +11,28 @@ import ( func main() { gitroot, _ := filepath.Abs(filepath.Dir(general.GetGitRoot())) - os.Chdir(gitroot + "/.git/hooks") - currentFileSplit := strings.Split(os.Args[0], "/") - currentFile := currentFileSplit[len(currentFileSplit)-1] - files := general.GetFilesList() + hookBase := filepath.Base(os.Args[0]) + hookPrefix := fmt.Sprintf("%s_", hookBase) os.Chdir(gitroot) - for _, file := range files { - if strings.HasPrefix(file, fmt.Sprintf("%s_", currentFile)) { - result, errCode := general.RunCommand(gitroot + "/.git/hooks/" + file) + filepath.Walk(gitroot, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return filepath.SkipDir + } + file := info.Name() + + if strings.HasPrefix(file, hookPrefix) { + result, errCode := general.RunCommand(filepath.Join(gitroot, "/.git/hooks", file)) if errCode != 0 { fmt.Fprintf(os.Stderr, "Error: %s", result) os.Exit(errCode) - return + return nil } } - } + return nil + }) }