branch.go 3.1 KB

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