2020-01-07 23:57:36 +05:30
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-07 23:57:36 +05:30
|
|
|
|
|
|
|
package wiki
|
|
|
|
|
|
|
|
import (
|
2022-01-20 04:56:57 +05:30
|
|
|
"context"
|
2020-01-07 23:57:36 +05:30
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-10-17 04:59:26 +05:30
|
|
|
system_model "code.gitea.io/gitea/models/system"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-07 23:57:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2024-03-08 13:00:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2020-01-07 23:57:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-21 01:31:19 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-07 23:57:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/sync"
|
2021-12-10 13:44:24 +05:30
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2024-01-13 03:20:38 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-01-07 23:57:36 +05:30
|
|
|
)
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
// TODO: use clustered lock (unique queue? or *abuse* cache)
|
|
|
|
var wikiWorkingPool = sync.NewExclusivePool()
|
2020-01-07 23:57:36 +05:30
|
|
|
|
2022-10-15 20:10:32 +05:30
|
|
|
const (
|
|
|
|
DefaultRemote = "origin"
|
|
|
|
)
|
|
|
|
|
2020-01-07 23:57:36 +05:30
|
|
|
// InitWiki initializes a wiki for repository,
|
|
|
|
// it does nothing when repository already has wiki.
|
2022-01-20 04:56:57 +05:30
|
|
|
func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
|
2020-01-07 23:57:36 +05:30
|
|
|
if repo.HasWiki() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
branch := repo.GetWikiBranchName()
|
|
|
|
|
2023-12-17 17:26:08 +05:30
|
|
|
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("InitRepository: %w", err)
|
2020-01-21 01:31:19 +05:30
|
|
|
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("createDelegateHooks: %w", err)
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
} 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)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-03-08 13:00:10 +05:30
|
|
|
if err := gitrepo.SetDefaultBranch(ctx, repo, to); err != nil {
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateDB()
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
// prepareGitPath try to find a suitable file path with file name by the given raw wiki name.
|
2021-07-08 04:53:09 +05:30
|
|
|
// return: existence, prepared file path with name, error
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
func prepareGitPath(gitRepo *git.Repository, branch string, wikiPath WebPath) (bool, string, error) {
|
2023-04-19 23:20:10 +05:30
|
|
|
unescaped := string(wikiPath) + ".md"
|
|
|
|
gitPath := WebPathToGitPath(wikiPath)
|
2021-07-08 04:53:09 +05:30
|
|
|
|
|
|
|
// Look for both files
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
filesInIndex, err := gitRepo.LsTree(branch, unescaped, gitPath)
|
2021-07-08 04:53:09 +05:30
|
|
|
if err != nil {
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
if strings.Contains(err.Error(), "Not a valid object name "+branch) {
|
2023-04-19 23:20:10 +05:30
|
|
|
return false, gitPath, nil
|
2021-08-01 22:34:32 +05:30
|
|
|
}
|
2021-07-08 04:53:09 +05:30
|
|
|
log.Error("%v", err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return false, gitPath, err
|
2021-07-08 04:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
foundEscaped := false
|
|
|
|
for _, filename := range filesInIndex {
|
|
|
|
switch filename {
|
|
|
|
case unescaped:
|
|
|
|
// if we find the unescaped file return it
|
|
|
|
return true, unescaped, nil
|
2023-04-19 23:20:10 +05:30
|
|
|
case gitPath:
|
2021-07-08 04:53:09 +05:30
|
|
|
foundEscaped = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not return whether the escaped file exists, and the escaped filename to keep backwards compatibility.
|
2023-04-19 23:20:10 +05:30
|
|
|
return foundEscaped, gitPath, nil
|
2021-07-08 04:53:09 +05:30
|
|
|
}
|
|
|
|
|
2022-09-05 01:24:23 +05:30
|
|
|
// updateWikiPage adds a new page or edits an existing page in repository wiki.
|
2023-04-19 23:20:10 +05:30
|
|
|
func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName WebPath, content, message string, isNew bool) (err error) {
|
2023-09-22 05:13:29 +05:30
|
|
|
err = repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
if err = validateWebPath(newWikiName); err != nil {
|
2020-01-07 23:57:36 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-12-25 15:29:32 +05:30
|
|
|
wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
|
|
|
defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2020-01-07 23:57:36 +05:30
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err = InitWiki(ctx, repo); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("InitWiki: %w", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.GetWikiBranchName())
|
2020-01-07 23:57:36 +05:30
|
|
|
|
2022-05-08 22:16:32 +05:30
|
|
|
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
2020-01-07 23:57:36 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2022-05-08 22:16:32 +05:30
|
|
|
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
|
2020-01-07 23:57:36 +05:30
|
|
|
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cloneOpts := git.CloneRepoOptions{
|
|
|
|
Bare: true,
|
|
|
|
Shared: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMasterBranch {
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
cloneOpts.Branch = repo.GetWikiBranchName()
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil {
|
2020-01-07 23:57:36 +05:30
|
|
|
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err := git.OpenRepository(ctx, basePath)
|
2020-01-07 23:57:36 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return fmt.Errorf("failed to open new temporary repository in: %s %w", basePath, err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
if hasMasterBranch {
|
|
|
|
if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil {
|
|
|
|
log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return fmt.Errorf("fnable to read HEAD tree to index in: %s %w", basePath, err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.GetWikiBranchName(), newWikiName)
|
2021-07-08 04:53:09 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:57:36 +05:30
|
|
|
if isNew {
|
2021-07-08 04:53:09 +05:30
|
|
|
if isWikiExist {
|
2022-08-25 08:01:57 +05:30
|
|
|
return repo_model.ErrWikiAlreadyExist{
|
2020-01-07 23:57:36 +05:30
|
|
|
Title: newWikiPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-08 04:53:09 +05:30
|
|
|
// avoid check existence again if wiki name is not changed since gitRepo.LsFiles(...) is not free.
|
|
|
|
isOldWikiExist := true
|
|
|
|
oldWikiPath := newWikiPath
|
|
|
|
if oldWikiName != newWikiName {
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.GetWikiBranchName(), oldWikiName)
|
2021-07-08 04:53:09 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2021-07-08 04:53:09 +05:30
|
|
|
if isOldWikiExist {
|
2020-01-07 23:57:36 +05:30
|
|
|
err := gitRepo.RemoveFilesFromIndex(oldWikiPath)
|
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("RemoveFilesFromIndex failed: %v", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here
|
|
|
|
|
|
|
|
objectHash, err := gitRepo.HashObject(strings.NewReader(content))
|
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("HashObject failed: %v", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gitRepo.AddObjectToIndex("100644", objectHash, newWikiPath); err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("AddObjectToIndex failed: %v", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := gitRepo.WriteTree()
|
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("WriteTree failed: %v", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
commitTreeOpts := git.CommitTreeOpts{
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:14:55 +05:30
|
|
|
committer := doer.NewGitSig()
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo, doer)
|
2020-01-07 23:57:36 +05:30
|
|
|
if sign {
|
|
|
|
commitTreeOpts.KeyID = signingKey
|
2021-12-10 06:57:50 +05:30
|
|
|
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
|
2020-09-19 22:14:55 +05:30
|
|
|
committer = signer
|
|
|
|
}
|
2020-01-07 23:57:36 +05:30
|
|
|
} else {
|
|
|
|
commitTreeOpts.NoGPGSign = true
|
|
|
|
}
|
|
|
|
if hasMasterBranch {
|
|
|
|
commitTreeOpts.Parents = []string{"HEAD"}
|
|
|
|
}
|
2020-09-19 22:14:55 +05:30
|
|
|
|
|
|
|
commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts)
|
2020-01-07 23:57:36 +05:30
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("CommitTree failed: %v", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 01:36:32 +05:30
|
|
|
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
|
2022-10-15 20:10:32 +05:30
|
|
|
Remote: DefaultRemote,
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.GetWikiBranchName()),
|
2022-05-08 22:16:32 +05:30
|
|
|
Env: repo_module.FullPushingEnvironment(
|
2020-01-07 23:57:36 +05:30
|
|
|
doer,
|
|
|
|
doer,
|
|
|
|
repo,
|
|
|
|
repo.Name+".wiki",
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
}); err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("Push failed: %v", err)
|
2020-03-28 09:43:18 +05:30
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-07 16:47:06 +05:30
|
|
|
return fmt.Errorf("failed to push: %w", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWikiPage adds a new wiki page with a given wikiPath.
|
2023-04-19 23:20:10 +05:30
|
|
|
func AddWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName WebPath, content, message string) error {
|
2022-01-20 04:56:57 +05:30
|
|
|
return updateWikiPage(ctx, doer, repo, "", wikiName, content, message, true)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// EditWikiPage updates a wiki page identified by its wikiPath,
|
|
|
|
// optionally also changing wikiPath.
|
2023-04-19 23:20:10 +05:30
|
|
|
func EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName WebPath, content, message string) error {
|
2022-01-20 04:56:57 +05:30
|
|
|
return updateWikiPage(ctx, doer, repo, oldWikiName, newWikiName, content, message, false)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWikiPage deletes a wiki page identified by its path.
|
2023-04-19 23:20:10 +05:30
|
|
|
func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName WebPath) (err error) {
|
2023-09-22 05:13:29 +05:30
|
|
|
err = repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-25 15:29:32 +05:30
|
|
|
wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
|
|
|
defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2020-01-07 23:57:36 +05:30
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err = InitWiki(ctx, repo); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("InitWiki: %w", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 22:16:32 +05:30
|
|
|
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
2020-01-07 23:57:36 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2022-05-08 22:16:32 +05:30
|
|
|
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
|
2020-01-07 23:57:36 +05:30
|
|
|
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
|
2020-01-07 23:57:36 +05:30
|
|
|
Bare: true,
|
|
|
|
Shared: true,
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
Branch: repo.GetWikiBranchName(),
|
2020-01-07 23:57:36 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err := git.OpenRepository(ctx, basePath)
|
2020-01-07 23:57:36 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return fmt.Errorf("failed to open new temporary repository in: %s %w", basePath, err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil {
|
|
|
|
log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
found, wikiPath, err := prepareGitPath(gitRepo, repo.GetWikiBranchName(), wikiName)
|
2021-07-19 21:44:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
if found {
|
|
|
|
err := gitRepo.RemoveFilesFromIndex(wikiPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here
|
|
|
|
|
|
|
|
tree, err := gitRepo.WriteTree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-19 23:20:10 +05:30
|
|
|
message := fmt.Sprintf("Delete page %q", wikiName)
|
2020-01-07 23:57:36 +05:30
|
|
|
commitTreeOpts := git.CommitTreeOpts{
|
|
|
|
Message: message,
|
|
|
|
Parents: []string{"HEAD"},
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:14:55 +05:30
|
|
|
committer := doer.NewGitSig()
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo, doer)
|
2020-01-07 23:57:36 +05:30
|
|
|
if sign {
|
|
|
|
commitTreeOpts.KeyID = signingKey
|
2021-12-10 06:57:50 +05:30
|
|
|
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
|
2020-09-19 22:14:55 +05:30
|
|
|
committer = signer
|
|
|
|
}
|
2020-01-07 23:57:36 +05:30
|
|
|
} else {
|
|
|
|
commitTreeOpts.NoGPGSign = true
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:14:55 +05:30
|
|
|
commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts)
|
2020-01-07 23:57:36 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 01:36:32 +05:30
|
|
|
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
|
2022-10-15 20:10:32 +05:30
|
|
|
Remote: DefaultRemote,
|
[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)
2024-01-30 16:48:53 +05:30
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.GetWikiBranchName()),
|
2023-04-24 02:52:16 +05:30
|
|
|
Env: repo_module.FullPushingEnvironment(
|
|
|
|
doer,
|
|
|
|
doer,
|
|
|
|
repo,
|
|
|
|
repo.Name+".wiki",
|
|
|
|
0,
|
|
|
|
),
|
2020-01-07 23:57:36 +05:30
|
|
|
}); err != nil {
|
2020-03-28 09:43:18 +05:30
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Push: %w", err)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-08 20:49:30 +05:30
|
|
|
|
|
|
|
// DeleteWiki removes the actual and local copy of repository wiki.
|
2022-01-20 04:56:57 +05:30
|
|
|
func DeleteWiki(ctx context.Context, repo *repo_model.Repository) error {
|
2024-01-13 03:20:38 +05:30
|
|
|
if err := repo_service.UpdateRepositoryUnits(ctx, repo, nil, []unit.Type{unit.TypeWiki}); err != nil {
|
2021-09-08 20:49:30 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-17 04:59:26 +05:30
|
|
|
system_model.RemoveAllWithNotice(ctx, "Delete repository wiki", repo.WikiPath())
|
2021-09-08 20:49:30 +05:30
|
|
|
return nil
|
|
|
|
}
|
2024-05-20 17:53:27 +05:30
|
|
|
|
|
|
|
func SearchWikiContents(ctx context.Context, repo *repo_model.Repository, keyword string) ([]*git.GrepResult, error) {
|
|
|
|
gitRepo, err := git.OpenRepository(ctx, repo.WikiPath())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
return git.GrepSearch(ctx, gitRepo, keyword, git.GrepOptions{
|
|
|
|
ContextLineNumber: 0,
|
|
|
|
IsFuzzy: true,
|
|
|
|
RefName: repo.GetWikiBranchName(),
|
|
|
|
MaxResultLimit: 10,
|
|
|
|
MatchesPerFile: 3,
|
|
|
|
})
|
|
|
|
}
|