-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dangling-whitespace: rule for trailing spaces
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
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("+ ") | ||
) |