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.4 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
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 err == models.ErrRepoNotExist {
  49. ctx.Handle(404, "RepoAssignment", err)
  50. } else if redirect {
  51. ctx.Redirect("/")
  52. return
  53. }
  54. ctx.Handle(200, "RepoAssignment", err)
  55. return
  56. }
  57. ctx.Repo.IsValid = true
  58. if ctx.User != nil {
  59. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  60. }
  61. ctx.Repo.Repository = repo
  62. scheme := "http"
  63. if base.EnableHttpsClone {
  64. scheme = "https"
  65. }
  66. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  67. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s://%s/%s/%s.git", scheme, base.Domain, user.LowerName, repo.LowerName)
  68. if len(params["branchname"]) == 0 {
  69. params["branchname"] = "master"
  70. }
  71. ctx.Data["Branchname"] = params["branchname"]
  72. ctx.Data["IsRepositoryValid"] = true
  73. ctx.Data["Repository"] = repo
  74. ctx.Data["Owner"] = user
  75. ctx.Data["Title"] = user.Name + "/" + repo.Name
  76. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  77. ctx.Data["RepositoryLink"] = ctx.Data["Title"]
  78. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  79. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  80. }
  81. }