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.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. git_model "code.gitea.io/gitea/models/git"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/container"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. )
  14. // SyncRepoBranches synchronizes branch table with repository branches
  15. func SyncRepoBranches(ctx context.Context, repoID, doerID int64) (int64, error) {
  16. repo, err := repo_model.GetRepositoryByID(ctx, repoID)
  17. if err != nil {
  18. return 0, err
  19. }
  20. log.Debug("SyncRepoBranches: in Repo[%d:%s]", repo.ID, repo.FullName())
  21. gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
  22. if err != nil {
  23. log.Error("OpenRepository[%s]: %w", repo.RepoPath(), err)
  24. return 0, err
  25. }
  26. defer gitRepo.Close()
  27. return SyncRepoBranchesWithRepo(ctx, repo, gitRepo, doerID)
  28. }
  29. func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, doerID int64) (int64, error) {
  30. allBranches := container.Set[string]{}
  31. {
  32. branches, _, err := gitRepo.GetBranchNames(0, 0)
  33. if err != nil {
  34. return 0, err
  35. }
  36. log.Trace("SyncRepoBranches[%s]: branches[%d]: %v", repo.FullName(), len(branches), branches)
  37. for _, branch := range branches {
  38. allBranches.Add(branch)
  39. }
  40. }
  41. dbBranches := make(map[string]*git_model.Branch)
  42. {
  43. branches, err := git_model.FindBranches(ctx, git_model.FindBranchOptions{
  44. ListOptions: db.ListOptions{
  45. ListAll: true,
  46. },
  47. RepoID: repo.ID,
  48. })
  49. if err != nil {
  50. return 0, err
  51. }
  52. for _, branch := range branches {
  53. dbBranches[branch.Name] = branch
  54. }
  55. }
  56. var toAdd []*git_model.Branch
  57. var toUpdate []*git_model.Branch
  58. var toRemove []int64
  59. for branch := range allBranches {
  60. dbb := dbBranches[branch]
  61. commit, err := gitRepo.GetBranchCommit(branch)
  62. if err != nil {
  63. return 0, err
  64. }
  65. if dbb == nil {
  66. toAdd = append(toAdd, &git_model.Branch{
  67. RepoID: repo.ID,
  68. Name: branch,
  69. CommitID: commit.ID.String(),
  70. CommitMessage: commit.Summary(),
  71. PusherID: doerID,
  72. CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
  73. })
  74. } else if commit.ID.String() != dbb.CommitID {
  75. toUpdate = append(toUpdate, &git_model.Branch{
  76. ID: dbb.ID,
  77. RepoID: repo.ID,
  78. Name: branch,
  79. CommitID: commit.ID.String(),
  80. CommitMessage: commit.Summary(),
  81. PusherID: doerID,
  82. CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
  83. })
  84. }
  85. }
  86. for _, dbBranch := range dbBranches {
  87. if !allBranches.Contains(dbBranch.Name) && !dbBranch.IsDeleted {
  88. toRemove = append(toRemove, dbBranch.ID)
  89. }
  90. }
  91. log.Trace("SyncRepoBranches[%s]: toAdd: %v, toUpdate: %v, toRemove: %v", repo.FullName(), toAdd, toUpdate, toRemove)
  92. if len(toAdd) == 0 && len(toRemove) == 0 && len(toUpdate) == 0 {
  93. return int64(len(allBranches)), nil
  94. }
  95. if err := db.WithTx(ctx, func(ctx context.Context) error {
  96. if len(toAdd) > 0 {
  97. if err := git_model.AddBranches(ctx, toAdd); err != nil {
  98. return err
  99. }
  100. }
  101. for _, b := range toUpdate {
  102. if _, err := db.GetEngine(ctx).ID(b.ID).
  103. Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted").
  104. Update(b); err != nil {
  105. return err
  106. }
  107. }
  108. if len(toRemove) > 0 {
  109. if err := git_model.DeleteBranches(ctx, repo.ID, doerID, toRemove); err != nil {
  110. return err
  111. }
  112. }
  113. return nil
  114. }); err != nil {
  115. return 0, err
  116. }
  117. return int64(len(allBranches)), nil
  118. }