Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --fullpath option #100

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/gocloc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CmdOptions struct {
NotMatch string `long:"not-match" description:"exclude file name (regex)"`
MatchDir string `long:"match-d" description:"include dir name (regex)"`
NotMatchDir string `long:"not-match-d" description:"exclude dir name (regex)"`
Fullpath bool `long:"fullpath" description:"apply match/not-match options to full file paths instead of base names"`
Debug bool `long:"debug" description:"dump debug log for developer"`
SkipDuplicated bool `long:"skip-duplicated" description:"skip duplicated files"`
ShowLang bool `long:"show-lang" description:"print about all languages and extensions"`
Expand Down Expand Up @@ -289,6 +290,7 @@ func main() {

clocOpts.Debug = opts.Debug
clocOpts.SkipDuplicated = opts.SkipDuplicated
clocOpts.Fullpath = opts.Fullpath

processor := gocloc.NewProcessor(languages, clocOpts)
result, err := processor.Analyze(paths)
Expand Down
1 change: 1 addition & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ClocOptions struct {
ReMatch *regexp.Regexp
ReNotMatchDir *regexp.Regexp
ReMatchDir *regexp.Regexp
Fullpath bool

// OnCode is triggered for each line of code.
OnCode func(line string)
Expand Down
9 changes: 7 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {

func checkOptionMatch(path string, info os.FileInfo, opts *ClocOptions) bool {
// check match directory & file options
if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(info.Name()) {
targetFile := info.Name()
if opts.Fullpath {
targetFile = path
}

Comment on lines 83 to +89

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should do filepath.Clean(path) to handle cases where ./dir/file.py is equal to dir/file.py?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path will be the value specified from gocloc's CLI options or the value of filepath.Walk in the specified directory.
If it is a value specified from the CLI option of gocloc, it should not be Cleaned because it will be a user input.
In the case of the value of filepath.Walk, it will be a Cleaned value and should not be Cleaned.

So we think Clean is not necessary here.

if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(targetFile) {
return false
}
if opts.ReMatch != nil && !opts.ReMatch.MatchString(info.Name()) {
if opts.ReMatch != nil && !opts.ReMatch.MatchString(targetFile) {
return false
}

Expand Down
38 changes: 38 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,42 @@ func TestCheckOptionMatch(t *testing.T) {
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is not ignore")
}

t.Run("--match option", func(t *testing.T) {
opts = &ClocOptions{
ReMatch: regexp.MustCompile("app.py"),
}
fi = MockFileInfo{FileName: "app.py", IsDirectory: false}
if !checkOptionMatch("test_dir/app.py", fi, opts) {
t.Errorf("invalid logic: match is not ignore")
}
})

t.Run("--match option with --fullpath option", func(t *testing.T) {
opts = &ClocOptions{
ReMatch: regexp.MustCompile("test_dir/app.py"),
Fullpath: true,
}
fi = MockFileInfo{FileName: "app.py", IsDirectory: false}
if !checkOptionMatch("test_dir/app.py", fi, opts) {
t.Errorf("invalid logic: match(with fullpath) is not ignore")
}
if checkOptionMatch("app.py", fi, opts) {
t.Errorf("invalid logic: match(with fullpath) is ignore")
}
})

t.Run("--not-match option with --fullpath option", func(t *testing.T) {
opts = &ClocOptions{
ReNotMatch: regexp.MustCompile("test_dir/app.py"),
Fullpath: true,
}
fi = MockFileInfo{FileName: "app.py", IsDirectory: false}
if checkOptionMatch("test_dir/app.py", fi, opts) {
t.Errorf("invalid logic: not-match(with fullpath) is ignore")
}
if !checkOptionMatch("app.py", fi, opts) {
t.Errorf("invalid logic: not-match(with fullpath) is not ignore")
}
})
}
Loading