aboutsummaryrefslogtreecommitdiff
path: root/file.go
diff options
context:
space:
mode:
Diffstat (limited to 'file.go')
-rw-r--r--file.go58
1 files changed, 17 insertions, 41 deletions
diff --git a/file.go b/file.go
index 714a6c5..75842dd 100644
--- a/file.go
+++ b/file.go
@@ -7,8 +7,10 @@ import (
"bytes"
"fmt"
"os"
+ "path/filepath"
"regexp"
"slices"
+ "strings"
libos "git.sr.ht/~shulhan/pakakeh.go/lib/os"
)
@@ -116,55 +118,29 @@ func (f *file) initLines(content []byte, maxLine int) (err error) {
}
func (f *file) scan(conv *SPDXConv) {
- f.detectComment()
- if f.isUnknown {
+ f.detectComment(&conv.cfg)
+ if f.isBinary || f.isUnknown {
return
}
f.scanLicenseID(conv)
f.scanCopyrightText(conv)
}
-func (f *file) detectComment() {
- if bytes.HasPrefix(f.topLines[0], []byte(`#!`)) {
+// detectComment get comment prefix and suffix using the "match-file-comment"
+// pattern in the configuration.
+func (f *file) detectComment(cfg *config) {
+ if len(f.topLines) != 0 && bytes.HasPrefix(f.topLines[0], []byte(`#!`)) {
f.hasSheBang = true
- f.commentPrefix = `# `
- return
}
- for _, line := range f.topLines {
- if bytes.HasPrefix(line, []byte(`#`)) {
- f.commentPrefix = `# `
- return
- }
- if bytes.HasPrefix(line, []byte(`//`)) {
- f.commentPrefix = `// `
- return
- }
- if bytes.HasPrefix(line, []byte(`/*`)) {
- f.commentPrefix = `// `
- return
- }
- if bytes.HasPrefix(line, []byte(`<!--`)) {
- f.commentPrefix = `<!-- `
- f.commentSuffix = ` -->`
- return
- }
- }
- for _, line := range f.bottomLines {
- if bytes.HasPrefix(line, []byte(`#`)) {
- f.commentPrefix = `# `
- return
- }
- if bytes.HasPrefix(line, []byte(`//`)) {
- f.commentPrefix = `// `
- return
- }
- if bytes.HasPrefix(line, []byte(`/*`)) {
- f.commentPrefix = `// `
- return
- }
- if bytes.HasPrefix(line, []byte(`<!--`)) {
- f.commentPrefix = `<!-- `
- f.commentSuffix = ` -->`
+ var basename = strings.ToLower(filepath.Base(f.path))
+ for _, mfc := range cfg.MatchFileComment {
+ if mfc.rePattern.MatchString(basename) {
+ if mfc.isDirectLicense() {
+ f.isBinary = true
+ return
+ }
+ f.commentPrefix = mfc.Prefix
+ f.commentSuffix = mfc.Suffix
return
}
}