1
0
Fork 0
mirror of https://github.com/vbatts/git-validation.git synced 2024-11-17 22:08:39 +00:00

Merge pull request #9 from vbatts/show-check

whitespace: switch to cheaper check
This commit is contained in:
Vincent Batts 2016-04-06 13:05:15 -04:00
commit 39d22b3fd4
2 changed files with 18 additions and 26 deletions

View file

@ -57,17 +57,21 @@ var FieldNames = map[string]string{
"%G?": "verification_flag", "%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. // Show returns the diff of a commit.
// //
// NOTE: This could be expensive for very large commits. // NOTE: This could be expensive for very large commits.
func Show(commit string) ([]byte, error) { func Show(commit string) ([]byte, error) {
cmd := exec.Command("git", "show", commit) cmd := exec.Command("git", "show", commit)
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
out, err := cmd.Output() return cmd.Output()
if err != nil {
return nil, err
}
return out, nil
} }
// CommitEntry represents a single commit's information from `git`. // CommitEntry represents a single commit's information from `git`.

View file

@ -1,9 +1,6 @@
package danglingwhitespace package danglingwhitespace
import ( import (
"bytes"
"fmt"
"github.com/vbatts/git-validation/git" "github.com/vbatts/git-validation/git"
"github.com/vbatts/git-validation/validate" "github.com/vbatts/git-validation/validate"
) )
@ -23,27 +20,18 @@ func init() {
} }
func ValidateDanglingWhitespace(c git.CommitEntry) (vr validate.Result) { 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.CommitEntry = c
vr.Msg = "commit does not have any whitespace errors"
vr.Pass = true vr.Pass = true
for _, line := range bytes.Split(diff, newLine) {
if !bytes.HasPrefix(line, diffAddLine) || bytes.Equal(line, diffAddLine) { _, err := git.Check(c["commit"])
continue if err != nil {
} vr.Pass = false
if len(bytes.TrimSpace(line)) != len(line) { if err.Error() == "exit status 2" {
vr.Pass = false vr.Msg = "has whitespace errors. See `git show --check " + c["commit"] + "`."
vr.Msg = fmt.Sprintf("line %q has trailiing spaces", string(line)) } else {
vr.Msg = "errored with: " + err.Error()
} }
} }
vr.Msg = "all added diff lines do not have trailing spaces"
return return
} }
var (
newLine = []byte("\n")
diffAddLine = []byte("+ ")
)