From e68b9d00a6e05b3a941f63ffb696f91e554ac5ec Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 18 Oct 2024 20:33:49 +0200 Subject: Adding upstream version 9.0.3. Signed-off-by: Daniel Baumann --- modules/repository/license.go | 112 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 modules/repository/license.go (limited to 'modules/repository/license.go') diff --git a/modules/repository/license.go b/modules/repository/license.go new file mode 100644 index 0000000..07ae92c --- /dev/null +++ b/modules/repository/license.go @@ -0,0 +1,112 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repository + +import ( + "bufio" + "bytes" + "fmt" + "regexp" + "strings" + + "code.gitea.io/gitea/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]", + "[name of copyright owner]", + "[name of copyright holder]", + "", + "", + "", + "", + "[one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]", + }, + Email: []string{ + "[EMAIL]", + }, + Repo: []string{ + "", + "", + }, + Year: []string{ + "", + "[YEAR]", + "{YEAR}", + "[yyyy]", + "[Year]", + "[year]", + }, + } + + // Some special placeholders for specific licenses. + // It's unsafe to apply them to all licenses. + if name == "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. + } + return ret +} -- cgit v1.2.3