Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dangling-whitespace: rule for trailing spaces #7

Merged
merged 1 commit into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions git/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ var FieldNames = map[string]string{
"%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`.
// See also FieldNames
type CommitEntry map[string]string
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
_ "github.com/vbatts/git-validation/rules/dco"
_ "github.com/vbatts/git-validation/rules/shortsubject"
"github.com/vbatts/git-validation/validate"
Expand Down
49 changes: 49 additions & 0 deletions rules/danglingwhitespace/rule.go
Original file line number Diff line number Diff line change
@@ -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"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't save the diff for reuse between multiple validators, I think it would be faster (no need to serialize the whole diff) and less work in this repo (no need to parse the diff or formulate an error message) to use git show --check {commit} (like opencontainers/runtime-spec@bdaf4467e, .tools: Run git show --check ... on each commit, 2015-09-12, opencontainers/runtime-spec#182).

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea. I'll add a method for check and switch to that instead.

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“trailiing” → “trailing”.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psssshhh

}
}
vr.Msg = "all added diff lines do not have trailing spaces"
return
}

var (
newLine = []byte("\n")
diffAddLine = []byte("+ ")
)