Skip to content

Commit

Permalink
fix: skip git lfs hook when calling manually (#855)
Browse files Browse the repository at this point in the history
* fix: skip git-lfs on pre-push hook when calling manually

* refactor: use maps instead of arrays
  • Loading branch information
mrexox authored Oct 23, 2024
1 parent fa89715 commit 453d026
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 56 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
19 changes: 7 additions & 12 deletions internal/git/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const (
LFSConfigFile = ".lfsconfig"
)

var lfsHooks = [...]string{
"post-checkout",
"post-commit",
"post-merge",
"pre-push",
var lfsHooks = map[string]struct{}{
"post-checkout": {},
"post-commit": {},
"post-merge": {},
"pre-push": {},
}

// IsLFSAvailable returns 'true' if git-lfs is installed.
Expand All @@ -25,11 +25,6 @@ func IsLFSAvailable() bool {

// IsLFSHook returns whether the hookName is supported by Git LFS.
func IsLFSHook(hookName string) bool {
for _, lfsHookName := range lfsHooks {
if lfsHookName == hookName {
return true
}
}

return false
_, ok := lfsHooks[hookName]
return ok
}
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
5 changes: 5 additions & 0 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (r *Runner) runLFSHook(ctx context.Context) error {
return nil
}

// Skip running git-lfs for pre-push hook when triggered manually
if len(r.GitArgs) == 0 && r.HookName == "pre-push" {
return nil
}

lfsRequiredFile := filepath.Join(r.Repo.RootPath, git.LFSRequiredFile)
lfsConfigFile := filepath.Join(r.Repo.RootPath, git.LFSConfigFile)

Expand Down

0 comments on commit 453d026

Please sign in to comment.