aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2025-03-10 06:40:37 +0800
committerGitHub <noreply@github.com>2025-03-10 06:40:37 +0800
commit7290bfaccb3f6cc14e8b422c19ada4d5548f331d (patch)
treeca8500de35fd12aeaf88a2a34e182fa2c1eb9df3 /services
parent3e53b011432b2e320397063df57992a90f9dc5e6 (diff)
downloadgitea-7290bfaccb3f6cc14e8b422c19ada4d5548f331d.tar.gz
gitea-7290bfaccb3f6cc14e8b422c19ada4d5548f331d.zip
Only keep popular licenses (#33832)
Fix #33467
Diffstat (limited to 'services')
-rw-r--r--services/repository/create.go2
-rw-r--r--services/repository/license.go49
-rw-r--r--services/repository/license_test.go10
3 files changed, 11 insertions, 50 deletions
diff --git a/services/repository/create.go b/services/repository/create.go
index 58912bdcd2..aa4c3905d4 100644
--- a/services/repository/create.go
+++ b/services/repository/create.go
@@ -310,7 +310,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
// update licenses
var licenses []string
if len(opts.License) > 0 {
- licenses = append(licenses, ConvertLicenseName(opts.License))
+ licenses = append(licenses, opts.License)
stdout, _, err := git.NewCommand("rev-parse", "HEAD").RunStdString(ctx, &git.RunOpts{Dir: repoPath})
if err != nil {
diff --git a/services/repository/license.go b/services/repository/license.go
index 2453be3c87..8622911fa2 100644
--- a/services/repository/license.go
+++ b/services/repository/license.go
@@ -14,7 +14,6 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/graceful"
- "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/options"
"code.gitea.io/gitea/modules/queue"
@@ -25,7 +24,6 @@ import (
var (
classifier *licenseclassifier.Classifier
LicenseFileName = "LICENSE"
- licenseAliases map[string]string
// licenseUpdaterQueue represents a queue to handle update repo licenses
licenseUpdaterQueue *queue.WorkerPoolQueue[*LicenseUpdaterOptions]
@@ -38,34 +36,6 @@ func AddRepoToLicenseUpdaterQueue(opts *LicenseUpdaterOptions) error {
return licenseUpdaterQueue.Push(opts)
}
-func loadLicenseAliases() error {
- if licenseAliases != nil {
- return nil
- }
-
- data, err := options.AssetFS().ReadFile("license", "etc", "license-aliases.json")
- if err != nil {
- return err
- }
- err = json.Unmarshal(data, &licenseAliases)
- if err != nil {
- return err
- }
- return nil
-}
-
-func ConvertLicenseName(name string) string {
- if err := loadLicenseAliases(); err != nil {
- return name
- }
-
- v, ok := licenseAliases[name]
- if ok {
- return v
- }
- return name
-}
-
func InitLicenseClassifier() error {
// threshold should be 0.84~0.86 or the test will be failed
classifier = licenseclassifier.NewClassifier(.85)
@@ -74,20 +44,13 @@ func InitLicenseClassifier() error {
return err
}
- existLicense := make(container.Set[string])
- if len(licenseFiles) > 0 {
- for _, licenseFile := range licenseFiles {
- licenseName := ConvertLicenseName(licenseFile)
- if existLicense.Contains(licenseName) {
- continue
- }
- existLicense.Add(licenseName)
- data, err := options.License(licenseFile)
- if err != nil {
- return err
- }
- classifier.AddContent("License", licenseFile, licenseName, data)
+ for _, licenseFile := range licenseFiles {
+ licenseName := licenseFile
+ data, err := options.License(licenseFile)
+ if err != nil {
+ return err
}
+ classifier.AddContent("License", licenseName, licenseName, data)
}
return nil
}
diff --git a/services/repository/license_test.go b/services/repository/license_test.go
index 9d3e0f36e3..9e74a268f5 100644
--- a/services/repository/license_test.go
+++ b/services/repository/license_test.go
@@ -11,6 +11,7 @@ import (
repo_module "code.gitea.io/gitea/modules/repository"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func Test_detectLicense(t *testing.T) {
@@ -33,9 +34,7 @@ func Test_detectLicense(t *testing.T) {
},
}
- repo_module.LoadRepoConfig()
- err := loadLicenseAliases()
- assert.NoError(t, err)
+ require.NoError(t, repo_module.LoadRepoConfig())
for _, licenseName := range repo_module.Licenses {
license, err := repo_module.GetLicense(licenseName, &repo_module.LicenseValues{
Owner: "Gitea",
@@ -48,12 +47,11 @@ func Test_detectLicense(t *testing.T) {
tests = append(tests, DetectLicenseTest{
name: fmt.Sprintf("single license test: %s", licenseName),
arg: string(license),
- want: []string{ConvertLicenseName(licenseName)},
+ want: []string{licenseName},
})
}
- err = InitLicenseClassifier()
- assert.NoError(t, err)
+ require.NoError(t, InitLicenseClassifier())
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
license, err := detectLicense(strings.NewReader(tt.arg))