Skip to content

Commit ea1afb9

Browse files
wolfogreGiteaBot
andauthored
Replace placeholders in licenses (#24354)
Replace #22117. Implement it in a more maintainable way. Some licenses have placeholders e.g. the BSD licenses start with this line: ``` Copyright (c) <year> <owner>. ``` This PR replaces the placeholders with the correct value when initialize a new repo. ### FAQ - Why not use a regex? It will be a pretty complicated regex which could be hard to maintain. - There're still missing placeholders. There are over 500 licenses, it's impossible for anyone to inspect all of them alone. Please help to add them if you find any, and it is also OK to leave them for the future. --------- Co-authored-by: Giteabot <[email protected]>
1 parent a866cb0 commit ea1afb9

File tree

4 files changed

+330
-3
lines changed

4 files changed

+330
-3
lines changed

build/generate-go-licenses.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,40 @@ func main() {
3535

3636
base, out := os.Args[1], os.Args[2]
3737

38+
// Add ext for excluded files because license_test.go will be included for some reason.
39+
// And there are more files that should be excluded, check with:
40+
//
41+
// go run github.com/google/[email protected] save . --force --save_path=.go-licenses 2>/dev/null
42+
// find .go-licenses -type f | while read FILE; do echo "${$(basename $FILE)##*.}"; done | sort -u
43+
// AUTHORS
44+
// COPYING
45+
// LICENSE
46+
// Makefile
47+
// NOTICE
48+
// gitignore
49+
// go
50+
// md
51+
// mod
52+
// sum
53+
// toml
54+
// txt
55+
// yml
56+
//
57+
// It could be removed once we have a better regex.
58+
excludedExt := map[string]bool{
59+
".gitignore": true,
60+
".go": true,
61+
".mod": true,
62+
".sum": true,
63+
".toml": true,
64+
".yml": true,
65+
}
3866
var paths []string
3967
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
4068
if err != nil {
4169
return err
4270
}
43-
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) {
71+
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt[filepath.Ext(entry.Name())] {
4472
return nil
4573
}
4674
paths = append(paths, path)

modules/repository/init.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,14 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
195195

196196
// LICENSE
197197
if len(opts.License) > 0 {
198-
data, err = options.License(opts.License)
198+
data, err = getLicense(opts.License, &licenseValues{
199+
Owner: repo.OwnerName,
200+
Email: authorSig.Email,
201+
Repo: repo.Name,
202+
Year: time.Now().Format("2006"),
203+
})
199204
if err != nil {
200-
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.License, err)
205+
return fmt.Errorf("getLicense[%s]: %w", opts.License, err)
201206
}
202207

203208
if err = os.WriteFile(filepath.Join(tmpDir, "LICENSE"), data, 0o644); err != nil {

modules/repository/license.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repository
5+
6+
import (
7+
"bufio"
8+
"bytes"
9+
"fmt"
10+
"regexp"
11+
"strings"
12+
13+
"code.gitea.io/gitea/modules/options"
14+
)
15+
16+
type licenseValues struct {
17+
Owner string
18+
Email string
19+
Repo string
20+
Year string
21+
}
22+
23+
func getLicense(name string, values *licenseValues) ([]byte, error) {
24+
data, err := options.License(name)
25+
if err != nil {
26+
return nil, fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
27+
}
28+
return fillLicensePlaceholder(name, values, data), nil
29+
}
30+
31+
func fillLicensePlaceholder(name string, values *licenseValues, origin []byte) []byte {
32+
placeholder := getLicensePlaceholder(name)
33+
34+
scanner := bufio.NewScanner(bytes.NewReader(origin))
35+
output := bytes.NewBuffer(nil)
36+
for scanner.Scan() {
37+
line := scanner.Text()
38+
if placeholder.MatchLine == nil || placeholder.MatchLine.MatchString(line) {
39+
for _, v := range placeholder.Owner {
40+
line = strings.ReplaceAll(line, v, values.Owner)
41+
}
42+
for _, v := range placeholder.Email {
43+
line = strings.ReplaceAll(line, v, values.Email)
44+
}
45+
for _, v := range placeholder.Repo {
46+
line = strings.ReplaceAll(line, v, values.Repo)
47+
}
48+
for _, v := range placeholder.Year {
49+
line = strings.ReplaceAll(line, v, values.Year)
50+
}
51+
}
52+
output.WriteString(line + "\n")
53+
}
54+
55+
return output.Bytes()
56+
}
57+
58+
type licensePlaceholder struct {
59+
Owner []string
60+
Email []string
61+
Repo []string
62+
Year []string
63+
MatchLine *regexp.Regexp
64+
}
65+
66+
func getLicensePlaceholder(name string) *licensePlaceholder {
67+
// Some universal placeholders.
68+
// If you want to add a new one, make sure you have check it by `grep -r 'NEW_WORD' options/license` and all of them are placeholders.
69+
ret := &licensePlaceholder{
70+
Owner: []string{
71+
"<name of author>",
72+
"<owner>",
73+
"[NAME]",
74+
"[name of copyright owner]",
75+
"[name of copyright holder]",
76+
"<COPYRIGHT HOLDERS>",
77+
"<copyright holders>",
78+
"<AUTHOR>",
79+
"<author's name or designee>",
80+
"[one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]",
81+
},
82+
Email: []string{
83+
"[EMAIL]",
84+
},
85+
Repo: []string{
86+
"<program>",
87+
"<one line to give the program's name and a brief idea of what it does.>",
88+
},
89+
Year: []string{
90+
"<year>",
91+
"[YEAR]",
92+
"{YEAR}",
93+
"[yyyy]",
94+
"[Year]",
95+
"[year]",
96+
},
97+
}
98+
99+
// Some special placeholders for specific licenses.
100+
// It's unsafe to apply them to all licenses.
101+
switch name {
102+
case "0BSD":
103+
return &licensePlaceholder{
104+
Owner: []string{"AUTHOR"},
105+
Email: []string{"EMAIL"},
106+
Year: []string{"YEAR"},
107+
MatchLine: regexp.MustCompile(`Copyright \(C\) YEAR by AUTHOR EMAIL`), // there is another AUTHOR in the file, but it's not a placeholder
108+
}
109+
110+
// Other special placeholders can be added here.
111+
}
112+
return ret
113+
}

modules/repository/license_test.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repository
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_getLicense(t *testing.T) {
14+
type args struct {
15+
name string
16+
values *licenseValues
17+
}
18+
tests := []struct {
19+
name string
20+
args args
21+
want string
22+
wantErr assert.ErrorAssertionFunc
23+
}{
24+
{
25+
name: "regular",
26+
args: args{
27+
name: "MIT",
28+
values: &licenseValues{Owner: "Gitea", Year: "2023"},
29+
},
30+
want: `MIT License
31+
32+
Copyright (c) 2023 Gitea
33+
34+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
35+
36+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
37+
38+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39+
`,
40+
wantErr: assert.NoError,
41+
},
42+
{
43+
name: "license not found",
44+
args: args{
45+
name: "notfound",
46+
},
47+
wantErr: assert.Error,
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
got, err := getLicense(tt.args.name, tt.args.values)
53+
if !tt.wantErr(t, err, fmt.Sprintf("getLicense(%v, %v)", tt.args.name, tt.args.values)) {
54+
return
55+
}
56+
assert.Equalf(t, tt.want, string(got), "getLicense(%v, %v)", tt.args.name, tt.args.values)
57+
})
58+
}
59+
}
60+
61+
func Test_fillLicensePlaceholder(t *testing.T) {
62+
type args struct {
63+
name string
64+
values *licenseValues
65+
origin string
66+
}
67+
tests := []struct {
68+
name string
69+
args args
70+
want string
71+
}{
72+
{
73+
name: "owner",
74+
args: args{
75+
name: "regular",
76+
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "[email protected]", Repo: "gitea"},
77+
origin: `
78+
<name of author>
79+
<owner>
80+
[NAME]
81+
[name of copyright owner]
82+
[name of copyright holder]
83+
<COPYRIGHT HOLDERS>
84+
<copyright holders>
85+
<AUTHOR>
86+
<author's name or designee>
87+
[one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]
88+
`,
89+
},
90+
want: `
91+
Gitea
92+
Gitea
93+
Gitea
94+
Gitea
95+
Gitea
96+
Gitea
97+
Gitea
98+
Gitea
99+
Gitea
100+
Gitea
101+
`,
102+
},
103+
{
104+
name: "email",
105+
args: args{
106+
name: "regular",
107+
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "[email protected]", Repo: "gitea"},
108+
origin: `
109+
[EMAIL]
110+
`,
111+
},
112+
want: `
113+
114+
`,
115+
},
116+
{
117+
name: "repo",
118+
args: args{
119+
name: "regular",
120+
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "[email protected]", Repo: "gitea"},
121+
origin: `
122+
<program>
123+
<one line to give the program's name and a brief idea of what it does.>
124+
`,
125+
},
126+
want: `
127+
gitea
128+
gitea
129+
`,
130+
},
131+
{
132+
name: "year",
133+
args: args{
134+
name: "regular",
135+
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "[email protected]", Repo: "gitea"},
136+
origin: `
137+
<year>
138+
[YEAR]
139+
{YEAR}
140+
[yyyy]
141+
[Year]
142+
[year]
143+
`,
144+
},
145+
want: `
146+
2023
147+
2023
148+
2023
149+
2023
150+
2023
151+
2023
152+
`,
153+
},
154+
{
155+
name: "0BSD",
156+
args: args{
157+
name: "0BSD",
158+
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "[email protected]", Repo: "gitea"},
159+
origin: `
160+
Copyright (C) YEAR by AUTHOR EMAIL
161+
162+
...
163+
164+
... THE AUTHOR BE LIABLE FOR ...
165+
`,
166+
},
167+
want: `
168+
Copyright (C) 2023 by Gitea [email protected]
169+
170+
...
171+
172+
... THE AUTHOR BE LIABLE FOR ...
173+
`,
174+
},
175+
}
176+
for _, tt := range tests {
177+
t.Run(tt.name, func(t *testing.T) {
178+
assert.Equalf(t, tt.want, string(fillLicensePlaceholder(tt.args.name, tt.args.values, []byte(tt.args.origin))), "fillLicensePlaceholder(%v, %v, %v)", tt.args.name, tt.args.values, tt.args.origin)
179+
})
180+
}
181+
}

0 commit comments

Comments
 (0)