From d06d40e71a479ee1d7f0b1998fc7726739590376 Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Sat, 22 Aug 2015 22:55:05 +0200 Subject: [PATCH 1/5] you actully CAN range over nil slices in Go as Go will just handle it like an empty slice. --- general/gochanges.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/general/gochanges.go b/general/gochanges.go index d3e4f3d..02c365b 100644 --- a/general/gochanges.go +++ b/general/gochanges.go @@ -47,9 +47,6 @@ 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) From 8b627dab535ff3cc3311cedf305ddbe67d1cd906 Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Sat, 22 Aug 2015 22:59:27 +0200 Subject: [PATCH 2/5] empty filenames don't have a suffix so elide that condition. Even the small speed win is marginal, since we just called exec before, which is way more expensive. --- general/gochanges.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/general/gochanges.go b/general/gochanges.go index 02c365b..9918078 100644 --- a/general/gochanges.go +++ b/general/gochanges.go @@ -48,7 +48,7 @@ func GetChangedGoFiles() (result []string) { resultLines := strings.Split(gitDiff, "\n") for _, filename := range resultLines { - if filename != "" && strings.HasSuffix(filename, ".go") { + if strings.HasSuffix(filename, ".go") { result = append(result, absolutePath+"/"+filename) } } From 4bf421d8482f3497cdeb4fa23debcbbda364492d Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Sat, 22 Aug 2015 23:08:05 +0200 Subject: [PATCH 3/5] Use filepath.Join in order to get correct results on windows, too. --- general/gochanges.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/general/gochanges.go b/general/gochanges.go index 9918078..e0216c9 100644 --- a/general/gochanges.go +++ b/general/gochanges.go @@ -49,7 +49,7 @@ func GetChangedGoFiles() (result []string) { resultLines := strings.Split(gitDiff, "\n") for _, filename := range resultLines { if strings.HasSuffix(filename, ".go") { - result = append(result, absolutePath+"/"+filename) + result = append(result, filepath.Join(absolutePath, filename)) } } return result From d81ea3f35ca60d17081648aedb96954a2b489d8b Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Sat, 22 Aug 2015 23:32:44 +0200 Subject: [PATCH 4/5] 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 + }) } From f1a92ba9fc6ad4e358295c7fe6ef5be472b28d7d Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Sat, 22 Aug 2015 23:52:09 +0200 Subject: [PATCH 5/5] Checking via len is more robust because sometimes such APIs return nil slices, sometimes empty slices. We want nothing to happen in both cases. --- githook-gobuild/hook.go | 2 +- githook-gofmt/hook.go | 2 +- githook-gotest/hook.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 99c683e..efca3ed 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 }