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

repo.go 5.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "github.com/go-martini/martini"
  10. "github.com/gogits/git"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. )
  15. func RepoAssignment(redirect bool, args ...bool) martini.Handler {
  16. return func(ctx *Context, params martini.Params) {
  17. // valid brachname
  18. var validBranch bool
  19. // display bare quick start if it is a bare repo
  20. var displayBare bool
  21. if len(args) >= 1 {
  22. validBranch = args[0]
  23. }
  24. if len(args) >= 2 {
  25. displayBare = args[1]
  26. }
  27. var (
  28. user *models.User
  29. err error
  30. )
  31. userName := params["username"]
  32. repoName := params["reponame"]
  33. refName := params["branchname"]
  34. // get repository owner
  35. ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName)
  36. if !ctx.Repo.IsOwner {
  37. user, err = models.GetUserByName(params["username"])
  38. if err != nil {
  39. if redirect {
  40. ctx.Redirect("/")
  41. return
  42. }
  43. ctx.Handle(200, "RepoAssignment", err)
  44. return
  45. }
  46. } else {
  47. user = ctx.User
  48. }
  49. if user == nil {
  50. if redirect {
  51. ctx.Redirect("/")
  52. return
  53. }
  54. ctx.Handle(200, "RepoAssignment", errors.New("invliad user account for single repository"))
  55. return
  56. }
  57. ctx.Repo.Owner = user
  58. // get repository
  59. repo, err := models.GetRepositoryByName(user.Id, repoName)
  60. if err != nil {
  61. if err == models.ErrRepoNotExist {
  62. ctx.Handle(404, "RepoAssignment", err)
  63. return
  64. } else if redirect {
  65. ctx.Redirect("/")
  66. return
  67. }
  68. ctx.Handle(500, "RepoAssignment", err)
  69. return
  70. }
  71. // Check access.
  72. if repo.IsPrivate {
  73. if ctx.User == nil {
  74. ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
  75. return
  76. }
  77. hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.AU_READABLE)
  78. if err != nil {
  79. ctx.Handle(500, "RepoAssignment(HasAccess)", err)
  80. return
  81. } else if !hasAccess {
  82. ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
  83. return
  84. }
  85. }
  86. ctx.Repo.HasAccess = true
  87. ctx.Data["HasAccess"] = true
  88. if repo.IsMirror {
  89. ctx.Repo.Mirror, err = models.GetMirror(repo.Id)
  90. if err != nil {
  91. ctx.Handle(500, "RepoAssignment(GetMirror)", err)
  92. return
  93. }
  94. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  95. }
  96. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  97. ctx.Repo.Repository = repo
  98. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  99. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  100. if err != nil {
  101. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  102. return
  103. }
  104. ctx.Repo.GitRepo = gitRepo
  105. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  106. tags, err := ctx.Repo.GitRepo.GetTags()
  107. if err != nil {
  108. ctx.Handle(500, "RepoAssignment(GetTags))", err)
  109. return
  110. }
  111. ctx.Repo.Repository.NumTags = len(tags)
  112. ctx.Data["Title"] = user.Name + "/" + repo.Name
  113. ctx.Data["Repository"] = repo
  114. ctx.Data["Owner"] = user
  115. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  116. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  117. ctx.Data["BranchName"] = ""
  118. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  119. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
  120. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  121. if ctx.Repo.Repository.IsGoget {
  122. ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", base.AppUrl, user.LowerName, repo.LowerName)
  123. ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", base.Domain, user.LowerName, repo.LowerName)
  124. }
  125. // when repo is bare, not valid branch
  126. if !ctx.Repo.Repository.IsBare && validBranch {
  127. detect:
  128. if len(refName) > 0 {
  129. if gitRepo.IsBranchExist(refName) {
  130. ctx.Repo.IsBranch = true
  131. ctx.Repo.BranchName = refName
  132. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
  133. if err != nil {
  134. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  135. return
  136. }
  137. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  138. } else if gitRepo.IsTagExist(refName) {
  139. ctx.Repo.IsBranch = true
  140. ctx.Repo.BranchName = refName
  141. ctx.Repo.Commit, err = gitRepo.GetCommitOfTag(refName)
  142. if err != nil {
  143. ctx.Handle(404, "RepoAssignment invalid tag", nil)
  144. return
  145. }
  146. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  147. } else if len(refName) == 40 {
  148. ctx.Repo.IsCommit = true
  149. ctx.Repo.CommitId = refName
  150. ctx.Repo.BranchName = refName
  151. ctx.Repo.Commit, err = gitRepo.GetCommit(refName)
  152. if err != nil {
  153. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  154. return
  155. }
  156. } else {
  157. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  158. return
  159. }
  160. } else {
  161. refName = ctx.Repo.Repository.DefaultBranch
  162. if len(refName) == 0 {
  163. refName = "master"
  164. }
  165. goto detect
  166. }
  167. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  168. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  169. log.Debug("Repo.Commit: %v", ctx.Repo.Commit)
  170. }
  171. log.Debug("displayBare: %v; IsBare: %v", displayBare, ctx.Repo.Repository.IsBare)
  172. // repo is bare and display enable
  173. if displayBare && ctx.Repo.Repository.IsBare {
  174. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  175. ctx.HTML(200, "repo/single_bare")
  176. return
  177. }
  178. if ctx.IsSigned {
  179. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  180. }
  181. ctx.Data["BranchName"] = ctx.Repo.BranchName
  182. brs, err := ctx.Repo.GitRepo.GetBranches()
  183. if err != nil {
  184. log.Error("RepoAssignment(GetBranches): %v", err)
  185. }
  186. ctx.Data["Branches"] = brs
  187. ctx.Data["CommitId"] = ctx.Repo.CommitId
  188. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  189. }
  190. }