/
hound.go
135 lines (113 loc) · 3.32 KB
/
hound.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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"
"io/ioutil"
"path/filepath"
"regexp"
"sync"
)
var (
mutex = &sync.Mutex{}
regexes = make(map[string]*regexp.Regexp)
)
// A Hound contains the local configuration filename and all regexp patterns
// used for sniffing git-diffs.
type Hound struct {
Fails []string `yaml:"fail"`
Warns []string `yaml:"warn"`
Skips []string `yaml:"skip"`
config string
}
// New initializes a new Hound instance by parsing regexp patterns from a
// local configuration file and returns a status bool.
func (h *Hound) New() bool {
config, err := h.loadConfig()
if err != nil {
return false
}
err = h.parseConfig(config)
if err != nil {
panic(err)
}
return true
}
// Sniff matches the passed git-diff hunk against all regexp patterns that
// were parsed from the local configuration.
func (h *Hound) Sniff(fileName string, hunk *diff.Hunk, smells chan<- smell, done chan<- bool) {
defer func() { done <- true }()
rxFileName, _ := h.regexp(`^\w+\/`)
fileName = rxFileName.ReplaceAllString(fileName, "")
if _, ok := h.matchPatterns(h.Skips, []byte(fileName)); ok {
return
}
rxModLines, _ := h.regexp(`(?m)^\+\s*(.+)$`)
matches := rxModLines.FindAllSubmatch(hunk.Body, -1)
for _, match := range matches {
line := match[1]
if pattern, warned := h.matchPatterns(h.Warns, line); warned {
smells <- smell{
pattern: pattern,
fileName: fileName,
line: line,
lineNum: hunk.NewStartLine,
severity: 1,
}
}
if pattern, failed := h.matchPatterns(h.Fails, line); failed {
smells <- smell{
pattern: pattern,
fileName: fileName,
line: line,
lineNum: hunk.NewStartLine,
severity: 2,
}
}
}
}
// loadConfig reads a local configuration file of regexp patterns and returns
// the contents of the file.
func (h *Hound) loadConfig() ([]byte, error) {
filename, _ := filepath.Abs(h.config)
return ioutil.ReadFile(filename)
}
// parseConfig parses a configuration byte array and returns an error.
func (h *Hound) parseConfig(config []byte) error {
return yaml.Unmarshal(config, h)
}
// regexp looks for the specified pattern in Hound's regexes cache, and if
// 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) {
// Make sure that we don't encounter a race condition where multiple
// goroutines a
mutex.Lock()
defer mutex.Unlock()
if regexes[pattern] != nil {
return regexes[pattern], nil
}
r, err := regexp.Compile(pattern)
if err == nil {
regexes[pattern] = r
}
return r, err
}
// match matches a byte array against a regexp pattern and returns a bool.
func (h *Hound) match(pattern string, subject []byte) bool {
r, err := h.regexp(pattern)
if err != nil {
panic(err)
}
return r.Match(subject)
}
// 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) {
for _, pattern := range patterns {
if match := h.match(pattern, subject); match {
return pattern, true
}
}
return "", false
}