Skip to content

Commit 59331e3

Browse files
authored
Merge pull request #100 from hhatto/add-fullpath-option
Add --fullpath option
2 parents e26badb + 81fc668 commit 59331e3

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

cmd/gocloc/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type CmdOptions struct {
5050
NotMatch string `long:"not-match" description:"exclude file name (regex)"`
5151
MatchDir string `long:"match-d" description:"include dir name (regex)"`
5252
NotMatchDir string `long:"not-match-d" description:"exclude dir name (regex)"`
53+
Fullpath bool `long:"fullpath" description:"apply match/not-match options to full file paths instead of base names"`
5354
Debug bool `long:"debug" description:"dump debug log for developer"`
5455
SkipDuplicated bool `long:"skip-duplicated" description:"skip duplicated files"`
5556
ShowLang bool `long:"show-lang" description:"print about all languages and extensions"`
@@ -289,6 +290,7 @@ func main() {
289290

290291
clocOpts.Debug = opts.Debug
291292
clocOpts.SkipDuplicated = opts.SkipDuplicated
293+
clocOpts.Fullpath = opts.Fullpath
292294

293295
processor := gocloc.NewProcessor(languages, clocOpts)
294296
result, err := processor.Analyze(paths)

option.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ClocOptions struct {
1212
ReMatch *regexp.Regexp
1313
ReNotMatchDir *regexp.Regexp
1414
ReMatchDir *regexp.Regexp
15+
Fullpath bool
1516

1617
// OnCode is triggered for each line of code.
1718
OnCode func(line string)

utils.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {
8282

8383
func checkOptionMatch(path string, info os.FileInfo, opts *ClocOptions) bool {
8484
// check match directory & file options
85-
if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(info.Name()) {
85+
targetFile := info.Name()
86+
if opts.Fullpath {
87+
targetFile = path
88+
}
89+
90+
if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(targetFile) {
8691
return false
8792
}
88-
if opts.ReMatch != nil && !opts.ReMatch.MatchString(info.Name()) {
93+
if opts.ReMatch != nil && !opts.ReMatch.MatchString(targetFile) {
8994
return false
9095
}
9196

utils_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,42 @@ func TestCheckOptionMatch(t *testing.T) {
105105
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
106106
t.Errorf("invalid logic: renotmatchdir is not ignore")
107107
}
108+
109+
t.Run("--match option", func(t *testing.T) {
110+
opts = &ClocOptions{
111+
ReMatch: regexp.MustCompile("app.py"),
112+
}
113+
fi = MockFileInfo{FileName: "app.py", IsDirectory: false}
114+
if !checkOptionMatch("test_dir/app.py", fi, opts) {
115+
t.Errorf("invalid logic: match is not ignore")
116+
}
117+
})
118+
119+
t.Run("--match option with --fullpath option", func(t *testing.T) {
120+
opts = &ClocOptions{
121+
ReMatch: regexp.MustCompile("test_dir/app.py"),
122+
Fullpath: true,
123+
}
124+
fi = MockFileInfo{FileName: "app.py", IsDirectory: false}
125+
if !checkOptionMatch("test_dir/app.py", fi, opts) {
126+
t.Errorf("invalid logic: match(with fullpath) is not ignore")
127+
}
128+
if checkOptionMatch("app.py", fi, opts) {
129+
t.Errorf("invalid logic: match(with fullpath) is ignore")
130+
}
131+
})
132+
133+
t.Run("--not-match option with --fullpath option", func(t *testing.T) {
134+
opts = &ClocOptions{
135+
ReNotMatch: regexp.MustCompile("test_dir/app.py"),
136+
Fullpath: true,
137+
}
138+
fi = MockFileInfo{FileName: "app.py", IsDirectory: false}
139+
if checkOptionMatch("test_dir/app.py", fi, opts) {
140+
t.Errorf("invalid logic: not-match(with fullpath) is ignore")
141+
}
142+
if !checkOptionMatch("app.py", fi, opts) {
143+
t.Errorf("invalid logic: not-match(with fullpath) is not ignore")
144+
}
145+
})
108146
}

0 commit comments

Comments
 (0)