pull.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. ctx.Data["Err_RepoName"] = true
  85. switch {
  86. case models.IsErrRepoAlreadyExist(err):
  87. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  88. case models.IsErrNameReserved(err):
  89. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  90. case models.IsErrNamePatternNotAllowed(err):
  91. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  92. default:
  93. ctx.Handle(500, "ForkPost", err)
  94. }
  95. return
  96. }
  97. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  98. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  99. }
  100. func CompareAndPullRequest(ctx *middleware.Context) {
  101. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  102. ctx.Data["PageIsComparePull"] = true
  103. repo := ctx.Repo.Repository
  104. // Get compare branch information.
  105. infos := strings.Split(ctx.Params("*"), "...")
  106. if len(infos) != 2 {
  107. ctx.Handle(404, "CompareAndPullRequest", nil)
  108. return
  109. }
  110. baseBranch := infos[0]
  111. ctx.Data["BaseBranch"] = baseBranch
  112. headInfos := strings.Split(infos[1], ":")
  113. if len(headInfos) != 2 {
  114. ctx.Handle(404, "CompareAndPullRequest", nil)
  115. return
  116. }
  117. headUser := headInfos[0]
  118. headBranch := headInfos[1]
  119. ctx.Data["HeadBranch"] = headBranch
  120. // TODO: check if branches are valid.
  121. fmt.Println(baseBranch, headUser, headBranch)
  122. // TODO: add organization support
  123. // Check if current user has fork of repository.
  124. headRepo, has := models.HasForkedRepo(ctx.User.Id, repo.ID)
  125. if !has {
  126. ctx.Handle(404, "HasForkedRepo", nil)
  127. return
  128. }
  129. headGitRepo, err := git.OpenRepository(models.RepoPath(ctx.User.Name, headRepo.Name))
  130. if err != nil {
  131. ctx.Handle(500, "OpenRepository", err)
  132. return
  133. }
  134. headBranches, err := headGitRepo.GetBranches()
  135. if err != nil {
  136. ctx.Handle(500, "GetBranches", err)
  137. return
  138. }
  139. ctx.Data["HeadBranches"] = headBranches
  140. // Setup information for new form.
  141. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  142. if ctx.Written() {
  143. return
  144. }
  145. // Get diff information.
  146. ctx.HTML(200, COMPARE_PULL)
  147. }
  148. func Pulls(ctx *middleware.Context) {
  149. ctx.Data["IsRepoToolbarPulls"] = true
  150. ctx.HTML(200, PULLS)
  151. }