Skip to content

Commit

Permalink
fix: don't fail on missing config file (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Mar 17, 2023
1 parent 961f396 commit 7bbde4e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ updates:
- package-ecosystem: "gomod"
directory: "/"
target-branch: "dependencies"
open-pull-requests-limit: 10
schedule:
interval: "weekly"
day: "monday"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

- fix: don't fail on missing config file ([#450](https://github.com/evilmartians/lefthook/pull/450)) by @mrexox

## 1.3.6 (2024-03-16)

- fix: stage fixed when root specified ([#449](https://github.com/evilmartians/lefthook/pull/449)) by @mrexox
Expand Down
15 changes: 15 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,25 @@ const (

var hookKeyRegexp = regexp.MustCompile(`^(?P<hookName>[^.]+)\.(scripts|commands)`)

// NotFoundError wraps viper.ConfigFileNotFoundError for lefthook.
type NotFoundError struct {
message string
}

// Error returns message of viper.ConfigFileNotFoundError.
func (err NotFoundError) Error() string {
return err.message
}

// Loads configs from the given directory with extensions.
func Load(fs afero.Fs, repo *git.Repository) (*Config, error) {
global, err := read(fs, repo.RootPath, "lefthook")
if err != nil {
var notFoundErr viper.ConfigFileNotFoundError
if ok := errors.As(err, &notFoundErr); ok {
return nil, NotFoundError{err.Error()}
}

return nil, err
}

Expand Down
6 changes: 6 additions & 0 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
// Load config
cfg, err := config.Load(l.Fs, l.repo)
if err != nil {
var notFoundErr config.NotFoundError
if ok := errors.As(err, &notFoundErr); ok {
log.Warn(err.Error())
return nil
}

return err
}
if err = cfg.Validate(); err != nil {
Expand Down

0 comments on commit 7bbde4e

Please sign in to comment.