Skip to content

Make wiki default branch name changable #29603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ var migrations = []Migration{
NewMigration("Use Slug instead of ID for Badges", v1_22.UseSlugInsteadOfIDForBadges),
// v288 -> v289
NewMigration("Add user_blocking table", v1_22.AddUserBlockingTable),
// v289 -> v290
NewMigration("Add default_wiki_branch to repository table", v1_22.AddDefaultUncycloBranch),
}

// GetCurrentDBVersion returns the current db version
Expand Down
18 changes: 18 additions & 0 deletions models/migrations/v1_22/v289.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_22 //nolint

import "xorm.io/xorm"

func AddDefaultUncycloBranch(x *xorm.Engine) error {
type Repository struct {
ID int64
DefaultUncycloBranch string
}
if err := x.Sync(&Repository{}); err != nil {
return err
}
_, err := x.Exec("UPDATE `repository` SET default_wiki_branch = 'master' WHERE (default_wiki_branch IS NULL) OR (default_wiki_branch = '')")
return err
}
4 changes: 4 additions & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type Repository struct {
OriginalServiceType api.GitServiceType `xorm:"index"`
OriginalURL string `xorm:"VARCHAR(2048)"`
DefaultBranch string
DefaultUncycloBranch string

NumWatches int
NumStars int
Expand Down Expand Up @@ -285,6 +286,9 @@ func (repo *Repository) AfterLoad() {
repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
repo.NumOpenActionRuns = repo.NumActionRuns - repo.NumClosedActionRuns
if repo.DefaultUncycloBranch == "" {
repo.DefaultUncycloBranch = setting.Repository.DefaultBranch
}
}

// LoadAttributes loads attributes of the repository.
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_base_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package git

import (
"context"
"errors"
"path/filepath"

gitealog "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

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

fs := osfs.New(repoPath)
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package git
import (
"bufio"
"context"
"errors"
"path/filepath"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

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

// 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!
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,8 @@ settings.branches.add_new_rule = Add New Rule
settings.advanced_settings = Advanced Settings
settings.wiki_desc = Enable Repository Uncyclo
settings.use_internal_wiki = Use Built-In Uncyclo
settings.default_wiki_branch_name = Default Uncyclo Branch Name
settings.failed_to_change_default_wiki_branch = Failed to change the default wiki branch.
settings.use_external_wiki = Use External Uncyclo
settings.external_wiki_url = External Uncyclo URL
settings.external_wiki_url_error = The external wiki URL is not a valid URL.
Expand Down
7 changes: 7 additions & 0 deletions routers/web/repo/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ func SettingsPost(ctx *context.Context) {
}
}

if form.DefaultUncycloBranch != "" {
if err := wiki_service.ChangeDefaultUncycloBranch(ctx, repo, form.DefaultUncycloBranch); err != nil {
log.Error("ChangeDefaultUncycloBranch failed, err: %v", err)
ctx.Flash.Warning(ctx.Tr("repo.settings.failed_to_change_default_wiki_branch"))
}
}

if form.EnableIssues && form.EnableExternalTracker && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
if !validation.IsValidExternalURL(form.ExternalTrackerURL) {
ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error"))
Expand Down
60 changes: 35 additions & 25 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,32 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
}

func findUncycloRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
wikiRepo, err := gitrepo.OpenUncycloRepository(ctx, ctx.Repo.Repository)
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil, nil, err
wikiGitRepo, errGitRepo := gitrepo.OpenUncycloRepository(ctx, ctx.Repo.Repository)
if errGitRepo != nil {
ctx.ServerError("OpenRepository", errGitRepo)
return nil, nil, errGitRepo
}

commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch)
if err != nil {
return wikiRepo, nil, err
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultUncycloBranch)
if git.IsErrNotExist(errCommit) {
// if the default branch recorded in database is out of sync, then re-sync it
gitRepoDefaultBranch, errBranch := wikiGitRepo.GetDefaultBranch()
if errBranch != nil {
return wikiGitRepo, nil, errBranch
}
// update the default branch in the database
errDb := repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, DefaultUncycloBranch: gitRepoDefaultBranch}, "default_wiki_branch")
if errDb != nil {
return wikiGitRepo, nil, errDb
}
ctx.Repo.Repository.DefaultUncycloBranch = gitRepoDefaultBranch
// retry to get the commit from the correct default branch
commit, errCommit = wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultUncycloBranch)
}
return wikiRepo, commit, nil
if errCommit != nil {
return wikiGitRepo, nil, errCommit
}
return wikiGitRepo, commit, nil
}

