repo.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "+models.RepoPath(userName, repoName), err)
  64. return
  65. }
  66. ctx.Repo.GitRepo = gitRepo
  67. ctx.Repo.Owner = user
  68. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  69. ctx.Data["Title"] = user.Name + "/" + repo.Name
  70. ctx.Data["Repository"] = repo
  71. ctx.Data["Owner"] = user
  72. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  73. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  74. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  75. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
  76. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  77. if repo.IsBare {
  78. ctx.Data["IsBareRepo"] = true
  79. ctx.HTML(200, "repo/single_bare")
  80. return
  81. }
  82. detect:
  83. if len(branchName) > 0 {
  84. // TODO check tag
  85. if models.IsBranchExist(user.Name, repoName, branchName) {
  86. ctx.Repo.IsBranch = true
  87. ctx.Repo.BranchName = branchName
  88. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName)
  89. if err != nil {
  90. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  91. return
  92. }
  93. ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String()
  94. } else if len(branchName) == 40 {
  95. ctx.Repo.IsCommit = true
  96. ctx.Repo.CommitId = branchName
  97. ctx.Repo.BranchName = branchName
  98. ctx.Repo.Commit, err = gitRepo.GetCommit(branchName)
  99. if err != nil {
  100. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  101. return
  102. }
  103. } else {
  104. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  105. return
  106. }
  107. } else {
  108. branchName = "master"
  109. goto detect
  110. }
  111. if ctx.IsSigned {
  112. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  113. }
  114. ctx.Data["BranchName"] = ctx.Repo.BranchName
  115. ctx.Data["CommitId"] = ctx.Repo.CommitId
  116. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  117. }
  118. }