Skip to content

Commit 43472b1

Browse files
committed
Fix dependency recycle
1 parent a4df01b commit 43472b1

File tree

14 files changed

+83
-66
lines changed

14 files changed

+83
-66
lines changed

models/git/branch.go

Lines changed: 10 additions & 0 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+
CommitCount int64 // the number of commits in this branch
112113
CommitMessage string `xorm:"TEXT"` // it only stores the message summary (the first line)
113114
PusherID int64
114115
Pusher *user_model.User `xorm:"-"`
@@ -251,6 +252,15 @@ func UpdateBranch(ctx context.Context, repoID, pusherID int64, branchName string
251252
})
252253
}
253254

255+
func UpdateBranchCommitCount(ctx context.Context, repoID int64, branchName string, commitCount int64) error {
256+
_, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repoID, branchName).
257+
Cols("commit_count").
258+
Update(&Branch{
259+
CommitCount: commitCount,
260+
})
261+
return err
262+
}
263+
254264
// AddDeletedBranch adds a deleted branch to the database
255265
func AddDeletedBranch(ctx context.Context, repoID int64, branchName string, deletedByID int64) error {
256266
branch, err := GetBranch(ctx, repoID, branchName)

models/migrations/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func prepareMigrationTasks() []*migration {
378378
newMigration(315, "Add Ephemeral to ActionRunner", v1_24.AddEphemeralToActionRunner),
379379
newMigration(316, "Add description for secrets and variables", v1_24.AddDescriptionForSecretsAndVariables),
380380
newMigration(317, "Add new index for action for heatmap", v1_24.AddNewIndexForUserDashboard),
381+
newMigration(318, "Add branch commits count for branch table", v1_24.AddBranchCommitsCount),
381382
}
382383
return preparedMigrations
383384
}

models/migrations/v1_24/v318.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_24 //nolint
5+
6+
import (
7+
"xorm.io/xorm"
8+
)
9+
10+
func AddBranchCommitsCount(x *xorm.Engine) error {
11+
type Branch struct {
12+
CommitCount int64 // the number of commits in this branch
13+
}
14+
return x.Sync(new(Branch))
15+
}

models/repo/repo.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,6 @@ func (repo *Repository) APIURL() string {
376376
return setting.AppURL + "api/v1/repos/" + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
377377
}
378378

379-
// GetCommitsCountCacheKey returns cache key used for commits count caching.
380-
func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool) string {
381-
var prefix string
382-
if isRef {
383-
prefix = "ref"
384-
} else {
385-
prefix = "commit"
386-
}
387-
return fmt.Sprintf("commits-count-%d-%s-%s", repo.ID, prefix, contextName)
388-
}
389-
390379
// LoadUnits loads repo units into repo.Units
391380
func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
392381
if repo.Units != nil {

modules/git/repo_commit.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,18 +502,8 @@ func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err e
502502
return len(stdout) > 0, err
503503
}
504504

