This commit is contained in:
yp05327 2023-06-09 07:46:27 +00:00
parent 388bc2600f
commit 7f2b5d0c8e
2 changed files with 25 additions and 11 deletions

View File

@ -36,12 +36,15 @@ func InitClassifier() error {
if err != nil {
return err
}
for _, lf := range licenseFiles {
data, err := options.License(lf)
if err != nil {
return err
if len(licenseFiles) > 0 {
for _, lf := range licenseFiles {
data, err := options.License(lf)
if err != nil {
return err
}
classifier.AddContent("License", lf, "license", data)
}
classifier.AddContent("License", lf, "license", data)
}
return nil
}
@ -240,12 +243,23 @@ func detectLicense(buf []byte) []string {
return nil
}
var licenses []string
results := classifier.Match(buf)
for _, r := range results.Matches {
matches := classifier.Match(buf)
licenseVariants := make(map[string][]string, len(matches.Matches))
for _, r := range matches.Matches {
if r.MatchType == "License" {
licenses = append(licenses, r.Name)
tag := fmt.Sprintf("%d-%d", r.StartLine, r.EndLine)
licenseVariants[tag] = append(licenseVariants[r.Variant], r.Name)
}
}
return licenses
var results []string
for _, licenses := range licenseVariants {
if len(licenses) == 1 {
results = append(results, licenses[0])
} else {
// TODO: reslove license detection conflict
results = append(results, licenses...)
}
}
return results
}

View File

@ -228,7 +228,7 @@ func Test_detectLicense(t *testing.T) {
assert.NoError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, detectLicense(tt.arg), "%s", tt.arg)
assert.Equalf(t, tt.want, detectLicense(tt.arg), "")
})
}
}