Skip to content

Commit

Permalink
fix: test sh syntax in files option and fix skip_output parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jan 12, 2024
1 parent 82c0b29 commit 9d8dc3d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
16 changes: 6 additions & 10 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var (
cmdGitPath = []string{"git", "rev-parse", "--git-dir"}
cmdAllFiles = []string{"git", "ls-files", "--cached"}
cmdCreateStash = []string{"git", "stash", "create"}
cmdStageFiles = []string{"git", "add"}
cmdRemotes = []string{"git", "branch", " --remotes"}
cmdHideUnstaged = []string{"git", "checkout", "--force", "--"}
)

// Repository represents a git repository.
Expand Down Expand Up @@ -112,7 +115,7 @@ func (r *Repository) PushFiles() ([]string, error) {
}

if len(r.headBranch) == 0 {
branches, err := r.Git.CmdLines([]string{"git", "branch", " --remotes"})
branches, err := r.Git.CmdLines(cmdRemotes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,14 +188,7 @@ func (r *Repository) SaveUnstaged(files []string) error {
}

func (r *Repository) HideUnstaged(files []string) error {
_, err := r.Git.Cmd(
append([]string{
"git",
"checkout",
"--force",
"--",
}, files...),
)
_, err := r.Git.Cmd(append(cmdHideUnstaged, files...))

return err
}
Expand Down Expand Up @@ -280,7 +276,7 @@ func (r *Repository) AddFiles(files []string) error {
}

_, err := r.Git.Cmd(
append([]string{"git", "add"}, files...),
append(cmdStageFiles, files...),
)

return err
Expand Down
16 changes: 10 additions & 6 deletions internal/log/skip_settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package log

import "strings"
import (
"strings"
)

const (
skipMeta = 1 << iota
Expand All @@ -21,12 +23,14 @@ func (s *SkipSettings) ApplySettings(tags string, skipOutput interface{}) {
switch typedSkipOutput := skipOutput.(type) {
case bool:
s.SkipAll(typedSkipOutput)
case []string:
if tags != "" {
typedSkipOutput = append(typedSkipOutput, strings.Split(tags, ",")...)
}
case []interface{}:
for _, skipOption := range typedSkipOutput {
s.applySetting(skipOption)
s.applySetting(skipOption.(string))
}
if tags != "" {
for _, skipOption := range strings.Split(tags, ",") {
s.applySetting(skipOption)
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/log/skip_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ func TestSkipSetting(t *testing.T) {
results map[string]bool
}{
{
settings: []string{},
settings: []interface{}{},
results: map[string]bool{},
},
{
settings: false,
results: map[string]bool{},
},
{
settings: []string{"failure", "execution"},
settings: []interface{}{"failure", "execution"},
results: map[string]bool{
"failure": true,
"execution": true,
},
},
{
settings: []string{
settings: []interface{}{
"meta",
"summary",
"success",
Expand Down
32 changes: 32 additions & 0 deletions testdata/sh_syntax_in_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
exec git init
exec lefthook install
exec git config user.email "[email protected]"
exec git config user.name "Your Name"

exec lefthook run echo_files
stdout '1.txt 10.txt'

-- lefthook.yml --
skip_output:
- meta # Skips lefthook version printing
- summary # Skips summary block (successful and failed steps) printing
- empty_summary # Skips summary heading when there are no steps to run
- success # Skips successful steps printing
- failure # Skips failed steps printing
- execution_info # Skips printing `EXECUTE > ...` logging
- skips

echo_files:
commands:
echo:
files: ls | grep 1
run: echo {files}

-- 1.txt --
1.txt

-- 10.txt --
10.txt

-- 20.txt --
20.txt

0 comments on commit 9d8dc3d

Please sign in to comment.