@@ -131,6 +131,9 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
131131func renderViewPage (ctx * context.Context ) (* git.Repository , * git.TreeEntry ) {
132132 wikiRepo , commit , err := findWikiRepoCommit (ctx )
133133 if err != nil {
134+ if wikiRepo != nil {
135+ wikiRepo .Close ()
136+ }
134137 if ! git .IsErrNotExist (err ) {
135138 ctx .ServerError ("GetBranchCommit" , err )
136139 }
@@ -354,17 +357,14 @@ func Wiki(ctx *context.Context) {
354357 }
355358
356359 wikiRepo , entry := renderViewPage (ctx )
357- if ctx .Written () {
358- if wikiRepo != nil {
359- wikiRepo .Close ()
360- }
361- return
362- }
363360 defer func () {
364361 if wikiRepo != nil {
365362 wikiRepo .Close ()
366363 }
367364 }()
365+ if ctx .Written () {
366+ return
367+ }
368368 if entry == nil {
369369 ctx .Data ["Title" ] = ctx .Tr ("repo.wiki" )
370370 ctx .HTML (200 , tplWikiStart )
@@ -399,17 +399,15 @@ func WikiRevision(ctx *context.Context) {
399399 }
400400
401401 wikiRepo , entry := renderRevisionPage (ctx )
402- if ctx .Written () {
403- if wikiRepo != nil {
404- wikiRepo .Close ()
405- }
406- return
407- }
408402 defer func () {
409403 if wikiRepo != nil {
410404 wikiRepo .Close ()
411405 }
412406 }()
407+
408+ if ctx .Written () {
409+ return
410+ }
413411 if entry == nil {
414412 ctx .Data ["Title" ] = ctx .Tr ("repo.wiki" )
415413 ctx .HTML (200 , tplWikiStart )
@@ -446,13 +444,14 @@ func WikiPages(ctx *context.Context) {
446444 }
447445 return
448446 }
449-
450- entries , err := commit .ListEntries ()
451- if err != nil {
447+ defer func () {
452448 if wikiRepo != nil {
453449 wikiRepo .Close ()
454450 }
451+ }()
455452
453+ entries , err := commit .ListEntries ()
454+ if err != nil {
456455 ctx .ServerError ("ListEntries" , err )
457456 return
458457 }
@@ -463,10 +462,6 @@ func WikiPages(ctx *context.Context) {
463462 }
464463 c , err := wikiRepo .GetCommitByPath (entry .Name ())
465464 if err != nil {
466- if wikiRepo != nil {
467- wikiRepo .Close ()
468- }
469-
470465 ctx .ServerError ("GetCommit" , err )
471466 return
472467 }
@@ -475,10 +470,6 @@ func WikiPages(ctx *context.Context) {
475470 if models .IsErrWikiInvalidFileName (err ) {
476471 continue
477472 }
478- if wikiRepo != nil {
479- wikiRepo .Close ()
480- }
481-
482473 ctx .ServerError ("WikiFilenameToName" , err )
483474 return
484475 }
@@ -490,21 +481,25 @@ func WikiPages(ctx *context.Context) {
490481 }
491482 ctx .Data ["Pages" ] = pages
492483
493- defer func () {
494- if wikiRepo != nil {
495- wikiRepo .Close ()
496- }
497- }()
498484 ctx .HTML (200 , tplWikiPages )
499485}
500486
501487// WikiRaw outputs raw blob requested by user (image for example)
502488func WikiRaw (ctx * context.Context ) {
503489 wikiRepo , commit , err := findWikiRepoCommit (ctx )
504- if err != nil {
490+ defer func () {
505491 if wikiRepo != nil {
492+ wikiRepo .Close ()
493+ }
494+ }()
495+
496+ if err != nil {
497+ if git .IsErrNotExist (err ) {
498+ ctx .NotFound ("findEntryForFile" , nil )
506499 return
507500 }
501+ ctx .ServerError ("findEntryForfile" , err )
502+ return
508503 }
509504
510505 providedPath := ctx .Params ("*" )
@@ -520,9 +515,7 @@ func WikiRaw(ctx *context.Context) {
520515
521516 if entry == nil {
522517 // Try to find a wiki page with that name
523- if strings .HasSuffix (providedPath , ".md" ) {
524- providedPath = providedPath [:len (providedPath )- 3 ]
525- }
518+ providedPath = strings .TrimSuffix (providedPath , ".md" )
526519
527520 wikiPath := wiki_service .NameToFilename (providedPath )
528521 entry , err = findEntryForFile (commit , wikiPath )
0 commit comments