Skip to content

Commit

Permalink
Refactor, do not panic
Browse files Browse the repository at this point in the history
  • Loading branch information
frankywahl committed Mar 23, 2022
1 parent 5b975ec commit 09c4942
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
44 changes: 10 additions & 34 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
Expand Down

0 comments on commit 09c4942

Please sign in to comment.