branch.go 3.4 KB

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