/
main.go
106 lines (90 loc) · 2.3 KB
/
main.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
/*
Command git-hound is a Git plugin that helps prevent sensitive data from being committed
into a repository by sniffing potential commits against regular expressions
*/
package main
import (
"flag"
"fmt"
"github.com/ezekg/git-hound/Godeps/_workspace/src/github.com/fatih/color"
"github.com/ezekg/git-hound/Godeps/_workspace/src/sourcegraph.com/sourcegraph/go-diff/diff"
"os"
)
var (
version = "0.5.3"
showVersion = flag.Bool("v", false, "Show version")
noColor = flag.Bool("no-color", false, "Disable color output")
config = flag.String("config", ".githound.yml", "Hound config file")
bin = flag.String("bin", "git", "Executable binary to use for git command")
)
func main() {
defer func() {
if r := recover(); r != nil {
color.Red(fmt.Sprintf("error: %s\n", r))
os.Exit(1)
}
}()
flag.Parse()
if *showVersion {
fmt.Printf("%s\n", version)
os.Exit(0)
}
if *noColor {
color.NoColor = true
}
hound := &Hound{config: *config}
git := &Command{bin: *bin}
if ok := hound.New(); ok {
out, _ := git.Exec("diff", "-U0", "--staged")
fileDiffs, err := diff.ParseMultiFileDiff([]byte(out))
if err != nil {
color.Red(fmt.Sprintf("%s\n", err))
os.Exit(1)
}
severeSmellCount := 0
hunkCount := 0
smells := make(chan smell)
done := make(chan bool)
for _, fileDiff := range fileDiffs {
fileName := fileDiff.NewName
hunks := fileDiff.GetHunks()
for _, hunk := range hunks {
go func(hunk *diff.Hunk) {
defer func() {
if r := recover(); r != nil {
color.Red(fmt.Sprintf("%s\n", r))
os.Exit(1)
}
}()
hound.Sniff(fileName, hunk, smells, done)
}(hunk)
hunkCount++
}
}
for c := 0; c < hunkCount; {
select {
case s := <-smells:
if s.severity > 1 {
severeSmellCount++
}
switch s.severity {
case 1:
color.Yellow(fmt.Sprintf("warning: %s\n", s.String()))
case 2:
color.Red(fmt.Sprintf("failure: %s\n", s.String()))
default:
color.Red(fmt.Sprintf("error: unknown severity given - %d\n", s.severity))
}
case <-done:
c++
}
}
if severeSmellCount > 0 {
fmt.Printf("%d severe smell(s) detected - please fix them before you can commit\n", severeSmellCount)
os.Exit(1)
}
}
out, code := git.Exec(flag.Args()...)
fmt.Print(out)
os.Exit(code)
}