Skip to content

Commit

Permalink
migrate to pcre regexp package
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Jan 5, 2017
1 parent 5f7ed60 commit 5d88998
Show file tree
Hide file tree
Showing 104 changed files with 25,251 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.5.1
- 1.7.4
- tip
sudo: false
before_install:
Expand Down
24 changes: 23 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Godeps/_workspace/.gitignore

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Code Climate](https://img.shields.io/codeclimate/github/ezekg/git-hound.svg?style=flat-square)](https://codeclimate.com/github/ezekg/git-hound)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/ezekg/git-hound)

Hound is a Git plugin that helps prevent sensitive data from being committed into a repository by sniffing potential commits against regular expressions.
Hound is a Git plugin that helps prevent sensitive data from being committed into a repository by sniffing potential commits against PCRE regular expressions.

## How does it work?
Upon commit, it runs the output of `git diff -U0 --staged` through the Hound, which matches every _added_ or _modified_ line against your provided list of regular expressions from a local `.githound.yml` file.
Expand Down Expand Up @@ -52,7 +52,6 @@ git hound sniff
| `-bin=file` | string | `git` | Executable binary to use for `git` command |

## Example `.githound.yml`
Please see [Go's regular expression syntax documentation](https://golang.org/pkg/regexp/syntax/) for usage options.

```yaml
# Output warning on match but continue
Expand All @@ -61,6 +60,7 @@ warn:
- '\/Users\/\w+\/'
# Fail immediately upon match
fail:
- '(?!.*[\s])(?=.*[A-Za-z])(?=.*[0-9])(?=.*[!@#$&*])?.{16,}'
- '(?i)db_(user(name)?|pass(word)?|name)\W*[:=,]\W*.+$'
- '(?i)pass(word)?\W*[:=,]\W*.+$'
# Skip on matched filename
Expand Down
43 changes: 30 additions & 13 deletions hound.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main

import (
"github.com/ezekg/git-hound/Godeps/_workspace/src/gopkg.in/yaml.v2"
"github.com/ezekg/git-hound/Godeps/_workspace/src/sourcegraph.com/sourcegraph/go-diff/diff"
"github.com/dlclark/regexp2"
"gopkg.in/yaml.v2"
"io/ioutil"
"path/filepath"
"regexp"
"sourcegraph.com/sourcegraph/go-diff/diff"
"sync"
)

var (
mutex = &sync.Mutex{}
regexes = make(map[string]*regexp.Regexp)
regexes = make(map[string]*regexp2.Regexp)
)

// A Hound contains the local configuration filename and all regexp patterns
Expand Down Expand Up @@ -45,16 +45,28 @@ func (h *Hound) Sniff(fileName string, hunk *diff.Hunk, smells chan<- smell, don
defer func() { done <- true }()

rxFileName, _ := h.regexp(`^\w+\/`)
fileName = rxFileName.ReplaceAllString(fileName, "")
if _, ok := h.matchPatterns(h.Skips, []byte(fileName)); ok {
fileName, _ = rxFileName.Replace(fileName, "", -1, -1)
if _, ok := h.matchPatterns(h.Skips, fileName); ok {
return
}

var matches []*regexp2.Match
rxModLines, _ := h.regexp(`(?m)^\+\s*(.+)$`)
matches := rxModLines.FindAllSubmatch(hunk.Body, -1)
match, _ := rxModLines.FindStringMatch(string(hunk.Body))

if match != nil {
matches = append(matches, match)

m, _ := rxModLines.FindNextMatch(match)
for m != nil {
matches = append(matches, m)
m, _ = rxModLines.FindNextMatch(m)
}
}

for _, match := range matches {
line := match[1]
groups := match.Groups()
line := groups[1].Capture.String()

if pattern, warned := h.matchPatterns(h.Warns, line); warned {
smells <- smell{
Expand Down Expand Up @@ -94,7 +106,7 @@ func (h *Hound) parseConfig(config []byte) error {
// it is available, it will fetch from it. If it is not available, it
// will compile the pattern and store it in the cache. Returns a Regexp
// and an error.
func (h *Hound) regexp(pattern string) (*regexp.Regexp, error) {
func (h *Hound) regexp(pattern string) (*regexp2.Regexp, error) {
// Make sure that we don't encounter a race condition where multiple
// goroutines a
mutex.Lock()
Expand All @@ -104,7 +116,7 @@ func (h *Hound) regexp(pattern string) (*regexp.Regexp, error) {
return regexes[pattern], nil
}

r, err := regexp.Compile(pattern)
r, err := regexp2.Compile(pattern, 0)
if err == nil {
regexes[pattern] = r
}
Expand All @@ -113,18 +125,23 @@ func (h *Hound) regexp(pattern string) (*regexp.Regexp, error) {
}

// match matches a byte array against a regexp pattern and returns a bool.
func (h *Hound) match(pattern string, subject []byte) bool {
func (h *Hound) match(pattern string, subject string) bool {
r, err := h.regexp(pattern)
if err != nil {
panic(err)
}

return r.Match(subject)
res, err := r.MatchString(subject)
if err != nil {
return false
}

return res
}

// matchPatterns matches a byte array against an array of regexp patterns and
// returns the matched pattern and a bool.
func (h *Hound) matchPatterns(patterns []string, subject []byte) (string, bool) {
func (h *Hound) matchPatterns(patterns []string, subject string) (string, bool) {
for _, pattern := range patterns {
if match := h.match(pattern, subject); match {
return pattern, true
Expand Down
2 changes: 1 addition & 1 deletion hound_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/ezekg/git-hound/Godeps/_workspace/src/sourcegraph.com/sourcegraph/go-diff/diff"
"sourcegraph.com/sourcegraph/go-diff/diff"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion smell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type smell struct {
pattern string
fileName string
line []byte
line string
lineNum int32
severity int
}
Expand Down
Binary file added vendor/github.com/dlclark/regexp2/.DS_Store
Binary file not shown.
25 changes: 25 additions & 0 deletions vendor/github.com/dlclark/regexp2/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/dlclark/regexp2/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions vendor/github.com/dlclark/regexp2/ATTRIB

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/dlclark/regexp2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/github.com/dlclark/regexp2/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5d88998

Please sign in to comment.