Skip to content

Commit 1bb9cb7

Browse files
committed
fix
1 parent ade6241 commit 1bb9cb7

File tree

15 files changed

+180
-81
lines changed

15 files changed

+180
-81
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ var migrations = []Migration{
562562
NewMigration("Use Slug instead of ID for Badges", v1_22.UseSlugInsteadOfIDForBadges),
563563
// v288 -> v289
564564
NewMigration("Add user_blocking table", v1_22.AddUserBlockingTable),
565+
// v289 -> v290
566+
NewMigration("Add default_wiki_branch to repository table", v1_22.AddDefaultWikiBranch),
565567
}
566568

567569
// GetCurrentDBVersion returns the current db version

models/migrations/v1_22/v289.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_22
5+
6+
import "xorm.io/xorm"
7+
8+
func AddDefaultWikiBranch(x *xorm.Engine) error {
9+
type Repository struct {
10+
ID int64
11+
DefaultWikiBranch string
12+
}
13+
if err := x.Sync(&Repository{}); err != nil {
14+
return err
15+
}
16+
_, err := x.Exec("UPDATE `repository` SET default_wiki_branch = 'master' WHERE (default_wiki_branch IS NULL) OR (default_wiki_branch = '')")
17+
return err
18+
}

models/repo/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ type Repository struct {
136136
OriginalServiceType api.GitServiceType `xorm:"index"`
137137
OriginalURL string `xorm:"VARCHAR(2048)"`
138138
DefaultBranch string
139+
DefaultWikiBranch string
139140

140141
NumWatches int
141142
NumStars int

modules/git/repo_base_gogit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package git
88

99
import (
1010
"context"
11-
"errors"
1211
"path/filepath"
1312

1413
gitealog "code.gitea.io/gitea/modules/log"
@@ -52,7 +51,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
5251
if err != nil {
5352
return nil, err
5453
} else if !isDir(repoPath) {
55-
return nil, errors.New("no such file or directory")
54+
return nil, util.NewNotExistErrorf("no such file or directory")
5655
}
5756

5857
fs := osfs.New(repoPath)

modules/git/repo_base_nogogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ package git
99
import (
1010
"bufio"
1111
"context"
12-
"errors"
1312
"path/filepath"
1413

1514
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/util"
1616
)
1717

1818
func init() {
@@ -54,7 +54,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
5454
if err != nil {
5555
return nil, err
5656
} else if !isDir(repoPath) {
57-
return nil, errors.New("no such file or directory")
57+
return nil, util.NewNotExistErrorf("no such file or directory")
5858
}
5959

6060
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,8 @@ settings.branches.add_new_rule = Add New Rule
20922092
settings.advanced_settings = Advanced Settings
20932093
settings.wiki_desc = Enable Repository Wiki
20942094
settings.use_internal_wiki = Use Built-In Wiki
2095+
settings.default_wiki_branch_name = Default Wiki Branch Name
2096+
settings.failed_to_change_default_wiki_branch = Failed to change the default wiki branch.
20952097
settings.use_external_wiki = Use External Wiki
20962098
settings.external_wiki_url = External Wiki URL
20972099
settings.external_wiki_url_error = The external wiki URL is not a valid URL.

routers/web/repo/setting/setting.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ func SettingsPost(ctx *context.Context) {
488488
}
489489
}
490490

491+
if form.ChangeDefaultWikiBranch != "" {
492+
if err := wiki_service.ChangeDefaultWikiBranch(ctx, repo, form.ChangeDefaultWikiBranch); err != nil {
493+
log.Error("ChangeDefaultWikiBranch failed, err: %v", err)
494+
ctx.Flash.Warning(ctx.Tr("repo.settings.failed_to_change_default_wiki_branch"))
495+
}
496+
}
497+
491498
if form.EnableIssues && form.EnableExternalTracker && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
492499
if !validation.IsValidExternalURL(form.ExternalTrackerURL) {
493500
ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error"))

routers/web/repo/wiki.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,31 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
9393
}
9494

9595
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
96-
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
96+
wikiGitRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
9797
if err != nil {
9898
ctx.ServerError("OpenRepository", err)
9999
return nil, nil, err
100100
}
101101

102-
commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch)
102+
commit, err := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
103+
if git.IsErrNotExist(err) {
104+
// if the default branch recorded in database is out of sync, then re-sync it
105+
if gitRepoDefaultBranch, err := wikiGitRepo.GetDefaultBranch(); err != nil {
106+
return wikiGitRepo, nil, err
107+
} else {
108+
err := repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, DefaultWikiBranch: gitRepoDefaultBranch}, "default_wiki_branch")
109+
if err != nil {
110+
return wikiGitRepo, nil, err
111+
}
112+
ctx.Repo.Repository.DefaultWikiBranch = gitRepoDefaultBranch
113+
}
114+
// retry to get the commit from the correct default branch
115+
commit, err = wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
116+
}
103117
if err != nil {
104-
return wikiRepo, nil, err
118+
return wikiGitRepo, nil, err
105119
}
106-
return wikiRepo, commit, nil
120+
return wikiGitRepo, commit, nil
107121
}
108122

