repo.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. "fmt"
  7. "path"
  8. "strings"
  9. "gopkg.in/macaron.v1"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. // RepoRef handles repository reference name including those contain `/`.
  16. func RepoRef() macaron.Handler {
  17. return func(ctx *Context) {
  18. // Empty repository does not have reference information.
  19. if ctx.Repo.Repository.IsBare {
  20. return
  21. }
  22. var (
  23. refName string
  24. err error
  25. )
  26. // For API calls.
  27. if ctx.Repo.GitRepo == nil {
  28. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  29. gitRepo, err := git.OpenRepository(repoPath)
  30. if err != nil {
  31. ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
  32. return
  33. }
  34. ctx.Repo.GitRepo = gitRepo
  35. }
  36. // Get default branch.
  37. if len(ctx.Params("*")) == 0 {
  38. refName = ctx.Repo.Repository.DefaultBranch
  39. if !ctx.Repo.GitRepo.IsBranchExist(refName) {
  40. brs, err := ctx.Repo.GitRepo.GetBranches()
  41. if err != nil {
  42. ctx.Handle(500, "GetBranches", err)
  43. return
  44. }
  45. refName = brs[0]
  46. }
  47. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
  48. if err != nil {
  49. ctx.Handle(500, "GetCommitOfBranch", err)
  50. return
  51. }
  52. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  53. ctx.Repo.IsBranch = true
  54. } else {
  55. hasMatched := false
  56. parts := strings.Split(ctx.Params("*"), "/")
  57. for i, part := range parts {
  58. refName = strings.TrimPrefix(refName+"/"+part, "/")
  59. if ctx.Repo.GitRepo.IsBranchExist(refName) ||
  60. ctx.Repo.GitRepo.IsTagExist(refName) {
  61. if i < len(parts)-1 {
  62. ctx.Repo.TreeName = strings.Join(parts[i+1:], "/")
  63. }
  64. hasMatched = true
  65. break
  66. }
  67. }
  68. if !hasMatched && len(parts[0]) == 40 {
  69. refName = parts[0]
  70. ctx.Repo.TreeName = strings.Join(parts[1:], "/")
  71. }
  72. if ctx.Repo.GitRepo.IsBranchExist(refName) {
  73. ctx.Repo.IsBranch = true
  74. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
  75. if err != nil {
  76. ctx.Handle(500, "GetCommitOfBranch", err)
  77. return
  78. }
  79. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  80. } else if ctx.Repo.GitRepo.IsTagExist(refName) {
  81. ctx.Repo.IsTag = true
  82. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfTag(refName)
  83. if err != nil {
  84. ctx.Handle(500, "GetCommitOfTag", err)
  85. return
  86. }
  87. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  88. } else if len(refName) == 40 {
  89. ctx.Repo.IsCommit = true
  90. ctx.Repo.CommitID = refName
  91. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
  92. if err != nil {
  93. ctx.Handle(404, "GetCommit", nil)
  94. return
  95. }
  96. } else {
  97. ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  98. return
  99. }
  100. }
  101. ctx.Repo.BranchName = refName
  102. ctx.Data["BranchName"] = ctx.Repo.BranchName
  103. ctx.Data["CommitID"] = ctx.Repo.CommitID
  104. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  105. ctx.Data["IsTag"] = ctx.Repo.IsTag
  106. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  107. ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
  108. if err != nil {
  109. ctx.Handle(500, "CommitsCount", err)
  110. return
  111. }
  112. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  113. }
  114. }
  115. func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
  116. // Non-fork repository will not return error in this method.
  117. if err := repo.GetBaseRepo(); err != nil {
  118. if models.IsErrRepoNotExist(err) {
  119. repo.IsFork = false
  120. repo.ForkID = 0
  121. return
  122. }
  123. ctx.Handle(500, "GetBaseRepo", err)
  124. return
  125. } else if err = repo.BaseRepo.GetOwner(); err != nil {
  126. ctx.Handle(500, "BaseRepo.GetOwner", err)
  127. return
  128. }
  129. bsaeRepo := repo.BaseRepo
  130. baseGitRepo, err := git.OpenRepository(models.RepoPath(bsaeRepo.Owner.Name, bsaeRepo.Name))
  131. if err != nil {
  132. ctx.Handle(500, "OpenRepository", err)
  133. return
  134. }
  135. if len(bsaeRepo.DefaultBranch) > 0 && baseGitRepo.IsBranchExist(bsaeRepo.DefaultBranch) {
  136. ctx.Data["BaseDefaultBranch"] = bsaeRepo.DefaultBranch
  137. } else {
  138. baseBranches, err := baseGitRepo.GetBranches()
  139. if err != nil {
  140. ctx.Handle(500, "GetBranches", err)
  141. return
  142. }
  143. if len(baseBranches) > 0 {
  144. ctx.Data["BaseDefaultBranch"] = baseBranches[0]
  145. }
  146. }
  147. }
  148. func RepoAssignment(args ...bool) macaron.Handler {
  149. return func(ctx *Context) {
  150. ctx.Repo = &RepoContext{}
  151. var (
  152. displayBare bool // To display bare page if it is a bare repo.
  153. )
  154. if len(args) >= 1 {
  155. displayBare = args[0]
  156. }
  157. var (
  158. owner *models.User
  159. err error
  160. )
  161. userName := ctx.Params(":username")
  162. repoName := ctx.Params(":reponame")
  163. refName := ctx.Params(":branchname")
  164. if len(refName) == 0 {
  165. refName = ctx.Params(":path")
  166. }
  167. // Check if the user is the same as the repository owner
  168. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  169. owner = ctx.User
  170. } else {
  171. owner, err = models.GetUserByName(userName)
  172. if err != nil {
  173. if models.IsErrUserNotExist(err) {
  174. ctx.Handle(404, "GetUserByName", err)
  175. } else {
  176. ctx.Handle(500, "GetUserByName", err)
  177. }
  178. return
  179. }
  180. }
  181. ctx.Repo.Owner = owner
  182. // Get repository.
  183. repo, err := models.GetRepositoryByName(owner.Id, repoName)
  184. if err != nil {
  185. if models.IsErrRepoNotExist(err) {
  186. ctx.Handle(404, "GetRepositoryByName", err)
  187. } else {
  188. ctx.Handle(500, "GetRepositoryByName", err)
  189. }
  190. return
  191. } else if err = repo.GetOwner(); err != nil {
  192. ctx.Handle(500, "GetOwner", err)
  193. return
  194. }
  195. // Admin has super access.
  196. if ctx.IsSigned && ctx.User.IsAdmin {
  197. ctx.Repo.AccessMode = models.ACCESS_MODE_OWNER
  198. } else {
  199. mode, err := models.AccessLevel(ctx.User, repo)
  200. if err != nil {
  201. ctx.Handle(500, "AccessLevel", err)
  202. return
  203. }
  204. ctx.Repo.AccessMode = mode
  205. }
  206. // Check access.
  207. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
  208. ctx.Handle(404, "no access right", err)
  209. return
  210. }
  211. ctx.Data["HasAccess"] = true
  212. if repo.IsMirror {
  213. ctx.Repo.Mirror, err = models.GetMirror(repo.ID)
  214. if err != nil {
  215. ctx.Handle(500, "GetMirror", err)
  216. return
  217. }
  218. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  219. }
  220. ctx.Repo.Repository = repo
  221. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  222. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  223. if err != nil {
  224. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  225. return
  226. }
  227. ctx.Repo.GitRepo = gitRepo
  228. ctx.Repo.RepoLink = repo.RepoLink()
  229. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  230. ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
  231. tags, err := ctx.Repo.GitRepo.GetTags()
  232. if err != nil {
  233. ctx.Handle(500, "GetTags", err)
  234. return
  235. }
  236. ctx.Data["Tags"] = tags
  237. ctx.Repo.Repository.NumTags = len(tags)
  238. if repo.IsFork {
  239. RetrieveBaseRepo(ctx, repo)
  240. if ctx.Written() {
  241. return
  242. }
  243. }
  244. ctx.Data["Title"] = owner.Name + "/" + repo.Name
  245. ctx.Data["Repository"] = repo
  246. ctx.Data["Owner"] = ctx.Repo.Repository.Owner
  247. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
  248. ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
  249. ctx.Data["IsRepositoryPusher"] = ctx.Repo.IsPusher()
  250. ctx.Data["DisableSSH"] = setting.DisableSSH
  251. ctx.Data["CloneLink"] = repo.CloneLink()
  252. ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
  253. if ctx.IsSigned {
  254. ctx.Data["IsWatchingRepo"] = models.IsWatching(ctx.User.Id, repo.ID)
  255. ctx.Data["IsStaringRepo"] = models.IsStaring(ctx.User.Id, repo.ID)
  256. }
  257. // repo is bare and display enable
  258. if ctx.Repo.Repository.IsBare {
  259. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  260. // NOTE: to prevent templating error
  261. ctx.Data["BranchName"] = ""
  262. if displayBare {
  263. if !ctx.Repo.IsAdmin() {
  264. ctx.Flash.Info(ctx.Tr("repo.repo_is_empty"), true)
  265. }
  266. ctx.HTML(200, "repo/bare")
  267. }
  268. return
  269. }
  270. ctx.Data["TagName"] = ctx.Repo.TagName
  271. brs, err := ctx.Repo.GitRepo.GetBranches()
  272. if err != nil {
  273. ctx.Handle(500, "GetBranches", err)
  274. return
  275. }
  276. ctx.Data["Branches"] = brs
  277. ctx.Data["BrancheCount"] = len(brs)
  278. // If not branch selected, try default one.
  279. // If default branch doesn't exists, fall back to some other branch.
  280. if len(ctx.Repo.BranchName) == 0 {
  281. if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  282. ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
  283. } else if len(brs) > 0 {
  284. ctx.Repo.BranchName = brs[0]
  285. }
  286. }
  287. ctx.Data["BranchName"] = ctx.Repo.BranchName
  288. ctx.Data["CommitID"] = ctx.Repo.CommitID
  289. if ctx.Query("go-get") == "1" {
  290. ctx.Data["GoGetImport"] = path.Join(setting.Domain, setting.AppSubUrl, owner.Name, repo.Name)
  291. prefix := path.Join(setting.AppUrl, owner.Name, repo.Name, "src", ctx.Repo.BranchName)
  292. ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
  293. ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
  294. }
  295. }
  296. }
  297. func RequireRepoAdmin() macaron.Handler {
  298. return func(ctx *Context) {
  299. if !ctx.Repo.IsAdmin() {
  300. ctx.Handle(404, ctx.Req.RequestURI, nil)
  301. return
  302. }
  303. }
  304. }
  305. func RequireRepoPusher() macaron.Handler {
  306. return func(ctx *Context) {
  307. if !ctx.Repo.IsPusher() {
  308. ctx.Handle(404, ctx.Req.RequestURI, nil)
  309. return
  310. }
  311. }
  312. }
  313. // GitHookService checks if repository Git hooks service has been enabled.
  314. func GitHookService() macaron.Handler {
  315. return func(ctx *Context) {
  316. if !ctx.User.CanEditGitHook() {
  317. ctx.Handle(404, "GitHookService", nil)
  318. return
  319. }
  320. }
  321. }