Skip to content

Commit 269a4fa

Browse files
committed
Fix bug
1 parent 43472b1 commit 269a4fa

File tree

8 files changed

+45
-30
lines changed

8 files changed

+45
-30
lines changed

routers/common/artifacts.go renamed to modules/actions/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 common
4+
package actions
55

66
import (
77
"net/http"

modules/git/ref.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func RefNameFromCommit(shortName string) RefName {
8484
return RefName(shortName)
8585
}
8686

87+
func RefNameFromObjectTypeAndShortName(objectType ObjectType, shortName string) RefName {
88+
switch objectType {
89+
case ObjectBranch:
90+
return RefNameFromBranch(shortName)
91+
case ObjectTag:
92+
return RefNameFromTag(shortName)
93+
case ObjectCommit:
94+
return RefNameFromCommit(shortName)
95+
}
96+
return RefName(shortName)
97+
}
98+
8799
func (ref RefName) String() string {
88100
return string(ref)
89101
}

routers/api/v1/utils/git.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ 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"
1615
)
1716

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

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

routers/web/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func CommitInfoCache(ctx *context.Context) {
7171
ctx.ServerError("GetBranchCommit", err)
7272
return
7373
}
74-
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
74+
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount(ctx)
7575
if err != nil {
7676
ctx.ServerError("GetCommitsCount", err)
7777
return

services/context/repo.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ 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"
3635

3736
"github.com/editorconfig/editorconfig-core-go/v2"
3837
)
@@ -164,12 +163,32 @@ func (r *Repository) CanCreateIssueDependencies(ctx context.Context, user *user_
164163
return r.Repository.IsDependenciesEnabled(ctx) && r.Permission.CanWriteIssuesOrPulls(isPull)
165164
}
166165

166+
func GetRefCommitsCount(ctx context.Context, repoID int64, refFullName git.RefName) (int64, error) {
167+
// Get the commit count of the branch or the tag
168+
switch {
169+
case refFullName.IsBranch():
170+
branch, err := git_model.GetBranch(ctx, repoID, refFullName.BranchName())
171+
if err != nil {
172+
return 0, err
173+
}
174+
return branch.CommitCount, nil
175+
case refFullName.IsTag():
176+
tag, err := repo_model.GetRelease(ctx, repoID, refFullName.TagName())
177+
if err != nil {
178+
return 0, err
179+
}
180+
return tag.NumCommits, nil
181+
default:
182+
return 0, nil
183+
}
184+
}
185+
167186
// GetCommitsCount returns cached commit count for current view
168187
func (r *Repository) GetCommitsCount(ctx context.Context) (int64, error) {
169188
if r.Commit == nil {
170189
return 0, nil
171190
}
172-
return repo_service.GetRefCommitsCount(ctx, r.Repository.ID, r.RefFullName)
191+
return GetRefCommitsCount(ctx, r.Repository.ID, r.RefFullName)
173192
}
174193

175194
// GetCommitGraphsCount returns cached commit count for current view

services/repository/cache.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,12 @@ package repository
66
import (
77
"context"
88

9-
git_model "code.gitea.io/gitea/models/git"
109
repo_model "code.gitea.io/gitea/models/repo"
1110
"code.gitea.io/gitea/modules/cache"
1211
"code.gitea.io/gitea/modules/git"
12+
context_service "code.gitea.io/gitea/services/context"
1313
)
1414

15-
func GetRefCommitsCount(ctx context.Context, repoID int64, refFullName git.RefName) (int64, error) {
16-
// Get the commit count of the branch or the tag
17-
switch {
18-
case refFullName.IsBranch():
19-
branch, err := git_model.GetBranch(ctx, repoID, refFullName.BranchName())
20-
if err != nil {
21-
return 0, err
22-
}
23-
return branch.CommitCount, nil
24-
case refFullName.IsTag():
25-
tag, err := repo_model.GetRelease(ctx, repoID, refFullName.TagName())
26-
if err != nil {
27-
return 0, err
28-
}
29-
return tag.NumCommits, nil
30-
default:
31-
return 0, nil
32-
}
33-
}
34-
3515
// CacheRef cachhe last commit information of the branch or the tag
3616
func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName git.RefName) error {
3717
commit, err := gitRepo.GetCommit(fullRefName.String())
@@ -40,7 +20,7 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep
4020
}
4121

4222
if gitRepo.LastCommitCache == nil {
43-
commitsCount, err := GetRefCommitsCount(ctx, repo.ID, fullRefName)
23+
commitsCount, err := context_service.GetRefCommitsCount(ctx, repo.ID, fullRefName)
4424
if err != nil {
4525
return err
4626
}

services/repository/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type namedLink struct { // TODO: better name?
2424
}
2525

2626
// LoadBranchesAndTags creates a new repository branch
27-
func LoadBranchesAndTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, repoLink string, commitSHA string) (*ContainedLinks, error) {
27+
func LoadBranchesAndTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, repoLink, commitSHA string) (*ContainedLinks, error) {
2828
containedTags, err := gitRepo.ListOccurrences(ctx, "tag", commitSHA)
2929
if err != nil {
3030
return nil, fmt.Errorf("encountered a problem while querying %s: %w", "tags", err)

services/repository/files/content.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/setting"
1717
api "code.gitea.io/gitea/modules/structs"
1818
"code.gitea.io/gitea/modules/util"
19+
context_service "code.gitea.io/gitea/services/context"
1920
)
2021

2122
// ContentType repo content type
@@ -165,7 +166,11 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
165166
}
166167
selfURLString := selfURL.String()
167168

168-
err = gitRepo.AddLastCommitCache(repo.GetCommitsCountCacheKey(ref, refType != git.ObjectCommit), repo.FullName(), commitID)
169+
refName := git.RefNameFromObjectTypeAndShortName(refType, ref)
170+
171+
commitsCount, _ := context_service.GetRefCommitsCount(ctx, repo.ID, refName)
172+
173+
err = gitRepo.AddLastCommitCache(commitsCount, repo.FullName(), commitID)
169174
if err != nil {
170175
return nil, err
171176
}

0 commit comments

Comments
 (0)