1
0
Fork 0
mirror of https://github.com/vbatts/git-validation.git synced 2025-05-29 16:22:26 +00:00

whitespace: switch to cheaper check

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-04-06 12:33:31 -04:00
parent 8193a24bc4
commit 3969e46e2b
2 changed files with 18 additions and 26 deletions

View file

@ -1,9 +1,6 @@
package danglingwhitespace
import (
"bytes"
"fmt"
"github.com/vbatts/git-validation/git"
"github.com/vbatts/git-validation/validate"
)
@ -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("+ ")
)