Skip to content

Commit 78b47ba

Browse files
committed
Add background cron task to fix wrong branch commit count information
1 parent 646d24e commit 78b47ba

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

models/git/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func UpdateBranch(ctx context.Context, repoID, pusherID int64, branchName string
255255

256256
func UpdateBranchCommitCount(ctx context.Context, repoID int64, branchName, commitID string, commitCount int64) error {
257257
_, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repoID, branchName).
258-
Cols("commit_count").
258+
Cols("commit_count", "commit_count_id").
259259
Update(&Branch{
260260
CommitCount: commitCount,
261261
CommitCountID: commitID,

models/git/branch_list.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,20 @@ func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64
146146
}
147147
return branchMap, nil
148148
}
149+
150+
func FindCommitsCountOutdatedBranches(ctx context.Context, startID, limit int64) (BranchList, error) {
151+
var branches BranchList
152+
if err := db.GetEngine(ctx).
153+
Join("INNER", "repository", "branch.repo_id = repository.id").
154+
And("repository.is_empty = ?", false).
155+
Where("id > ?", startID).
156+
And(
157+
builder.Expr("commit_count_id IS NULL").
158+
Or(builder.Expr("commit_id <> commit_count_id")),
159+
).
160+
Asc("id").
161+
Limit(int(limit)).Find(&branches); err != nil {
162+
return nil, err
163+
}
164+
return branches, nil
165+
}

modules/gitrepo/commit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/git"
10+
)
11+
12+
func GetCommit(ctx context.Context, repo Repository, commitID string) (*git.Commit, error) {
13+
gitRepo, err := git.OpenRepository(ctx, repoPath(repo))
14+
if err != nil {
15+
return nil, err
16+
}
17+
defer gitRepo.Close()
18+
return gitRepo.GetCommit(commitID)
19+
}
20+
21+
func CommitsCount(ctx context.Context, repo Repository, ref string) (int64, error) {
22+
return git.CommitsCount(ctx, git.CommitsCountOptions{
23+
RepoPath: repoPath(repo),
24+
Revision: []string{ref},
25+
})
26+
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,7 @@ dashboard.sync_branch.started = Branches Sync started
30163016
dashboard.sync_tag.started = Tags Sync started
30173017
dashboard.rebuild_issue_indexer = Rebuild issue indexer
30183018
dashboard.sync_repo_licenses = Sync repo licenses
3019+
dashboard.sync_branch_commits_count = Sync Branch commits counts
30193020
30203021
users.user_manage_panel = User Account Management
30213022
users.new_account = Create User Account

services/repository/branch.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
738738
}
739739

740740
func SyncBranchCommitsCount(ctx context.Context) error {
741+
startID := int64(0)
741742
for {
742743
select {
743744
case <-ctx.Done():
@@ -746,8 +747,22 @@ func SyncBranchCommitsCount(ctx context.Context) error {
746747
}
747748

748749
// search all branches commits count are not synced
750+
branches, err := git_model.FindCommitsCountOutdatedBranches(ctx, startID, 100)
751+
if err != nil {
752+
return err
753+
}
754+
if len(branches) == 0 {
755+
return nil
756+
}
757+
758+
if err := branches.LoadRepo(ctx); err != nil {
759+
return err
760+
}
749761

750762
for _, branch := range branches {
763+
if branch.ID > startID {
764+
startID = branch.ID
765+
}
751766
if err := syncBranchCommitsCount(ctx, branch); err != nil {
752767
log.Error("syncBranchCommitsCount: %v", err)
753768
}
@@ -756,27 +771,14 @@ func SyncBranchCommitsCount(ctx context.Context) error {
756771
}
757772

758773
func syncBranchCommitsCount(ctx context.Context, branch *git_model.Branch) error {
759-
if err := branch.LoadRepo(ctx); err != nil {
760-
return err
761-
}
762-
commitID, err := gitrepo.GetBranchCommitID(ctx, branch.Repo, branch.Name)
763-
if err != nil {
764-
return err
765-
}
766-
767-
var cols []string
768-
if commitID != branch.CommitID {
769-
branch.CommitID = commitID
770-
cols = append(cols, "commit_id")
774+
if branch.CommitID == "" {
775+
return nil
771776
}
772777

773-
commit, err := gitrepo.GetCommit(ctx, branch.Repo, commitID)
774-
775-
commitsCount, err := commit.CommitsCount()
778+
commitsCount, err := gitrepo.CommitsCount(ctx, branch.Repo, branch.CommitID)
776779
if err != nil {
777780
return err
778781
}
779782

780-
git_model.UpdateBranchCommitCount(ctx, branch.RepoID, branch.Name, commit.ID, commitsCount)
781-
return nil
783+
return git_model.UpdateBranchCommitCount(ctx, branch.RepoID, branch.Name, branch.CommitID, commitsCount)
782784
}

services/repository/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func UpdateRepoBranchCommitsCount(ctx context.Context, repo *repo_model.Reposito
302302
if err != nil {
303303
return fmt.Errorf("newCommit.CommitsCount: %w", err)
304304
}
305-
return git_model.UpdateBranchCommitCount(ctx, repo.ID, branch, commitsCount)
305+
return git_model.UpdateBranchCommitCount(ctx, repo.ID, branch, newCommit.ID.String(), commitsCount)
306306
}
307307

308308
func pushUpdateBranch(ctx context.Context, repo *repo_model.Repository, pusher *user_model.User, opts *repo_module.PushUpdateOptions, newCommit *git.Commit) ([]*git.Commit, error) {

0 commit comments

Comments
 (0)