repo.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/gogs/models"
  11. "github.com/gogits/gogs/modules/base"
  12. )
  13. func RepoAssignment(redirect bool) martini.Handler {
  14. return func(ctx *Context, params martini.Params) {
  15. // assign false first
  16. ctx.Data["IsRepositoryValid"] = false
  17. var (
  18. user *models.User
  19. err error
  20. )
  21. // get repository owner
  22. ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(params["username"])
  23. if !ctx.Repo.IsOwner {
  24. user, err = models.GetUserByName(params["username"])
  25. if err != nil {
  26. if redirect {
  27. ctx.Redirect("/")
  28. return
  29. }
  30. ctx.Handle(200, "RepoAssignment", err)
  31. return
  32. }
  33. } else {
  34. user = ctx.User
  35. }
  36. if user == nil {
  37. if redirect {
  38. ctx.Redirect("/")
  39. return
  40. }
  41. ctx.Handle(200, "RepoAssignment", errors.New("invliad user account for single repository"))
  42. return
  43. }
  44. ctx.Repo.Owner = user
  45. // get repository
  46. repo, err := models.GetRepositoryByName(user.Id, params["reponame"])
  47. if err != nil {
  48. if redirect {
  49. ctx.Redirect("/")
  50. return
  51. }
  52. ctx.Handle(200, "RepoAssignment", err)
  53. return
  54. }
  55. ctx.Repo.IsValid = true
  56. if ctx.User != nil {
  57. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  58. }
  59. ctx.Repo.Repository = repo
  60. ctx.Repo.CloneLink.SSH = fmt.Sprintf("git@%s:%s/%s.git", base.Domain, user.LowerName, repo.LowerName)
  61. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("https://%s/%s/%s.git", base.Domain, user.LowerName, repo.LowerName)
  62. ctx.Data["IsRepositoryValid"] = true
  63. ctx.Data["Repository"] = repo
  64. ctx.Data["Owner"] = user
  65. ctx.Data["Title"] = user.Name + "/" + repo.Name
  66. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  67. ctx.Data["RepositoryLink"] = ctx.Data["Title"]
  68. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  69. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  70. }
  71. }