Skip to content

Commit 646d24e

Browse files
committed
Add cron task to update the wrong commits counts
1 parent 269a4fa commit 646d24e

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

models/git/branch.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type Branch struct {
109109
Repo *repo_model.Repository `xorm:"-"`
110110
Name string `xorm:"UNIQUE(s) NOT NULL"` // git's ref-name is case-sensitive internally, however, in some databases (mssql, mysql, by default), it's case-insensitive at the moment
111111
CommitID string
112+
CommitCountID string // the commit id of the commit count
112113
CommitCount int64 // the number of commits in this branch
113114
CommitMessage string `xorm:"TEXT"` // it only stores the message summary (the first line)
114115
PusherID int64
@@ -252,11 +253,12 @@ func UpdateBranch(ctx context.Context, repoID, pusherID int64, branchName string
252253
})
253254
}
254255

255-
func UpdateBranchCommitCount(ctx context.Context, repoID int64, branchName string, commitCount int64) error {
256+
func UpdateBranchCommitCount(ctx context.Context, repoID int64, branchName, commitID string, commitCount int64) error {
256257
_, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repoID, branchName).
257258
Cols("commit_count").
258259
Update(&Branch{
259-
CommitCount: commitCount,
260+
CommitCount: commitCount,
261+
CommitCountID: commitID,
260262
})
261263
return err
262264
}

models/migrations/v1_24/v318.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
func AddBranchCommitsCount(x *xorm.Engine) error {
1111
type Branch struct {
12-
CommitCount int64 // the number of commits in this branch
12+
CommitCountID string // the commit id of the commit count
13+
CommitCount int64 // the number of commits in this branch
1314
}
1415
return x.Sync(new(Branch))
1516
}

services/cron/tasks_basic.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ func registerSyncRepoLicenses() {
166166
})
167167
}
168168

169+
func registerSyncBranchCommitsCount() {
170+
RegisterTaskFatal("sync_branch_commits_count", &BaseConfig{
171+
Enabled: true,
172+
RunAtStart: true,
173+
Schedule: "@midnight",
174+
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
175+
return repo_service.SyncBranchCommitsCount(ctx)
176+
})
177+
}
178+
169179
func initBasicTasks() {
170180
if setting.Mirror.Enabled {
171181
registerUpdateMirrorTask()
@@ -183,4 +193,5 @@ func initBasicTasks() {
183193
registerCleanupPackages()
184194
}
185195
registerSyncRepoLicenses()
196+
registerSyncBranchCommitsCount()
186197
}

services/repository/branch.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,47 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
736736
info.BaseHasNewCommits = info.HeadCommitsBehind > 0
737737
return info, nil
738738
}
739+
740+
func SyncBranchCommitsCount(ctx context.Context) error {
741+
for {
742+
select {
743+
case <-ctx.Done():
744+
return nil
745+
default:
746+
}
747+
748+
// search all branches commits count are not synced
749+
750+
for _, branch := range branches {
751+
if err := syncBranchCommitsCount(ctx, branch); err != nil {
752+
log.Error("syncBranchCommitsCount: %v", err)
753+
}
754+
}
755+
}
756+
}
757+
758+
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")
771+
}
772+
773+
commit, err := gitrepo.GetCommit(ctx, branch.Repo, commitID)
774+
775+
commitsCount, err := commit.CommitsCount()
776+
if err != nil {
777+
return err
778+
}
779+
780+
git_model.UpdateBranchCommitCount(ctx, branch.RepoID, branch.Name, commit.ID, commitsCount)
781+
return nil
782+
}

0 commit comments

Comments
 (0)