pull.go 4.6 KB

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