mirror of
https://github.com/vbatts/git-validation.git
synced 2024-11-17 13:58:39 +00:00
dangling-whitespace: rule for trailing spaces
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
1a447a0dfc
commit
8193a24bc4
3 changed files with 63 additions and 0 deletions
|
@ -57,6 +57,19 @@ var FieldNames = map[string]string{
|
||||||
"%G?": "verification_flag",
|
"%G?": "verification_flag",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// CommitEntry represents a single commit's information from `git`.
|
// CommitEntry represents a single commit's information from `git`.
|
||||||
// See also FieldNames
|
// See also FieldNames
|
||||||
type CommitEntry map[string]string
|
type CommitEntry map[string]string
|
||||||
|
|
1
main.go
1
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
|
||||||
_ "github.com/vbatts/git-validation/rules/dco"
|
_ "github.com/vbatts/git-validation/rules/dco"
|
||||||
_ "github.com/vbatts/git-validation/rules/shortsubject"
|
_ "github.com/vbatts/git-validation/rules/shortsubject"
|
||||||
"github.com/vbatts/git-validation/validate"
|
"github.com/vbatts/git-validation/validate"
|
||||||
|
|
49
rules/danglingwhitespace/rule.go
Normal file
49
rules/danglingwhitespace/rule.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package danglingwhitespace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/vbatts/git-validation/git"
|
||||||
|
"github.com/vbatts/git-validation/validate"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DanglingWhitespace is the rule for checking the presence of dangling
|
||||||
|
// whitespaces on line endings.
|
||||||
|
DanglingWhitespace = validate.Rule{
|
||||||
|
Name: "dangling-whitespace",
|
||||||
|
Description: "checking the presence of dangling whitespaces on line endings",
|
||||||
|
Run: ValidateDanglingWhitespace,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
validate.RegisterRule(DanglingWhitespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vr.Msg = "all added diff lines do not have trailing spaces"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
newLine = []byte("\n")
|
||||||
|
diffAddLine = []byte("+ ")
|
||||||
|
)
|
Loading…
Reference in a new issue