diff --git a/cmd/install.go b/cmd/install.go index d1c59ba..18c6caf 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -24,7 +24,7 @@ var installCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { folder, err := git.TopLevel() if err != nil { - panic(err) + return fmt.Errorf("could not get top level: %v", err) } originalDir := filepath.Join(folder, ".git", "hooks") destinationDir := filepath.Join(folder, ".git", "hooks.back") diff --git a/cmd/uninstall.go b/cmd/uninstall.go index 2c26c5a..d8e4afb 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -20,7 +20,7 @@ var uninstallCmd = &cobra.Command{ Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if folder, err := git.TopLevel(); err != nil { - panic(err) + return fmt.Errorf("could not get top level: %v", err) } else { originalDir := filepath.Join(folder, ".git", "hooks") destinationDir := filepath.Join(folder, ".git", "hooks.back") diff --git a/git/git.go b/git/git.go index 66a07e5..c1d3696 100644 --- a/git/git.go +++ b/git/git.go @@ -2,54 +2,30 @@ package git import ( + "bytes" + "fmt" "os/exec" "strings" ) -// IsGitRepository return true if the working repository -// has been initialized as a working repository -func IsGitRepository() bool { - cmd := exec.Command("git", "rev-parse", "--show-toplevel") - if err := cmd.Run(); err != nil { - return false - } - return true -} - // TopLevel returns a path to the root of your git directory // It has an error if you are not currently in a Git repository func TopLevel() (string, error) { cmd := exec.Command("git", "rev-parse", "--show-toplevel") + errOut := &bytes.Buffer{} + cmd.Stderr = errOut out, err := cmd.Output() if err != nil { - return "", err + return "", fmt.Errorf("%v", errOut.String()) } return strings.TrimSpace(string(out[:])), nil } -// GlobalHookPath hooks path -func GlobalHookPath() ([]string, error) { - cmd := exec.Command("git", "config", "--get-all", "hooks.global") - out, err := cmd.Output() - if err != nil { - return []string{}, err - } - return strings.Split(strings.TrimSpace(string(out[:])), "\n"), nil -} - -// UserHookPath hooks path -func UserHookPath() ([]string, error) { - cmd := exec.Command("git", "config", "--get-all", "hooks.user") - out, err := cmd.Output() - if err != nil { - return []string{}, err - } - return strings.Split(strings.TrimSpace(string(out[:])), "\n"), nil -} - -// LocalHookPath hooks path -func LocalHookPath() ([]string, error) { - cmd := exec.Command("git", "config", "--get-all", "hooks.local") +// HookPaths returns all the paths set for a given hook. +// +// It looks up the config of hooks.% to identify locations +func HookPaths(t string) ([]string, error) { + cmd := exec.Command("git", "config", "--get-all", fmt.Sprintf("hooks.%s", t)) out, err := cmd.Output() if err != nil { return []string{}, err diff --git a/hook/hook.go b/hook/hook.go index 8838731..5a7bc1c 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -64,13 +64,10 @@ func isExecutable(f os.FileInfo) bool { func allPath() []string { paths := []string{} - pathFuncs := []func() ([]string, error){ - git.GlobalHookPath, - git.UserHookPath, - git.LocalHookPath, - } - for _, pathFunc := range pathFuncs { - if out, err := pathFunc(); err == nil { + + hookTypes := []string{"global", "local", "user"} + for _, hookType := range hookTypes { + if out, err := git.HookPaths(hookType); err == nil { paths = append(paths, out...) } }