summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorGergely Nagy <forgejo@gergo.csillger.hu>2024-01-30 12:18:53 +0100
committerEarl Warren <contact@earl-warren.org>2024-02-05 16:57:47 +0100
commit2ca4862f8b841e7b41834840bfae92a896ad2c21 (patch)
tree2eda8315d4077d47504871e7ac099a3d98257454 /modules
parent[GITEA] Use correct translations for pull request (diff)
downloadforgejo-2ca4862f8b841e7b41834840bfae92a896ad2c21.tar.xz
forgejo-2ca4862f8b841e7b41834840bfae92a896ad2c21.zip
[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)
Diffstat (limited to 'modules')
-rw-r--r--modules/context/repo.go1
-rw-r--r--modules/repository/repo.go26
-rw-r--r--modules/structs/repo.go3
3 files changed, 29 insertions, 1 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go
index 75ebfec705..b48f6ded26 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -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.
diff --git a/modules/repository/repo.go b/modules/repository/repo.go
index fc3af04071..65b50b2e45 100644
--- a/modules/repository/repo.go
+++ b/modules/repository/repo.go
@@ -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
}
diff --git a/modules/structs/repo.go b/modules/structs/repo.go
index 51e175fba8..483723b575 100644
--- a/modules/structs/repo.go
+++ b/modules/structs/repo.go
@@ -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.