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
+19 -15
View File
@@ -116,21 +116,26 @@ func Graph(ctx *context.Context) {
hidePRRefs := ctx.FormBool("hide-pr-refs")
ctx.Data["HidePRRefs"] = hidePRRefs
branches := ctx.FormStrings("branch")
realBranches := make([]string, len(branches))
copy(realBranches, branches)
for i, branch := range realBranches {
if strings.HasPrefix(branch, "--") {
realBranches[i] = git.BranchPrefix + branch
}
}
ctx.Data["SelectedBranches"] = realBranches
files := ctx.FormStrings("file")
ctx.Data["SelectedBranches"] = branches
graphCommitsCount, err := ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, realBranches, files)
branchRefs := make([]string, 0, len(branches))
for _, branchName := range branches {
branchRef := branchName
if !strings.HasPrefix(branchRef, "refs/") {
branchRef = git.BranchPrefix + branchRef
}
branchRefs = append(branchRefs, branchRef)
}
files := ctx.FormStrings("file")
graphCommitsCount, err := ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, branchRefs, files)
if err != nil {
log.Warn("GetCommitGraphsCount error for generate graph exclude prs: %t branches: %s in %-v, Will Ignore branches and try again. Underlying Error: %v", hidePRRefs, branches, ctx.Repo.Repository, err)
realBranches = []string{}
graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, realBranches, files)
if len(branchRefs) > 0 {
// maybe: "fatal: bad revision '.....'" if a ref doesn't exist
// maybe it's better to show a 404 page instead of the unclear retry, anyway, a retry isn't harmful, so keep the old behavior
branchRefs = nil
graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, branchRefs, files)
}
if err != nil {
ctx.ServerError("GetCommitGraphsCount", err)
return
@@ -138,8 +143,7 @@ func Graph(ctx *context.Context) {
}
page := ctx.FormInt("page")
graph, err := gitgraph.GetCommitGraph(ctx, ctx.Repo.GitRepo, page, 0, hidePRRefs, realBranches, files)
graph, err := gitgraph.GetCommitGraph(ctx, ctx.Repo.GitRepo, page, 0, hidePRRefs, branchRefs, files)
if err != nil {
ctx.ServerError("GetCommitGraph", err)
return