Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --all-including-untracked-files #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ func newRunCmd(opts *lefthook.Options) *cobra.Command {

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

runCmd.Flags().StringSliceVar(&runArgs.Files, "files", nil, "run on specified files. takes precedence over --all-files")
runCmd.Flags().BoolVar(
&runArgs.AllFilesIncludingUntracked, "all-including-untracked-files", false,
"run hooks on all files, including untracked ones",
)

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

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

Expand Down
4 changes: 3 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ You can also specify a flag to run only some commands:
$ 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:
and optionally run either on all files in the Git index (any `{staged_files}` placeholder acts as `{all_files}`) or a list of files:

```bash
$ lefthook run pre-commit --all-files
Expand All @@ -139,6 +139,8 @@ $ lefthook run pre-commit --files file1.js,file2.js

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

In the same way, you can use `--all-including-untracked-files`.

### `lefthook version`

You can check version with `lefthook version` and you can also check the commit hash with `lefthook version --full`
Expand Down
9 changes: 5 additions & 4 deletions internal/config/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package config
import "strings"

const (
SubFiles string = "{files}"
SubAllFiles string = "{all_files}"
SubStagedFiles string = "{staged_files}"
PushFiles string = "{push_files}"
SubFiles string = "{files}"
SubAllFiles string = "{all_files}"
SubAllFilesIncludingUntracked string = "{all_including_untracked_files}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sanmai-NL , do you really need this template? Why can't a custom {files} be used instead?

SubStagedFiles string = "{staged_files}"
PushFiles string = "{push_files}"
)

func isRunnerFilesCompatible(runner string) bool {
Expand Down
29 changes: 18 additions & 11 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (
)

const (
cmdRootPath = "git rev-parse --show-toplevel"
cmdHooksPath = "git rev-parse --git-path hooks"
cmdInfoPath = "git rev-parse --git-path info"
cmdGitPath = "git rev-parse --git-dir"
cmdStagedFiles = "git diff --name-only --cached --diff-filter=ACMR"
cmdAllFiles = "git ls-files --cached"
cmdPushFilesBase = "git diff --name-only HEAD @{push}"
cmdPushFilesHead = "git diff --name-only HEAD %s"
cmdStatusShort = "git status --short"
cmdCreateStash = "git stash create"
cmdListStash = "git stash list"
cmdRootPath = "git rev-parse --show-toplevel"
cmdHooksPath = "git rev-parse --git-path hooks"
cmdInfoPath = "git rev-parse --git-path info"
cmdGitPath = "git rev-parse --git-dir"
cmdStagedFiles = "git diff --name-only --cached --diff-filter=ACMR"
cmdAllFiles = "git ls-files --cached"
cmdAllFilesIncludingUntracked = "git ls-files --cached --others --exclude-standard"
cmdPushFilesBase = "git diff --name-only HEAD @{push}"
cmdPushFilesHead = "git diff --name-only HEAD %s"
cmdStatusShort = "git status --short"
cmdCreateStash = "git stash create"
cmdListStash = "git stash list"

stashMessage = "lefthook auto backup"
unstagedPatchName = "lefthook-unstaged.patch"
Expand Down Expand Up @@ -103,6 +104,12 @@ func (r *Repository) AllFiles() ([]string, error) {
return r.FilesByCommand(cmdAllFiles)
}

// StagedFiles returns a list of all files in repository
// or an error if git command fails.
func (r *Repository) AllFilesIncludingUntracked() ([]string, error) {
return r.FilesByCommand(cmdAllFilesIncludingUntracked)
}

// PushFiles returns a list of files that are ready to be pushed
// or an error if git command fails.
func (r *Repository) PushFiles() ([]string, error) {
Expand Down
34 changes: 18 additions & 16 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ const (
)

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

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

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

Expand Down
9 changes: 6 additions & 3 deletions internal/lefthook/run/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@
stagedFiles = func() ([]string, error) { return r.Files, nil }
case r.AllFiles:
stagedFiles = r.Repo.AllFiles
case r.AllFilesIncludingUntracked:
stagedFiles = r.Repo.AllFilesIncludingUntracked
Comment on lines +82 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no need to use {all_including_untracked_files} template we can move this part to internal/lefthook/run.go and pass those files via Files option. I suggested pretty similar solution here

default:
stagedFiles = r.Repo.StagedFiles
}

filesFns := map[string]func() ([]string, error){
config.SubStagedFiles: stagedFiles,
config.PushFiles: r.Repo.PushFiles,
config.SubAllFiles: r.Repo.AllFiles,
config.SubStagedFiles: stagedFiles,

Check failure on line 89 in internal/lefthook/run/prepare_command.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
config.PushFiles: r.Repo.PushFiles,
config.SubAllFiles: r.Repo.AllFiles,
config.SubAllFilesIncludingUntracked: r.Repo.AllFilesIncludingUntracked,
config.SubFiles: func() ([]string, error) {
return r.Repo.FilesByCommand(filesCmd)
},
Expand Down
23 changes: 12 additions & 11 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ const (
var surroundingQuotesRegexp = regexp.MustCompile(`^'(.*)'$`)

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

// Runner responds for actual execution and handling the results.
Expand Down
Loading