From 2c874a9d8070edebc69202d2cfc635f4969af84c Mon Sep 17 00:00:00 2001 From: Zach Ahn Date: Thu, 28 Nov 2024 02:27:00 -0500 Subject: [PATCH] feat: add option to skip running LFS hooks (#879) * feat: add option to entirely skip LFS * feat: add CLI flag to entirely skip LFS * chore: skip LFS As of this commit, this repository does not use git LFS. You can verify this through two ways: 1. By running `git lfs ls-files` and seeing which files are using LFS. 2. By checking the `.gitattributes` file in this repo to see if LFS is enabled on any files. * chore: add a small test that skips LFS --------- Co-authored-by: Zach Ahn --- .lefthook.toml | 2 ++ cmd/run.go | 5 +++++ internal/config/config.go | 1 + internal/lefthook/run.go | 2 ++ internal/lefthook/runner/runner.go | 5 +++++ internal/lefthook/runner/runner_test.go | 17 +++++++++++++++++ 6 files changed, 32 insertions(+) diff --git a/.lefthook.toml b/.lefthook.toml index ef93e82e..6f56018d 100644 --- a/.lefthook.toml +++ b/.lefthook.toml @@ -1,3 +1,5 @@ +skip_lfs = true + [pre-commit] parallel = true diff --git a/cmd/run.go b/cmd/run.go index 5f52188a..22a9b614 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -69,6 +69,11 @@ func (run) New(opts *lefthook.Options) *cobra.Command { "skip updating git hooks", ) + runCmd.Flags().BoolVar( + &runArgs.SkipLFS, "skip-lfs", false, + "skip running git lfs", + ) + runCmd.Flags().BoolVar( &runArgs.FilesFromStdin, "files-from-stdin", false, "get files from standard input, null-separated", diff --git a/internal/config/config.go b/internal/config/config.go index 23f11549..aab7e5e9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -37,6 +37,7 @@ type Config struct { NoTTY bool `mapstructure:"no_tty,omitempty"` AssertLefthookInstalled bool `mapstructure:"assert_lefthook_installed,omitempty"` Colors interface{} `mapstructure:"colors,omitempty"` + SkipLFS bool `mapstructure:"skip_lfs,omitempty"` // Deprecated: use Remotes Remote *Remote `mapstructure:"remote,omitempty"` diff --git a/internal/lefthook/run.go b/internal/lefthook/run.go index 08560552..b27f04e9 100644 --- a/internal/lefthook/run.go +++ b/internal/lefthook/run.go @@ -27,6 +27,7 @@ type RunArgs struct { FilesFromStdin bool Force bool NoAutoInstall bool + SkipLFS bool Files []string RunOnlyCommands []string } @@ -162,6 +163,7 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error { GitArgs: gitArgs, LogSettings: logSettings, DisableTTY: cfg.NoTTY || args.NoTTY, + SkipLFS: cfg.SkipLFS || args.SkipLFS, Files: args.Files, Force: args.Force, RunOnlyCommands: args.RunOnlyCommands, diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index d7a60413..2ceb9692 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -43,6 +43,7 @@ type Options struct { GitArgs []string LogSettings log.Settings DisableTTY bool + SkipLFS bool Force bool Files []string RunOnlyCommands []string @@ -125,6 +126,10 @@ func (r *Runner) RunAll(ctx context.Context, sourceDirs []string) ([]Result, err } func (r *Runner) runLFSHook(ctx context.Context) error { + if r.Options.SkipLFS { + return nil + } + if !git.IsLFSHook(r.HookName) { return nil } diff --git a/internal/lefthook/runner/runner_test.go b/internal/lefthook/runner/runner_test.go index d4330aa1..f8bc08cd 100644 --- a/internal/lefthook/runner/runner_test.go +++ b/internal/lefthook/runner/runner_test.go @@ -93,6 +93,7 @@ func TestRunAll(t *testing.T) { success, fail []Result gitCommands []string force bool + skipLFS bool }{ "empty hook": { hookName: "post-commit", @@ -716,6 +717,21 @@ func TestRunAll(t *testing.T) { "git diff --name-only HEAD @{push}", }, }, + "with LFS disabled": { + hookName: "post-checkout", + skipLFS: true, + existingFiles: []string{ + filepath.Join(root, "README.md"), + }, + hook: &config.Hook{ + Commands: map[string]*config.Command{ + "ok": { + Run: "success", + }, + }, + }, + success: []Result{succeeded("ok")}, + }, } { fs := afero.NewMemMapFs() repo.Fs = fs @@ -727,6 +743,7 @@ func TestRunAll(t *testing.T) { LogSettings: log.NewSettings(), GitArgs: tt.args, Force: tt.force, + SkipLFS: tt.skipLFS, }, executor: executor{}, cmd: cmd{},