Skip to content

Commit 3969e46

Browse files
committed
whitespace: switch to cheaper check
Signed-off-by: Vincent Batts <[email protected]>
1 parent 8193a24 commit 3969e46

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

git/commits.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff 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.
6371
func 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`.

rules/danglingwhitespace/rule.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package danglingwhitespace
22

33
import (
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

2522
func 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-
)

0 commit comments

Comments
 (0)