gitea源码

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