@@ -93,17 +93,32 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
93
93
}
94
94
95
95
func 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
100
100
}
101
101
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 )
105
117
}
106
- return wikiRepo , commit , nil
118
+ if errCommit != nil {
119
+ return wikiGitRepo , nil , errCommit
120
+ }
121
+ return wikiGitRepo , commit , nil
107
122
}
108
123
109
124
// wikiContentsByEntry returns the contents of the wiki page referenced by the
@@ -316,7 +331,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
316
331
}
317
332
318
333
// get commit count - wiki revisions
319
- commitsCount , _ := wikiRepo .FileCommitsCount (wiki_service . DefaultBranch , pageFilename )
334
+ commitsCount , _ := wikiRepo .FileCommitsCount (ctx . Repo . Repository . DefaultWikiBranch , pageFilename )
320
335
ctx .Data ["CommitCount" ] = commitsCount
321
336
322
337
return wikiRepo , entry
@@ -368,7 +383,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
368
383
ctx .Data ["footerContent" ] = ""
369
384
370
385
// get commit count - wiki revisions
371
- commitsCount , _ := wikiRepo .FileCommitsCount (wiki_service . DefaultBranch , pageFilename )
386
+ commitsCount , _ := wikiRepo .FileCommitsCount (ctx . Repo . Repository . DefaultWikiBranch , pageFilename )
372
387
ctx .Data ["CommitCount" ] = commitsCount
373
388
374
389
// get page
@@ -380,7 +395,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
380
395
// get Commit Count
381
396
commitsHistory , err := wikiRepo .CommitsByFileAndRange (
382
397
git.CommitsByFileAndRangeOptions {
383
- Revision : wiki_service . DefaultBranch ,
398
+ Revision : ctx . Repo . Repository . DefaultWikiBranch ,
384
399
File : pageFilename ,
385
400
Page : page ,
386
401
})
@@ -402,20 +417,17 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
402
417
403
418
func renderEditPage (ctx * context.Context ) {
404
419
wikiRepo , commit , err := findWikiRepoCommit (ctx )
405
- if err != nil {
420
+ defer func () {
406
421
if wikiRepo != nil {
407
- wikiRepo .Close ()
422
+ _ = wikiRepo .Close ()
408
423
}
424
+ }()
425
+ if err != nil {
409
426
if ! git .IsErrNotExist (err ) {
410
427
ctx .ServerError ("GetBranchCommit" , err )
411
428
}
412
429
return
413
430
}
414
- defer func () {
415
- if wikiRepo != nil {
416
- wikiRepo .Close ()
417
- }
418
- }()
419
431
420
432
// get requested pagename
421
433
pageName := wiki_service .WebPathFromRequest (ctx .PathParamRaw ("*" ))
@@ -584,17 +596,15 @@ func WikiPages(ctx *context.Context) {
584
596
ctx .Data ["CanWriteWiki" ] = ctx .Repo .CanWrite (unit .TypeWiki ) && ! ctx .Repo .Repository .IsArchived
585
597
586
598
wikiRepo , commit , err := findWikiRepoCommit (ctx )
587
- if err != nil {
588
- if wikiRepo != nil {
589
- wikiRepo .Close ()
590
- }
591
- return
592
- }
593
599
defer func () {
594
600
if wikiRepo != nil {
595
- wikiRepo .Close ()
601
+ _ = wikiRepo .Close ()
596
602
}
597
603
}()
604
+ if err != nil {
605
+ ctx .Redirect (ctx .Repo .RepoLink + "/wiki" )
606
+ return
607
+ }
598
608
599
609
entries , err := commit .ListEntries ()
600
610
if err != nil {
0 commit comments