Skip to content

Commit 7eb9935

Browse files
Merge branch 'main' into pacman-packages
2 parents 8b63213 + f9a9b08 commit 7eb9935

File tree

142 files changed

+7056
-1209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+7056
-1209
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ TEST_PGSQL_DBNAME ?= testgitea
179179
TEST_PGSQL_USERNAME ?= postgres
180180
TEST_PGSQL_PASSWORD ?= postgres
181181
TEST_PGSQL_SCHEMA ?= gtestschema
182+
TEST_MINIO_ENDPOINT ?= minio:9000
182183
TEST_MSSQL_HOST ?= mssql:1433
183184
TEST_MSSQL_DBNAME ?= gitea
184185
TEST_MSSQL_USERNAME ?= sa
@@ -574,6 +575,7 @@ generate-ini-pgsql:
574575
-e 's|{{TEST_PGSQL_USERNAME}}|${TEST_PGSQL_USERNAME}|g' \
575576
-e 's|{{TEST_PGSQL_PASSWORD}}|${TEST_PGSQL_PASSWORD}|g' \
576577
-e 's|{{TEST_PGSQL_SCHEMA}}|${TEST_PGSQL_SCHEMA}|g' \
578+
-e 's|{{TEST_MINIO_ENDPOINT}}|${TEST_MINIO_ENDPOINT}|g' \
577579
-e 's|{{REPO_TEST_DIR}}|${REPO_TEST_DIR}|g' \
578580
-e 's|{{TEST_LOGGER}}|$(or $(TEST_LOGGER),test$(COMMA)file)|g' \
579581
-e 's|{{TEST_TYPE}}|$(or $(TEST_TYPE),integration)|g' \

assets/go-licenses.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/generate-licenses.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
14
//go:build ignore
25

36
package main
47

58
import (
69
"archive/tar"
710
"compress/gzip"
11+
"crypto/md5"
12+
"encoding/hex"
813
"flag"
914
"fmt"
1015
"io"
@@ -15,6 +20,8 @@ import (
1520
"path/filepath"
1621
"strings"
1722

23+
"code.gitea.io/gitea/build/license"
24+
"code.gitea.io/gitea/modules/json"
1825
"code.gitea.io/gitea/modules/util"
1926
)
2027

@@ -77,7 +84,7 @@ func main() {
7784
}
7885

7986
tr := tar.NewReader(gz)
80-
87+
aliasesFiles := make(map[string][]string)
8188
for {
8289
hdr, err := tr.Next()
8390

@@ -97,26 +104,73 @@ func main() {
97104
continue
98105
}
99106

100-
if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
107+
fileBaseName := filepath.Base(hdr.Name)
108+
licenseName := strings.TrimSuffix(fileBaseName, ".txt")
109+
110+
if strings.HasPrefix(fileBaseName, "README") {
101111
continue
102112
}
103113

104-
if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
114+
if strings.HasPrefix(fileBaseName, "deprecated_") {
105115
continue
106116
}
107-
out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
117+
out, err := os.Create(path.Join(destination, licenseName))
108118
if err != nil {
109119
log.Fatalf("Failed to create new file. %s", err)
110120
}
111121

112122
defer out.Close()
113123

114-
if _, err := io.Copy(out, tr); err != nil {
124+
// some license files have same content, so we need to detect these files and create a convert map into a json file
125+
// Later we use this convert map to avoid adding same license content with different license name
126+
h := md5.New()
127+
// calculate md5 and write file in the same time
128+
r := io.TeeReader(tr, h)
129+
if _, err := io.Copy(out, r); err != nil {
115130
log.Fatalf("Failed to write new file. %s", err)
116131
} else {
117132
fmt.Printf("Written %s\n", out.Name())
133+
134+
md5 := hex.EncodeToString(h.Sum(nil))
135+
aliasesFiles[md5] = append(aliasesFiles[md5], licenseName)
118136
}
119137
}
120138

139+
// generate convert license name map
140+
licenseAliases := make(map[string]string)
141+
for _, fileNames := range aliasesFiles {
142+
if len(fileNames) > 1 {
143+
licenseName := license.GetLicenseNameFromAliases(fileNames)
144+
if licenseName == "" {
145+
// license name should not be empty as expected
146+
// if it is empty, we need to rewrite the logic of GetLicenseNameFromAliases
147+
log.Fatalf("GetLicenseNameFromAliases: license name is empty")
148+
}
149+
for _, fileName := range fileNames {
150+
licenseAliases[fileName] = licenseName
151+
}
152+
}
153+
}
154+
// save convert license name map to file
155+
b, err := json.Marshal(licenseAliases)
156+
if err != nil {
157+
log.Fatalf("Failed to create json bytes. %s", err)
158+
}
159+
160+
licenseAliasesDestination := filepath.Join(destination, "etc", "license-aliases.json")
161+
if err := os.MkdirAll(filepath.Dir(licenseAliasesDestination), 0o755); err != nil {
162+
log.Fatalf("Failed to create directory for license aliases json file. %s", err)
163+
}
164+
165+
f, err := os.Create(licenseAliasesDestination)
166+
if err != nil {
167+
log.Fatalf("Failed to create license aliases json file. %s", err)
168+
}
169+
defer f.Close()
170+
171+
if _, err = f.Write(b); err != nil {
172+
log.Fatalf("Failed to write license aliases json file. %s", err)
173+
}
174+
121175
fmt.Println("Done")
122176
}

build/license/aliasgenerator.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package license
5+
6+
import "strings"
7+
8+
func GetLicenseNameFromAliases(fnl []string) string {
9+
if len(fnl) == 0 {
10+
return ""
11+
}
12+
13+
shortestItem := func(list []string) string {
14+
s := list[0]
15+
for _, l := range list[1:] {
16+
if len(l) < len(s) {
17+
s = l
18+
}
19+
}
20+
return s
21+
}
22+
allHasPrefix := func(list []string, s string) bool {
23+
for _, l := range list {
24+
if !strings.HasPrefix(l, s) {
25+
return false
26+
}
27+
}
28+
return true
29+
}
30+
31+
sl := shortestItem(fnl)
32+
slv := strings.Split(sl, "-")
33+
var result string
34+
for i := len(slv); i >= 0; i-- {
35+
result = strings.Join(slv[:i], "-")
36+
if allHasPrefix(fnl, result) {
37+
return result
38+
}
39+
}
40+
return ""
41+
}

build/license/aliasgenerator_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package license
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGetLicenseNameFromAliases(t *testing.T) {
13+
tests := []struct {
14+
target string
15+
inputs []string
16+
}{
17+
{
18+
// real case which you can find in license-aliases.json
19+
target: "AGPL-1.0",
20+
inputs: []string{
21+
"AGPL-1.0-only",
22+
"AGPL-1.0-or-late",
23+
},
24+
},
25+
{
26+
target: "",
27+
inputs: []string{
28+
"APSL-1.0",
29+
"AGPL-1.0-only",
30+
"AGPL-1.0-or-late",
31+
},
32+
},
33+
}
34+
35+
for _, tt := range tests {
36+
result := GetLicenseNameFromAliases(tt.inputs)
37+
assert.Equal(t, result, tt.target)
38+
}
39+
}

0 commit comments

Comments
 (0)