Skip to content

Commit 09dc1cd

Browse files
committed
Some fix
1 parent 29f6a3f commit 09dc1cd

File tree

8 files changed

+40
-17
lines changed

8 files changed

+40
-17
lines changed

models/repo/repo.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ 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+
379390
// LoadUnits loads repo units into repo.Units
380391
func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
381392
if repo.Units != nil {

routers/api/v1/utils/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
3838
sha = MustConvertToSHA1(ctx, ctx.Repo, sha)
3939

4040
if ctx.Repo.GitRepo != nil {
41-
commitsCount, _ := context.GetRefCommitsCount(ctx, ctx.Repo.Repository.ID, git.RefName(ref))
41+
commitsCount, _ := context.GetRefCommitsCount(ctx, ctx.Repo.Repository.ID, ctx.Repo.GitRepo, git.RefName(ref))
4242
err := ctx.Repo.GitRepo.AddLastCommitCache(commitsCount, ctx.Repo.Repository.FullName(), sha)
4343
if err != nil {
4444
log.Error("Unable to get commits count for %s in %s. Error: %v", sha, ctx.Repo.Repository.FullName(), err)

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.Repository, ctx.Repo.GitRepo, ctx.Repo.RepoLink, ctx.PathParam("sha"))
266+
response, err := repo_service.LoadBranchesAndTags(ctx, ctx.Repo, ctx.PathParam("sha"))
267267
if err == nil {
268268
ctx.JSON(http.StatusOK, response)
269269
return

services/context/repo.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ func (r *Repository) CanCreateIssueDependencies(ctx context.Context, user *user_
163163
return r.Repository.IsDependenciesEnabled(ctx) && r.Permission.CanWriteIssuesOrPulls(isPull)
164164
}
165165

166-
func GetRefCommitsCount(ctx context.Context, repoID int64, refFullName git.RefName) (int64, error) {
166+
// getCommitsCountCacheKey returns cache key used for commits count caching.
167+
func getCommitsCountCacheKey(contextName string, repoID int64) string {
168+
return fmt.Sprintf("commits-count-%d-commit-%s", repoID, contextName)
169+
}
170+
171+
func GetRefCommitsCount(ctx context.Context, repoID int64, gitRepo *git.Repository, refFullName git.RefName) (int64, error) {
167172
// Get the commit count of the branch or the tag
168173
switch {
169174
case refFullName.IsBranch():
@@ -178,6 +183,14 @@ func GetRefCommitsCount(ctx context.Context, repoID int64, refFullName git.RefNa
178183
return 0, err
179184
}
180185
return tag.NumCommits, nil
186+
case refFullName.RefType() == git.RefTypeCommit:
187+
return cache.GetInt64(getCommitsCountCacheKey(string(refFullName), repoID), func() (int64, error) {
188+
commit, err := gitRepo.GetCommit(string(refFullName))
189+
if err != nil {
190+
return 0, err
191+
}
192+
return commit.CommitsCount()
193+
})
181194
default:
182195
return 0, nil
183196
}
@@ -188,7 +201,7 @@ func (r *Repository) GetCommitsCount(ctx context.Context) (int64, error) {
188201
if r.Commit == nil {
189202
return 0, nil
190203
}
191-
return GetRefCommitsCount(ctx, r.Repository.ID, r.RefFullName)
204+
return GetRefCommitsCount(ctx, r.Repository.ID, r.GitRepo, r.RefFullName)
192205
}
193206

194207
// GetCommitGraphsCount returns cached commit count for current view

services/mirror/mirror_pull.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
410410
log.Trace("SyncMirrors [repo: %-v Wiki]: git remote update complete", m.Repo)
411411
}
412412

413-
repo_service.SyncRepoBranchesCommitsCount(ctx, m.Repo)
413+
if err := repo_service.SyncRepoBranchesCommitsCount(ctx, m.Repo); err != nil {
414+
log.Error("SyncMirrors [repo: %-v]: failed to sync branches commits count: %v", m.Repo, err)
415+
}
414416

415417
m.UpdatedUnix = timeutil.TimeStampNow()
416418
return parseRemoteUpdateOutput(output, m.GetRemoteName()), true
@@ -606,8 +608,6 @@ func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, re
606608
hasDefault = hasDefault || name == defaultBranchName
607609
hasMaster = hasMaster || name == "master"
608610
hasMain = hasMain || name == "main"
609-
610-
// TODO: update branch commits count
611611
}
612612

613613
if len(firstName) > 0 {

services/repository/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep
2020
}
2121

2222
if gitRepo.LastCommitCache == nil {
23-
commitsCount, err := context_service.GetRefCommitsCount(ctx, repo.ID, fullRefName)
23+
commitsCount, err := context_service.GetRefCommitsCount(ctx, repo.ID, gitRepo, fullRefName)
2424
if err != nil {
2525
return err
2626
}

services/repository/commit.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"context"
88
"fmt"
99

10-
repo_model "code.gitea.io/gitea/models/repo"
11-
"code.gitea.io/gitea/modules/git"
1210
"code.gitea.io/gitea/modules/util"
11+
gitea_ctx "code.gitea.io/gitea/services/context"
1312
)
1413

1514
type ContainedLinks struct { // TODO: better name?
@@ -24,32 +23,32 @@ type namedLink struct { // TODO: better name?
2423
}
2524

2625
// LoadBranchesAndTags creates a new repository branch
27-
func LoadBranchesAndTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, repoLink, commitSHA string) (*ContainedLinks, error) {
28-
containedTags, err := gitRepo.ListOccurrences(ctx, "tag", commitSHA)
26+
func LoadBranchesAndTags(ctx context.Context, baseRepo *gitea_ctx.Repository, commitSHA string) (*ContainedLinks, error) {
27+
containedTags, err := baseRepo.GitRepo.ListOccurrences(ctx, "tag", commitSHA)
2928
if err != nil {
3029
return nil, fmt.Errorf("encountered a problem while querying %s: %w", "tags", err)
3130
}
32-
containedBranches, err := gitRepo.ListOccurrences(ctx, "branch", commitSHA)
31+
containedBranches, err := baseRepo.GitRepo.ListOccurrences(ctx, "branch", commitSHA)
3332
if err != nil {
3433
return nil, fmt.Errorf("encountered a problem while querying %s: %w", "branches", err)
3534
}
3635

3736
result := &ContainedLinks{
38-
DefaultBranch: repo.DefaultBranch,
37+
DefaultBranch: baseRepo.Repository.DefaultBranch,
3938
Branches: make([]*namedLink, 0, len(containedBranches)),
4039
Tags: make([]*namedLink, 0, len(containedTags)),
4140
}
4241
for _, tag := range containedTags {
4342
// TODO: Use a common method to get the link to a branch/tag instead of hard-coding it here
4443
result.Tags = append(result.Tags, &namedLink{
4544
Name: tag,
46-
WebLink: fmt.Sprintf("%s/src/tag/%s", repoLink, util.PathEscapeSegments(tag)),
45+
WebLink: fmt.Sprintf("%s/src/tag/%s", baseRepo.RepoLink, util.PathEscapeSegments(tag)),
4746
})
4847
}
4948
for _, branch := range containedBranches {
5049
result.Branches = append(result.Branches, &namedLink{
5150
Name: branch,
52-
WebLink: fmt.Sprintf("%s/src/branch/%s", repoLink, util.PathEscapeSegments(branch)),
51+
WebLink: fmt.Sprintf("%s/src/branch/%s", baseRepo.RepoLink, util.PathEscapeSegments(branch)),
5352
})
5453
}
5554
return result, nil

services/repository/files/content.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
168168

169169
refName := git.RefNameFromObjectTypeAndShortName(refType, ref)
170170

171-
commitsCount, _ := context_service.GetRefCommitsCount(ctx, repo.ID, refName)
171+
commitsCount, _ := context_service.GetRefCommitsCount(ctx, repo.ID, gitRepo, refName)
172172

173173
err = gitRepo.AddLastCommitCache(commitsCount, repo.FullName(), commitID)
174174
if err != nil {

0 commit comments

Comments
 (0)