mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-04-22 15:09:25 +00:00
Some checks failed
/ release (push) Waiting to run
testing / backend-checks (push) Has been skipped
testing / frontend-checks (push) Has been skipped
testing / test-unit (push) Has been skipped
testing / test-e2e (push) Has been skipped
testing / test-mysql (push) Has been skipped
testing / test-pgsql (push) Has been skipped
testing / test-sqlite (push) Has been skipped
testing / test-remote-cacher (redis) (push) Has been skipped
testing / test-remote-cacher (valkey) (push) Has been skipped
testing / test-remote-cacher (garnet) (push) Has been skipped
testing / test-remote-cacher (redict) (push) Has been skipped
testing / security-check (push) Has been skipped
Integration tests for the release process / release-simulation (push) Has been cancelled
Co-authored-by: Renovate Bot <forgejo-renovate-action@forgejo.org> Co-committed-by: Renovate Bot <forgejo-renovate-action@forgejo.org>
117 lines
3 KiB
Go
117 lines
3 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"forgejo.org/modules/options"
|
|
)
|
|
|
|
type LicenseValues struct {
|
|
Owner string
|
|
Email string
|
|
Repo string
|
|
Year string
|
|
}
|
|
|
|
func GetLicense(name string, values *LicenseValues) ([]byte, error) {
|
|
data, err := options.License(name)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
|
|
}
|
|
return fillLicensePlaceholder(name, values, data), nil
|
|
}
|
|
|
|
func fillLicensePlaceholder(name string, values *LicenseValues, origin []byte) []byte {
|
|
placeholder := getLicensePlaceholder(name)
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(origin))
|
|
output := bytes.NewBuffer(nil)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if placeholder.MatchLine == nil || placeholder.MatchLine.MatchString(line) {
|
|
for _, v := range placeholder.Owner {
|
|
line = strings.ReplaceAll(line, v, values.Owner)
|
|
}
|
|
for _, v := range placeholder.Email {
|
|
line = strings.ReplaceAll(line, v, values.Email)
|
|
}
|
|
for _, v := range placeholder.Repo {
|
|
line = strings.ReplaceAll(line, v, values.Repo)
|
|
}
|
|
for _, v := range placeholder.Year {
|
|
line = strings.ReplaceAll(line, v, values.Year)
|
|
}
|
|
}
|
|
output.WriteString(line + "\n")
|
|
}
|
|
|
|
return output.Bytes()
|
|
}
|
|
|
|
type licensePlaceholder struct {
|
|
Owner []string
|
|
Email []string
|
|
Repo []string
|
|
Year []string
|
|
MatchLine *regexp.Regexp
|
|
}
|
|
|
|
func getLicensePlaceholder(name string) *licensePlaceholder {
|
|
// Some universal placeholders.
|
|
// 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.
|
|
ret := &licensePlaceholder{
|
|
Owner: []string{
|
|
"<name of author>",
|
|
"<owner>",
|
|
"[NAME]",
|
|
"[name of copyright owner]",
|
|
"[name of copyright holder]",
|
|
"<COPYRIGHT HOLDERS>",
|
|
"<copyright holders>",
|
|
"<AUTHOR>",
|
|
"<author's name or designee>",
|
|
"[one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]",
|
|
},
|
|
Email: []string{
|
|
"[EMAIL]",
|
|
},
|
|
Repo: []string{
|
|
"<program>",
|
|
"<one line to give the program's name and a brief idea of what it does.>",
|
|
},
|
|
Year: []string{
|
|
"<year>",
|
|
"[YEAR]",
|
|
"{YEAR}",
|
|
"[yyyy]",
|
|
"[Year]",
|
|
"[year]",
|
|
},
|
|
}
|
|
|
|
// Some special placeholders for specific licenses.
|
|
// It's unsafe to apply them to all licenses.
|
|
switch name {
|
|
case "0BSD":
|
|
return &licensePlaceholder{
|
|
Owner: []string{"AUTHOR"},
|
|
Email: []string{"EMAIL"},
|
|
Year: []string{"YEAR"},
|
|
MatchLine: regexp.MustCompile(`Copyright \(C\) YEAR by AUTHOR EMAIL`), // there is another AUTHOR in the file, but it's not a placeholder
|
|
}
|
|
|
|
// Other special placeholders can be added here.
|
|
case "BSD-4-Clause":
|
|
ret.Owner = append(ret.Owner, "COPYRIGHT HOLDER")
|
|
ret.Owner = append(ret.Owner, "the organization")
|
|
}
|
|
return ret
|
|
}
|