Skip to content
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

Support .lefthook.yml and .lefthook-local.yml #520

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newUninstallCmd(opts *lefthook.Options) *cobra.Command {

uninstallCmd.Flags().BoolVarP(
&args.RemoveConfig, "remove-configs", "c", false,
"remove lefthook.yml and lefthook-local.yml",
"remove lefthook.yml, lefthook-local.yml, .lefthook.yml, and .lefthook-local.yml",
)

return &uninstallCmd
Expand Down
49 changes: 40 additions & 9 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const (
DefaultDotConfigName = ".lefthook.yml"
DefaultConfigName = "lefthook.yml"
DefaultSourceDir = ".lefthook"
DefaultSourceDirLocal = ".lefthook-local"
Expand All @@ -34,13 +35,8 @@ func (err NotFoundError) Error() string {

// 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")
global, err := readOne(fs, repo.RootPath, []string{".lefthook", "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 Expand Up @@ -81,9 +77,28 @@ func read(fs afero.Fs, path string, name string) (*viper.Viper, error) {
return v, nil
}

// mergeAll merges remotes and extends from .lefthook and .lefthook-local.
func readOne(fs afero.Fs, path string, names []string) (*viper.Viper, error) {
for _, name := range names {
v, err := read(fs, path, name)
if err != nil {
var notFoundErr viper.ConfigFileNotFoundError
if ok := errors.As(err, &notFoundErr); ok {
continue
} else {
return nil, err
}
}

return v, nil
}

return nil, NotFoundError{fmt.Sprintf("No config files with names %q could not be found in \"%s\"", names, path)}
}

// mergeAll merges (.lefthook or lefthook) and (extended config) and (remote)
// and (.lefthook-local or .lefthook-local) configs.
func mergeAll(fs afero.Fs, repo *git.Repository) (*viper.Viper, error) {
extends, err := read(fs, repo.RootPath, "lefthook")
extends, err := readOne(fs, repo.RootPath, []string{".lefthook", "lefthook"})
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand All @@ -96,7 +111,7 @@ func mergeAll(fs afero.Fs, repo *git.Repository) (*viper.Viper, error) {
return nil, err
}

if err := merge("lefthook-local", "", extends); err == nil {
if err := mergeOne([]string{".lefthook-local", "lefthook-local"}, "", extends); err == nil {
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
if err = extend(extends, repo.RootPath); err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,6 +188,22 @@ func merge(name, path string, v *viper.Viper) error {
return nil
}

func mergeOne(names []string, path string, v *viper.Viper) error {
for _, name := range names {
err := merge(name, path, v)
if err == nil {
break
} else {
var notFoundErr viper.ConfigFileNotFoundError
if ok := errors.As(err, &notFoundErr); !ok {
return err
}
}
}

return nil
}

func unmarshalConfigs(base, extra *viper.Viper, c *Config) error {
c.Hooks = make(map[string]*Hook)

Expand Down
198 changes: 195 additions & 3 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,93 @@ func TestLoad(t *testing.T) {
name string
global, local, remote string
remoteConfigPath string
extends map[string]string
otherFiles map[string]string
result *Config
}{
{
name: "with global, dot",
otherFiles: map[string]string{
".lefthook.yml": `
pre-commit:
commands:
tests:
run: yarn test
`,
},
result: &Config{
SourceDir: DefaultSourceDir,
SourceDirLocal: DefaultSourceDirLocal,
Colors: nil,
Hooks: map[string]*Hook{
"pre-commit": {
Parallel: false,
Commands: map[string]*Command{
"tests": {
Run: "yarn test",
},
},
},
},
},
},
{
name: "with global, nodot",
otherFiles: map[string]string{
"lefthook.yml": `
pre-commit:
commands:
tests:
run: yarn test
`,
},
result: &Config{
SourceDir: DefaultSourceDir,
SourceDirLocal: DefaultSourceDirLocal,
Colors: nil,
Hooks: map[string]*Hook{
"pre-commit": {
Parallel: false,
Commands: map[string]*Command{
"tests": {
Run: "yarn test",
},
},
},
},
},
},
{
name: "with global, dot has priority",
otherFiles: map[string]string{
".lefthook.yml": `
pre-commit:
commands:
tests:
run: yarn test1
`,
"lefthook.yml": `
pre-commit:
commands:
tests:
run: yarn test2
`,
},
result: &Config{
SourceDir: DefaultSourceDir,
SourceDirLocal: DefaultSourceDirLocal,
Colors: nil,
Hooks: map[string]*Hook{
"pre-commit": {
Parallel: false,
Commands: map[string]*Command{
"tests": {
Run: "yarn test1",
},
},
},
},
},
},
{
name: "simple",
global: `
Expand Down Expand Up @@ -144,6 +228,114 @@ pre-push:
},
},
},
{
name: "with overrides, dot",
otherFiles: map[string]string{
".lefthook.yml": `
pre-push:
scripts:
"global-extend":
runner: bash
`,
".lefthook-local.yml": `
pre-push:
scripts:
"local-extend":
runner: bash
`,
},
result: &Config{
SourceDir: DefaultSourceDir,
SourceDirLocal: DefaultSourceDirLocal,
Colors: nil,
Hooks: map[string]*Hook{
"pre-push": {
Scripts: map[string]*Script{
"global-extend": {
Runner: "bash",
},
"local-extend": {
Runner: "bash",
},
},
},
},
},
},
{
name: "with overrides, dot, nodot",
otherFiles: map[string]string{
"lefthook.yml": `
pre-push:
scripts:
"global-extend":
runner: bash
`,
".lefthook-local.yml": `
pre-push:
scripts:
"local-extend":
runner: bash
`,
},
result: &Config{
SourceDir: DefaultSourceDir,
SourceDirLocal: DefaultSourceDirLocal,
Colors: nil,
Hooks: map[string]*Hook{
"pre-push": {
Scripts: map[string]*Script{
"global-extend": {
Runner: "bash",
},
"local-extend": {
Runner: "bash",
},
},
},
},
},
},
{
name: "with overrides, dot has priority",
otherFiles: map[string]string{
"lefthook.yml": `
pre-push:
scripts:
"global-extend":
runner: bash
`,
".lefthook-local.yml": `
pre-push:
scripts:
"local-extend":
runner: bash1
`,
"lefthook-local.yml": `
pre-push:
scripts:
"local-extend":
runner: bash2
`,
},
result: &Config{
SourceDir: DefaultSourceDir,
SourceDirLocal: DefaultSourceDirLocal,
Colors: nil,
Hooks: map[string]*Hook{
"pre-push": {
Scripts: map[string]*Script{
"global-extend": {
Runner: "bash",
},
"local-extend": {
Runner: "bash1",
},
},
},
},
},
},
{
name: "with extra hooks",
global: `
Expand Down Expand Up @@ -356,7 +548,7 @@ pre-push:
run: echo remote
`,
remoteConfigPath: filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook", "examples", "config.yml"),
extends: map[string]string{
otherFiles: map[string]string{
"global-extend.yml": `
pre-push:
scripts:
Expand Down Expand Up @@ -440,7 +632,7 @@ pre-push:
}
}

for name, content := range tt.extends {
for name, content := range tt.otherFiles {
path := filepath.Join(
root,
filepath.Join(strings.Split(name, "/")...),
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (

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

Expand Down Expand Up @@ -99,7 +99,7 @@ func (l *Lefthook) configExists(path string) bool {
}

func (l *Lefthook) createConfig(path string) error {
file := filepath.Join(path, config.DefaultConfigName)
file := filepath.Join(path, config.DefaultDotConfigName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this behavior unchanged? We'll change this in later major release. I think it would be great to just support .lefthook.yml and .lefthook-local.yml by now, fix possible issue, decide on what to do when both variants exist and so on. So, this transition will be smooth. WDYT?

Copy link
Contributor Author

@hyperupcall hyperupcall Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good! The docs don't mention of the dot-prefixed variants so keeping the behavior unchanged means there is no chance of confusion if one is generated or has priority over the non-dot-prefixed variants.


err := afero.WriteFile(l.Fs, file, templates.Config(), configFileMode)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/lefthook/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestLefthookInstall(t *testing.T) {
t.Errorf("unexpected error: %s", err)
}

dotConfigPath := filepath.Join(root, ".lefthook.yml")
configPath := filepath.Join(root, "lefthook.yml")

hookPath := func(hook string) string {
Expand All @@ -43,7 +44,7 @@ func TestLefthookInstall(t *testing.T) {
}{
{
name: "without a config file",
wantExist: []string{configPath},
wantExist: []string{dotConfigPath},
},
{
name: "simple default config",
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func (l *Lefthook) Uninstall(args *UninstallArgs) error {

if args.RemoveConfig {
for _, glob := range []string{
".lefthook.y*ml",
"lefthook.y*ml",
".lefthook-local.y*ml",
"lefthook-local.y*ml",
} {
l.removeFile(filepath.Join(l.repo.RootPath, glob))
Expand Down
2 changes: 1 addition & 1 deletion testdata/install.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exec git init
exec lefthook install
exists lefthook.yml
exists .lefthook.yml
! stderr .