Skip to content

Commit

Permalink
fix: auto stage non-standard files (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jun 19, 2023
1 parent d7aeb70 commit dbef30e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ build:
build-with-coverage:
go build -cover -ldflags "-s -w -X github.com/evilmartians/lefthook/internal/version.commit=$(COMMIT_HASH)" -o lefthook

install: build
cp lefthook $(GOPATH)/bin/

test:
go test -cpu 24 -race -count=1 -timeout=30s ./...

test-integration: build-with-coverage
./lefthook dump
./lefthook dump --json
./lefthook dump --toml
test-integrity: install
go test -cpu 24 -race -count=1 -timeout=30s -tags=integrity integrity_test.go

bench:
go test -cpu 24 -race -run=Bench -bench=. ./...
Expand Down
31 changes: 19 additions & 12 deletions internal/lefthook/runner/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error,
},
}

filteredFiles := []string{}
filesFiltered := make([]string, 0)
runString := command.Run
for filesType, filesFn := range filesTypeToFn {
// Checking substitutions and skipping execution if it is empty.
Expand All @@ -79,28 +79,31 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error,
return nil, nil, errors.New("no files for inspection")
}

filesPrepared := prepareFiles(command, files)
if len(filesPrepared) == 0 {
filtered := filterFiles(command, files)
filesFiltered = append(filesFiltered, filtered...)

prepared := escapeFiles(filtered)
if len(prepared) == 0 {
return nil, nil, errors.New("no files for inspection")
}
filteredFiles = append(filteredFiles, filesPrepared...)
runString = replaceQuoted(runString, filesType, filesPrepared)

runString = replaceQuoted(runString, filesType, prepared)
}
}

if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) {
if len(filesFiltered) == 0 && config.HookUsesStagedFiles(r.HookName) {
files, err := r.Repo.StagedFiles()
if err == nil {
if len(prepareFiles(command, files)) == 0 {
if len(filterFiles(command, files)) == 0 {
return nil, nil, errors.New("no matching staged files")
}
}
}

if len(filteredFiles) == 0 && config.HookUsesPushFiles(r.HookName) {
if len(filesFiltered) == 0 && config.HookUsesPushFiles(r.HookName) {
files, err := r.Repo.PushFiles()
if err == nil {
if len(prepareFiles(command, files)) == 0 {
if len(filterFiles(command, files)) == 0 {
return nil, nil, errors.New("no matching push files")
}
}
Expand All @@ -111,7 +114,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error,
log.Debug("[lefthook] executing: ", runString)

return &commandArgs{
files: filteredFiles,
files: filesFiltered,
all: strings.Split(runString, " "),
}, nil, nil
}
Expand All @@ -124,7 +127,7 @@ func (r *Runner) replacePositionalArguments(runString string) string {
return runString
}

func prepareFiles(command *config.Command, files []string) []string {
func filterFiles(command *config.Command, files []string) []string {
if files == nil {
return []string{}
}
Expand All @@ -137,7 +140,11 @@ func prepareFiles(command *config.Command, files []string) []string {

log.Debug("[lefthook] files after filters:\n", files)

// Escape file names to prevent unexpected bugs
return files
}

// Escape file names to prevent unexpected bugs.
func escapeFiles(files []string) []string {
var filesEsc []string
for _, fileName := range files {
if len(fileName) > 0 {
Expand Down
7 changes: 5 additions & 2 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,16 @@ func (r *Runner) runCommand(name string, command *config.Command) {

if finished && config.HookUsesStagedFiles(r.HookName) && command.StageFixed {
files := args.files

if len(files) == 0 {
stagedFiles, err := r.Repo.StagedFiles()
var err error
files, err = r.Repo.StagedFiles()
if err != nil {
log.Warn("Couldn't stage fixed files:", err)
return
}
files = prepareFiles(command, stagedFiles)

files = filterFiles(command, files)
}

if len(command.Root) > 0 {
Expand Down
9 changes: 7 additions & 2 deletions testdata/stage_fixed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ min_version: 1.1.1
pre-commit:
commands:
edit_file:
run: echo newline >> file.txt
run: |
echo newline >> "[file].js"
echo newline >> file.txt
stage_fixed: true

-- file.txt --
firstline
sometext

-- [file].js --
somecode
19 changes: 19 additions & 0 deletions testdata/stage_fixed_505.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exec git init
exec lefthook install
exec git config user.email "you@example.com"
exec git config user.name "Your Name"
exec git add -A
exec git status --short
exec git commit -m 'test stage_fixed'
exec git status --short
! stdout .

-- lefthook.yml --
pre-commit:
commands:
edit_file:
run: echo "{staged_files}" && echo newline >> "[file].js"
stage_fixed: true

-- [file].js --
somecode

0 comments on commit dbef30e

Please sign in to comment.