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 2.3 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
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. scheme := "http"
  61. if base.EnableHttpsClone {
  62. scheme = "https"
  63. }
  64. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  65. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s://%s/%s/%s.git", scheme, base.Domain, user.LowerName, repo.LowerName)
  66. if len(params["branchname"]) == 0 {
  67. params["branchname"] = "master"
  68. }
  69. ctx.Data["Branchname"] = params["branchname"]
  70. ctx.Data["IsRepositoryValid"] = true
  71. ctx.Data["Repository"] = repo
  72. ctx.Data["Owner"] = user
  73. ctx.Data["Title"] = user.Name + "/" + repo.Name
  74. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  75. ctx.Data["RepositoryLink"] = ctx.Data["Title"]
  76. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  77. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  78. }
  79. }