@@ -135,6 +135,9 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
135135func renderViewPage (ctx * context.Context ) (* git.Repository , * git.TreeEntry ) {
136136 wikiRepo , commit , err := findWikiRepoCommit (ctx )
137137 if err != nil {
138+ if wikiRepo != nil {
139+ wikiRepo .Close ()
140+ }
138141 if ! git .IsErrNotExist (err ) {
139142 ctx .ServerError ("GetBranchCommit" , err )
140143 }
@@ -222,13 +225,19 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
222225
223226 var buf strings.Builder
224227 if err := markdown .Render (rctx , bytes .NewReader (data ), & buf ); err != nil {
228+ if wikiRepo != nil {
229+ wikiRepo .Close ()
230+ }
225231 ctx .ServerError ("Render" , err )
226232 return nil , nil
227233 }
228234 ctx .Data ["content" ] = buf .String ()
229235
230236 buf .Reset ()
231237 if err := markdown .Render (rctx , bytes .NewReader (sidebarContent ), & buf ); err != nil {
238+ if wikiRepo != nil {
239+ wikiRepo .Close ()
240+ }
232241 ctx .ServerError ("Render" , err )
233242 return nil , nil
234243 }
@@ -237,6 +246,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
237246
238247 buf .Reset ()
239248 if err := markdown .Render (rctx , bytes .NewReader (footerContent ), & buf ); err != nil {
249+ if wikiRepo != nil {
250+ wikiRepo .Close ()
251+ }
240252 ctx .ServerError ("Render" , err )
241253 return nil , nil
242254 }
@@ -377,17 +389,14 @@ func Wiki(ctx *context.Context) {
377389 }
378390
379391 wikiRepo , entry := renderViewPage (ctx )
380- if ctx .Written () {
381- if wikiRepo != nil {
382- wikiRepo .Close ()
383- }
384- return
385- }
386392 defer func () {
387393 if wikiRepo != nil {
388394 wikiRepo .Close ()
389395 }
390396 }()
397+ if ctx .Written () {
398+ return
399+ }
391400 if entry == nil {
392401 ctx .Data ["Title" ] = ctx .Tr ("repo.wiki" )
393402 ctx .HTML (http .StatusOK , tplWikiStart )
@@ -422,17 +431,15 @@ func WikiRevision(ctx *context.Context) {
422431 }
423432
424433 wikiRepo , entry := renderRevisionPage (ctx )
425- if ctx .Written () {
426- if wikiRepo != nil {
427- wikiRepo .Close ()
428- }
429- return
430- }
431434 defer func () {
432435 if wikiRepo != nil {
433436 wikiRepo .Close ()
434437 }
435438 }()
439+
440+ if ctx .Written () {
441+ return
442+ }
436443 if entry == nil {
437444 ctx .Data ["Title" ] = ctx .Tr ("repo.wiki" )
438445 ctx .HTML (http .StatusOK , tplWikiStart )
@@ -469,13 +476,14 @@ func WikiPages(ctx *context.Context) {
469476 }
470477 return
471478 }
472-
473- entries , err := commit .ListEntries ()
474- if err != nil {
479+ defer func () {
475480 if wikiRepo != nil {
476481 wikiRepo .Close ()
477482 }
483+ }()
478484
485+ entries , err := commit .ListEntries ()
486+ if err != nil {
479487 ctx .ServerError ("ListEntries" , err )
480488 return
481489 }
@@ -486,10 +494,6 @@ func WikiPages(ctx *context.Context) {
486494 }
487495 c , err := wikiRepo .GetCommitByPath (entry .Name ())
488496 if err != nil {
489- if wikiRepo != nil {
490- wikiRepo .Close ()
491- }
492-
493497 ctx .ServerError ("GetCommit" , err )
494498 return
495499 }
@@ -498,10 +502,6 @@ func WikiPages(ctx *context.Context) {
498502 if models .IsErrWikiInvalidFileName (err ) {
499503 continue
500504 }
501- if wikiRepo != nil {
502- wikiRepo .Close ()
503- }
504-
505505 ctx .ServerError ("WikiFilenameToName" , err )
506506 return
507507 }
@@ -513,21 +513,25 @@ func WikiPages(ctx *context.Context) {
513513 }
514514 ctx .Data ["Pages" ] = pages
515515
516- defer func () {
517- if wikiRepo != nil {
518- wikiRepo .Close ()
519- }
520- }()
521516 ctx .HTML (http .StatusOK , tplWikiPages )
522517}
523518
524519// WikiRaw outputs raw blob requested by user (image for example)
525520func WikiRaw (ctx * context.Context ) {
526521 wikiRepo , commit , err := findWikiRepoCommit (ctx )
527- if err != nil {
522+ defer func () {
528523 if wikiRepo != nil {
524+ wikiRepo .Close ()
525+ }
526+ }()
527+
528+ if err != nil {
529+ if git .IsErrNotExist (err ) {
530+ ctx .NotFound ("findEntryForFile" , nil )
529531 return
530532 }
533+ ctx .ServerError ("findEntryForfile" , err )
534+ return
531535 }
532536
533537 providedPath := ctx .Params ("*" )
@@ -543,9 +547,7 @@ func WikiRaw(ctx *context.Context) {
543547
544548 if entry == nil {
545549 // Try to find a wiki page with that name
546- if strings .HasSuffix (providedPath , ".md" ) {
547- providedPath = providedPath [:len (providedPath )- 3 ]
548- }
550+ providedPath = strings .TrimSuffix (providedPath , ".md" )
549551
550552 wikiPath := wiki_service .NameToFilename (providedPath )
551553 entry , err = findEntryForFile (commit , wikiPath )
0 commit comments