gitea源码

branch.go 3.9KB

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