// wikiContentsByEntry returns the contents of the wiki page referenced by the
Expand Down Expand Up @@ -316,7 +331,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
}

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultUncycloBranch, pageFilename)
ctx.Data["CommitCount"] = commitsCount

return wikiRepo, entry
Expand Down Expand Up @@ -368,7 +383,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.Data["footerContent"] = ""

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultUncycloBranch, pageFilename)
ctx.Data["CommitCount"] = commitsCount

// get page
Expand All @@ -380,7 +395,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
// get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: wiki_service.DefaultBranch,
Revision: ctx.Repo.Repository.DefaultUncycloBranch,
File: pageFilename,
Page: page,
})
Expand All @@ -402,20 +417,17 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)

func renderEditPage(ctx *context.Context) {
wikiRepo, commit, err := findUncycloRepoCommit(ctx)
if err != nil {
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
_ = wikiRepo.Close()
}
}()
if err != nil {
if !git.IsErrNotExist(err) {
ctx.ServerError("GetBranchCommit", err)
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()

// get requested pagename
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
Expand Down Expand Up @@ -584,17 +596,15 @@ func UncycloPages(ctx *context.Context) {
ctx.Data["CanWriteUncyclo"] = ctx.Repo.CanWrite(unit.TypeUncyclo) && !ctx.Repo.Repository.IsArchived

wikiRepo, commit, err := findUncycloRepoCommit(ctx)
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
_ = wikiRepo.Close()
}
}()
if err != nil {
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
return
}

entries, err := commit.ListEntries()
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions routers/web/repo/wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"testing"

"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
Expand Down Expand Up @@ -221,3 +222,32 @@ func TestUncycloRaw(t *testing.T) {
}
}
}

func TestDefaultUncycloBranch(t *testing.T) {
unittest.PrepareTestEnv(t)

assert.NoError(t, repo_model.UpdateRepositoryCols(db.DefaultContext, &repo_model.Repository{ID: 1, DefaultUncycloBranch: "wrong-branch"}))

ctx, _ := contexttest.MockContext(t, "user2/repo1/wiki")
ctx.SetParams("*", "Home")
contexttest.LoadRepo(t, ctx, 1)
assert.Equal(t, "wrong-branch", ctx.Repo.Repository.DefaultUncycloBranch)
Uncyclo(ctx) // after the visiting, the out-of-sync database record will update the branch name to "master"
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "master", ctx.Repo.Repository.DefaultUncycloBranch)

// invalid branch name should fail
assert.Error(t, wiki_service.ChangeDefaultUncycloBranch(db.DefaultContext, repo, "the bad name"))
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "master", repo.DefaultUncycloBranch)

// the same branch name, should succeed (actually a no-op)
assert.NoError(t, wiki_service.ChangeDefaultUncycloBranch(db.DefaultContext, repo, "master"))
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "master", repo.DefaultUncycloBranch)

// change to another name
assert.NoError(t, wiki_service.ChangeDefaultUncycloBranch(db.DefaultContext, repo, "main"))
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "main", repo.DefaultUncycloBranch)
}
1 change: 1 addition & 0 deletions services/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type RepoSettingForm struct {
EnableCode bool
EnableUncyclo bool
EnableExternalUncyclo bool
DefaultUncycloBranch string
ExternalUncycloURL string
EnableIssues bool
EnableExternalTracker bool
Expand Down
2 changes: 2 additions & 0 deletions services/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
}

repo.DefaultBranch = setting.Repository.DefaultBranch
repo.DefaultUncycloBranch = setting.Repository.DefaultBranch

if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
Expand Down Expand Up @@ -240,6 +241,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
TrustModel: opts.TrustModel,
IsMirror: opts.IsMirror,
DefaultBranch: opts.DefaultBranch,
DefaultUncycloBranch: setting.Repository.DefaultBranch,
ObjectFormatName: opts.ObjectFormatName,
}

Expand Down
Loading