You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 repo
  5. import (
  6. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/middleware"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. const (
  14. FORK base.TplName = "repo/pulls/fork"
  15. PULLS base.TplName = "repo/pulls"
  16. )
  17. func getForkRepository(ctx *middleware.Context) *models.Repository {
  18. forkRepo, err := models.GetRepositoryById(ctx.ParamsInt64(":repoid"))
  19. if err != nil {
  20. if models.IsErrRepoNotExist(err) {
  21. ctx.Handle(404, "GetRepositoryById", nil)
  22. } else {
  23. ctx.Handle(500, "GetRepositoryById", err)
  24. }
  25. return nil
  26. }
  27. ctx.Data["repo_name"] = forkRepo.Name
  28. ctx.Data["desc"] = forkRepo.Description
  29. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  30. if err = forkRepo.GetOwner(); err != nil {
  31. ctx.Handle(500, "GetOwner", err)
  32. return nil
  33. }
  34. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  35. if err := ctx.User.GetOrganizations(); err != nil {
  36. ctx.Handle(500, "GetOrganizations", err)
  37. return nil
  38. }
  39. ctx.Data["Orgs"] = ctx.User.Orgs
  40. return forkRepo
  41. }
  42. func Fork(ctx *middleware.Context) {
  43. ctx.Data["Title"] = ctx.Tr("new_fork")
  44. getForkRepository(ctx)
  45. if ctx.Written() {
  46. return
  47. }
  48. ctx.Data["ContextUser"] = ctx.User
  49. ctx.HTML(200, FORK)
  50. }
  51. func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
  52. ctx.Data["Title"] = ctx.Tr("new_fork")
  53. forkRepo := getForkRepository(ctx)
  54. if ctx.Written() {
  55. return
  56. }
  57. ctxUser := checkContextUser(ctx, form.Uid)
  58. if ctx.Written() {
  59. return
  60. }
  61. ctx.Data["ContextUser"] = ctxUser
  62. if ctx.HasError() {
  63. ctx.HTML(200, FORK)
  64. return
  65. }
  66. repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.Id)
  67. if has {
  68. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  69. return
  70. }
  71. // Check ownership of organization.
  72. if ctxUser.IsOrganization() {
  73. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  74. ctx.Error(403)
  75. return
  76. }
  77. }
  78. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  79. if err != nil {
  80. switch {
  81. case models.IsErrRepoAlreadyExist(err):
  82. ctx.Data["Err_RepoName"] = true
  83. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  84. case models.IsErrNameReserved(err):
  85. ctx.Data["Err_RepoName"] = true
  86. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  87. case models.IsErrNamePatternNotAllowed(err):
  88. ctx.Data["Err_RepoName"] = true
  89. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  90. default:
  91. ctx.Handle(500, "ForkPost", err)
  92. }
  93. return
  94. }
  95. log.Trace("Repository forked[%d]: %s/%s", forkRepo.Id, ctxUser.Name, repo.Name)
  96. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  97. }
  98. func Pulls(ctx *middleware.Context) {
  99. ctx.Data["IsRepoToolbarPulls"] = true
  100. ctx.HTML(200, PULLS)
  101. }