From d81ea3f35ca60d17081648aedb96954a2b489d8b Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Sat, 22 Aug 2015 23:32:44 +0200 Subject: [PATCH] filepath love * use filepath to get correct result of file path operation on every OS. * use filepath.Walk to get a better idea what is being done here * simpler way to get the basename of a program; also works on Windows --- general/gochanges.go | 10 ---------- main.go | 26 +++++++++++++++++--------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/general/gochanges.go b/general/gochanges.go index e0216c9..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" @@ -55,15 +54,6 @@ func GetChangedGoFiles() (result []string) { 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/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 + }) }