gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. git_model "code.gitea.io/gitea/models/git"
  6. access_model "code.gitea.io/gitea/models/perm/access"
  7. unit_model "code.gitea.io/gitea/models/unit"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/services/context"
  10. repo_service "code.gitea.io/gitea/services/repository"
  11. )
  12. type RecentBranchesPromptDataStruct struct {
  13. RecentlyPushedNewBranches []*git_model.RecentlyPushedNewBranch
  14. }
  15. func prepareRecentlyPushedNewBranches(ctx *context.Context) {
  16. if ctx.Doer == nil {
  17. return
  18. }
  19. if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
  20. log.Error("GetBaseRepo: %v", err)
  21. return
  22. }
  23. opts := git_model.FindRecentlyPushedNewBranchesOptions{
  24. Repo: ctx.Repo.Repository,
  25. BaseRepo: ctx.Repo.Repository,
  26. }
  27. if ctx.Repo.Repository.IsFork {
  28. opts.BaseRepo = ctx.Repo.Repository.BaseRepo
  29. }
  30. baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
  31. if err != nil {
  32. log.Error("GetUserRepoPermission: %v", err)
  33. return
  34. }
  35. if !opts.Repo.CanContentChange() || !opts.BaseRepo.CanContentChange() {
  36. return
  37. }
  38. if !opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || !baseRepoPerm.CanRead(unit_model.TypePullRequests) {
  39. return
  40. }
  41. var finalBranches []*git_model.RecentlyPushedNewBranch
  42. branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts)
  43. if err != nil {
  44. log.Error("FindRecentlyPushedNewBranches failed: %v", err)
  45. return
  46. }
  47. for _, branch := range branches {
  48. divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
  49. branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
  50. opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
  51. )
  52. if err != nil {
  53. log.Error("GetBranchDivergingInfo failed: %v", err)
  54. continue
  55. }
  56. branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
  57. baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
  58. if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
  59. finalBranches = append(finalBranches, branch)
  60. }
  61. }
  62. if len(finalBranches) > 0 {
  63. ctx.Data["RecentBranchesPromptData"] = RecentBranchesPromptDataStruct{finalBranches}
  64. }
  65. }