Skip to content
Merged
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
6 changes: 2 additions & 4 deletions .lint-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ golangci:
- gocognit # 1
- revive # 1
- path: ^git/commits\.go$
linters: # 37 findings
- wrapcheck # 11
linters: # 26 findings
- noctx # 10
- gosec # 8
- varnamelen # 3
Expand Down Expand Up @@ -137,9 +136,8 @@ golangci:
- paralleltest # 2
- testpackage # 1
- path: ^validate/runner\.go$
linters: # 17 findings
linters: # 11 findings
- forbidigo # 7
- wrapcheck # 6
- varnamelen # 2
- err113 # 1
- gocognit # 1
29 changes: 29 additions & 0 deletions cmd/git-validation/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,35 @@ func TestContractDefaultRunOverAHistory(t *testing.T) {
}
}

// TestContractWhitespaceErrorsAreNamed: a commit that adds trailing
// whitespace fails the dangling-whitespace rule with the command that shows
// them, and a commit that does not passes it.
//
//nolint:paralleltest // newRepo sets the environment with t.Setenv
func TestContractWhitespaceErrorsAreNamed(t *testing.T) {
r := newRepo(t)

clean := r.commit("a", "a\n", "Clean change", true)
dirty := r.commit("b", "b \n", "Dirty change", true)

code, out := r.run(nil, "--run", "dangling-whitespace", "-v")
if code != 1 {
t.Errorf("a commit with trailing whitespace: exit %d, want 1", code)
}

for _, line := range []string{
" * " + dirty + " \"Dirty change\" ... FAIL\n",
// The rule names the commit by its whole hash, which the short one starts.
" - FAIL - has whitespace errors. See `git show --check " + dirty,
" * " + clean + " \"Clean change\" ... PASS\n",
" - PASS - commit does not have any whitespace errors\n",
} {
if !strings.Contains(out, line) {
t.Errorf("output lacks %q:\n%s", line, out)
}
}
}

// TestContractVerboseAndQuiet: -v prints every rule's result under its
// commit; -q prints nothing for a passing commit and one line per failing
// rule, the hash and the reason, with -v or without; QUIET in the
Expand Down
22 changes: 18 additions & 4 deletions git/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func gitVersion() (string, error) {

buf, err := cmd.Output()
if err != nil {
return "", err
return "", fmt.Errorf("git version: %w", err)
}

return strings.Fields(string(buf))[2], nil
Expand Down Expand Up @@ -226,7 +226,9 @@ func Check(commit string) ([]byte, error) {
}

if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
gitNewEnough, err := gitVersionNewerThan(versionWithExcludes)
var gitNewEnough bool

gitNewEnough, err = gitVersionNewerThan(versionWithExcludes)
if err != nil {
return nil, err
}
Expand All @@ -246,7 +248,14 @@ func Check(commit string) ([]byte, error) {

cmd.Stderr = os.Stderr

return cmd.Output()
// The output holds what git found when it exits non-zero, which is how
// it says there are whitespace errors: both go back to the caller.
out, err := cmd.Output()
if err != nil {
return out, fmt.Errorf("git log --check %s: %w", commit, err)
}

return out, nil
}

// Show returns the diff of a commit.
Expand All @@ -257,7 +266,12 @@ func Show(commit string) ([]byte, error) {

cmd.Stderr = os.Stderr

return cmd.Output()
out, err := cmd.Output()
if err != nil {
return out, fmt.Errorf("git show %s: %w", commit, err)
}

return out, nil
}

// CommitEntry represents a single commit's information from `git`.
Expand Down
13 changes: 12 additions & 1 deletion rules/danglingwhitespace/rule.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package danglingwhitespace

import (
"errors"
"os/exec"

"github.com/forkcloser/git-validation/git"
"github.com/forkcloser/git-validation/validate"
)

// whitespaceErrors is the status git log --check exits with when it finds
// whitespace errors.
const whitespaceErrors = 2

// DanglingWhitespace is the rule for checking the presence of dangling
// whitespaces on line endings.
var DanglingWhitespace = validate.Rule{
Expand All @@ -27,7 +34,11 @@ func ValidateDanglingWhitespace(r validate.Rule, c git.CommitEntry) (vr validate
_, err := git.Check(c["commit"])
if err != nil {
vr.Pass = false
if err.Error() == "exit status 2" {

// git log --check exits 2 when it finds whitespace errors; anything
// else is git failing.
var exit *exec.ExitError
if errors.As(err, &exit) && exit.ExitCode() == whitespaceErrors {
vr.Msg = "has whitespace errors. See `git show --check " + c["commit"] + "`."
} else {
vr.Msg = "errored with: " + err.Error()
Expand Down
12 changes: 6 additions & 6 deletions validate/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e

cwd, err = os.Getwd()
if err != nil {
return nil, err
return nil, fmt.Errorf("finding the working directory: %w", err)
}
defer func() {
if chdirErr := os.Chdir(cwd); chdirErr != nil {
Expand All @@ -56,14 +56,14 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e
}()

if err = os.Chdir(newroot); err != nil {
return nil, err
return nil, fmt.Errorf("entering %q: %w", newroot, err)
}

commitrange, err = git.FetchHeadCommit()
if err != nil {
commitrange, err = git.HeadCommit()
if err != nil {
return nil, err
return nil, fmt.Errorf("finding a commit to validate from in %q: %w", newroot, err)
}
}

Expand All @@ -82,7 +82,7 @@ func NewRunner(root string, rules []Rule, commitrange string) (runner *Runner, e
func (r *Runner) Run() (err error) {
cwd, err := os.Getwd()
if err != nil {
return err
return fmt.Errorf("finding the working directory: %w", err)
}
defer func() {
if chdirErr := os.Chdir(cwd); chdirErr != nil {
Expand All @@ -91,13 +91,13 @@ func (r *Runner) Run() (err error) {
}()

if err = os.Chdir(r.Root); err != nil {
return err
return fmt.Errorf("entering %q: %w", r.Root, err)
}

// collect the entries
c, err := git.Commits(r.CommitRange)
if err != nil {
return err
return fmt.Errorf("listing the commits of %q: %w", r.CommitRange, err)
}

for _, commit := range c {
Expand Down
Loading