@@ -93,17 +93,32 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
9393}
9494
9595func findWikiRepoCommit (ctx * context.Context ) (* git.Repository , * git.Commit , error ) {
96- wikiRepo , err := gitrepo .OpenWikiRepository (ctx , ctx .Repo .Repository )
97- if err != nil {
98- ctx .ServerError ("OpenRepository" , err )
99- return nil , nil , err
96+ wikiGitRepo , errGitRepo := gitrepo .OpenWikiRepository (ctx , ctx .Repo .Repository )
97+ if errGitRepo != nil {
98+ ctx .ServerError ("OpenRepository" , errGitRepo )
99+ return nil , nil , errGitRepo
100100 }
101101
102- commit , err := wikiRepo .GetBranchCommit (wiki_service .DefaultBranch )
103- if err != nil {
104- return wikiRepo , nil , err
102+ commit , errCommit := wikiGitRepo .GetBranchCommit (ctx .Repo .Repository .DefaultWikiBranch )
103+ if git .IsErrNotExist (errCommit ) {
104+ // if the default branch recorded in database is out of sync, then re-sync it
105+ gitRepoDefaultBranch , errBranch := wikiGitRepo .GetDefaultBranch ()
106+ if errBranch != nil {
107+ return wikiGitRepo , nil , errBranch
108+ }
109+ // update the default branch in the database
110+ errDb := repo_model .UpdateRepositoryCols (ctx , & repo_model.Repository {ID : ctx .Repo .Repository .ID , DefaultWikiBranch : gitRepoDefaultBranch }, "default_wiki_branch" )
111+ if errDb != nil {
112+ return wikiGitRepo , nil , errDb
113+ }
114+ ctx .Repo .Repository .DefaultWikiBranch = gitRepoDefaultBranch
115+ // retry to get the commit from the correct default branch
116+ commit , errCommit = wikiGitRepo .GetBranchCommit (ctx .Repo .Repository .DefaultWikiBranch )
105117 }
106- return wikiRepo , commit , nil
118+ if errCommit != nil {
119+ return wikiGitRepo , nil , errCommit
120+ }
121+ return wikiGitRepo , commit , nil
107122}
108123
109124// wikiContentsByEntry returns the contents of the wiki page referenced by the
@@ -316,7 +331,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
316331 }
317332
318333 // get commit count - wiki revisions
319- commitsCount , _ := wikiRepo .FileCommitsCount (wiki_service . DefaultBranch , pageFilename )
334+ commitsCount , _ := wikiRepo .FileCommitsCount (ctx . Repo . Repository . DefaultWikiBranch , pageFilename )
320335 ctx .Data ["CommitCount" ] = commitsCount
321336
322337 return wikiRepo , entry
@@ -368,7 +383,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
368383 ctx .Data ["footerContent" ] = ""
369384
370385 // get commit count - wiki revisions
371- commitsCount , _ := wikiRepo .FileCommitsCount (wiki_service . DefaultBranch , pageFilename )
386+ commitsCount , _ := wikiRepo .FileCommitsCount (ctx . Repo . Repository . DefaultWikiBranch , pageFilename )
372387 ctx .Data ["CommitCount" ] = commitsCount
373388
374389 // get page
@@ -380,7 +395,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
380395 // get Commit Count
381396 commitsHistory , err := wikiRepo .CommitsByFileAndRange (
382397 git.CommitsByFileAndRangeOptions {
383- Revision : wiki_service . DefaultBranch ,
398+ Revision : ctx . Repo . Repository . DefaultWikiBranch ,
384399 File : pageFilename ,
385400 Page : page ,
386401 })
@@ -402,20 +417,17 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
402417
403418func renderEditPage (ctx * context.Context ) {
404419 wikiRepo , commit , err := findWikiRepoCommit (ctx )
405- if err != nil {
420+ defer func () {
406421 if wikiRepo != nil {
407- wikiRepo .Close ()
422+ _ = wikiRepo .Close ()
408423 }
424+ }()
425+ if err != nil {
409426 if ! git .IsErrNotExist (err ) {
410427 ctx .ServerError ("GetBranchCommit" , err )
411428 }
412429 return
413430 }
414- defer func () {
415- if wikiRepo != nil {
416- wikiRepo .Close ()
417- }
418- }()
419431
420432 // get requested pagename
421433 pageName := wiki_service .WebPathFromRequest (ctx .PathParamRaw ("*" ))
@@ -584,17 +596,15 @@ func WikiPages(ctx *context.Context) {
584596 ctx .Data ["CanWriteWiki" ] = ctx .Repo .CanWrite (unit .TypeWiki ) && ! ctx .Repo .Repository .IsArchived
585597
586598 wikiRepo , commit , err := findWikiRepoCommit (ctx )
587- if err != nil {
588- if wikiRepo != nil {
589- wikiRepo .Close ()
590- }
591- return
592- }
593599 defer func () {
594600 if wikiRepo != nil {
595- wikiRepo .Close ()
601+ _ = wikiRepo .Close ()
596602 }
597603 }()
604+ if err != nil {
605+ ctx .Redirect (ctx .Repo .RepoLink + "/wiki" )
606+ return
607+ }
598608
599609 entries , err := commit .ListEntries ()
600610 if err != nil {
0 commit comments