mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-01 13:44:06 +01:00
Merge pull request 'Allow changing the repo Wiki branch to main' (#2264) from algernon/forgejo:wiki/default-branch-name into forgejo-dependency
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2264 Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
commit
7420102b1b
20 changed files with 371 additions and 31 deletions
|
@ -48,6 +48,8 @@ var migrations = []*Migration{
|
|||
NewMigration("Add default_permissions to repo_unit", forgejo_v1_22.AddDefaultPermissionsToRepoUnit),
|
||||
// v4 -> v5
|
||||
NewMigration("create the forgejo_repo_flag table", forgejo_v1_22.CreateRepoFlagTable),
|
||||
// v5 -> v6
|
||||
NewMigration("Add wiki_branch to repository", forgejo_v1_22.AddWikiBranchToRepository),
|
||||
}
|
||||
|
||||
// GetCurrentDBVersion returns the current Forgejo database version.
|
||||
|
|
24
models/forgejo_migrations/v1_22/v6.go
Normal file
24
models/forgejo_migrations/v1_22/v6.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package v1_22 //nolint
|
||||
|
||||
import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func AddWikiBranchToRepository(x *xorm.Engine) error {
|
||||
type Repository struct {
|
||||
ID int64
|
||||
WikiBranch string
|
||||
}
|
||||
|
||||
if err := x.Sync(&Repository{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update existing repositories to use `master` as the wiki branch, for
|
||||
// compatilibty's sake.
|
||||
_, err := x.Exec("UPDATE repository SET wiki_branch = 'master' WHERE wiki_branch = '' OR wiki_branch IS NULL")
|
||||
return err
|
||||
}
|
|
@ -135,6 +135,7 @@ type Repository struct {
|
|||
OriginalServiceType api.GitServiceType `xorm:"index"`
|
||||
OriginalURL string `xorm:"VARCHAR(2048)"`
|
||||
DefaultBranch string
|
||||
WikiBranch string
|
||||
|
||||
NumWatches int
|
||||
NumStars int
|
||||
|
@ -204,6 +205,13 @@ func (repo *Repository) GetOwnerName() string {
|
|||
return repo.OwnerName
|
||||
}
|
||||
|
||||
func (repo *Repository) GetWikiBranchName() string {
|
||||
if repo.WikiBranch == "" {
|
||||
return setting.Repository.DefaultBranch
|
||||
}
|
||||
return repo.WikiBranch
|
||||
}
|
||||
|
||||
// SanitizedOriginalURL returns a sanitized OriginalURL
|
||||
func (repo *Repository) SanitizedOriginalURL() string {
|
||||
if repo.OriginalURL == "" {
|
||||
|
|
|
@ -400,6 +400,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
|
|||
ctx.Data["PushMirrors"] = pushMirrors
|
||||
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
|
||||
ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty
|
||||
ctx.Data["DefaultWikiBranchName"] = setting.Repository.DefaultBranch
|
||||
}
|
||||
|
||||
// RepoIDAssignment returns a handler which assigns the repo to the context.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
// 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 repository
|
||||
|
@ -99,7 +100,6 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
|
|||
Mirror: true,
|
||||
Quiet: true,
|
||||
Timeout: migrateTimeout,
|
||||
Branch: "master",
|
||||
SkipTLSVerify: setting.Migrations.SkipTLSVerify,
|
||||
}); err != nil {
|
||||
log.Warn("Clone wiki: %v", err)
|
||||
|
@ -107,6 +107,30 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
|
|||
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
|
||||
}
|
||||
} else {
|
||||
// Figure out the branch of the wiki we just cloned. We assume
|
||||
// that the default branch is to be used, and we'll use the same
|
||||
// name as the source.
|
||||
gitRepo, err := git.OpenRepository(ctx, wikiPath)
|
||||
if err != nil {
|
||||
log.Warn("Failed to open wiki repository during migration: %v", err)
|
||||
if err := util.RemoveAll(wikiPath); err != nil {
|
||||
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
|
||||
}
|
||||
return repo, err
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
|
||||
branch, err := gitRepo.GetDefaultBranch()
|
||||
if err != nil {
|
||||
log.Warn("Failed to get the default branch of a migrated wiki repo: %v", err)
|
||||
if err := util.RemoveAll(wikiPath); err != nil {
|
||||
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
|
||||
}
|
||||
|
||||
return repo, err
|
||||
}
|
||||
repo.WikiBranch = branch
|
||||
|
||||
if err := git.WriteCommitGraph(ctx, wikiPath); err != nil {
|
||||
return repo, err
|
||||
}
|
||||
|
|
|
@ -88,6 +88,7 @@ type Repository struct {
|
|||
ExternalTracker *ExternalTracker `json:"external_tracker,omitempty"`
|
||||
HasWiki bool `json:"has_wiki"`
|
||||
ExternalWiki *ExternalWiki `json:"external_wiki,omitempty"`
|
||||
WikiBranch string `json:"wiki_branch,omitempty"`
|
||||
HasPullRequests bool `json:"has_pull_requests"`
|
||||
HasProjects bool `json:"has_projects"`
|
||||
HasReleases bool `json:"has_releases"`
|
||||
|
@ -175,6 +176,8 @@ type EditRepoOption struct {
|
|||
ExternalWiki *ExternalWiki `json:"external_wiki,omitempty"`
|
||||
// sets the default branch for this repository.
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
// sets the branch used for this repository's wiki.
|
||||
WikiBranch *string `json:"wiki_branch,omitempty"`
|
||||
// either `true` to allow pull requests, or `false` to prevent pull request.
|
||||
HasPullRequests *bool `json:"has_pull_requests,omitempty"`
|
||||
// either `true` to enable project unit, or `false` to disable them.
|
||||
|
|
|
@ -2119,7 +2119,7 @@ settings.transfer_abort = Cancel transfer
|
|||
settings.transfer_abort_invalid = You cannot cancel a non existent repository transfer.
|
||||
settings.transfer_abort_success = The repository transfer to %s was successfully canceled.
|
||||
settings.transfer_desc = Transfer this repository to a user or to an organization for which you have administrator rights.
|
||||
settings.transfer_form_title = Enter the repository name as confirmation:
|
||||
settings.enter_repo_name = Enter the repository name as confirmation:
|
||||
settings.transfer_in_progress = There is currently an ongoing transfer. Please cancel it if you will like to transfer this repository to another user.
|
||||
settings.transfer_notices_1 = - You will lose access to the repository if you transfer it to an individual user.
|
||||
settings.transfer_notices_2 = - You will keep access to the repository if you transfer it to an organization that you (co-)own.
|
||||
|
@ -2141,6 +2141,13 @@ settings.trust_model.committer.desc = Valid signatures will only be marked "trus
|
|||
settings.trust_model.collaboratorcommitter = Collaborator+Committer
|
||||
settings.trust_model.collaboratorcommitter.long = Collaborator+Committer: Trust signatures by collaborators which match the committer
|
||||
settings.trust_model.collaboratorcommitter.desc = Valid signatures by collaborators of this repository will be marked "trusted" if they match the committer. Otherwise, valid signatures will be marked "untrusted" if the signature matches the committer and "unmatched" otherwise. This will force Forgejo to be marked as the committer on signed commits with the actual committer marked as Co-Authored-By: and Co-Committed-By: trailer in the commit. The default Forgejo key must match a User in the database.
|
||||
settings.wiki_rename_branch_main = Normalize the Wiki branch name
|
||||
settings.wiki_rename_branch_main_desc = Rename the branch used internally by the Wiki to "%s". This is a permanent and cannot be undone.
|
||||
settings.wiki_rename_branch_main_notices_1 = This operation <strong>CANNOT</strong> be undone.
|
||||
settings.wiki_rename_branch_main_notices_2 = This will premanently rename the the internal branch of %s's repository wiki. Existing checkouts will need to be updated.
|
||||
settings.wiki_branch_rename_success = The repository wiki's branch name has been successfully normalized.
|
||||
settings.wiki_branch_rename_failure = Failed to normalize the repository wiki's branch name.
|
||||
settings.confirm_wiki_branch_rename = Rename the wiki branch
|
||||
settings.wiki_delete = Delete Wiki Data
|
||||
settings.wiki_delete_desc = Deleting repository wiki data is permanent and cannot be undone.
|
||||
settings.wiki_delete_notices_1 = - This will permanently delete and disable the repository wiki for %s.
|
||||
|
|
|
@ -34,6 +34,7 @@ import (
|
|||
"code.gitea.io/gitea/services/convert"
|
||||
"code.gitea.io/gitea/services/issue"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
wiki_service "code.gitea.io/gitea/services/wiki"
|
||||
)
|
||||
|
||||
// Search repositories via options
|
||||
|
@ -740,6 +741,18 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
|
|||
repo.DefaultBranch = *opts.DefaultBranch
|
||||
}
|
||||
|
||||
// Wiki branch is updated if changed
|
||||
if opts.WikiBranch != nil && repo.WikiBranch != *opts.WikiBranch {
|
||||
if err := wiki_service.NormalizeWikiBranch(ctx, repo, *opts.WikiBranch); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "NormalizeWikiBranch", err)
|
||||
return err
|
||||
}
|
||||
// While NormalizeWikiBranch updates the db, we need to update *this*
|
||||
// instance of `repo`, so that the `UpdateRepository` below will not
|
||||
// reset the branch back.
|
||||
repo.WikiBranch = *opts.WikiBranch
|
||||
}
|
||||
|
||||
if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
|
||||
return err
|
||||
|
|
|
@ -872,6 +872,27 @@ func SettingsPost(ctx *context.Context) {
|
|||
ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
|
||||
|
||||
case "rename-wiki-branch":
|
||||
if !ctx.Repo.IsOwner() {
|
||||
ctx.Error(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if repo.FullName() != form.RepoName {
|
||||
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := wiki_service.NormalizeWikiBranch(ctx, repo, setting.Repository.DefaultBranch); err != nil {
|
||||
log.Error("Normalize Wiki branch: %v", err.Error())
|
||||
ctx.Flash.Error(ctx.Tr("repo.settings.wiki_branch_rename_failure"))
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
|
||||
return
|
||||
}
|
||||
log.Trace("Repository wiki normalized: %s#%s", repo.FullName(), setting.Repository.DefaultBranch)
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("repo.settings.wiki_branch_rename_success"))
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
|
||||
|
||||
case "archive":
|
||||
if !ctx.Repo.IsOwner() {
|
||||
ctx.Error(http.StatusForbidden)
|
||||
|
|
|
@ -99,7 +99,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch)
|
||||
commit, err := wikiRepo.GetBranchCommit(ctx.Repo.Repository.GetWikiBranchName())
|
||||
if err != nil {
|
||||
return wikiRepo, nil, err
|
||||
}
|
||||
|
@ -316,7 +316,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.GetWikiBranchName(), pageFilename)
|
||||
ctx.Data["CommitCount"] = commitsCount
|
||||
|
||||
return wikiRepo, entry
|
||||
|
@ -368,7 +368,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.GetWikiBranchName(), pageFilename)
|
||||
ctx.Data["CommitCount"] = commitsCount
|
||||
|
||||
// get page
|
||||
|
@ -380,7 +380,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.GetWikiBranchName(),
|
||||
File: pageFilename,
|
||||
Page: page,
|
||||
})
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -68,7 +68,7 @@
|
|||
<input type="hidden" name="action" value="delete">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
|
|
@ -769,6 +769,17 @@
|
|||
</div>
|
||||
</div>
|
||||
{{if .Permission.CanRead $.UnitTypeWiki}}
|
||||
{{if ne $.Repository.GetWikiBranchName .DefaultWikiBranchName}}
|
||||
<div class="flex-item">
|
||||
<div class="flex-item-main">
|
||||
<div class="flex-item-title">{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}</div>
|
||||
<div class="flex-item-body">{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_desc" .DefaultWikiBranchName}}</div>
|
||||
</div>
|
||||
<div class="flex-item-trailing">
|
||||
<button class="ui basic red show-modal button" data-modal="#rename-wiki-branch-modal">{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}</button>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
<div class="flex-item">
|
||||
<div class="flex-item-main">
|
||||
<div class="flex-item-title">{{ctx.Locale.Tr "repo.settings.wiki_delete"}}</div>
|
||||
|
@ -831,7 +842,7 @@
|
|||
<input type="hidden" name="action" value="convert">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -862,7 +873,7 @@
|
|||
<input type="hidden" name="action" value="convert_fork">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -894,7 +905,7 @@
|
|||
<input type="hidden" name="action" value="transfer">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -932,7 +943,7 @@
|
|||
<input type="hidden" name="action" value="delete">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -964,7 +975,7 @@
|
|||
<input type="hidden" name="action" value="delete-wiki">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -980,6 +991,40 @@
|
|||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{if ne $.Repository.GetWikiBranchName .DefaultWikiBranchName}}
|
||||
<div class="ui small modal" id="rename-wiki-branch-modal">
|
||||
<div class="header">
|
||||
{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}
|
||||
</div>
|
||||
<div class="content">
|
||||
<div class="ui warning message">
|
||||
<ul>
|
||||
<li>{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_notices_1" | Safe}}</li>
|
||||
<li>{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_notices_2" .Repository.Name}}</li>
|
||||
</ul>
|
||||
</div>
|
||||
<form class="ui form" action="{{.Link}}" method="post">
|
||||
{{.CsrfTokenHtml}}
|
||||
<input type="hidden" name="action" value="rename-wiki-branch">
|
||||
<div class="field">
|
||||
<label>
|
||||
{{ctx.Locale.Tr "repo.settings.enter_repo_name"}}
|
||||
<span class="text red">{{.Repository.FullName}}</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="required field">
|
||||
<label for="repo_name">{{ctx.Locale.Tr "repo.repo_name"}}</label>
|
||||
<input id="repo_name" name="repo_name" required>
|
||||
</div>
|
||||
|
||||
<div class="text right actions">
|
||||
<button class="ui cancel button">{{ctx.Locale.Tr "settings.cancel"}}</button>
|
||||
<button class="ui red button">{{ctx.Locale.Tr "repo.settings.confirm_wiki_branch_rename"}}</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{if not .Repository.IsMirror}}
|
||||
|
|
9
templates/swagger/v1_json.tmpl
generated
9
templates/swagger/v1_json.tmpl
generated
|
@ -19839,6 +19839,11 @@
|
|||
"description": "a URL with more information about the repository.",
|
||||
"type": "string",
|
||||
"x-go-name": "Website"
|
||||
},
|
||||
"wiki_branch": {
|
||||
"description": "sets the branch used for this repository's wiki.",
|
||||
"type": "string",
|
||||
"x-go-name": "WikiBranch"
|
||||
}
|
||||
},
|
||||
"x-go-package": "code.gitea.io/gitea/modules/structs"
|
||||
|
@ -22792,6 +22797,10 @@
|
|||
"website": {
|
||||
"type": "string",
|
||||
"x-go-name": "Website"
|
||||
},
|
||||
"wiki_branch": {
|
||||
"type": "string",
|
||||
"x-go-name": "WikiBranch"
|
||||
}
|
||||
},
|
||||
"x-go-package": "code.gitea.io/gitea/modules/structs"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
@ -21,6 +22,30 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAPIRenameWikiBranch(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
username := "user2"
|
||||
session := loginUser(t, username)
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
||||
|
||||
repoURLStr := fmt.Sprintf("/api/v1/repos/%s/%s", username, "repo1")
|
||||
wikiBranch := "wiki"
|
||||
req := NewRequestWithJSON(t, "PATCH", repoURLStr, &api.EditRepoOption{
|
||||
WikiBranch: &wikiBranch,
|
||||
}).AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
assert.Equal(t, "wiki", repo.WikiBranch)
|
||||
|
||||
req = NewRequest(t, "GET", repoURLStr)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
var repoData *api.Repository
|
||||
DecodeJSON(t, resp, &repoData)
|
||||
assert.Equal(t, "wiki", repoData.WikiBranch)
|
||||
}
|
||||
|
||||
func TestAPIGetWikiPage(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
|
|
|
@ -89,6 +89,39 @@ func TestDangerZoneConfirmation(t *testing.T) {
|
|||
})
|
||||
})
|
||||
|
||||
t.Run("Rename wiki branch", func(t *testing.T) {
|
||||
session := loginUser(t, "user2")
|
||||
|
||||
// NOTE: No need to rename the wiki branch here to make the form appear.
|
||||
// We can submit it anyway, even if it doesn't appear on the web.
|
||||
|
||||
t.Run("Fail", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings", map[string]string{
|
||||
"_csrf": GetCSRF(t, session, "/user2/repo1/settings"),
|
||||
"action": "rename-wiki-branch",
|
||||
"repo_name": "repo1",
|
||||
})
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
mustInvalidRepoName(resp)
|
||||
})
|
||||
t.Run("Pass", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings", map[string]string{
|
||||
"_csrf": GetCSRF(t, session, "/user2/repo1/settings"),
|
||||
"action": "rename-wiki-branch",
|
||||
"repo_name": "user2/repo1",
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
|
||||
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
|
||||
assert.NotNil(t, flashCookie)
|
||||
assert.EqualValues(t, "success%3DThe%2Brepository%2Bwiki%2527s%2Bbranch%2Bname%2Bhas%2Bbeen%2Bsuccessfully%2Bnormalized.", flashCookie.Value)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Delete wiki", func(t *testing.T) {
|
||||
session := loginUser(t, "user2")
|
||||
|
||||
|
|
74
tests/integration/repo_wiki_test.go
Normal file
74
tests/integration/repo_wiki_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWikiBranchNormalize(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
username := "user2"
|
||||
session := loginUser(t, username)
|
||||
settingsURLStr := "/user2/repo1/settings"
|
||||
|
||||
assertNormalizeButton := func(present bool) string {
|
||||
req := NewRequest(t, "GET", settingsURLStr) //.AddTokenAuth(token)
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
htmlDoc.AssertElement(t, "button[data-modal='#rename-wiki-branch-modal']", present)
|
||||
|
||||
return htmlDoc.GetCSRF()
|
||||
}
|
||||
|
||||
// By default the repo wiki branch is empty
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
assert.Empty(t, repo.WikiBranch)
|
||||
|
||||
// This means we default to setting.Repository.DefaultBranch
|
||||
assert.Equal(t, setting.Repository.DefaultBranch, repo.GetWikiBranchName())
|
||||
|
||||
// Which further means that the "Normalize wiki branch" parts do not appear on settings
|
||||
assertNormalizeButton(false)
|
||||
|
||||
// Lets rename the branch!
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
||||
repoURLStr := fmt.Sprintf("/api/v1/repos/%s/%s", username, repo.Name)
|
||||
wikiBranch := "wiki"
|
||||
req := NewRequestWithJSON(t, "PATCH", repoURLStr, &api.EditRepoOption{
|
||||
WikiBranch: &wikiBranch,
|
||||
}).AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
// The wiki branch should now be changed
|
||||
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
assert.Equal(t, wikiBranch, repo.GetWikiBranchName())
|
||||
|
||||
// And as such, the button appears!
|
||||
csrf := assertNormalizeButton(true)
|
||||
|
||||
// Invoking the normalization renames the wiki branch back to the default
|
||||
req = NewRequestWithValues(t, "POST", settingsURLStr, map[string]string{
|
||||
"_csrf": csrf,
|
||||
"action": "rename-wiki-branch",
|
||||
"repo_name": repo.FullName(),
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
|
||||
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
assert.Equal(t, setting.Repository.DefaultBranch, repo.GetWikiBranchName())
|
||||
assertNormalizeButton(false)
|
||||
}
|
Loading…
Reference in a new issue