Skip to content

Commit

Permalink
Merge pull request #9 from vbatts/show-check
Browse files Browse the repository at this point in the history
whitespace: switch to cheaper check
  • Loading branch information
vbatts committed Apr 6, 2016
2 parents 8193a24 + 3969e46 commit 39d22b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
14 changes: 9 additions & 5 deletions git/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ var FieldNames = map[string]string{
"%G?": "verification_flag",
}

// Check warns if changes introduce whitespace errors.
// Returns non-zero if any issues are found.
func Check(commit string) ([]byte, error) {
cmd := exec.Command("git", "show", "--check", commit)
cmd.Stderr = os.Stderr
return cmd.Output()
}

// Show returns the diff of a commit.
//
// NOTE: This could be expensive for very large commits.
func Show(commit string) ([]byte, error) {
cmd := exec.Command("git", "show", commit)
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return nil, err
}
return out, nil
return cmd.Output()
}

// CommitEntry represents a single commit's information from `git`.
Expand Down
30 changes: 9 additions & 21 deletions rules/danglingwhitespace/rule.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package danglingwhitespace

import (
"bytes"
"fmt"

"github.com/vbatts/git-validation/git"
"github.com/vbatts/git-validation/validate"
)
Expand All @@ -23,27 +20,18 @@ func init() {
}

func ValidateDanglingWhitespace(c git.CommitEntry) (vr validate.Result) {
diff, err := git.Show(c["commit"])
if err != nil {
return validate.Result{Pass: false, Msg: err.Error(), CommitEntry: c}
}

vr.CommitEntry = c
vr.Msg = "commit does not have any whitespace errors"
vr.Pass = true
for _, line := range bytes.Split(diff, newLine) {
if !bytes.HasPrefix(line, diffAddLine) || bytes.Equal(line, diffAddLine) {
continue
}
if len(bytes.TrimSpace(line)) != len(line) {
vr.Pass = false
vr.Msg = fmt.Sprintf("line %q has trailiing spaces", string(line))

_, err := git.Check(c["commit"])
if err != nil {
vr.Pass = false
if err.Error() == "exit status 2" {
vr.Msg = "has whitespace errors. See `git show --check " + c["commit"] + "`."
} else {
vr.Msg = "errored with: " + err.Error()
}
}
vr.Msg = "all added diff lines do not have trailing spaces"
return
}

var (
newLine = []byte("\n")
diffAddLine = []byte("+ ")
)

0 comments on commit 39d22b3

Please sign in to comment.