You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ref.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 (ref RefName) String() string {
  67. return string(ref)
  68. }
  69. func (ref RefName) IsBranch() bool {
  70. return strings.HasPrefix(string(ref), BranchPrefix)
  71. }
  72. func (ref RefName) IsTag() bool {
  73. return strings.HasPrefix(string(ref), TagPrefix)
  74. }
  75. func (ref RefName) IsRemote() bool {
  76. return strings.HasPrefix(string(ref), RemotePrefix)
  77. }
  78. func (ref RefName) IsPull() bool {
  79. return strings.HasPrefix(string(ref), PullPrefix) && strings.IndexByte(string(ref)[len(PullPrefix):], '/') > -1
  80. }
  81. func (ref RefName) IsFor() bool {
  82. return strings.HasPrefix(string(ref), ForPrefix)
  83. }
  84. func (ref RefName) nameWithoutPrefix(prefix string) string {
  85. if strings.HasPrefix(string(ref), prefix) {
  86. return strings.TrimPrefix(string(ref), prefix)
  87. }
  88. return ""
  89. }
  90. // TagName returns simple tag name if it's an operation to a tag
  91. func (ref RefName) TagName() string {
  92. return ref.nameWithoutPrefix(TagPrefix)
  93. }
  94. // BranchName returns simple branch name if it's an operation to branch
  95. func (ref RefName) BranchName() string {
  96. return ref.nameWithoutPrefix(BranchPrefix)
  97. }
  98. // PullName returns the pull request name part of refs like refs/pull/<pull_name>/head
  99. func (ref RefName) PullName() string {
  100. refName := string(ref)
  101. lastIdx := strings.LastIndexByte(refName[len(PullPrefix):], '/')
  102. if strings.HasPrefix(refName, PullPrefix) && lastIdx > -1 {
  103. return refName[len(PullPrefix) : lastIdx+len(PullPrefix)]
  104. }
  105. return ""
  106. }
  107. // ForBranchName returns the branch name part of refs like refs/for/<branch_name>
  108. func (ref RefName) ForBranchName() string {
  109. return ref.nameWithoutPrefix(ForPrefix)
  110. }
  111. func (ref RefName) RemoteName() string {
  112. return ref.nameWithoutPrefix(RemotePrefix)
  113. }
  114. // ShortName returns the short name of the reference name
  115. func (ref RefName) ShortName() string {
  116. refName := string(ref)
  117. if ref.IsBranch() {
  118. return ref.BranchName()
  119. }
  120. if ref.IsTag() {
  121. return ref.TagName()
  122. }
  123. if ref.IsRemote() {
  124. return ref.RemoteName()
  125. }
  126. if ref.IsPull() {
  127. return ref.PullName()
  128. }
  129. if ref.IsFor() {
  130. return ref.ForBranchName()
  131. }
  132. return refName
  133. }
  134. // RefGroup returns the group type of the reference
  135. // Using the name of the directory under .git/refs
  136. func (ref RefName) RefGroup() string {
  137. if ref.IsBranch() {
  138. return "heads"
  139. }
  140. if ref.IsTag() {
  141. return "tags"
  142. }
  143. if ref.IsRemote() {
  144. return "remotes"
  145. }
  146. if ref.IsPull() {
  147. return "pull"
  148. }
  149. if ref.IsFor() {
  150. return "for"
  151. }
  152. return ""
  153. }
  154. // RefType returns the simple ref type of the reference, e.g. branch, tag
  155. // It's differrent from RefGroup, which is using the name of the directory under .git/refs
  156. // Here we using branch but not heads, using tag but not tags
  157. func (ref RefName) RefType() string {
  158. var refType string
  159. if ref.IsBranch() {
  160. refType = "branch"
  161. } else if ref.IsTag() {
  162. refType = "tag"
  163. }
  164. return refType
  165. }
  166. // RefURL returns the absolute URL for a ref in a repository
  167. func RefURL(repoURL, ref string) string {
  168. refFullName := RefName(ref)
  169. refName := util.PathEscapeSegments(refFullName.ShortName())
  170. switch {
  171. case refFullName.IsBranch():
  172. return repoURL + "/src/branch/" + refName
  173. case refFullName.IsTag():
  174. return repoURL + "/src/tag/" + refName
  175. case !Sha1ObjectFormat.IsValid(ref):
  176. // assume they mean a branch
  177. return repoURL + "/src/branch/" + refName
  178. default:
  179. return repoURL + "/src/commit/" + refName
  180. }
  181. }