code.gitea.io/gitea@v1.22.3/modules/repository/license.go (about) 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 }