[GITEA] Allow changing the repo Wiki branch to main

Previously, the repo wiki was hardcoded to use `master` as its branch,
this change makes it possible to use `main` (or something else, governed
by `[repository].DEFAULT_BRANCH`, a setting that already exists and
defaults to `main`).

The way it is done is that a new column is added to the `repository`
table: `wiki_branch`. The migration will make existing repositories
default to `master`, for compatibility's sake, even if they don't have a
Wiki (because it's easier to do that). Newly created repositories will
default to `[repository].DEFAULT_BRANCH` instead.

The Wiki service was updated to use the branch name stored in the
database, and fall back to the default if it is empty.

Old repositories with Wikis using the older `master` branch will have
the option to do a one-time transition to `main`, available via the
repository settings in the "Danger Zone". This option will only be
available for repositories that have the internal wiki enabled, it is
not empty, and the wiki branch is not `[repository].DEFAULT_BRANCH`.

When migrating a repository with a Wiki, Forgejo will use the same
branch name for the wiki as the source repository did. If that's not the
same as the default, the option to normalize it will be available after
the migration's done.

Additionally, the `/api/v1/{owner}/{repo}` endpoint was updated: it will
now include the wiki branch name in `GET` requests, and allow changing
the wiki branch via `PATCH`.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit d87c526d2a313fa45093ab49b78bb30322b33298)
This commit is contained in:
Gergely Nagy 2024-01-30 12:18:53 +01:00 committed by Earl Warren
parent 361617eea0
commit 2ca4862f8b
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
19 changed files with 364 additions and 24 deletions

View file

@ -208,6 +208,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR
ExternalTracker: externalTracker,
InternalTracker: internalTracker,
HasWiki: hasWiki,
WikiBranch: repo.WikiBranch,
HasProjects: hasProjects,
HasReleases: hasReleases,
HasPackages: hasPackages,

View file

@ -173,6 +173,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
}
repo.DefaultBranch = setting.Repository.DefaultBranch
repo.WikiBranch = setting.Repository.DefaultBranch
if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
@ -240,6 +241,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
TrustModel: opts.TrustModel,
IsMirror: opts.IsMirror,
DefaultBranch: opts.DefaultBranch,
WikiBranch: setting.Repository.DefaultBranch,
ObjectFormatName: opts.ObjectFormatName,
}

View file

