From 6d5a85007ca2f19635332edcb0b980c552a78204 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 13 Apr 2025 15:55:22 -0700 Subject: [PATCH 1/3] Use tag's database num commits instead of read the commit counts from git data --- services/context/repo.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/services/context/repo.go b/services/context/repo.go index 6f5c772f5e994..5b18e0d14e958 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -927,13 +927,27 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() // only used by the branch selector dropdown: AllowCreateNewRef - ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount() - if err != nil { - ctx.ServerError("GetCommitsCount", err) - return + // if it's a tag, we just get the commits count from database + if ctx.Repo.RefFullName.IsTag() { + rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, ctx.Repo.RefFullName.TagName()) + if err != nil { + if repo_model.IsErrReleaseNotExist(err) { + ctx.NotFound(err) + return + } + ctx.ServerError("GetRelease", err) + return + } + ctx.Repo.CommitsCount = rel.NumCommits + } else { + ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount() + if err != nil { + ctx.ServerError("GetCommitsCount", err) + return + } + ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount + ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache()) } - ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount - ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache()) } } From ba0e7be1874e9dba02e8009e9d9a3b8ef8c0a92b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 13 Apr 2025 16:04:55 -0700 Subject: [PATCH 2/3] Update num commits if commits count is less than zero --- models/repo/release.go | 5 +++++ services/context/repo.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/models/repo/release.go b/models/repo/release.go index 663d310bc027d..b4637665ab6f7 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -161,6 +161,11 @@ func UpdateRelease(ctx context.Context, rel *Release) error { return err } +func UpdateReleaseNumCommits(ctx context.Context, rel *Release) error { + _, err := db.GetEngine(ctx).ID(rel.ID).Cols("num_commits").Update(rel) + return err +} + // AddReleaseAttachments adds a release attachments func AddReleaseAttachments(ctx context.Context, releaseID int64, attachmentUUIDs []string) (err error) { // Check attachments diff --git a/services/context/repo.go b/services/context/repo.go index 5b18e0d14e958..acf829abee7b5 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -938,6 +938,18 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { ctx.ServerError("GetRelease", err) return } + // for mirror tags, the number of commist may not be set + if rel.NumCommits <= 0 { + rel.NumCommits, err = ctx.Repo.GetCommitsCount() + if err != nil { + ctx.ServerError("GetCommitsCount", err) + return + } + if err := repo_model.UpdateReleaseNumCommits(ctx, rel); err != nil { + ctx.ServerError("UpdateReleaseNumCommits", err) + return + } + } ctx.Repo.CommitsCount = rel.NumCommits } else { ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount() From d2b9cab1dada3d46223b70f3c619f1dc0602e5a0 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 13 Apr 2025 16:07:38 -0700 Subject: [PATCH 3/3] Fix bug --- services/context/repo.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/services/context/repo.go b/services/context/repo.go index acf829abee7b5..64f929829f066 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -920,11 +920,8 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { ctx.Data["RefFullName"] = ctx.Repo.RefFullName ctx.Data["RefTypeNameSubURL"] = ctx.Repo.RefTypeNameSubURL() ctx.Data["TreePath"] = ctx.Repo.TreePath - ctx.Data["BranchName"] = ctx.Repo.BranchName - ctx.Data["CommitID"] = ctx.Repo.CommitID - ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() // only used by the branch selector dropdown: AllowCreateNewRef // if it's a tag, we just get the commits count from database @@ -957,9 +954,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { ctx.ServerError("GetCommitsCount", err) return } - ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount - ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache()) } + ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount + ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache()) } }