Skip to content

Commit 7d44043

Browse files
wxiaoguanglunny
authored andcommitted
fix
1 parent e2cc853 commit 7d44043

File tree

6 files changed

+54
-74
lines changed

6 files changed

+54
-74
lines changed

modules/git/ref.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package git
55

66
import (
77
"regexp"
8+
"slices"
89
"strings"
910

10-
"code.gitea.io/gitea/modules/setting"
1111
"code.gitea.io/gitea/modules/util"
1212
)
1313

@@ -85,20 +85,6 @@ func RefNameFromCommit(shortName string) RefName {
8585
return RefName(shortName)
8686
}
8787

88-
func RefNameFromTypeAndShortName(tp RefType, shortName string) RefName {
89-
switch tp {
90-
case RefTypeBranch:
91-
return RefNameFromBranch(shortName)
92-
case RefTypeTag:
93-
return RefNameFromTag(shortName)
94-
case RefTypeCommit:
95-
return RefNameFromCommit(shortName)
96-
default:
97-
setting.PanicInDevOrTesting("Unknown RefType: %v", tp)
98-
return ""
99-
}
100-
}
101-
10288
func (ref RefName) String() string {
10389
return string(ref)
10490
}
@@ -235,3 +221,11 @@ func (ref RefName) RefWebLinkPath() string {
235221
}
236222
return string(refType) + "/" + util.PathEscapeSegments(ref.ShortName())
237223
}
224+
225+
func RefNameFromUserInput(ref string, allowedTypes ...RefType) RefName {
226+
refName := RefName(ref)
227+
if !slices.Contains(allowedTypes, refName.RefType()) {
228+
return ""
229+
}
230+
return refName
231+
}

routers/web/repo/find.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package repo
55

