branch.go 3.2 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(ctx *context.Context) []*Branch {
  22. rawBranches, err := ctx.Repo.Repository.GetBranches()
  23. if err != nil {
  24. ctx.Handle(500, "GetBranches", err)
  25. return nil
  26. }
  27. protectBranches, err := models.GetProtectBranchesByRepoID(ctx.Repo.Repository.ID)
  28. if err != nil {
  29. ctx.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. ctx.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. ctx.Data["AllowPullRequest"] = ctx.Repo.Repository.AllowsPulls()
  51. return branches
  52. }
  53. func Branches(ctx *context.Context) {
  54. ctx.Data["Title"] = ctx.Tr("repo.git_branches")
  55. ctx.Data["PageIsBranchesOverview"] = true
  56. branches := loadBranches(ctx)
  57. if ctx.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 == ctx.Repo.BranchName:
  66. ctx.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. ctx.Data["ActiveBranches"] = activeBranches
  74. ctx.Data["StaleBranches"] = staleBranches
  75. ctx.HTML(200, BRANCHES_OVERVIEW)
  76. }
  77. func AllBranches(ctx *context.Context) {
  78. ctx.Data["Title"] = ctx.Tr("repo.git_branches")
  79. ctx.Data["PageIsBranchesAll"] = true
  80. branches := loadBranches(ctx)
  81. if ctx.Written() {
  82. return
  83. }
  84. ctx.Data["Branches"] = branches
  85. ctx.HTML(200, BRANCHES_ALL)
  86. }
  87. func DeleteBranchPost(ctx *context.Context) {
  88. branchName := ctx.Params("*")
  89. commitID := ctx.Query("commit")
  90. defer func() {
  91. redirectTo := ctx.Query("redirect_to")
  92. if len(redirectTo) == 0 {
  93. redirectTo = ctx.Repo.RepoLink
  94. }
  95. ctx.Redirect(redirectTo)
  96. }()
  97. if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
  98. return
  99. }
  100. if len(commitID) > 0 {
  101. branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
  102. if err != nil {
  103. log.Error(2, "GetBranchCommitID: %v", err)
  104. return
  105. }
  106. if branchCommitID != commitID {
  107. ctx.Flash.Error(ctx.Tr("repo.pulls.delete_branch_has_new_commits"))
  108. return
  109. }
  110. }
  111. if err := ctx.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. }