]> source.dussan.org Git - gitea.git/commitdiff
Get latest commit statuses from database instead of git data on dashboard for reposit...
authorLunny Xiao <xiaolunwen@gmail.com>
Mon, 3 Jul 2023 01:53:05 +0000 (09:53 +0800)
committerGitHub <noreply@github.com>
Mon, 3 Jul 2023 01:53:05 +0000 (01:53 +0000)
related #24638

models/git/branch_list.go
routers/web/repo/repo.go

index da78248c0bc0171f44ae09a7eff0395d844f48bb..836d4ffc41777bb181073e8d7e2c66b740f56daf 100644 (file)
@@ -130,3 +130,20 @@ func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, err
        }
        return branches, nil
 }
+
+func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64]string) (map[int64]string, error) {
+       cond := builder.NewCond()
+       for repoID, branchName := range repoBranches {
+               cond = cond.Or(builder.And(builder.Eq{"repo_id": repoID}, builder.Eq{"name": branchName}))
+       }
+       var branches []*Branch
+       if err := db.GetEngine(ctx).
+               Where(cond).Find(&branches); err != nil {
+               return nil, err
+       }
+       branchMap := make(map[int64]string, len(branches))
+       for _, branch := range branches {
+               branchMap[branch.RepoID] = branch.CommitID
+       }
+       return branchMap, nil
+}
index a1e1346b38c4b38c17ee73d7bff6ec7c5e7f0ea3..781a12b2d92707ea7039218ae67143b617be5bd1 100644 (file)
@@ -579,13 +579,15 @@ func SearchRepo(ctx *context.Context) {
 
        // collect the latest commit of each repo
        // at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment
-       repoIDsToLatestCommitSHAs := make(map[int64]string, len(repos))
+       repoBranchNames := make(map[int64]string, len(repos))
        for _, repo := range repos {
-               commitID, err := repo_service.GetBranchCommitID(ctx, repo, repo.DefaultBranch)
-               if err != nil {
-                       continue
-               }
-               repoIDsToLatestCommitSHAs[repo.ID] = commitID
+               repoBranchNames[repo.ID] = repo.DefaultBranch
+       }
+
+       repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNames)
+       if err != nil {
+               log.Error("FindBranchesByRepoAndBranchName: %v", err)
+               return
        }
 
        // call the database O(1) times to get the commit statuses for all repos