Skip to content

Commit

Permalink
🐛 Fix hook location detection
Browse files Browse the repository at this point in the history
Hook location detection had flawed logic and was only checking for a
global hook. If the global hook was not detected it was considered an
error and aborted with a failure.

Both global and repository hooks now do not consider failure if they
cannot find a hook. This allows for the true error message to be
reported if neither location can be detected.

The only case for a repository failure is if the hook commands were run
while not in a Git repository and the global hook location is unset.
  • Loading branch information
mikelorant committed Feb 17, 2023
1 parent 387ee52 commit 70af6fa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
12 changes: 2 additions & 10 deletions internal/hook/locate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ var (
var ErrLocation = errors.New("no hook location found")

func Locate(run Runner) (string, error) {
var err error

glob, err := runCmd(run, gitCommand, gitGlobalArgs)
if err != nil {
return "", fmt.Errorf("unable to check global hook: %w", err)
}
glob, _ := runCmd(run, gitCommand, gitGlobalArgs)

if glob != "" {
return glob, nil
}

repo, err := runCmd(run, gitCommand, gitRepositoryArgs)
if err != nil {
return "", fmt.Errorf("unable to check repository hook: %w", err)
}
repo, _ := runCmd(run, gitCommand, gitRepositoryArgs)

if repo != "" {
return repo, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/hook/locate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestLocate(t *testing.T) {
globErr: errMock,
},
want: want{
err: "unable to check global hook: unable to run command: error",
err: "no hook location found",
},
},
{
Expand All @@ -121,7 +121,7 @@ func TestLocate(t *testing.T) {
repoErr: errMock,
},
want: want{
err: "unable to check repository hook: unable to run command: error",
err: "no hook location found",
},
},
{
Expand Down

0 comments on commit 70af6fa

Please sign in to comment.