chore: fix legacy git ref problems (#38827)

1. add correct "refs/heads" prefix to the branch name for commit graph
2. fix incorrect cache key in GetCommitGraphsCount
3. remove the "--" trim for the tag name, there is no security vulnerability, we never do so anywhere else

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
wxiaoguang
2026-08-08 07:04:36 +08:00
committed by GitHub
parent 09f78aed19
commit f899dfd6e0
5 changed files with 31 additions and 27 deletions
+7 -5
View File
@@ -22,6 +22,7 @@ import (
repo_model "gitea.dev/models/repo"
unit_model "gitea.dev/models/unit"
user_model "gitea.dev/models/user"
"gitea.dev/modules/base"
"gitea.dev/modules/cache"
"gitea.dev/modules/git"
"gitea.dev/modules/httplib"
@@ -252,16 +253,17 @@ func (r *Repository) CanCreateIssueDependencies(ctx context.Context, user *user_
}
// GetCommitGraphsCount returns cached commit count for current view
func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool, branches, files []string) (int64, error) {
cacheKey := fmt.Sprintf("commits-count-%d-graph-%t-%s-%s", r.Repository.ID, hidePRRefs, branches, files)
func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool, refs, files []string) (int64, error) {
refFileKey := strings.Join(refs, "\x00") + "\x00\x00" + strings.Join(files, "\x00")
refFileKey = base.EncodeSha256(refFileKey)
cacheKey := cache.SafeCacheKey(fmt.Sprintf("git-commits-graph-count:%d:%v", r.Repository.ID, hidePRRefs), refFileKey)
return cache.GetInt64(cacheKey, func() (int64, error) {
if len(branches) == 0 {
if len(refs) == 0 {
return git.AllCommitsCount(ctx, r.Repository, hidePRRefs, files...)
}
return git.CommitsCount(ctx, r.Repository,
git.CommitsCountOptions{
Revision: branches,
Revision: refs,
RelPath: files,
})
})