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 4.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. )
  14. func RepoAssignment(redirect bool, args ...bool) martini.Handler {
  15. return func(ctx *Context, params martini.Params) {
  16. // valid brachname
  17. var validBranch bool
  18. // display bare quick start if it is a bare repo
  19. var displayBare bool
  20. if len(args) >= 1 {
  21. validBranch = args[0]
  22. }
  23. if len(args) >= 2 {
  24. displayBare = args[1]
  25. }
  26. var (
  27. user *models.User
  28. err error
  29. )
  30. userName := params["username"]
  31. repoName := params["reponame"]
  32. branchName := params["branchname"]
  33. // get repository owner
  34. ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName)
  35. if !ctx.Repo.IsOwner {
  36. user, err = models.GetUserByName(params["username"])
  37. if err != nil {
  38. if redirect {
  39. ctx.Redirect("/")
  40. return
  41. }
  42. ctx.Handle(200, "RepoAssignment", err)
  43. return
  44. }
  45. } else {
  46. user = ctx.User
  47. }
  48. if user == nil {
  49. if redirect {
  50. ctx.Redirect("/")
  51. return
  52. }
  53. ctx.Handle(200, "RepoAssignment", errors.New("invliad user account for single repository"))
  54. return
  55. }
  56. // get repository
  57. repo, err := models.GetRepositoryByName(user.Id, repoName)
  58. if err != nil {
  59. if err == models.ErrRepoNotExist {
  60. ctx.Handle(404, "RepoAssignment", err)
  61. } else if redirect {
  62. ctx.Redirect("/")
  63. return
  64. }
  65. ctx.Handle(404, "RepoAssignment", err)
  66. return
  67. }
  68. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  69. ctx.Repo.Repository = repo
  70. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  71. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  72. if err != nil {
  73. ctx.Handle(404, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  74. return
  75. }
  76. ctx.Repo.GitRepo = gitRepo
  77. ctx.Repo.Owner = user
  78. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  79. ctx.Data["Title"] = user.Name + "/" + repo.Name
  80. ctx.Data["Repository"] = repo
  81. ctx.Data["Owner"] = user
  82. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  83. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  84. ctx.Data["BranchName"] = ""
  85. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  86. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
  87. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  88. // when repo is bare, not valid branch
  89. if !ctx.Repo.Repository.IsBare && validBranch {
  90. detect:
  91. if len(branchName) > 0 {
  92. // TODO check tag
  93. if models.IsBranchExist(user.Name, repoName, branchName) {
  94. ctx.Repo.IsBranch = true
  95. ctx.Repo.BranchName = branchName
  96. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName)
  97. if err != nil {
  98. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  99. return
  100. }
  101. ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String()
  102. } else if len(branchName) == 40 {
  103. ctx.Repo.IsCommit = true
  104. ctx.Repo.CommitId = branchName
  105. ctx.Repo.BranchName = branchName
  106. ctx.Repo.Commit, err = gitRepo.GetCommit(branchName)
  107. if err != nil {
  108. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  109. return
  110. }
  111. } else {
  112. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  113. return
  114. }
  115. } else {
  116. branchName = "master"
  117. goto detect
  118. }
  119. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  120. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  121. }
  122. // repo is bare and display enable
  123. if displayBare && ctx.Repo.Repository.IsBare {
  124. ctx.HTML(200, "repo/single_bare")
  125. return
  126. }
  127. if ctx.IsSigned {
  128. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  129. }
  130. ctx.Data["BranchName"] = ctx.Repo.BranchName
  131. ctx.Data["CommitId"] = ctx.Repo.CommitId
  132. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  133. }
  134. }