您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

license.go 2.8KB

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