Skip to content

Commit

Permalink
fix: detect recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jun 21, 2024
1 parent 64cdbce commit 0360313
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ func Load(fs afero.Fs, repo *git.Repository) (*Config, error) {
}

func read(fs afero.Fs, path string, name string) (*viper.Viper, error) {
// v := viper.New()
// v.SetFs(fs)
// v.AddConfigPath(path)
// v.SetConfigName(name)

// // Allow overwriting settings with ENV variables
// v.SetEnvPrefix("LEFTHOOK")
// v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// v.AutomaticEnv()

v := newViper(fs, path)
v.SetConfigName(name)

Expand Down Expand Up @@ -223,7 +213,18 @@ func mergeRemotes(fs afero.Fs, repo *git.Repository, v *viper.Viper) error {

// extend merges all files listed in 'extends' option into the config.
func extend(fs afero.Fs, v *viper.Viper, root string) error {
return extendRecursive(fs, v, root, make(map[string]struct{}))
}

// extendRecursive merges extends.
// If extends contain other extends they get merged too.
func extendRecursive(fs afero.Fs, v *viper.Viper, root string, extends map[string]struct{}) error {
for _, path := range v.GetStringSlice("extends") {
if _, contains := extends[path]; contains {
return fmt.Errorf("possible recursion in extends: path %s is specified multiple times", path)
}
extends[path] = struct{}{}

if !filepath.IsAbs(path) {
path = filepath.Join(root, path)
}
Expand All @@ -234,7 +235,7 @@ func extend(fs afero.Fs, v *viper.Viper, root string) error {
return err
}

if err := extend(fs, extendV, root); err != nil {
if err := extendRecursive(fs, extendV, root, extends); err != nil {
return err
}

Expand Down

0 comments on commit 0360313

Please sign in to comment.