gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "regexp"
  6. "strings"
  7. "code.gitea.io/gitea/modules/util"
  8. )
  9. const (
  10. // RemotePrefix is the base directory of the remotes information of git.
  11. RemotePrefix = "refs/remotes/"
  12. // PullPrefix is the base directory of the pull information of git.
  13. PullPrefix = "refs/pull/"
  14. )
  15. // refNamePatternInvalid is regular expression with unallowed characters in git reference name
  16. // They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
  17. // They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
  18. var refNamePatternInvalid = regexp.MustCompile(
  19. `[\000-\037\177 \\~^:?*[]|` + // No absolutely invalid characters
  20. `(?:^[/.])|` + // Not HasPrefix("/") or "."
  21. `(?:/\.)|` + // no "/."
  22. `(?:\.lock$)|(?:\.lock/)|` + // No ".lock/"" or ".lock" at the end
  23. `(?:\.\.)|` + // no ".." anywhere
  24. `(?://)|` + // no "//" anywhere
  25. `(?:@{)|` + // no "@{"
  26. `(?:[/.]$)|` + // no terminal '/' or '.'
  27. `(?:^@$)`) // Not "@"
  28. // IsValidRefPattern ensures that the provided string could be a valid reference
  29. func IsValidRefPattern(name string) bool {
  30. return !refNamePatternInvalid.MatchString(name)
  31. }
  32. func SanitizeRefPattern(name string) string {
  33. return refNamePatternInvalid.ReplaceAllString(name, "_")
  34. }
  35. // Reference represents a Git ref.
  36. type Reference struct {
  37. Name string
  38. repo *Repository
  39. Object ObjectID // The id of this commit object
  40. Type string
  41. }
  42. // Commit return the commit of the reference
  43. func (ref *Reference) Commit() (*Commit, error) {
  44. return ref.repo.getCommit(ref.Object)
  45. }
  46. // ShortName returns the short name of the reference
  47. func (ref *Reference) ShortName() string {
  48. return RefName(ref.Name).ShortName()
  49. }
  50. // RefGroup returns the group type of the reference
  51. func (ref *Reference) RefGroup() string {
  52. return RefName(ref.Name).RefGroup()
  53. }
  54. // ForPrefix special ref to create a pull request: refs/for/<target-branch>/<topic-branch>
  55. // or refs/for/<targe-branch> -o topic='<topic-branch>'
  56. const ForPrefix = "refs/for/"
  57. // TODO: /refs/for-review for suggest change interface
  58. // RefName represents a full git reference name
  59. type RefName string
  60. func RefNameFromBranch(shortName string) RefName {
  61. return RefName(BranchPrefix + shortName)
  62. }
  63. func RefNameFromTag(shortName string) RefName {
  64. return RefName(TagPrefix + shortName)
  65. }
  66. func RefNameFromCommit(shortName string) RefName {
  67. return RefName(shortName)
  68. }
  69. func (ref RefName) String() string {
  70. return string(ref)
  71. }
  72. func (ref RefName) IsBranch() bool {
  73. return strings.HasPrefix(string(ref), BranchPrefix)
  74. }
  75. func (ref RefName) IsTag() bool {
  76. return strings.HasPrefix(string(ref), TagPrefix)
  77. }
  78. func (ref RefName) IsRemote() bool {
  79. return strings.HasPrefix(string(ref), RemotePrefix)
  80. }
  81. func (ref RefName) IsPull() bool {
  82. return strings.HasPrefix(string(ref), PullPrefix) && strings.IndexByte(string(ref)[len(PullPrefix):], '/') > -1
  83. }
  84. func (ref RefName) IsFor() bool {
  85. return strings.HasPrefix(string(ref), ForPrefix)
  86. }
  87. func (ref RefName) nameWithoutPrefix(prefix string) string {
  88. if after, ok := strings.CutPrefix(string(ref), prefix); ok {
  89. return after
  90. }
  91. return ""
  92. }
  93. // TagName returns simple tag name if it's an operation to a tag
  94. func (ref RefName) TagName() string {
  95. return ref.nameWithoutPrefix(TagPrefix)
  96. }
  97. // BranchName returns simple branch name if it's an operation to branch
  98. func (ref RefName) BranchName() string {
  99. return ref.nameWithoutPrefix(BranchPrefix)
  100. }
  101. // PullName returns the pull request name part of refs like refs/pull/<pull_name>/head
  102. func (ref RefName) PullName() string {
  103. refName := string(ref)
  104. lastIdx := strings.LastIndexByte(refName[len(PullPrefix):], '/')
  105. if strings.HasPrefix(refName, PullPrefix) && lastIdx > -1 {
  106. return refName[len(PullPrefix) : lastIdx+len(PullPrefix)]
  107. }
  108. return ""
  109. }
  110. // ForBranchName returns the branch name part of refs like refs/for/<branch_name>
  111. func (ref RefName) ForBranchName() string {
  112. return ref.nameWithoutPrefix(ForPrefix)
  113. }
  114. func (ref RefName) RemoteName() string {
  115. return ref.nameWithoutPrefix(RemotePrefix)
  116. }
  117. // ShortName returns the short name of the reference name
  118. func (ref RefName) ShortName() string {
  119. if ref.IsBranch() {
  120. return ref.BranchName()
  121. }
  122. if ref.IsTag() {
  123. return ref.TagName()
  124. }
  125. if ref.IsRemote() {
  126. return ref.RemoteName()
  127. }
  128. if ref.IsPull() {
  129. return ref.PullName()
  130. }
  131. if ref.IsFor() {
  132. return ref.ForBranchName()
  133. }
  134. return string(ref) // usually it is a commit ID
  135. }
  136. // RefGroup returns the group type of the reference
  137. // Using the name of the directory under .git/refs
  138. func (ref RefName) RefGroup() string {
  139. if ref.IsBranch() {
  140. return "heads"
  141. }
  142. if ref.IsTag() {
  143. return "tags"
  144. }
  145. if ref.IsRemote() {
  146. return "remotes"
  147. }
  148. if ref.IsPull() {
  149. return "pull"
  150. }
  151. if ref.IsFor() {
  152. return "for"
  153. }
  154. return ""
  155. }
  156. // RefType is a simple ref type of the reference, it is used for UI and webhooks
  157. type RefType string
  158. const (
  159. RefTypeBranch RefType = "branch"
  160. RefTypeTag RefType = "tag"
  161. RefTypeCommit RefType = "commit"
  162. )
  163. // RefType returns the simple ref type of the reference, e.g. branch, tag
  164. // It's different from RefGroup, which is using the name of the directory under .git/refs
  165. func (ref RefName) RefType() RefType {
  166. switch {
  167. case ref.IsBranch():
  168. return RefTypeBranch
  169. case ref.IsTag():
  170. return RefTypeTag
  171. case IsStringLikelyCommitID(nil, string(ref), 6):
  172. return RefTypeCommit
  173. }
  174. return ""
  175. }
  176. // RefWebLinkPath returns a path for the reference that can be used in a web link:
  177. // * "branch/<branch_name>"
  178. // * "tag/<tag_name>"
  179. // * "commit/<commit_id>"
  180. // It returns an empty string if the reference is not a branch, tag or commit.
  181. func (ref RefName) RefWebLinkPath() string {
  182. refType := ref.RefType()
  183. if refType == "" {
  184. return ""
  185. }
  186. return string(refType) + "/" + util.PathEscapeSegments(ref.ShortName())
  187. }