branch.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. BRANCHES_OVERVIEW = "repo/branches/overview"
  19. BRANCHES_ALL = "repo/branches/all"
  20. )
  21. type Branch struct {
  22. Name string
  23. Commit *git.Commit
  24. IsProtected bool
  25. }
  26. func loadBranches(c *context.Context) []*Branch {
  27. rawBranches, err := c.Repo.Repository.GetBranches()
  28. if err != nil {
  29. c.Handle(500, "GetBranches", err)
  30. return nil
  31. }
  32. protectBranches, err := models.GetProtectBranchesByRepoID(c.Repo.Repository.ID)
  33. if err != nil {
  34. c.Handle(500, "GetProtectBranchesByRepoID", err)
  35. return nil
  36. }
  37. branches := make([]*Branch, len(rawBranches))
  38. for i := range rawBranches {
  39. commit, err := rawBranches[i].GetCommit()
  40. if err != nil {
  41. c.Handle(500, "GetCommit", err)
  42. return nil
  43. }
  44. branches[i] = &Branch{
  45. Name: rawBranches[i].Name,
  46. Commit: commit,
  47. }
  48. for j := range protectBranches {
  49. if branches[i].Name == protectBranches[j].Name {
  50. branches[i].IsProtected = true
  51. break
  52. }
  53. }
  54. }
  55. c.Data["AllowPullRequest"] = c.Repo.Repository.AllowsPulls()
  56. return branches
  57. }
  58. func Branches(c *context.Context) {
  59. c.Data["Title"] = c.Tr("repo.git_branches")
  60. c.Data["PageIsBranchesOverview"] = true
  61. branches := loadBranches(c)
  62. if c.Written() {
  63. return
  64. }
  65. now := time.Now()
  66. activeBranches := make([]*Branch, 0, 3)
  67. staleBranches := make([]*Branch, 0, 3)
  68. for i := range branches {
  69. switch {
  70. case branches[i].Name == c.Repo.BranchName:
  71. c.Data["DefaultBranch"] = branches[i]
  72. case branches[i].Commit.Committer.When.Add(30 * 24 * time.Hour).After(now): // 30 days
  73. activeBranches = append(activeBranches, branches[i])
  74. case branches[i].Commit.Committer.When.Add(3 * 30 * 24 * time.Hour).Before(now): // 90 days
  75. staleBranches = append(staleBranches, branches[i])
  76. }
  77. }
  78. c.Data["ActiveBranches"] = activeBranches
  79. c.Data["StaleBranches"] = staleBranches
  80. c.HTML(200, BRANCHES_OVERVIEW)
  81. }
  82. func AllBranches(c *context.Context) {
  83. c.Data["Title"] = c.Tr("repo.git_branches")
  84. c.Data["PageIsBranchesAll"] = true
  85. branches := loadBranches(c)
  86. if c.Written() {
  87. return
  88. }
  89. c.Data["Branches"] = branches
  90. c.HTML(200, BRANCHES_ALL)
  91. }
  92. func DeleteBranchPost(c *context.Context) {
  93. branchName := c.Params("*")
  94. commitID := c.Query("commit")
  95. defer func() {
  96. redirectTo := c.Query("redirect_to")
  97. if !tool.IsSameSiteURLPath(redirectTo) {
  98. redirectTo = c.Repo.RepoLink
  99. }
  100. c.Redirect(redirectTo)
  101. }()
  102. if !c.Repo.GitRepo.IsBranchExist(branchName) {
  103. return
  104. }
  105. if len(commitID) > 0 {
  106. branchCommitID, err := c.Repo.GitRepo.GetBranchCommitID(branchName)
  107. if err != nil {
  108. raven.CaptureErrorAndWait(err, nil)
  109. log.Error(2, "Failed to get commit ID of branch %q: %v", branchName, err)
  110. return
  111. }
  112. if branchCommitID != commitID {
  113. c.Flash.Error(c.Tr("repo.pulls.delete_branch_has_new_commits"))
  114. return
  115. }
  116. }
  117. if err := c.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  118. Force: true,
  119. }); err != nil {
  120. raven.CaptureErrorAndWait(err, nil)
  121. log.Error(2, "Failed to delete branch %q: %v", branchName, err)
  122. return
  123. }
  124. if err := models.PrepareWebhooks(c.Repo.Repository, models.HOOK_EVENT_DELETE, &api.DeletePayload{
  125. Ref: branchName,
  126. RefType: "branch",
  127. PusherType: api.PUSHER_TYPE_USER,
  128. Repo: c.Repo.Repository.APIFormat(nil),
  129. Sender: c.User.APIFormat(),
  130. }); err != nil {
  131. return
  132. }
  133. }