Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure wiki repos are all closed #16886

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
if wikiRepo != nil {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
wikiRepo.Close()
}
if !git.IsErrNotExist(err) {
ctx.ServerError("GetBranchCommit", err)
}
Expand Down Expand Up @@ -222,13 +225,19 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {

var buf strings.Builder
if err := markdown.Render(rctx, bytes.NewReader(data), &buf); err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
ctx.ServerError("Render", err)
return nil, nil
}
ctx.Data["content"] = buf.String()

buf.Reset()
if err := markdown.Render(rctx, bytes.NewReader(sidebarContent), &buf); err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
ctx.ServerError("Render", err)
return nil, nil
}
Expand All @@ -237,6 +246,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {

buf.Reset()
if err := markdown.Render(rctx, bytes.NewReader(footerContent), &buf); err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
ctx.ServerError("Render", err)
return nil, nil
}
Expand Down Expand Up @@ -377,17 +389,14 @@ func Wiki(ctx *context.Context) {
}

wikiRepo, entry := renderViewPage(ctx)
if ctx.Written() {
if wikiRepo != nil {
wikiRepo.Close()
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()
if ctx.Written() {
return
}
if entry == nil {
ctx.Data["Title"] = ctx.Tr("repo.wiki")
ctx.HTML(http.StatusOK, tplWikiStart)
Expand Down Expand Up @@ -422,17 +431,15 @@ func WikiRevision(ctx *context.Context) {
}

wikiRepo, entry := renderRevisionPage(ctx)
if ctx.Written() {
if wikiRepo != nil {
wikiRepo.Close()
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()

if ctx.Written() {
return
}
if entry == nil {
ctx.Data["Title"] = ctx.Tr("repo.wiki")
ctx.HTML(http.StatusOK, tplWikiStart)
Expand Down Expand Up @@ -469,13 +476,14 @@ func WikiPages(ctx *context.Context) {
}
return
}

entries, err := commit.ListEntries()
if err != nil {
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()

entries, err := commit.ListEntries()
if err != nil {
ctx.ServerError("ListEntries", err)
return
}
Expand All @@ -486,10 +494,6 @@ func WikiPages(ctx *context.Context) {
}
c, err := wikiRepo.GetCommitByPath(entry.Name())
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}

ctx.ServerError("GetCommit", err)
return
}
Expand All @@ -498,10 +502,6 @@ func WikiPages(ctx *context.Context) {
if models.IsErrWikiInvalidFileName(err) {
continue
}
if wikiRepo != nil {
wikiRepo.Close()
}

ctx.ServerError("WikiFilenameToName", err)
return
}
Expand All @@ -513,21 +513,25 @@ func WikiPages(ctx *context.Context) {
}
ctx.Data["Pages"] = pages

defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()
ctx.HTML(http.StatusOK, tplWikiPages)
}

// WikiRaw outputs raw blob requested by user (image for example)
func WikiRaw(ctx *context.Context) {
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()

if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("findEntryForFile", nil)
return
}
ctx.ServerError("findEntryForfile", err)
return
}

providedPath := ctx.Params("*")
Expand All @@ -543,9 +547,7 @@ func WikiRaw(ctx *context.Context) {

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

wikiPath := wiki_service.NameToFilename(providedPath)
entry, err = findEntryForFile(commit, wikiPath)
Expand Down