branch.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "gitote/gitote/pkg/tool"
  11. "time"
  12. raven "github.com/getsentry/raven-go"
  13. "gitlab.com/gitote/git-module"
  14. api "gitlab.com/gitote/go-gitote-client"
  15. log "gopkg.in/clog.v1"
  16. )
  17. const (
  18. // BranchesOverview page template
  19. BranchesOverview = "repo/branches/overview"
  20. // BranchesAll page template
  21. BranchesAll = "repo/branches/all"
  22. )
  23. type Branch struct {
  24. Name string
  25. Commit *git.Commit
  26. IsProtected bool
  27. }
  28. func loadBranches(c *context.Context) []*Branch {
  29. rawBranches, err := c.Repo.Repository.GetBranches()
  30. if err != nil {
  31. c.Handle(500, "GetBranches", err)
  32. return nil
  33. }
  34. protectBranches, err := models.GetProtectBranchesByRepoID(c.Repo.Repository.ID)
  35. if err != nil {
  36. c.Handle(500, "GetProtectBranchesByRepoID", err)
  37. return nil
  38. }
  39. branches := make([]*Branch, len(rawBranches))
  40. for i := range rawBranches {
  41. commit, err := rawBranches[i].GetCommit()
  42. if err != nil {
  43. c.Handle(500, "GetCommit", err)
  44. return nil
  45. }
  46. branches[i] = &Branch{
  47. Name: rawBranches[i].Name,
  48. Commit: commit,
  49. }
  50. for j := range protectBranches {
  51. if branches[i].Name == protectBranches[j].Name {
  52. branches[i].IsProtected = true
  53. break
  54. }
  55. }
  56. }
  57. c.Data["AllowPullRequest"] = c.Repo.Repository.AllowsPulls()
  58. return branches
  59. }
  60. // Branches shows branches page
  61. func Branches(c *context.Context) {
  62. c.Data["Title"] = c.Tr("repo.git_branches")
  63. c.Data["PageIsBranchesOverview"] = true
  64. branches := loadBranches(c)
  65. if c.Written() {
  66. return
  67. }
  68. now := time.Now()
  69. activeBranches := make([]*Branch, 0, 3)
  70. staleBranches := make([]*Branch, 0, 3)
  71. for i := range branches {
  72. switch {
  73. case branches[i].Name == c.Repo.BranchName:
  74. c.Data["DefaultBranch"] = branches[i]
  75. case branches[i].Commit.Committer.When.Add(30 * 24 * time.Hour).After(now): // 30 days
  76. activeBranches = append(activeBranches, branches[i])
  77. case branches[i].Commit.Committer.When.Add(3 * 30 * 24 * time.Hour).Before(now): // 90 days
  78. staleBranches = append(staleBranches, branches[i])
  79. }
  80. }
  81. c.Data["ActiveBranches"] = activeBranches
  82. c.Data["StaleBranches"] = staleBranches
  83. c.HTML(200, BranchesOverview)
  84. }
  85. // AllBranches shows all branches page
  86. func AllBranches(c *context.Context) {
  87. c.Data["Title"] = c.Tr("repo.git_branches")
  88. c.Data["PageIsBranchesAll"] = true
  89. branches := loadBranches(c)
  90. if c.Written() {
  91. return
  92. }
  93. c.Data["Branches"] = branches
  94. c.HTML(200, BranchesAll)
  95. }
  96. func DeleteBranchPost(c *context.Context) {
  97. branchName := c.Params("*")
  98. commitID := c.Query("commit")
  99. defer func() {
  100. redirectTo := c.Query("redirect_to")
  101. if !tool.IsSameSiteURLPath(redirectTo) {
  102. redirectTo = c.Repo.RepoLink
  103. }
  104. c.Redirect(redirectTo)
  105. }()
  106. if !c.Repo.GitRepo.IsBranchExist(branchName) {
  107. return
  108. }
  109. if len(commitID) > 0 {
  110. branchCommitID, err := c.Repo.GitRepo.GetBranchCommitID(branchName)
  111. if err != nil {
  112. raven.CaptureErrorAndWait(err, nil)
  113. log.Error(2, "Failed to get commit ID of branch %q: %v", branchName, err)
  114. return
  115. }
  116. if branchCommitID != commitID {
  117. c.Flash.Error(c.Tr("repo.pulls.delete_branch_has_new_commits"))
  118. return
  119. }
  120. }
  121. if err := c.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  122. Force: true,
  123. }); err != nil {
  124. raven.CaptureErrorAndWait(err, nil)
  125. log.Error(2, "Failed to delete branch %q: %v", branchName, err)
  126. return
  127. }
  128. if err := models.PrepareWebhooks(c.Repo.Repository, models.HOOK_EVENT_DELETE, &api.DeletePayload{
  129. Ref: branchName,
  130. RefType: "branch",
  131. PusherType: api.PUSHER_TYPE_USER,
  132. Repo: c.Repo.Repository.APIFormat(nil),
  133. Sender: c.User.APIFormat(),
  134. }); err != nil {
  135. return
  136. }
  137. }