repo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/codegangsta/martini"
  10. "github.com/gogits/git"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. )
  14. func RepoAssignment(redirect bool) martini.Handler {
  15. return func(ctx *Context, params martini.Params) {
  16. // assign false first
  17. ctx.Data["IsRepositoryValid"] = false
  18. var (
  19. user *models.User
  20. err error
  21. )
  22. userName := params["username"]
  23. repoName := params["reponame"]
  24. branchName := params["branchname"]
  25. // get repository owner
  26. ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName)
  27. if !ctx.Repo.IsOwner {
  28. user, err = models.GetUserByName(params["username"])
  29. if err != nil {
  30. if redirect {
  31. ctx.Redirect("/")
  32. return
  33. }
  34. ctx.Handle(200, "RepoAssignment", err)
  35. return
  36. }
  37. } else {
  38. user = ctx.User
  39. }
  40. if user == nil {
  41. if redirect {
  42. ctx.Redirect("/")
  43. return
  44. }
  45. ctx.Handle(200, "RepoAssignment", errors.New("invliad user account for single repository"))
  46. return
  47. }
  48. // get repository
  49. repo, err := models.GetRepositoryByName(user.Id, repoName)
  50. if err != nil {
  51. if err == models.ErrRepoNotExist {
  52. ctx.Handle(404, "RepoAssignment", err)
  53. } else if redirect {
  54. ctx.Redirect("/")
  55. return
  56. }
  57. ctx.Handle(404, "RepoAssignment", err)
  58. return
  59. }
  60. ctx.Repo.Repository = repo
  61. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  62. if err != nil {
  63. ctx.Handle(404, "RepoAssignment Invalid repo", err)
  64. return
  65. }
  66. ctx.Repo.GitRepo = gitRepo
  67. detect:
  68. if len(branchName) > 0 {
  69. // TODO check tag
  70. if models.IsBranchExist(user.Name, repoName, branchName) {
  71. ctx.Repo.IsBranch = true
  72. ctx.Repo.BranchName = branchName
  73. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName)
  74. if err != nil {
  75. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  76. return
  77. }
  78. ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String()
  79. } else if len(branchName) == 40 {
  80. ctx.Repo.IsCommit = true
  81. ctx.Repo.CommitId = branchName
  82. ctx.Repo.BranchName = branchName
  83. ctx.Repo.Commit, err = gitRepo.GetCommit(branchName)
  84. if err != nil {
  85. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  86. return
  87. }
  88. } else {
  89. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  90. return
  91. }
  92. } else {
  93. branchName = "master"
  94. goto detect
  95. }
  96. if ctx.IsSigned {
  97. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  98. }
  99. ctx.Repo.Owner = user
  100. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  101. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
  102. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  103. ctx.Data["BranchName"] = ctx.Repo.BranchName
  104. ctx.Data["CommitId"] = ctx.Repo.CommitId
  105. ctx.Data["Repository"] = repo
  106. ctx.Data["Owner"] = user
  107. ctx.Data["Title"] = user.Name + "/" + repo.Name
  108. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  109. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  110. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  111. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  112. }
  113. }