-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
259 lines (247 loc) · 5.5 KB
/
main.go
File metadata and controls
259 lines (247 loc) · 5.5 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"fmt"
"os"
"runtime/pprof"
"github.com/boyter/scc/v3/processor"
"github.com/spf13/cobra"
)
var cpuProfile string
func main() {
rootCmd := &cobra.Command{
Use: "dcd",
Short: "dcd",
Long: fmt.Sprintf("dcd\nVersion %s\nBen Boyter <ben@boyter.org>", version),
Version: version,
Run: func(cmd *cobra.Command, args []string) {
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create CPU profile: %s\n", err)
os.Exit(1)
}
if err := pprof.StartCPUProfile(f); err != nil {
f.Close()
fmt.Fprintf(os.Stderr, "could not start CPU profile: %s\n", err)
os.Exit(1)
}
defer pprof.StopCPUProfile()
defer f.Close()
}
// PBM mode: if any PBM flag is set, validate all three are present
pbmFlags := 0
if pbmFileA != "" {
pbmFlags++
}
if pbmFileB != "" {
pbmFlags++
}
if pbmOutput != "" {
pbmFlags++
}
if pbmFlags > 0 && pbmFlags < 3 {
fmt.Println("error: --pbm-file-a, --pbm-file-b, and --pbm-output must all be specified together")
os.Exit(1)
}
if pbmFlags == 3 {
processPBM()
return
}
if (ignoreBlocksStart != "") != (ignoreBlocksEnd != "") {
fmt.Println("error: --ignore-blocks-start and --ignore-blocks-end must both be specified together")
os.Exit(1)
}
if codeOnly {
ignoreComments = true
ignoreStrings = true
}
sccFilterActive = ignoreComments || ignoreStrings
if sccFilterActive {
processor.ProcessConstants()
}
dirFilePaths = args
if len(dirFilePaths) == 0 {
dirFilePaths = append(dirFilePaths, ".")
}
duplicateCount := process()
if cpuProfile != "" {
pprof.StopCPUProfile()
}
if duplicateThreshold >= 0 && duplicateCount > int64(duplicateThreshold) {
os.Exit(1)
}
},
}
flags := rootCmd.PersistentFlags()
flags.IntVarP(
&minMatchLength,
"match-length",
"m",
6,
"min match length",
)
flags.BoolVar(
&processSameFile,
"process-same-file",
false,
"",
)
flags.StringSliceVarP(
&allowListExtensions,
"include-ext",
"i",
[]string{},
"limit to file extensions [comma separated list: e.g. go,java,js]",
)
flags.BoolVar(
&ignoreIgnoreFile,
"no-ignore",
false,
"disables .ignore file logic",
)
flags.BoolVar(
&ignoreGitIgnore,
"no-gitignore",
false,
"disables .gitignore file logic",
)
flags.StringSliceVarP(
&locationExcludePattern,
"exclude-pattern",
"x",
[]string{},
"file and directory locations matching case sensitive patterns will be ignored [comma separated list: e.g. vendor,_test.go]",
)
flags.IntVar(
&minifiedLineByteLength,
"min-line-length",
255,
"number of bytes per average line for file to be considered minified",
)
flags.Int64Var(
&maxReadSizeBytes,
"max-read-size-bytes",
10000000,
"number of bytes to read into a file with the remaining content ignored",
)
flags.BoolVarP(
&verbose,
"verbose",
"v",
false,
"verbose output",
)
flags.Uint8VarP(
&fuzzValue,
"fuzz",
"f",
0,
"fuzzy value where higher numbers allow increasingly fuzzy lines to match, values 0-255 where 0 indicates exact match",
)
flags.IntVarP(
&gapTolerance,
"gap-tolerance",
"g",
0,
"allow gaps of up to N lines when matching duplicate blocks, bridging over inserted, deleted, or modified lines (0 = no gaps allowed)",
)
flags.IntVar(
&maxGapBridges,
"max-gap-bridges",
1,
"maximum number of gap bridges allowed per duplicate match (increase for noisier but more permissive matching)",
)
flags.IntVar(
&maxHoleSize,
"max-hole-size",
0,
"allow up to N consecutive modified lines (holes) within a duplicate diagonal (0 = no holes allowed)",
)
flags.BoolVar(
&duplicatesBothWays,
"duplicates-both-ways",
false,
"report duplicates from both file perspectives (default reports each pair once)",
)
flags.StringVar(
&singleFilePath,
"file",
"",
"compare a single file against the rest of the codebase",
)
flags.StringVar(
&pbmFileA,
"pbm-file-a",
"",
"first file to compare for PBM scatter plot output",
)
flags.StringVar(
&pbmFileB,
"pbm-file-b",
"",
"second file to compare for PBM scatter plot output",
)
flags.StringVar(
&pbmOutput,
"pbm-output",
"",
"output path for PBM scatter plot file",
)
flags.StringVar(
&ignoreBlocksStart,
"ignore-blocks-start",
"",
"marker string to start ignoring lines (e.g. duplicate-disable)",
)
flags.StringVar(
&ignoreBlocksEnd,
"ignore-blocks-end",
"",
"marker string to stop ignoring lines (e.g. duplicate-enable)",
)
flags.StringVar(
&formatOutput,
"format",
"",
"output format: text (default), json, or html",
)
flags.IntVar(
&duplicateThreshold,
"duplicate-threshold",
0,
"exit with code 1 when total duplicate lines exceed this threshold (0 to fail on any duplicates, -1 to disable)",
)
flags.BoolVar(
&ignoreComments,
"ignore-comments",
false,
"exclude comment lines from duplicate detection (uses scc language detection)",
)
flags.BoolVar(
&ignoreStrings,
"ignore-strings",
false,
"exclude string literal content from duplicate detection (uses scc language detection)",
)
flags.BoolVar(
&codeOnly,
"code-only",
false,
"shorthand for --ignore-comments --ignore-strings",
)
flags.BoolVar(
&showProgress,
"progress",
false,
"show progress on stderr",
)
flags.StringVar(
&cpuProfile,
"cpu-profile",
"",
"write CPU profile to file (for use with go tool pprof or PGO)",
)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}