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 7.2 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. "net/url"
  9. "strings"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/git"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. )
  16. func RepoAssignment(redirect bool, args ...bool) martini.Handler {
  17. return func(ctx *Context, params martini.Params) {
  18. log.Trace(fmt.Sprint(args))
  19. // valid brachname
  20. var validBranch bool
  21. // display bare quick start if it is a bare repo
  22. var displayBare bool
  23. if len(args) >= 1 {
  24. // Note: argument has wrong value in Go1.3 martini.
  25. // validBranch = args[0]
  26. validBranch = true
  27. }
  28. if len(args) >= 2 {
  29. // displayBare = args[1]
  30. displayBare = true
  31. }
  32. var (
  33. user *models.User
  34. err error
  35. isTrueOwner bool
  36. )
  37. userName := params["username"]
  38. repoName := params["reponame"]
  39. refName := params["branchname"]
  40. // Collaborators who have write access can be seen as owners.
  41. if ctx.IsSigned {
  42. ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.AU_WRITABLE)
  43. if err != nil {
  44. ctx.Handle(500, "RepoAssignment(HasAccess)", err)
  45. return
  46. }
  47. isTrueOwner = ctx.User.LowerName == strings.ToLower(userName)
  48. }
  49. if !isTrueOwner {
  50. user, err = models.GetUserByName(userName)
  51. if err != nil {
  52. if err == models.ErrUserNotExist {
  53. ctx.Handle(404, "RepoAssignment(GetUserByName)", err)
  54. return
  55. } else if redirect {
  56. ctx.Redirect("/")
  57. return
  58. }
  59. ctx.Handle(500, "RepoAssignment(GetUserByName)", err)
  60. return
  61. }
  62. } else {
  63. user = ctx.User
  64. }
  65. if user == nil {
  66. if redirect {
  67. ctx.Redirect("/")
  68. return
  69. }
  70. ctx.Handle(403, "RepoAssignment", errors.New("invliad user account for single repository"))
  71. return
  72. }
  73. ctx.Repo.Owner = user
  74. // get repository
  75. repo, err := models.GetRepositoryByName(user.Id, repoName)
  76. if err != nil {
  77. if err == models.ErrRepoNotExist {
  78. ctx.Handle(404, "RepoAssignment", err)
  79. return
  80. } else if redirect {
  81. ctx.Redirect("/")
  82. return
  83. }
  84. ctx.Handle(500, "RepoAssignment", err)
  85. return
  86. }
  87. // Check access.
  88. if repo.IsPrivate && !ctx.Repo.IsOwner {
  89. if ctx.User == nil {
  90. ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
  91. return
  92. }
  93. hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.AU_READABLE)
  94. if err != nil {
  95. ctx.Handle(500, "RepoAssignment(HasAccess)", err)
  96. return
  97. } else if !hasAccess {
  98. ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
  99. return
  100. }
  101. }
  102. ctx.Repo.HasAccess = true
  103. ctx.Data["HasAccess"] = true
  104. if repo.IsMirror {
  105. ctx.Repo.Mirror, err = models.GetMirror(repo.Id)
  106. if err != nil {
  107. ctx.Handle(500, "RepoAssignment(GetMirror)", err)
  108. return
  109. }
  110. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  111. }
  112. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  113. repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
  114. ctx.Repo.Repository = repo
  115. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  116. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  117. if err != nil {
  118. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  119. return
  120. }
  121. ctx.Repo.GitRepo = gitRepo
  122. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  123. tags, err := ctx.Repo.GitRepo.GetTags()
  124. if err != nil {
  125. ctx.Handle(500, "RepoAssignment(GetTags))", err)
  126. return
  127. }
  128. ctx.Repo.Repository.NumTags = len(tags)
  129. ctx.Data["Title"] = user.Name + "/" + repo.Name
  130. ctx.Data["Repository"] = repo
  131. ctx.Data["Owner"] = user
  132. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  133. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  134. ctx.Data["BranchName"] = ""
  135. if base.SshPort != 22 {
  136. ctx.Repo.CloneLink.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  137. } else {
  138. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  139. }
  140. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
  141. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  142. if ctx.Repo.Repository.IsGoget {
  143. ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", base.AppUrl, user.LowerName, repo.LowerName)
  144. ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", base.Domain, user.LowerName, repo.LowerName)
  145. }
  146. // when repo is bare, not valid branch
  147. if !ctx.Repo.Repository.IsBare && validBranch {
  148. detect:
  149. if len(refName) > 0 {
  150. if gitRepo.IsBranchExist(refName) {
  151. ctx.Repo.IsBranch = true
  152. ctx.Repo.BranchName = refName
  153. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
  154. if err != nil {
  155. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  156. return
  157. }
  158. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  159. } else if gitRepo.IsTagExist(refName) {
  160. ctx.Repo.IsBranch = true
  161. ctx.Repo.BranchName = refName
  162. ctx.Repo.Commit, err = gitRepo.GetCommitOfTag(refName)
  163. if err != nil {
  164. ctx.Handle(404, "RepoAssignment invalid tag", nil)
  165. return
  166. }
  167. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  168. } else if len(refName) == 40 {
  169. ctx.Repo.IsCommit = true
  170. ctx.Repo.CommitId = refName
  171. ctx.Repo.BranchName = refName
  172. ctx.Repo.Commit, err = gitRepo.GetCommit(refName)
  173. if err != nil {
  174. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  175. return
  176. }
  177. } else {
  178. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  179. return
  180. }
  181. } else {
  182. if len(refName) == 0 {
  183. if gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  184. refName = ctx.Repo.Repository.DefaultBranch
  185. } else {
  186. brs, err := gitRepo.GetBranches()
  187. if err != nil {
  188. ctx.Handle(500, "RepoAssignment(GetBranches))", err)
  189. return
  190. }
  191. refName = brs[0]
  192. }
  193. }
  194. goto detect
  195. }
  196. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  197. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  198. }
  199. log.Debug("displayBare: %v; IsBare: %v", displayBare, ctx.Repo.Repository.IsBare)
  200. // repo is bare and display enable
  201. if displayBare && ctx.Repo.Repository.IsBare {
  202. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  203. ctx.HTML(200, "repo/single_bare")
  204. return
  205. }
  206. if ctx.IsSigned {
  207. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  208. }
  209. ctx.Data["BranchName"] = ctx.Repo.BranchName
  210. brs, err := ctx.Repo.GitRepo.GetBranches()
  211. if err != nil {
  212. log.Error("RepoAssignment(GetBranches): %v", err)
  213. }
  214. ctx.Data["Branches"] = brs
  215. ctx.Data["CommitId"] = ctx.Repo.CommitId
  216. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  217. }
  218. }
  219. func RequireOwner() martini.Handler {
  220. return func(ctx *Context) {
  221. if !ctx.Repo.IsOwner {
  222. if !ctx.IsSigned {
  223. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  224. ctx.Redirect("/user/login")
  225. return
  226. }
  227. ctx.Handle(404, ctx.Req.RequestURI, nil)
  228. return
  229. }
  230. }
  231. }