You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

branch_list.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. user_model "code.gitea.io/gitea/models/user"
  8. "code.gitea.io/gitea/modules/container"
  9. "code.gitea.io/gitea/modules/util"
  10. "xorm.io/builder"
  11. "xorm.io/xorm"
  12. )
  13. type BranchList []*Branch
  14. func (branches BranchList) LoadDeletedBy(ctx context.Context) error {
  15. ids := container.Set[int64]{}
  16. for _, branch := range branches {
  17. if !branch.IsDeleted {
  18. continue
  19. }
  20. ids.Add(branch.DeletedByID)
  21. }
  22. usersMap := make(map[int64]*user_model.User, len(ids))
  23. if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&usersMap); err != nil {
  24. return err
  25. }
  26. for _, branch := range branches {
  27. if !branch.IsDeleted {
  28. continue
  29. }
  30. branch.DeletedBy = usersMap[branch.DeletedByID]
  31. if branch.DeletedBy == nil {
  32. branch.DeletedBy = user_model.NewGhostUser()
  33. }
  34. }
  35. return nil
  36. }
  37. func (branches BranchList) LoadPusher(ctx context.Context) error {
  38. ids := container.Set[int64]{}
  39. for _, branch := range branches {
  40. if branch.PusherID > 0 { // pusher_id maybe zero because some branches are sync by backend with no pusher
  41. ids.Add(branch.PusherID)
  42. }
  43. }
  44. usersMap := make(map[int64]*user_model.User, len(ids))
  45. if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&usersMap); err != nil {
  46. return err
  47. }
  48. for _, branch := range branches {
  49. if branch.PusherID <= 0 {
  50. continue
  51. }
  52. branch.Pusher = usersMap[branch.PusherID]
  53. if branch.Pusher == nil {
  54. branch.Pusher = user_model.NewGhostUser()
  55. }
  56. }
  57. return nil
  58. }
  59. type FindBranchOptions struct {
  60. db.ListOptions
  61. RepoID int64
  62. ExcludeBranchNames []string
  63. IsDeletedBranch util.OptionalBool
  64. OrderBy string
  65. Keyword string
  66. }
  67. func (opts FindBranchOptions) ToConds() builder.Cond {
  68. cond := builder.NewCond()
  69. if opts.RepoID > 0 {
  70. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  71. }
  72. if len(opts.ExcludeBranchNames) > 0 {
  73. cond = cond.And(builder.NotIn("name", opts.ExcludeBranchNames))
  74. }
  75. if !opts.IsDeletedBranch.IsNone() {
  76. cond = cond.And(builder.Eq{"is_deleted": opts.IsDeletedBranch.IsTrue()})
  77. }
  78. if opts.Keyword != "" {
  79. cond = cond.And(builder.Like{"name", opts.Keyword})
  80. }
  81. return cond
  82. }
  83. func CountBranches(ctx context.Context, opts FindBranchOptions) (int64, error) {
  84. return db.GetEngine(ctx).Where(opts.ToConds()).Count(&Branch{})
  85. }
  86. func orderByBranches(sess *xorm.Session, opts FindBranchOptions) *xorm.Session {
  87. if !opts.IsDeletedBranch.IsFalse() { // if deleted branch included, put them at the end
  88. sess = sess.OrderBy("is_deleted ASC")
  89. }
  90. if opts.OrderBy == "" {
  91. // the commit_time might be the same, so add the "name" to make sure the order is stable
  92. opts.OrderBy = "commit_time DESC, name ASC"
  93. }
  94. return sess.OrderBy(opts.OrderBy)
  95. }
  96. func FindBranches(ctx context.Context, opts FindBranchOptions) (BranchList, error) {
  97. sess := db.GetEngine(ctx).Where(opts.ToConds())
  98. if opts.PageSize > 0 && !opts.IsListAll() {
  99. sess = db.SetSessionPagination(sess, &opts.ListOptions)
  100. }
  101. sess = orderByBranches(sess, opts)
  102. var branches []*Branch
  103. return branches, sess.Find(&branches)
  104. }
  105. func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, error) {
  106. sess := db.GetEngine(ctx).Select("name").Where(opts.ToConds())
  107. if opts.PageSize > 0 && !opts.IsListAll() {
  108. sess = db.SetSessionPagination(sess, &opts.ListOptions)
  109. }
  110. sess = orderByBranches(sess, opts)
  111. var branches []string
  112. if err := sess.Table("branch").Find(&branches); err != nil {
  113. return nil, err
  114. }
  115. return branches, nil
  116. }
  117. func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64]string) (map[int64]string, error) {
  118. cond := builder.NewCond()
  119. for repoID, branchName := range repoBranches {
  120. cond = cond.Or(builder.And(builder.Eq{"repo_id": repoID}, builder.Eq{"name": branchName}))
  121. }
  122. var branches []*Branch
  123. if err := db.GetEngine(ctx).
  124. Where(cond).Find(&branches); err != nil {
  125. return nil, err
  126. }
  127. branchMap := make(map[int64]string, len(branches))
  128. for _, branch := range branches {
  129. branchMap[branch.RepoID] = branch.CommitID
  130. }
  131. return branchMap, nil
  132. }