66
import (
77
"net/http"
8+
"net/url"
89

10+
"code.gitea.io/gitea/modules/git"
911
"code.gitea.io/gitea/modules/templates"
1012
"code.gitea.io/gitea/modules/util"
1113
"code.gitea.io/gitea/services/context"
@@ -18,8 +20,8 @@ const (
1820
// FindFiles render the page to find repository files
1921
func FindFiles(ctx *context.Context) {
2022
path := ctx.PathParam("*")
21-
ctx.Data["TreeLink"] = ctx.Repo.RepoLink + "/src/" + util.PathEscapeSegments(path)
22-
ctx.Data["DataLink"] = ctx.Repo.RepoLink + "/tree-list/" + util.PathEscapeSegments(path) +
23-
"?ref_type=" + ctx.FormTrim("ref_type") + "&ref_name=" + ctx.FormTrim("ref_name")
23+
ref := git.RefNameFromUserInput(ctx.FormTrim("ref"), git.RefTypeBranch, git.RefTypeTag, git.RefTypeCommit)
24+
ctx.Data["TreeLink"] = ctx.Repo.RepoLink + "/src/" + ref.RefWebLinkPath() + "/" + util.PathEscapeSegments(path)
25+
ctx.Data["DataLink"] = ctx.Repo.RepoLink + "/tree-list/" + util.PathEscapeSegments(path) + "?ref=" + url.QueryEscape(ctx.FormTrim("ref"))
2426
ctx.HTML(http.StatusOK, tplFindFiles)
2527
}

routers/web/repo/treelist.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import (
1515

1616
// TreeList get all files' entries of a repository
1717
func TreeList(ctx *context.Context) {
18-
tree, err := ctx.Repo.Commit.SubTree("/")
18+
_, commit, err := ctx.Repo.GetRefCommit(ctx.FormString("ref"), git.RefTypeBranch, git.RefTypeTag, git.RefTypeCommit)
19+
if err != nil {
20+
ctx.ServerError("GetRefCommit", err)
21+
return
22+
}
23+
tree, err := commit.SubTree("/")
1924
if err != nil {
2025
ctx.ServerError("Repo.Commit.SubTree", err)
2126
return

routers/web/web.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,7 @@ func registerRoutes(m *web.Router) {
11581158
m.Group("/{username}/{reponame}", func() {
11591159
m.Get("/find/*", repo.FindFiles)
11601160
m.Get("/find", repo.FindFiles)
1161-
m.Get("/tree-list/*", context.RepoRefByQueries(), repo.TreeList)
1162-
m.Get("/tree-list", context.RepoRefByQueries(), repo.TreeList)
1161+
m.Get("/tree-list", repo.TreeList)
11631162
m.Get("/compare", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff)
11641163
m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists).
11651164
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).

services/context/repo.go

+31-52
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ func (r *Repository) GetObjectFormat() git.ObjectFormat {
9191
return git.ObjectFormatFromName(r.Repository.ObjectFormatName)
9292
}
9393

94+
func (r *Repository) GetRefCommit(ref string, allowedTypes ...git.RefType) (git.RefName, *git.Commit, error) {
95+
refName := git.RefNameFromUserInput(ref, allowedTypes...)
96+
if refName == "" {
97+
return "", nil, errors.New("invalid ref")
98+
}
99+
commitID, err := r.GitRepo.GetRefCommitID(refName.String())
100+
if err != nil {
101+
return "", nil, err
102+
}
103+
commit, err := r.GitRepo.GetCommit(commitID)
104+
if err != nil {
105+
return "", nil, err
106+
}
107+
return refName, commit, nil
108+
}
109+
94110
// RepoMustNotBeArchived checks if a repo is archived
95111
func RepoMustNotBeArchived() func(ctx *Context) {
96112
return func(ctx *Context) {
@@ -780,6 +796,20 @@ type RepoRefByTypeOptions struct {
780796
IgnoreNotExistErr bool
781797
}
782798

799+
func repoRefFullName(typ git.RefType, shortName string) git.RefName {
800+
switch typ {
801+
case git.RefTypeBranch:
802+
return git.RefNameFromBranch(shortName)
803+
case git.RefTypeTag:
804+
return git.RefNameFromTag(shortName)
805+
case git.RefTypeCommit:
806+
return git.RefNameFromCommit(shortName)
807+
default:
808+
setting.PanicInDevOrTesting("Unknown RepoRefType: %v", typ)
809+
return git.RefNameFromBranch("main") // just a dummy result, it shouldn't happen
810+
}
811+
}
812+
783813
// RepoRefByType handles repository reference name for a specific type
784814
// of repository reference
785815
func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func(*Context) {
@@ -842,7 +872,7 @@ func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func
842872
} else {
843873
refShortName = getRefName(ctx.Base, ctx.Repo, reqPath, refType)
844874
}
845-
ctx.Repo.RefFullName = git.RefNameFromTypeAndShortName(refType, refShortName)
875+
ctx.Repo.RefFullName = repoRefFullName(refType, refShortName)
846876
isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)
847877
if isRenamedBranch && has {
848878
renamedBranchName := ctx.Data["RenamedBranchName"].(string)
@@ -939,57 +969,6 @@ func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func
939969
}
940970
}
941971

942-
func RepoRefByQueries() func(ctx *Context) {
943-
return func(ctx *Context) {
944-
if ctx.Repo.Repository.IsEmpty {
945-
// assume the user is viewing the (non-existent) default branch
946-
ctx.Repo.IsViewBranch = true
947-
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
948-
ctx.Repo.RefFullName = git.RefNameFromBranch(ctx.Repo.BranchName)
949-
return
950-
}
951-
952-
var err error
953-
if ctx.Repo.GitRepo == nil {
954-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
955-
if err != nil {
956-
ctx.ServerError(fmt.Sprintf("Open Repository %v failed", ctx.Repo.Repository.FullName()), err)
957-
return
958-
}
959-
}
960-
961-
ctx.Repo.RefFullName = git.RefNameFromTypeAndShortName(git.RefType(ctx.FormTrim("ref_type")), ctx.FormTrim("ref_name"))
962-
if ctx.Repo.RefFullName == "" {
963-
ctx.Error(http.StatusBadRequest, "invalid ref type or name")
964-
return
965-
}
966-
967-
switch ctx.Repo.RefFullName.RefType() {
968-
case git.RefTypeBranch:
969-
ctx.Repo.IsViewBranch = true
970-
ctx.Repo.BranchName = ctx.Repo.RefFullName.ShortName()
971-
case git.RefTypeTag:
972-
ctx.Repo.IsViewTag = true
973-
ctx.Repo.TagName = ctx.Repo.RefFullName.ShortName()
974-
case git.RefTypeCommit:
975-
ctx.Repo.IsViewCommit = true
976-
ctx.Repo.CommitID = ctx.Repo.RefFullName.ShortName()
977-
}
978-
979-
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(ctx.Repo.RefFullName.String())
980-
if err != nil {
981-
if git.IsErrNotExist(err) {
982-
ctx.NotFound("GetCommit", err)
983-
} else {
984-
ctx.ServerError("GetCommit", err)
985-
}
986-
return
987-
}
988-
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
989-
ctx.Repo.TreePath = ctx.PathParam("*")
990-
}
991-
}
992-
993972
// GitHookService checks if repository Git hooks service has been enabled.
994973
func GitHookService() func(ctx *Context) {
995974
return func(ctx *Context) {

templates/repo/home.tmpl

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
<!-- Show go to file if on home page -->
6464
{{if $isTreePathRoot}}
65-
<a href="{{.Repository.Link}}/find/{{.TreePath}}?ref_type={{.RefFullName.RefType}}&ref_name={{.RefFullName.ShortName}}" class="ui compact basic button">{{ctx.Locale.Tr "repo.find_file.go_to_file"}}</a>
65+
{{/* FIXME: it should still use RefTypeNameSubURL, otherwise the link is ugly */}}
66+
<a href="{{.Repository.Link}}/find/{{.TreePath}}?ref={{.RefFullName}}" class="ui compact basic button">{{ctx.Locale.Tr "repo.find_file.go_to_file"}}</a>
6667
{{end}}
6768

6869
{{if and .CanWriteCode .IsViewBranch (not .Repository.IsMirror) (not .Repository.IsArchived) (not .IsViewFile)}}

0 commit comments

Comments
 (0)