pull.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "fmt"
  7. "strings"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/middleware"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. const (
  17. FORK base.TplName = "repo/pulls/fork"
  18. COMPARE_PULL base.TplName = "repo/pulls/compare"
  19. PULLS base.TplName = "repo/pulls"
  20. )
  21. func getForkRepository(ctx *middleware.Context) *models.Repository {
  22. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  23. if err != nil {
  24. if models.IsErrRepoNotExist(err) {
  25. ctx.Handle(404, "GetRepositoryByID", nil)
  26. } else {
  27. ctx.Handle(500, "GetRepositoryByID", err)
  28. }
  29. return nil
  30. }
  31. ctx.Data["repo_name"] = forkRepo.Name
  32. ctx.Data["desc"] = forkRepo.Description
  33. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  34. if err = forkRepo.GetOwner(); err != nil {
  35. ctx.Handle(500, "GetOwner", err)
  36. return nil
  37. }
  38. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  39. if err := ctx.User.GetOrganizations(); err != nil {
  40. ctx.Handle(500, "GetOrganizations", err)
  41. return nil
  42. }
  43. ctx.Data["Orgs"] = ctx.User.Orgs
  44. return forkRepo
  45. }
  46. func Fork(ctx *middleware.Context) {
  47. ctx.Data["Title"] = ctx.Tr("new_fork")
  48. getForkRepository(ctx)
  49. if ctx.Written() {
  50. return
  51. }
  52. ctx.Data["ContextUser"] = ctx.User
  53. ctx.HTML(200, FORK)
  54. }
  55. func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
  56. ctx.Data["Title"] = ctx.Tr("new_fork")
  57. forkRepo := getForkRepository(ctx)
  58. if ctx.Written() {
  59. return
  60. }
  61. ctxUser := checkContextUser(ctx, form.Uid)
  62. if ctx.Written() {
  63. return
  64. }
  65. ctx.Data["ContextUser"] = ctxUser
  66. if ctx.HasError() {
  67. ctx.HTML(200, FORK)
  68. return
  69. }
  70. repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID)
  71. if has {
  72. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  73. return
  74. }
  75. // Check ownership of organization.
  76. if ctxUser.IsOrganization() {
  77. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  78. ctx.Error(403)
  79. return
  80. }
  81. }
  82. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  83. if err != nil {
  84. switch {
  85. case models.IsErrRepoAlreadyExist(err):
  86. ctx.Data["Err_RepoName"] = true
  87. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  88. case models.IsErrNameReserved(err):
  89. ctx.Data["Err_RepoName"] = true
  90. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  91. case models.IsErrNamePatternNotAllowed(err):
  92. ctx.Data["Err_RepoName"] = true
  93. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  94. default:
  95. ctx.Handle(500, "ForkPost", err)
  96. }
  97. return
  98. }
  99. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  100. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  101. }
  102. func CompareAndPullRequest(ctx *middleware.Context) {
  103. // Get compare information.
  104. infos := strings.Split(ctx.Params("*"), "...")
  105. if len(infos) != 2 {
  106. ctx.Handle(404, "CompareAndPullRequest", nil)
  107. return
  108. }
  109. baseBranch := infos[0]
  110. ctx.Data["BaseBranch"] = baseBranch
  111. headInfos := strings.Split(infos[1], ":")
  112. if len(headInfos) != 2 {
  113. ctx.Handle(404, "CompareAndPullRequest", nil)
  114. return
  115. }
  116. headUser := headInfos[0]
  117. headBranch := headInfos[1]
  118. ctx.Data["HeadBranch"] = headBranch
  119. // TODO: check if branches are valid.
  120. fmt.Println(baseBranch, headUser, headBranch)
  121. // TODO: add organization support
  122. // Check if current user has fork of repository.
  123. headRepo, has := models.HasForkedRepo(ctx.User.Id, ctx.Repo.Repository.ID)
  124. if !has {
  125. ctx.Handle(404, "HasForkedRepo", nil)
  126. return
  127. }
  128. headGitRepo, err := git.OpenRepository(models.RepoPath(ctx.User.Name, headRepo.Name))
  129. if err != nil {
  130. ctx.Handle(500, "OpenRepository", err)
  131. return
  132. }
  133. headBranches, err := headGitRepo.GetBranches()
  134. if err != nil {
  135. ctx.Handle(500, "GetBranches", err)
  136. return
  137. }
  138. ctx.Data["HeadBranches"] = headBranches
  139. ctx.HTML(200, COMPARE_PULL)
  140. }
  141. func Pulls(ctx *middleware.Context) {
  142. ctx.Data["IsRepoToolbarPulls"] = true
  143. ctx.HTML(200, PULLS)
  144. }