forked from qax-os/goreporter
/
copycheck.go
executable file
·195 lines (176 loc) · 4.35 KB
/
copycheck.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package copycheck
import (
"bufio"
"flag"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/360EntSecGroup-Skylar/goreporter/linters/copycheck/job"
"github.com/360EntSecGroup-Skylar/goreporter/linters/copycheck/output"
"github.com/360EntSecGroup-Skylar/goreporter/linters/copycheck/syntax"
"github.com/golang/glog"
)
const defaultThreshold = 15
var (
//flag.Bool("vendor", false, "check files in vendor directory")
vendor = false
//flag.Bool("verbose", false, "explain what is being done")
verbose = false
//flag.Int("threshold", defaultThreshold, "minimum token sequence as a clone")
threshold = 50
//flag.Bool("files", false, "files names from stdin")
files = false
// flag.Bool("html", false, "html output")
html = false
//flag.Bool("plumbing", false, "plumbing output for consumption by scripts or tools")
plumbing = false
)
const (
vendorDirPrefix = "vendor" + string(filepath.Separator)
vendorDirInPath = string(filepath.Separator) + "vendor" + string(filepath.Separator)
)
func CopyCheck(projectPath string, except string) (result [][]string) {
flag.Parse()
if html && plumbing {
glog.Errorln("you can have either plumbing or HTML output")
return result
}
paths := []string{projectPath}
if verbose {
glog.Errorln("Building suffix tree")
}
schan := job.Parse(filesFeed(paths, except))
t, data, done := job.BuildTree(schan)
<-done
// finish stream
t.Update(&syntax.Node{Type: -1})
if verbose {
glog.Errorln("Searching for clones")
}
mchan := t.FindDuplOver(threshold)
duplChan := make(chan syntax.Match)
go func() {
for m := range mchan {
match := syntax.FindSyntaxUnits(*data, m, threshold)
if len(match.Frags) > 0 {
duplChan <- match
}
}
close(duplChan)
}()
return printDupls(duplChan)
}
func filesFeed(paths []string, except string) chan string {
if files {
fchan := make(chan string)
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
f := s.Text()
f = strings.TrimPrefix(f, "./")
fchan <- f
}
close(fchan)
}()
return fchan
}
return crawlPaths(paths, except)
}
func crawlPaths(paths []string, except string) chan string {
fchan := make(chan string)
go func() {
for _, path := range paths {
info, err := os.Lstat(path)
if err != nil {
glog.Errorln(err)
break
}
if !info.IsDir() {
fchan <- path
continue
}
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if checkExcept(path, except) {
return nil
}
if !vendor && (strings.HasPrefix(path, vendorDirPrefix) ||
strings.Contains(path, vendorDirInPath)) {
return nil
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") {
fchan <- path
}
return nil
})
}
close(fchan)
}()
return fchan
}
func checkExcept(path, except string) bool {
if except == "" || except == " " {
return false
}
excepts := strings.Split(except, ",")
for _, val := range excepts {
if val != "" && val != " " {
if strings.Contains(path, val) {
return true
}
}
}
return false
}
type LocalFileReader struct{}
func (LocalFileReader) ReadFile(node *syntax.Node) ([]byte, error) {
return ioutil.ReadFile(node.Filename)
}
func printDupls(duplChan <-chan syntax.Match) (copys [][]string) {
groups := make(map[string][][]*syntax.Node)
for dupl := range duplChan {
groups[dupl.Hash] = append(groups[dupl.Hash], dupl.Frags...)
}
keys := make([]string, 0, len(groups))
for k := range groups {
keys = append(keys, k)
}
sort.Strings(keys)
p := getPrinter()
for _, k := range keys {
uniq := unique(groups[k])
if len(uniq) > 1 {
// p.Print(uniq)
copys = append(copys, p.SPrint(uniq))
}
}
// p.Finish()
return copys
}
func getPrinter() output.Printer {
fr := LocalFileReader{}
if html {
return output.NewHtmlPrinter(os.Stdout, fr)
} else if plumbing {
return output.NewPlumbingPrinter(os.Stdout, fr)
}
return output.NewTextPrinter(os.Stdout, fr)
}
func unique(group [][]*syntax.Node) [][]*syntax.Node {
fileMap := make(map[string]map[int]struct{})
var newGroup [][]*syntax.Node
for _, seq := range group {
node := seq[0]
file, ok := fileMap[node.Filename]
if !ok {
file = make(map[int]struct{})
fileMap[node.Filename] = file
}
if _, ok := file[node.Pos]; !ok {
file[node.Pos] = struct{}{}
newGroup = append(newGroup, seq)
}
}
return newGroup
}