Skip to content

Commit d91e977

Browse files
committed
Ensure wiki repos are all closed (go-gitea#16886)
Backport go-gitea#16886 There are multiple places where wiki git repositories are not properly closed. This PR ensures they are closed. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 73e5c36 commit d91e977

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

routers/repo/wiki.go

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package repo
88
import (
99
"fmt"
1010
"io/ioutil"
11+
"net/http"
1112
"net/url"
1213
"path/filepath"
1314
"strings"
@@ -131,6 +132,9 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
131132
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
132133
wikiRepo, commit, err := findUncycloRepoCommit(ctx)
133134
if err != nil {
135+
if wikiRepo != nil {
136+
wikiRepo.Close()
137+
}
134138
if !git.IsErrNotExist(err) {
135139
ctx.ServerError("GetBranchCommit", err)
136140
}
@@ -354,17 +358,14 @@ func Uncyclo(ctx *context.Context) {
354358
}
355359

356360
wikiRepo, entry := renderViewPage(ctx)
357-
if ctx.Written() {
358-
if wikiRepo != nil {
359-
wikiRepo.Close()
360-
}
361-
return
362-
}
363361
defer func() {
364362
if wikiRepo != nil {
365363
wikiRepo.Close()
366364
}
367365
}()
366+
if ctx.Written() {
367+
return
368+
}
368369
if entry == nil {
369370
ctx.Data["Title"] = ctx.Tr("repo.wiki")
370371
ctx.HTML(200, tplUncycloStart)
@@ -399,17 +400,15 @@ func UncycloRevision(ctx *context.Context) {
399400
}
400401

401402
wikiRepo, entry := renderRevisionPage(ctx)
402-
if ctx.Written() {
403-
if wikiRepo != nil {
404-
wikiRepo.Close()
405-
}
406-
return
407-
}
408403
defer func() {
409404
if wikiRepo != nil {
410405
wikiRepo.Close()
411406
}
412407
}()
408+
409+
if ctx.Written() {
410+
return
411+
}
413412
if entry == nil {
414413
ctx.Data["Title"] = ctx.Tr("repo.wiki")
415414
ctx.HTML(200, tplUncycloStart)
@@ -446,13 +445,14 @@ func UncycloPages(ctx *context.Context) {
446445
}
447446
return
448447
}
449-
450-
entries, err := commit.ListEntries()
451-
if err != nil {
448+
defer func() {
452449
if wikiRepo != nil {
453450
wikiRepo.Close()
454451
}
452+
}()
455453

454+
entries, err := commit.ListEntries()
455+
if err != nil {
456456
ctx.ServerError("ListEntries", err)
457457
return
458458
}
@@ -463,10 +463,6 @@ func UncycloPages(ctx *context.Context) {
463463
}
464464
c, err := wikiRepo.GetCommitByPath(entry.Name())
465465
if err != nil {
466-
if wikiRepo != nil {
467-
wikiRepo.Close()
468-
}
469-
470466
ctx.ServerError("GetCommit", err)
471467
return
472468
}
@@ -475,10 +471,6 @@ func UncycloPages(ctx *context.Context) {
475471
if models.IsErrUncycloInvalidFileName(err) {
476472
continue
477473
}
478-
if wikiRepo != nil {
479-
wikiRepo.Close()
480-
}
481-
482474
ctx.ServerError("UncycloFilenameToName", err)
483475
return
484476
}
@@ -490,21 +482,25 @@ func UncycloPages(ctx *context.Context) {
490482
}
491483
ctx.Data["Pages"] = pages
492484

485+
ctx.HTML(http.StatusOK, tplUncycloPages)
486+
}
487+
488+
// UncycloRaw outputs raw blob requested by user (image for example)
489+
func UncycloRaw(ctx *context.Context) {
490+
wikiRepo, commit, err := findUncycloRepoCommit(ctx)
493491
defer func() {
494492
if wikiRepo != nil {
495493
wikiRepo.Close()
496494
}
497495
}()
498-
ctx.HTML(200, tplUncycloPages)
499-
}
500496

501-
// UncycloRaw outputs raw blob requested by user (image for example)
502-
func UncycloRaw(ctx *context.Context) {
503-
wikiRepo, commit, err := findUncycloRepoCommit(ctx)
504497
if err != nil {
505-
if wikiRepo != nil {
498+
if git.IsErrNotExist(err) {
499+
ctx.NotFound("findEntryForFile", nil)
506500
return
507501
}
502+
ctx.ServerError("findEntryForfile", err)
503+
return
508504
}
509505

510506
providedPath := ctx.Params("*")
@@ -520,9 +516,7 @@ func UncycloRaw(ctx *context.Context) {
520516

521517
if entry == nil {
522518
// Try to find a wiki page with that name
523-
if strings.HasSuffix(providedPath, ".md") {
524-
providedPath = providedPath[:len(providedPath)-3]
525-
}
519+
providedPath = strings.TrimSuffix(providedPath, ".md")
526520

527521
wikiPath := wiki_service.NameToFilename(providedPath)
528522
entry, err = findEntryForFile(commit, wikiPath)

0 commit comments

Comments
 (0)