Skip to content

Commit

Permalink
feat: add option to skip running LFS hooks (#879)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
zachahn and zachahn-gusto authored Nov 28, 2024
1 parent f53894f commit 2c874a9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .lefthook.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
skip_lfs = true

[pre-commit]
parallel = true

Expand Down
5 changes: 5 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type RunArgs struct {
FilesFromStdin bool
Force bool
NoAutoInstall bool
SkipLFS bool
Files []string
RunOnlyCommands []string
}
Expand Down Expand Up @@ -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,
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 @@ -43,6 +43,7 @@ type Options struct {
GitArgs []string
LogSettings log.Settings
DisableTTY bool
SkipLFS bool
Force bool
Files []string
RunOnlyCommands []string
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestRunAll(t *testing.T) {
success, fail []Result
gitCommands []string
force bool
skipLFS bool
}{
"empty hook": {
hookName: "post-commit",
Expand Down Expand Up @@ -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
Expand All @@ -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{},
Expand Down

0 comments on commit 2c874a9

Please sign in to comment.