branch.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/base"
  11. "github.com/gogits/gogs/pkg/context"
  12. )
  13. const (
  14. BRANCHES_OVERVIEW base.TplName = "repo/branches/overview"
  15. BRANCHES_ALL base.TplName = "repo/branches/all"
  16. )
  17. type Branch struct {
  18. Name string
  19. Commit *git.Commit
  20. IsProtected bool
  21. }
  22. func loadBranches(ctx *context.Context) []*Branch {
  23. rawBranches, err := ctx.Repo.Repository.GetBranches()
  24. if err != nil {
  25. ctx.Handle(500, "GetBranches", err)
  26. return nil
  27. }
  28. protectBranches, err := models.GetProtectBranchesByRepoID(ctx.Repo.Repository.ID)
  29. if err != nil {
  30. ctx.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. ctx.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. ctx.Data["AllowPullRequest"] = ctx.Repo.Repository.AllowsPulls()
  52. return branches
  53. }
  54. func Branches(ctx *context.Context) {
  55. ctx.Data["Title"] = ctx.Tr("repo.git_branches")
  56. ctx.Data["PageIsBranchesOverview"] = true
  57. branches := loadBranches(ctx)
  58. if ctx.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 == ctx.Repo.BranchName:
  67. ctx.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. ctx.Data["ActiveBranches"] = activeBranches
  75. ctx.Data["StaleBranches"] = staleBranches
  76. ctx.HTML(200, BRANCHES_OVERVIEW)
  77. }
  78. func AllBranches(ctx *context.Context) {
  79. ctx.Data["Title"] = ctx.Tr("repo.git_branches")
  80. ctx.Data["PageIsBranchesAll"] = true
  81. branches := loadBranches(ctx)
  82. if ctx.Written() {
  83. return
  84. }
  85. ctx.Data["Branches"] = branches
  86. ctx.HTML(200, BRANCHES_ALL)
  87. }
  88. func DeleteBranchPost(ctx *context.Context) {
  89. branchName := ctx.Params("*")
  90. commitID := ctx.Query("commit")
  91. defer func() {
  92. redirectTo := ctx.Query("redirect_to")
  93. if len(redirectTo) == 0 {
  94. redirectTo = ctx.Repo.RepoLink
  95. }
  96. ctx.Redirect(redirectTo)
  97. }()
  98. if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
  99. return
  100. }
  101. if len(commitID) > 0 {
  102. branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
  103. if err != nil {
  104. log.Error(2, "GetBranchCommitID: %v", err)
  105. return
  106. }
  107. if branchCommitID != commitID {
  108. ctx.Flash.Error(ctx.Tr("repo.pulls.delete_branch_has_new_commits"))
  109. return
  110. }
  111. }
  112. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  113. Force: true,
  114. }); err != nil {
  115. log.Error(2, "DeleteBranch '%s': %v", branchName, err)
  116. return
  117. }
  118. }