branch.go 3.9 KB

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