@@ -738,6 +738,7 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
738738}
739739
740740func 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
758773func 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}
0 commit comments