Skip to content

Commit

Permalink
refactor: use maps instead of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Oct 23, 2024
1 parent 1938c4a commit bb1f9a6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
5 changes: 4 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cmd

import (
_ "embed"
"sort"

"github.com/spf13/cobra"
"golang.org/x/exp/maps"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/lefthook"
Expand All @@ -22,7 +24,8 @@ func (add) New(opts *lefthook.Options) *cobra.Command {
if len(args) != 0 {
return
}
ret = config.AvailableHooks[:]
ret = maps.Keys(config.AvailableHooks)
sort.Strings(ret)
return
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"sort"

"github.com/spf13/cobra"
"golang.org/x/exp/maps"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/lefthook"
Expand All @@ -19,7 +22,9 @@ func (run) New(opts *lefthook.Options) *cobra.Command {
return
}
ret = lefthook.ConfigHookCompletions(opts)
ret = append(ret, config.AvailableHooks[:]...)
other := maps.Keys(config.AvailableHooks)
sort.Strings(other)
ret = append(ret, other...)
return
}

Expand Down
71 changes: 33 additions & 38 deletions internal/config/available_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ const ChecksumFileName = "lefthook.checksum"
const GhostHookName = "prepare-commit-msg"

// AvailableHooks - list of hooks taken from https://git-scm.com/docs/githooks.
var AvailableHooks = [...]string{
"pre-commit",
"pre-push",
"commit-msg",
"applypatch-msg",
"fsmonitor-watchman",
"p4-changelist",
"p4-post-changelist",
"p4-pre-submit",
"p4-prepare-changelist",
"post-applypatch",
"post-checkout",
"post-commit",
"post-index-change",
"post-merge",
"post-receive",
"post-rewrite",
"post-update",
"pre-applypatch",
"pre-auto-gc",
"pre-merge-commit",
"pre-rebase",
"pre-receive",
"prepare-commit-msg",
"proc-receive",
"push-to-checkout",
"rebase",
"reference-transaction",
"sendemail-validate",
"update",
var AvailableHooks = map[string]struct{}{
"pre-commit": {},
"pre-push": {},
"commit-msg": {},
"applypatch-msg": {},
"fsmonitor-watchman": {},
"p4-changelist": {},
"p4-post-changelist": {},
"p4-pre-submit": {},
"p4-prepare-changelist": {},
"post-applypatch": {},
"post-checkout": {},
"post-commit": {},
"post-index-change": {},
"post-merge": {},
"post-receive": {},
"post-rewrite": {},
"post-update": {},
"pre-applypatch": {},
"pre-auto-gc": {},
"pre-merge-commit": {},
"pre-rebase": {},
"pre-receive": {},
"prepare-commit-msg": {},
"proc-receive": {},
"push-to-checkout": {},
"rebase": {},
"reference-transaction": {},
"sendemail-validate": {},
"update": {},
}

func HookUsesStagedFiles(hook string) bool {
Expand All @@ -47,12 +47,7 @@ func HookUsesPushFiles(hook string) bool {
return hook == "pre-push"
}

func HookAvailable(hook string) bool {
for _, name := range AvailableHooks {
if name == hook {
return true
}
}

return false
func KnownHook(hook string) bool {
_, ok := AvailableHooks[hook]
return ok
}
2 changes: 1 addition & 1 deletion internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func mergeLocal(v *viper.Viper) error {
func unmarshalConfigs(base, extra *viper.Viper, c *Config) error {
c.Hooks = make(map[string]*Hook)

for _, hookName := range AvailableHooks {
for hookName := range AvailableHooks {
if err := addHook(hookName, base, extra, c); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Add(opts *Options, args *AddArgs) error {

// Creates a hook, given in args. The hook is a Lefthook hook.
func (l *Lefthook) Add(args *AddArgs) error {
if !config.HookAvailable(args.Hook) {
if !config.KnownHook(args.Hook) {
return fmt.Errorf("Skip adding, hook is unavailable: %s", args.Hook)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"path/filepath"
"slices"
"time"

"github.com/evilmartians/lefthook/internal/config"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
// Find the hook
hook, ok := cfg.Hooks[hookName]
if !ok {
if slices.Contains(config.AvailableHooks[:], hookName) {
if config.KnownHook(hookName) {
log.Debugf("[lefthook] skip: Hook %s doesn't exist in the config", hookName)
return nil
}
Expand Down

0 comments on commit bb1f9a6

Please sign in to comment.