Skip to content

Commit 5cddab4

Browse files
authored
Make wiki default branch name changable (#29603)
Fix #29000 Fix #28685 Fix #18568 Related: #27497 And by the way fix #24036, add a Cancel button there (one line)
1 parent da15d61 commit 5cddab4

File tree

17 files changed

+232
-89
lines changed

17 files changed

+232
-89
lines changed

models/migrations/migrations.go

+2
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

+18
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 //nolint
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

+4
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
@@ -285,6 +286,9 @@ func (repo *Repository) AfterLoad() {
285286
repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
286287
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
287288
repo.NumOpenActionRuns = repo.NumActionRuns - repo.NumClosedActionRuns
289+
if repo.DefaultWikiBranch == "" {
290+
repo.DefaultWikiBranch = setting.Repository.DefaultBranch
291+
}
288292
}
289293

290294
// LoadAttributes loads attributes of the repository.

modules/git/repo_base_gogit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ package git
88

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

1413
gitealog "code.gitea.io/gitea/modules/log"
1514
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/modules/util"
1616

1717
"github.com/go-git/go-billy/v5"
1818
"github.com/go-git/go-billy/v5/osfs"
@@ -52,7 +52,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
5252
if err != nil {
5353
return nil, err
5454
} else if !isDir(repoPath) {
55-
return nil, errors.New("no such file or directory")
55+
return nil, util.NewNotExistErrorf("no such file or directory")
5656
}
5757

5858
fs := osfs.New(repoPath)

modules/git/repo_base_nogogit.go

+2-2
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

+2
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

+7
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ func SettingsPost(ctx *context.Context) {
488488
}
489489
}
490490

491+
if form.DefaultWikiBranch != "" {
492+
if err := wiki_service.ChangeDefaultWikiBranch(ctx, repo, form.DefaultWikiBranch); 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

+35-25
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,32 @@ 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)
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

403418
func 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 {

routers/web/repo/wiki_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"code.gitea.io/gitea/models/db"
1213
repo_model "code.gitea.io/gitea/models/repo"
1314
"code.gitea.io/gitea/models/unittest"
1415
"code.gitea.io/gitea/modules/git"
@@ -221,3 +222,32 @@ func TestWikiRaw(t *testing.T) {
221222
}
222223
}
223224
}
225+
226+
func TestDefaultWikiBranch(t *testing.T) {
227+
unittest.PrepareTestEnv(t)
228+
229+
assert.NoError(t, repo_model.UpdateRepositoryCols(db.DefaultContext, &repo_model.Repository{ID: 1, DefaultWikiBranch: "wrong-branch"}))
230+
231+
ctx, _ := contexttest.MockContext(t, "user2/repo1/wiki")
232+
ctx.SetParams("*", "Home")
233+
contexttest.LoadRepo(t, ctx, 1)
234+
assert.Equal(t, "wrong-branch", ctx.Repo.Repository.DefaultWikiBranch)
235+
Wiki(ctx) // after the visiting, the out-of-sync database record will update the branch name to "master"
236+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
237+
assert.Equal(t, "master", ctx.Repo.Repository.DefaultWikiBranch)
238+
239+
// invalid branch name should fail
240+
assert.Error(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "the bad name"))
241+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
242+
assert.Equal(t, "master", repo.DefaultWikiBranch)
243+
244+
// the same branch name, should succeed (actually a no-op)
245+
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "master"))
246+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
247+
assert.Equal(t, "master", repo.DefaultWikiBranch)
248+
249+
// change to another name
250+
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repo, "main"))
251+
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
252+
assert.Equal(t, "main", repo.DefaultWikiBranch)
253+
}

services/forms/repo_form.go

+1
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+
DefaultWikiBranch string
136137
ExternalWikiURL string
137138
EnableIssues bool
138139
EnableExternalTracker bool

services/repository/create.go

+2
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)