File tree Expand file tree Collapse file tree 2 files changed +18
-26
lines changed Expand file tree Collapse file tree 2 files changed +18
-26
lines changed Original file line number Diff line number Diff line change @@ -57,17 +57,21 @@ var FieldNames = map[string]string{
5757 "%G?" : "verification_flag" ,
5858}
5959
60+ // Check warns if changes introduce whitespace errors.
61+ // Returns non-zero if any issues are found.
62+ func Check (commit string ) ([]byte , error ) {
63+ cmd := exec .Command ("git" , "show" , "--check" , commit )
64+ cmd .Stderr = os .Stderr
65+ return cmd .Output ()
66+ }
67+
6068// Show returns the diff of a commit.
6169//
6270// NOTE: This could be expensive for very large commits.
6371func Show (commit string ) ([]byte , error ) {
6472 cmd := exec .Command ("git" , "show" , commit )
6573 cmd .Stderr = os .Stderr
66- out , err := cmd .Output ()
67- if err != nil {
68- return nil , err
69- }
70- return out , nil
74+ return cmd .Output ()
7175}
7276
7377// CommitEntry represents a single commit's information from `git`.
Original file line number Diff line number Diff line change 11package danglingwhitespace
22
33import (
4- "bytes"
5- "fmt"
6-
74 "github.com/vbatts/git-validation/git"
85 "github.com/vbatts/git-validation/validate"
96)
@@ -23,27 +20,18 @@ func init() {
2320}
2421
2522func ValidateDanglingWhitespace (c git.CommitEntry ) (vr validate.Result ) {
26- diff , err := git .Show (c ["commit" ])
27- if err != nil {
28- return validate.Result {Pass : false , Msg : err .Error (), CommitEntry : c }
29- }
30-
3123 vr .CommitEntry = c
24+ vr .Msg = "commit does not have any whitespace errors"
3225 vr .Pass = true
33- for _ , line := range bytes .Split (diff , newLine ) {
34- if ! bytes .HasPrefix (line , diffAddLine ) || bytes .Equal (line , diffAddLine ) {
35- continue
36- }
37- if len (bytes .TrimSpace (line )) != len (line ) {
38- vr .Pass = false
39- vr .Msg = fmt .Sprintf ("line %q has trailiing spaces" , string (line ))
26+
27+ _ , err := git .Check (c ["commit" ])
28+ if err != nil {
29+ vr .Pass = false
30+ if err .Error () == "exit status 2" {
31+ vr .Msg = "has whitespace errors. See `git show --check " + c ["commit" ] + "`."
32+ } else {
33+ vr .Msg = "errored with: " + err .Error ()
4034 }
4135 }
42- vr .Msg = "all added diff lines do not have trailing spaces"
4336 return
4437}
45-
46- var (
47- newLine = []byte ("\n " )
48- diffAddLine = []byte ("+ " )
49- )
You can’t perform that action at this time.
0 commit comments