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