Skip to content

Commit

Permalink
feat: add files, all-files, and commands flags (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
nihalgonsalves committed Aug 15, 2023
1 parent 357ae9a commit 48cd1be
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
9 changes: 9 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ func newRunCmd(opts *lefthook.Options) *cobra.Command {
"run hook non-interactively, disable spinner",
)

runCmd.Flags().BoolVar(
&runArgs.AllFiles, "all-files", false,
"run hooks on all files",
)

runCmd.Flags().StringSliceVar(&runArgs.Files, "files", nil, "run on specified files. takes precedence over --all-files")

runCmd.Flags().StringSliceVar(&runArgs.RunOnlyCommands, "commands", nil, "run only specified commands")

return &runCmd
}
15 changes: 15 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ Or run manually also
$ lefthook run pre-commit
```

You can also specify a flag to run only some commands:

```bash
$ lefthook run pre-commit --commands lint
```

and optionally run either on all files (any `{staged_files}` placeholder acts as `{all_files}`) or a list of files:

```bash
$ lefthook run pre-commit --all-files
$ lefthook run pre-commit --files file1.js,file2.js
```

(if both are specified, `--all-files` is ignored)

### `lefthook version`

You can check version with `lefthook version` and you can also check the commit hash with `lefthook version --full`
Expand Down
24 changes: 15 additions & 9 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const (
)

type RunArgs struct {
NoTTY bool
NoTTY bool
AllFiles bool
Files []string
RunOnlyCommands []string
}

func Run(opts *Options, args RunArgs, hookName string, gitArgs []string) error {
Expand Down Expand Up @@ -106,14 +109,17 @@ Run 'lefthook install' manually.`,

run := runner.NewRunner(
runner.Opts{
Fs: l.Fs,
Repo: l.repo,
Hook: hook,
HookName: hookName,
GitArgs: gitArgs,
ResultChan: resultChan,
SkipSettings: logSettings,
DisableTTY: cfg.NoTTY || args.NoTTY,
Fs: l.Fs,
Repo: l.repo,
Hook: hook,
HookName: hookName,
GitArgs: gitArgs,
ResultChan: resultChan,
SkipSettings: logSettings,
DisableTTY: cfg.NoTTY || args.NoTTY,
AllFiles: args.AllFiles,
Files: args.Files,
RunOnlyCommands: args.RunOnlyCommands,
},
)

Expand Down
9 changes: 8 additions & 1 deletion internal/lefthook/runner/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error,
filesCommand = command.Files
}

stagedFiles := r.Repo.StagedFiles
if len(r.Files) > 0 {
stagedFiles = func() ([]string, error) { return r.Files, nil }
} else if r.AllFiles {
stagedFiles = r.Repo.AllFiles
}

filesTypeToFn := map[string]func() ([]string, error){
config.SubStagedFiles: r.Repo.StagedFiles,
config.SubStagedFiles: stagedFiles,
config.PushFiles: r.Repo.PushFiles,
config.SubAllFiles: r.Repo.AllFiles,
config.SubFiles: func() ([]string, error) {
Expand Down
25 changes: 16 additions & 9 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"sync"
Expand All @@ -30,14 +31,17 @@ const (
var surroundingQuotesRegexp = regexp.MustCompile(`^'(.*)'$`)

type Opts struct {
Fs afero.Fs
Repo *git.Repository
Hook *config.Hook
HookName string
GitArgs []string
ResultChan chan Result
SkipSettings log.SkipSettings
DisableTTY bool
Fs afero.Fs
Repo *git.Repository
Hook *config.Hook
HookName string
GitArgs []string
ResultChan chan Result
SkipSettings log.SkipSettings
DisableTTY bool
AllFiles bool
Files []string
RunOnlyCommands []string
}

// Runner responds for actual execution and handling the results.
Expand Down Expand Up @@ -307,7 +311,10 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
func (r *Runner) runCommands() {
commands := make([]string, 0, len(r.Hook.Commands))
for name := range r.Hook.Commands {
commands = append(commands, name)
if len(r.RunOnlyCommands) == 0 || slices.Contains(r.RunOnlyCommands, name) {
commands = append(commands, name)
}

}

sort.Strings(commands)
Expand Down

0 comments on commit 48cd1be

Please sign in to comment.