Skip to content

Commit 0815d00

Browse files
pascaldekloehhatto
authored andcommitted
File matching options.
1 parent 3ddbb04 commit 0815d00

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

cmd/gocloc/main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type CmdOptions struct {
4747
OutputType string `long:"output-type" default:"default" description:"output type [values: default,cloc-xml,sloccount,json]"`
4848
ExcludeExt string `long:"exclude-ext" description:"exclude file name extensions (separated commas)"`
4949
IncludeLang string `long:"include-lang" description:"include language name (separated commas)"`
50+
Match string `long:"match" description:"include file name (regex)"`
51+
NotMatch string `long:"not-match" description:"exclude file name (regex)"`
5052
MatchDir string `long:"match-d" description:"include dir name (regex)"`
5153
NotMatchDir string `long:"not-match-d" description:"exclude dir name (regex)"`
5254
Debug bool `long:"debug" description:"dump debug log for developer"`
@@ -239,13 +241,19 @@ func main() {
239241
}
240242
}
241243

242-
// setup option for not match directory
243-
if opts.NotMatchDir != "" {
244-
clocOpts.ReNotMatchDir = regexp.MustCompile(opts.NotMatchDir)
244+
// directory and file matching options
245+
if opts.Match != "" {
246+
clocOpts.ReMatch = regexp.MustCompile(opts.Match)
247+
}
248+
if opts.NotMatch != "" {
249+
clocOpts.ReNotMatch = regexp.MustCompile(opts.NotMatch)
245250
}
246251
if opts.MatchDir != "" {
247252
clocOpts.ReMatchDir = regexp.MustCompile(opts.MatchDir)
248253
}
254+
if opts.NotMatchDir != "" {
255+
clocOpts.ReNotMatchDir = regexp.MustCompile(opts.NotMatchDir)
256+
}
249257

250258
// setup option for include languages
251259
for _, lang := range strings.Split(opts.IncludeLang, ",") {

option.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type ClocOptions struct {
88
SkipDuplicated bool
99
ExcludeExts map[string]struct{}
1010
IncludeLangs map[string]struct{}
11+
ReNotMatch *regexp.Regexp
12+
ReMatch *regexp.Regexp
1113
ReNotMatchDir *regexp.Regexp
1214
ReMatchDir *regexp.Regexp
1315

utils.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {
8181
return false
8282
}
8383

84-
func checkOptionMatch(path string, opts *ClocOptions) bool {
84+
func checkOptionMatch(path string, info os.FileInfo, opts *ClocOptions) bool {
85+
// check match directory & file options
86+
if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(info.Name()) {
87+
return false
88+
}
89+
if opts.ReMatch != nil && !opts.ReMatch.MatchString(info.Name()) {
90+
return false
91+
}
92+
8593
dir := filepath.Dir(path)
8694
if opts.ReNotMatchDir != nil && opts.ReNotMatchDir.MatchString(dir) {
8795
return false
@@ -109,8 +117,8 @@ func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions)
109117
return nil
110118
}
111119

112-
// check not-match directory
113-
if match := checkOptionMatch(path, opts); !match {
120+
// check match & not-match directory
121+
if match := checkOptionMatch(path, info, opts); !match {
114122
return nil
115123
}
116124

utils_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"regexp"
66
"testing"
7+
"time"
78

89
"github.com/spf13/afero"
910
)
@@ -51,37 +52,55 @@ func TestCheckDefaultIgnore(t *testing.T) {
5152
}
5253
}
5354

55+
type MockFileInfo struct {
56+
FileName string
57+
IsDirectory bool
58+
}
59+
60+
func (mfi MockFileInfo) Name() string { return mfi.FileName }
61+
func (mfi MockFileInfo) Size() int64 { return int64(8) }
62+
func (mfi MockFileInfo) Mode() os.FileMode { return os.ModePerm }
63+
func (mfi MockFileInfo) ModTime() time.Time { return time.Now() }
64+
func (mfi MockFileInfo) IsDir() bool { return mfi.IsDirectory }
65+
func (mfi MockFileInfo) Sys() interface{} { return nil }
66+
5467
func TestCheckOptionMatch(t *testing.T) {
5568
opts := &ClocOptions{}
56-
if !checkOptionMatch("/", opts) {
69+
fi := MockFileInfo{FileName: "/", IsDirectory: true}
70+
if !checkOptionMatch("/", fi, opts) {
5771
t.Errorf("invalid logic: renotmatchdir is nil")
5872
}
5973

6074
opts.ReNotMatchDir = regexp.MustCompile("thisisdir-not-match")
61-
if !checkOptionMatch("/thisisdir/one.go", opts) {
75+
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
76+
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
6277
t.Errorf("invalid logic: renotmatchdir is nil")
6378
}
6479

6580
opts.ReNotMatchDir = regexp.MustCompile("thisisdir")
66-
if checkOptionMatch("/thisisdir/one.go", opts) {
81+
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
82+
if checkOptionMatch("/thisisdir/one.go", fi, opts) {
6783
t.Errorf("invalid logic: renotmatchdir is ignore")
6884
}
6985

7086
opts = &ClocOptions{}
7187
opts.ReMatchDir = regexp.MustCompile("thisisdir")
72-
if !checkOptionMatch("/thisisdir/one.go", opts) {
88+
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
89+
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
7390
t.Errorf("invalid logic: renotmatchdir is not ignore")
7491
}
7592

7693
opts.ReMatchDir = regexp.MustCompile("thisisdir-not-match")
77-
if checkOptionMatch("/thisisdir/one.go", opts) {
94+
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
95+
if checkOptionMatch("/thisisdir/one.go", fi, opts) {
7896
t.Errorf("invalid logic: renotmatchdir is ignore")
7997
}
8098

8199
opts = &ClocOptions{}
82100
opts.ReNotMatchDir = regexp.MustCompile("thisisdir-not-match")
83101
opts.ReMatchDir = regexp.MustCompile("thisisdir")
84-
if !checkOptionMatch("/thisisdir/one.go", opts) {
102+
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
103+
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
85104
t.Errorf("invalid logic: renotmatchdir is not ignore")
86105
}
87106
}

0 commit comments

Comments
 (0)