branch.go 3.6 KB

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