@ -1,5 +1,6 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package wiki
@ -26,7 +27,6 @@ var wikiWorkingPool = sync.NewExclusivePool()
const (
DefaultRemote = "origin"
DefaultBranch = "master"
)
// InitWiki initializes a wiki for repository,
@ -36,26 +36,74 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
return nil
}
branch := repo.GetWikiBranchName()
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
return fmt.Errorf("InitRepository: %w", err)
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
return fmt.Errorf("createDelegateHooks: %w", err)
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+DefaultBranch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
return fmt.Errorf("unable to set default wiki branch to master: %w", err)
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix + branch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
return fmt.Errorf("unable to set default wiki branch to %s: %w", branch, err)
}
return nil
}
// NormalizeWikiBranch renames a repository wiki's branch to `setting.Repository.DefaultBranch`
func NormalizeWikiBranch(ctx context.Context, repo *repo_model.Repository, to string) error {
from := repo.GetWikiBranchName()
if err := repo.MustNotBeArchived(); err != nil {
return err
}
updateDB := func() error {
repo.WikiBranch = to
return repo_model.UpdateRepositoryCols(ctx, repo, "wiki_branch")
}
if !repo.HasWiki() {
return updateDB()
}
if from == to {
return nil
}
gitRepo, err := git.OpenRepository(ctx, repo.WikiPath())
if err != nil {
return err
}
defer gitRepo.Close()
if gitRepo.IsBranchExist(to) {
return nil
}
if !gitRepo.IsBranchExist(from) {
return nil
}
if err := gitRepo.RenameBranch(from, to); err != nil {
return err
}
if err := gitRepo.SetDefaultBranch(to); err != nil {
return err
}
return updateDB()
}
// prepareGitPath try to find a suitable file path with file name by the given raw wiki name.
// return: existence, prepared file path with name, error
func prepareGitPath(gitRepo *git.Repository, wikiPath WebPath) (bool, string, error) {
func prepareGitPath(gitRepo *git.Repository, branch string, wikiPath WebPath) (bool, string, error) {
unescaped := string(wikiPath) + ".md"
gitPath := WebPathToGitPath(wikiPath)
// Look for both files
filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, gitPath)
filesInIndex, err := gitRepo.LsTree(branch, unescaped, gitPath)
if err != nil {
if strings.Contains(err.Error(), "Not a valid object name master") {
if strings.Contains(err.Error(), "Not a valid object name "+branch) {
return false, gitPath, nil
}
log.Error("%v", err)
@ -94,7 +142,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("InitWiki: %w", err)
}
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), DefaultBranch)
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.GetWikiBranchName())
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
if err != nil {
@ -112,7 +160,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
}
if hasMasterBranch {
cloneOpts.Branch = DefaultBranch
cloneOpts.Branch = repo.GetWikiBranchName()
}
if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil {
@ -134,7 +182,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
}
}
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, newWikiName)
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.GetWikiBranchName(), newWikiName)
if err != nil {
return err
}
@ -150,7 +198,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
isOldWikiExist := true
oldWikiPath := newWikiPath
if oldWikiName != newWikiName {
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, oldWikiName)
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.GetWikiBranchName(), oldWikiName)
if err != nil {
return err
}
@ -211,7 +259,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: DefaultRemote,
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.GetWikiBranchName()),
Env: repo_module.FullPushingEnvironment(
doer,
doer,
@ -268,7 +316,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
Bare: true,
Shared: true,
Branch: DefaultBranch,
Branch: repo.GetWikiBranchName(),
}); err != nil {
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err)
@ -286,7 +334,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err)
}
found, wikiPath, err := prepareGitPath(gitRepo, wikiName)
found, wikiPath, err := prepareGitPath(gitRepo, repo.GetWikiBranchName(), wikiName)
if err != nil {
return err
}
@ -330,7 +378,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: DefaultRemote,
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.GetWikiBranchName()),
Env: repo_module.FullPushingEnvironment(
doer,
doer,

View file

@ -170,7 +170,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
return
}
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree(DefaultBranch)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
gitPath := WebPathToGitPath(webPath)
entry, err := masterTree.GetTreeEntryByPath(gitPath)
@ -215,7 +215,7 @@ func TestRepository_EditWikiPage(t *testing.T) {
// Now need to show that the page has been added:
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
assert.NoError(t, err)
masterTree, err := gitRepo.GetTree(DefaultBranch)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
gitPath := WebPathToGitPath(webPath)
entry, err := masterTree.GetTreeEntryByPath(gitPath)
@ -242,7 +242,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
return
}
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree(DefaultBranch)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
gitPath := WebPathToGitPath("Home")
_, err = masterTree.GetTreeEntryByPath(gitPath)
@ -280,7 +280,7 @@ func TestPrepareWikiFileName(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
webPath := UserTitleToWebPath("", tt.arg)
existence, newWikiPath, err := prepareGitPath(gitRepo, webPath)
existence, newWikiPath, err := prepareGitPath(gitRepo, "master", webPath)
if (err != nil) != tt.wantErr {
assert.NoError(t, err)
return
@ -312,7 +312,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) {
}
defer gitRepo.Close()
existence, newWikiPath, err := prepareGitPath(gitRepo, "Home")
existence, newWikiPath, err := prepareGitPath(gitRepo, "master", "Home")
assert.False(t, existence)
assert.NoError(t, err)
assert.EqualValues(t, "Home.md", newWikiPath)