Skip to content

Commit 1e73dd2

Browse files
lunnyzeripath
authored andcommitted
Fix wiki raw view on sub path (#10002) (#10040)
* Fix wiki raw view on sub path * Add test for subpath wiki raw file * Fix bug
1 parent 315026c commit 1e73dd2

File tree

6 files changed

+12
-19
lines changed

6 files changed

+12
-19
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0cf15c3f66ec8384480ed9c3cf87c9e97fbb0ec3
1+
423313fbd38093bb10d0c8387db9105409c6f196

routers/repo/wiki.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,20 @@ type PageMeta struct {
6666

6767
// findEntryForFile finds the tree entry for a target filepath.
6868
func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) {
69-
entries, err := commit.ListEntries()
70-
if err != nil {
69+
entry, err := commit.GetTreeEntryByPath(target)
70+
if err != nil && !git.IsErrNotExist(err) {
7171
return nil, err
7272
}
73-
// The longest name should be checked first
74-
for _, entry := range entries {
75-
if entry.IsRegular() && entry.Name() == target {
76-
return entry, nil
77-
}
73+
if entry != nil {
74+
return entry, nil
7875
}
76+
7977
// Then the unescaped, shortest alternative
8078
var unescapedTarget string
8179
if unescapedTarget, err = url.QueryUnescape(target); err != nil {
8280
return nil, err
8381
}
84-
for _, entry := range entries {
85-
if entry.IsRegular() && entry.Name() == unescapedTarget {
86-
return entry, nil
87-
}
88-
}
89-
return nil, nil
82+
return commit.GetTreeEntryByPath(unescapedTarget)
9083
}
9184

9285
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
@@ -123,10 +116,9 @@ func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
123116
// wikiContentsByName returns the contents of a wiki page, along with a boolean
124117
// indicating whether the page exists. Writes to ctx if an error occurs.
125118
func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName string) ([]byte, *git.TreeEntry, string, bool) {
126-
var entry *git.TreeEntry
127-
var err error
128119
pageFilename := wiki_service.NameToFilename(wikiName)
129-
if entry, err = findEntryForFile(commit, pageFilename); err != nil {
120+
entry, err := findEntryForFile(commit, pageFilename)
121+
if err != nil && !git.IsErrNotExist(err) {
130122
ctx.ServerError("findEntryForFile", err)
131123
return nil, nil, "", false
132124
} else if entry == nil {
@@ -518,7 +510,7 @@ func WikiRaw(ctx *context.Context) {
518510
if commit != nil {
519511
// Try to find a file with that name
520512
entry, err = findEntryForFile(commit, providedPath)
521-
if err != nil {
513+
if err != nil && !git.IsErrNotExist(err) {
522514
ctx.ServerError("findFile", err)
523515
return
524516
}
@@ -531,7 +523,7 @@ func WikiRaw(ctx *context.Context) {
531523

532524
wikiPath := wiki_service.NameToFilename(providedPath)
533525
entry, err = findEntryForFile(commit, wikiPath)
534-
if err != nil {
526+
if err != nil && !git.IsErrNotExist(err) {
535527
ctx.ServerError("findFile", err)
536528
return
537529
}

routers/repo/wiki_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func TestDeleteWikiPagePost(t *testing.T) {
192192
func TestWikiRaw(t *testing.T) {
193193
for filepath, filetype := range map[string]string{
194194
"jpeg.jpg": "image/jpeg",
195+
"images/jpeg.jpg": "image/jpeg",
195196
"Page With Spaced Name": "text/plain; charset=utf-8",
196197
"Page-With-Spaced-Name": "text/plain; charset=utf-8",
197198
"Page With Spaced Name.md": "text/plain; charset=utf-8",

0 commit comments

Comments
 (0)