Skip to content

feat: load from .config #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/mdbook/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Lefthook supports the following file names for the main config:

If there are more than 1 file in the project, only one will be used, and you'll never know which one. So, please, use one format in a project.

Filenames without the leading dot will also be looked up from the [`.config` subdirectory](https://github.com/pi0/config-dir).

Lefthook also merges an extra config with the name `lefthook-local`. All supported formats can be applied to this `-local` config. If you name your main config with the leading dot, like `.lefthook.json`, the `-local` config also must be named with the leading dot: `.lefthook-local.json`.

## Options
Expand Down
9 changes: 5 additions & 4 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const (

var (
hookKeyRegexp = regexp.MustCompile(`^(?P<hookName>[^.]+)\.(scripts|commands|jobs)`)
localConfigNames = []string{"lefthook-local", ".lefthook-local"}
mainConfigNames = []string{"lefthook", ".lefthook"}
LocalConfigNames = []string{"lefthook-local", ".lefthook-local", filepath.Join(".config", "lefthook-local")}
MainConfigNames = []string{"lefthook", ".lefthook", filepath.Join(".config", "lefthook")}
extensions = []string{
".yml",
".yaml",
Expand Down Expand Up @@ -65,6 +65,7 @@ func loadOne(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) e
continue
}

log.Debug("loading config: ", config)
if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], mergeJobsOption); err != nil {
return err
}
Expand All @@ -80,7 +81,7 @@ func LoadKoanf(filesystem afero.Fs, repo *git.Repository) (*koanf.Koanf, *koanf.
main := koanf.New(".")

// Load main (e.g. lefthook.yml)
if err := loadOne(main, filesystem, repo.RootPath, mainConfigNames); err != nil {
if err := loadOne(main, filesystem, repo.RootPath, MainConfigNames); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -119,7 +120,7 @@ func LoadKoanf(filesystem afero.Fs, repo *git.Repository) (*koanf.Koanf, *koanf.

// Load optional local config (e.g. lefthook-local.yml)
var noLocal bool
if err := loadOne(secondary, filesystem, repo.RootPath, localConfigNames); err != nil {
if err := loadOne(secondary, filesystem, repo.RootPath, LocalConfigNames); err != nil {
var configNotFoundErr ConfigNotFoundError
if ok := errors.As(err, &configNotFoundErr); !ok {
return nil, nil, err
Expand Down
30 changes: 30 additions & 0 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,36 @@ pre-commit:
jobs:
- name: job 1
run: wrap {cmd}
`,
},
result: &Config{
SourceDir: ".lefthook",
SourceDirLocal: ".lefthook-local",
Hooks: map[string]*Hook{
"pre-commit": {
Jobs: []*Job{
{
Name: "job 1",
Run: "wrap echo from job 1",
},
},
},
},
},
},
"with .cofig/lefthook.yml": {
files: map[string]string{
filepath.Join(".config", "lefthook.yml"): `
pre-commit:
jobs:
- name: job 1
run: echo from job 1
`,
filepath.Join(".config", "lefthook-local.yml"): `
pre-commit:
jobs:
- name: job 1
run: wrap {cmd}
`,
},
result: &Config{
Expand Down
45 changes: 20 additions & 25 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/gobwas/glob"
"github.com/spf13/afero"

"github.com/evilmartians/lefthook/internal/config"
Expand All @@ -29,7 +28,6 @@ const (

var (
lefthookChecksumRegexp = regexp.MustCompile(`(\w+)\s+(\d+)`)
configGlob = glob.MustCompile("{.,}lefthook.{yml,yaml,json,toml}")
errNoConfig = errors.New("no lefthook config found")
)

Expand Down Expand Up @@ -86,18 +84,23 @@ func (l *Lefthook) readOrCreateConfig() (*config.Config, error) {
}

func (l *Lefthook) configExists(path string) bool {
paths, err := afero.ReadDir(l.Fs, path)
if err != nil {
return false
}
configPath, _ := l.findMainConfig(path)
return configPath != ""
}

for _, file := range paths {
if ok := configGlob.Match(file.Name()); ok {
return true
func (l *Lefthook) findMainConfig(path string) (string, error) {
for _, name := range config.MainConfigNames {
for _, extension := range []string{
".yml", ".yaml", ".toml", ".json",
} {
configPath := filepath.Join(path, name+extension)
if ok, _ := afero.Exists(l.Fs, configPath); ok {
return configPath, nil
}
}
}

return false
return "", errNoConfig
}

func (l *Lefthook) createConfig(path string) error {
Expand Down Expand Up @@ -319,26 +322,18 @@ func (l *Lefthook) hooksSynchronized(cfg *config.Config) bool {
return storedChecksum == configChecksum
}

func (l *Lefthook) configLastUpdateTimestamp() (timestamp int64, err error) {
paths, err := afero.ReadDir(l.Fs, l.repo.RootPath)
func (l *Lefthook) configLastUpdateTimestamp() (int64, error) {
configPath, err := l.findMainConfig(l.repo.RootPath)
if err != nil {
return
}
var config os.FileInfo
for _, file := range paths {
if ok := configGlob.Match(file.Name()); ok {
config = file
break
}
return 0, err
}

if config == nil {
err = errNoConfig
return
config, err := l.Fs.Stat(configPath)
if err != nil {
return 0, err
}

timestamp = config.ModTime().Unix()
return
return config.ModTime().Unix(), nil
}

func (l *Lefthook) addChecksumFile(checksum string) error {
Expand Down
8 changes: 2 additions & 6 deletions internal/lefthook/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/afero"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/log"
)

Expand Down Expand Up @@ -33,12 +34,7 @@ func (l *Lefthook) Uninstall(args *UninstallArgs) error {
}

if args.RemoveConfig {
for _, name := range []string{
".lefthook",
"lefthook",
".lefthook-local",
"lefthook-local",
} {
for _, name := range append(config.MainConfigNames, config.LocalConfigNames...) {
for _, extension := range []string{
".yml", ".yaml", ".toml", ".json",
} {
Expand Down
Loading