109123
// wikiContentsByEntry returns the contents of the wiki page referenced by the
@@ -316,7 +330,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
316330
}
317331

318332
// get commit count - wiki revisions
319-
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
333+
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename)
320334
ctx.Data["CommitCount"] = commitsCount
321335

322336
return wikiRepo, entry
@@ -368,7 +382,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
368382
ctx.Data["footerContent"] = ""
369383

370384
// get commit count - wiki revisions
371-
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
385+
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename)
372386
ctx.Data["CommitCount"] = commitsCount
373387

374388
// get page
@@ -380,7 +394,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
380394
// get Commit Count
381395
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
382396
git.CommitsByFileAndRangeOptions{
383-
Revision: wiki_service.DefaultBranch,
397+
Revision: ctx.Repo.Repository.DefaultWikiBranch,
384398
File: pageFilename,
385399
Page: page,
386400
})
@@ -402,20 +416,17 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
402416

403417
func renderEditPage(ctx *context.Context) {
404418
wikiRepo, commit, err := findWikiRepoCommit(ctx)
405-
if err != nil {
419+
defer func() {
406420
if wikiRepo != nil {
407-
wikiRepo.Close()
421+
_ = wikiRepo.Close()
408422
}
423+
}()
424+
if err != nil {
409425
if !git.IsErrNotExist(err) {
410426
ctx.ServerError("GetBranchCommit", err)
411427
}
412428
return
413429
}
414-
defer func() {
415-
if wikiRepo != nil {
416-
wikiRepo.Close()
417-
}
418-
}()
419430

420431
// get requested pagename
421432
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
@@ -584,17 +595,15 @@ func WikiPages(ctx *context.Context) {
584595
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
585596

586597
wikiRepo, commit, err := findWikiRepoCommit(ctx)
587-
if err != nil {
588-
if wikiRepo != nil {
589-
wikiRepo.Close()
590-
}
591-
return
592-
}
593598
defer func() {
594599
if wikiRepo != nil {
595-
wikiRepo.Close()
600+
_ = wikiRepo.Close()
596601
}
597602
}()
603+
if err != nil {
604+
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
605+
return
606+
}
598607

599608
entries, err := commit.ListEntries()
600609
if err != nil {

services/forms/repo_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ type RepoSettingForm struct {
133133
EnableCode bool
134134
EnableWiki bool
135135
EnableExternalWiki bool
136+
ChangeDefaultWikiBranch string
136137
ExternalWikiURL string
137138
EnableIssues bool
138139
EnableExternalTracker bool

services/repository/create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
173173
}
174174

175175
repo.DefaultBranch = setting.Repository.DefaultBranch
176+
repo.DefaultWikiBranch = setting.Repository.DefaultBranch
176177

177178
if len(opts.DefaultBranch) > 0 {
178179
repo.DefaultBranch = opts.DefaultBranch
@@ -240,6 +241,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
240241
TrustModel: opts.TrustModel,
241242
IsMirror: opts.IsMirror,
242243
DefaultBranch: opts.DefaultBranch,
244+
DefaultWikiBranch: setting.Repository.DefaultBranch,
243245
ObjectFormatName: opts.ObjectFormatName,
244246
}
245247

0 commit comments

Comments
 (0)