branch.go 3.6 KB

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