505-
func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error {
505+
func (repo *Repository) AddLastCommitCache(commitsCount int64, fullName, sha string) error {
506506
if repo.LastCommitCache == nil {
507-
commitsCount, err := cache.GetInt64(cacheKey, func() (int64, error) {
508-
commit, err := repo.GetCommit(sha)
509-
if err != nil {
510-
return 0, err
511-
}
512-
return commit.CommitsCount()
513-
})
514-
if err != nil {
515-
return err
516-
}
517507
repo.LastCommitCache = NewLastCommitCache(commitsCount, fullName, repo, cache.GetCache())
518508
}
519509
return nil

routers/api/v1/utils/git.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/gitrepo"
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/services/context"
15+
repo_service "code.gitea.io/gitea/services/repository"
1516
)
1617

1718
// ResolveRefOrSha resolve ref to sha if exist
@@ -38,7 +39,8 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
3839
sha = MustConvertToSHA1(ctx, ctx.Repo, sha)
3940

4041
if ctx.Repo.GitRepo != nil {
41-
err := ctx.Repo.GitRepo.AddLastCommitCache(ctx.Repo.Repository.GetCommitsCountCacheKey(ref, ref != sha), ctx.Repo.Repository.FullName(), sha)
42+
commitsCount, _ := repo_service.GetRefCommitsCount(ctx, ctx.Repo.Repository.ID, git.RefName(ref))
43+
err := ctx.Repo.GitRepo.AddLastCommitCache(commitsCount, ctx.Repo.Repository.FullName(), sha)
4244
if err != nil {
4345
log.Error("Unable to get commits count for %s in %s. Error: %v", sha, ctx.Repo.Repository.FullName(), err)
4446
}

modules/actions/artifacts.go renamed to routers/common/artifacts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package actions
4+
package common
55

66
import (
77
"net/http"

routers/web/repo/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func FileHistory(ctx *context.Context) {
263263
}
264264

265265
func LoadBranchesAndTags(ctx *context.Context) {
266-
response, err := repo_service.LoadBranchesAndTags(ctx, ctx.Repo, ctx.PathParam("sha"))
266+
response, err := repo_service.LoadBranchesAndTags(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.RepoLink, ctx.PathParam("sha"))
267267
if err == nil {
268268
ctx.JSON(http.StatusOK, response)
269269
return

services/context/repo.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"code.gitea.io/gitea/modules/setting"
3333
"code.gitea.io/gitea/modules/util"
3434
asymkey_service "code.gitea.io/gitea/services/asymkey"
35+
repo_service "code.gitea.io/gitea/services/repository"
3536

3637
"github.com/editorconfig/editorconfig-core-go/v2"
3738
)
@@ -164,15 +165,11 @@ func (r *Repository) CanCreateIssueDependencies(ctx context.Context, user *user_
164165
}
165166

166167
// GetCommitsCount returns cached commit count for current view
167-
func (r *Repository) GetCommitsCount() (int64, error) {
168+
func (r *Repository) GetCommitsCount(ctx context.Context) (int64, error) {
168169
if r.Commit == nil {
169170
return 0, nil
170171
}
171-
contextName := r.RefFullName.ShortName()
172-
isRef := r.RefFullName.IsBranch() || r.RefFullName.IsTag()
173-
return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, isRef), func() (int64, error) {
174-
return r.Commit.CommitsCount()
175-
})
172+
return repo_service.GetRefCommitsCount(ctx, r.Repository.ID, r.RefFullName)
176173
}
177174

178175
// GetCommitGraphsCount returns cached commit count for current view
@@ -782,7 +779,7 @@ func RepoRefByDefaultBranch() func(*Context) {
782779
ctx.Repo.RefFullName = git.RefNameFromBranch(ctx.Repo.Repository.DefaultBranch)
783780
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
784781
ctx.Repo.Commit, _ = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
785-
ctx.Repo.CommitsCount, _ = ctx.Repo.GetCommitsCount()
782+
ctx.Repo.CommitsCount, _ = ctx.Repo.GetCommitsCount(ctx)
786783
ctx.Data["RefFullName"] = ctx.Repo.RefFullName
787784
ctx.Data["BranchName"] = ctx.Repo.BranchName
788785
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
@@ -931,7 +928,7 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
931928

932929
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() // only used by the branch selector dropdown: AllowCreateNewRef
933930

934-
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
931+
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount(ctx)
935932
if err != nil {
936933
ctx.ServerError("GetCommitsCount", err)
937934
return

services/mirror/mirror_pull.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
repo_model "code.gitea.io/gitea/models/repo"
1313
system_model "code.gitea.io/gitea/models/system"
14-
"code.gitea.io/gitea/modules/cache"
1514
"code.gitea.io/gitea/modules/git"
1615
giturl "code.gitea.io/gitea/modules/git/url"
1716
"code.gitea.io/gitea/modules/gitrepo"
@@ -411,17 +410,6 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
411410
log.Trace("SyncMirrors [repo: %-v Wiki]: git remote update complete", m.Repo)
412411
}
413412

414-
log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo)
415-
branches, _, err := gitrepo.GetBranchesByPath(ctx, m.Repo, 0, 0)
416-
if err != nil {
417-
log.Error("SyncMirrors [repo: %-v]: failed to GetBranches: %v", m.Repo, err)
418-
return nil, false
419-
}
420-
421-
for _, branch := range branches {
422-
cache.Remove(m.Repo.GetCommitsCountCacheKey(branch.Name, true))
423-
}
424-
425413
m.UpdatedUnix = timeutil.TimeStampNow()
426414
return parseRemoteUpdateOutput(output, m.GetRemoteName()), true
427415
}
@@ -616,6 +604,8 @@ func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, re
616604
hasDefault = hasDefault || name == defaultBranchName
617605
hasMaster = hasMaster || name == "master"
618606
hasMain = hasMain || name == "main"
607+
608+
// TODO: update branch commits count
619609
}
620610

621611
if len(firstName) > 0 {

0 commit comments

Comments
 (0)