remove GetBlobAll

This commit is contained in:
yp05327 2023-06-14 08:51:41 +00:00
parent c831ee31bf
commit ae27d02aa8
3 changed files with 19 additions and 21 deletions

View File

@ -168,16 +168,16 @@ func detectLicenseByEntry(file *git.TreeEntry) ([]string, error) {
}
blob := file.Blob()
contentBuf, err := blob.GetBlobAll()
content, err := blob.GetBlobContent(setting.UI.MaxDisplayFileSize)
if err != nil {
return nil, fmt.Errorf("GetBlobAll: %w", err)
}
return detectLicense(contentBuf), nil
return detectLicense(content), nil
}
// detectLicense returns the licenses detected by the given content buff
func detectLicense(buf []byte) []string {
if len(buf) == 0 {
func detectLicense(content string) []string {
if len(content) == 0 {
return nil
}
if classifier == nil {
@ -185,7 +185,11 @@ func detectLicense(buf []byte) []string {
return nil
}
matches := classifier.Match(buf)
matches, err := classifier.MatchFrom(strings.NewReader(content))
if err != nil {
log.Error("licenseclassifier.MatchFrom: %v", err)
return nil
}
var results []string
for _, r := range matches.Matches {
if r.MatchType == "License" {

View File

@ -34,17 +34,6 @@ func (b *Blob) GetBlobContent(limit int64) (string, error) {
return string(buf), err
}
// GetBlobAll Gets the all content of the blob as bytes
func (b *Blob) GetBlobAll() ([]byte, error) {
dataRc, err := b.DataAsync()
if err != nil {
return nil, err
}
buf, _ := io.ReadAll(dataRc)
_ = dataRc.Close()
return buf, nil
}
// GetBlobLineCount gets line count of the blob
func (b *Blob) GetBlobLineCount() (int, error) {
reader, err := b.DataAsync()

View File

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/options"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
licenseclassifier "github.com/google/licenseclassifier/v2"
@ -263,16 +264,16 @@ func detectLicenseByEntry(file *git.TreeEntry) ([]string, error) {
}
blob := file.Blob()
contentBuf, err := blob.GetBlobAll()
content, err := blob.GetBlobContent(setting.UI.MaxDisplayFileSize)
if err != nil {
return nil, fmt.Errorf("GetBlobAll: %w", err)
}
return detectLicense(contentBuf), nil
return detectLicense(content), nil
}
// detectLicense returns the licenses detected by the given content buff
func detectLicense(buf []byte) []string {
if len(buf) == 0 {
func detectLicense(content string) []string {
if len(content) == 0 {
return nil
}
if classifier == nil {
@ -280,7 +281,11 @@ func detectLicense(buf []byte) []string {
return nil
}
matches := classifier.Match(buf)
matches, err := classifier.MatchFrom(strings.NewReader(content))
if err != nil {
log.Error("licenseclassifier.MatchFrom: %v", err)
return nil
}
var results []string
for _, r := range matches.Matches {
if r.MatchType == "License" {