2020-01-07 23:57:36 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. 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 (
|
2023-04-19 23:20:10 +05:30
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2020-01-07 23:57:36 +05:30
|
|
|
"testing"
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
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"
|
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
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2021-08-01 22:34:32 +05:30
|
|
|
|
2023-09-08 10:21:15 +05:30
|
|
|
_ "code.gitea.io/gitea/models/actions"
|
|
|
|
|
2020-01-07 23:57:36 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2023-09-28 07:08:53 +05:30
|
|
|
unittest.MainTest(m)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
func TestWebPathSegments(t *testing.T) {
|
|
|
|
a := WebPathSegments("a%2Fa/b+c/d-e/f-g.-")
|
|
|
|
assert.EqualValues(t, []string{"a/a", "b c", "d e", "f-g"}, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserTitleToWebPath(t *testing.T) {
|
2020-01-07 23:57:36 +05:30
|
|
|
type test struct {
|
2023-04-19 23:20:10 +05:30
|
|
|
Expected string
|
|
|
|
UserTitle string
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
for _, test := range []test{
|
2023-05-06 16:54:18 +05:30
|
|
|
{"unnamed", ""},
|
|
|
|
{"unnamed", "."},
|
|
|
|
{"unnamed", ".."},
|
2020-01-07 23:57:36 +05:30
|
|
|
{"wiki-name", "wiki name"},
|
2023-04-23 16:46:30 +05:30
|
|
|
{"title.md.-", "title.md"},
|
2023-04-19 23:20:10 +05:30
|
|
|
{"wiki-name.-", "wiki-name"},
|
|
|
|
{"the+wiki-name.-", "the wiki-name"},
|
|
|
|
{"a%2Fb", "a/b"},
|
|
|
|
{"a%25b", "a%b"},
|
2020-01-07 23:57:36 +05:30
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, test.Expected, UserTitleToWebPath("", test.UserTitle))
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
func TestWebPathToDisplayName(t *testing.T) {
|
2020-01-07 23:57:36 +05:30
|
|
|
type test struct {
|
|
|
|
Expected string
|
2023-04-19 23:20:10 +05:30
|
|
|
WebPath WebPath
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
for _, test := range []test{
|
|
|
|
{"wiki name", "wiki-name"},
|
2023-04-19 23:20:10 +05:30
|
|
|
{"wiki-name", "wiki-name.-"},
|
|
|
|
{"name with / slash", "name-with %2F slash"},
|
|
|
|
{"name with % percent", "name-with %25 percent"},
|
|
|
|
{"2000-01-02 meeting", "2000-01-02+meeting.-.md"},
|
2023-08-09 12:27:45 +05:30
|
|
|
{"a b", "a%20b.md"},
|
2020-01-07 23:57:36 +05:30
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
_, displayName := WebPathToUserTitle(test.WebPath)
|
|
|
|
assert.EqualValues(t, test.Expected, displayName)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
func TestWebPathToGitPath(t *testing.T) {
|
2020-01-07 23:57:36 +05:30
|
|
|
type test struct {
|
|
|
|
Expected string
|
2023-04-19 23:20:10 +05:30
|
|
|
WikiName WebPath
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
for _, test := range []test{
|
2023-04-19 23:20:10 +05:30
|
|
|
{"wiki-name.md", "wiki%20name"},
|
|
|
|
{"wiki-name.md", "wiki+name"},
|
2023-08-09 12:27:45 +05:30
|
|
|
{"wiki name.md", "wiki%20name.md"},
|
|
|
|
{"wiki%20name.md", "wiki%2520name.md"},
|
2023-04-19 23:20:10 +05:30
|
|
|
{"2000-01-02-meeting.md", "2000-01-02+meeting"},
|
|
|
|
{"2000-01-02 meeting.-.md", "2000-01-02%20meeting.-"},
|
2020-01-07 23:57:36 +05:30
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, test.Expected, WebPathToGitPath(test.WikiName))
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
func TestGitPathToWebPath(t *testing.T) {
|
2020-01-07 23:57:36 +05:30
|
|
|
type test struct {
|
|
|
|
Expected string
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
for _, test := range []test{
|
2023-04-19 23:20:10 +05:30
|
|
|
{"hello-world", "hello-world.md"}, // this shouldn't happen, because it should always have a ".-" suffix
|
|
|
|
{"hello-world", "hello world.md"},
|
|
|
|
{"hello-world.-", "hello-world.-.md"},
|
|
|
|
{"hello+world.-", "hello world.-.md"},
|
|
|
|
{"symbols-%2F", "symbols %2F.md"},
|
2020-01-07 23:57:36 +05:30
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
name, err := GitPathToWebPath(test.Filename)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, test.Expected, name)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
for _, badFilename := range []string{
|
|
|
|
"nofileextension",
|
|
|
|
"wrongfileextension.txt",
|
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
_, err := GitPathToWebPath(badFilename)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.Error(t, err)
|
2022-08-25 08:01:57 +05:30
|
|
|
assert.True(t, repo_model.IsErrWikiInvalidFileName(err))
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
2023-04-19 23:20:10 +05:30
|
|
|
_, err := GitPathToWebPath("badescaping%%.md")
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.Error(t, err)
|
2022-08-25 08:01:57 +05:30
|
|
|
assert.False(t, repo_model.IsErrWikiInvalidFileName(err))
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
|
2023-04-19 23:20:10 +05:30
|
|
|
func TestUserWebGitPathConsistency(t *testing.T) {
|
|
|
|
maxLen := 20
|
|
|
|
b := make([]byte, maxLen)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
l := rand.Intn(maxLen)
|
|
|
|
for j := 0; j < l; j++ {
|
|
|
|
r := rand.Intn(0x80-0x20) + 0x20
|
|
|
|
b[j] = byte(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
userTitle := strings.TrimSpace(string(b[:l]))
|
2023-05-06 16:54:18 +05:30
|
|
|
if userTitle == "" || userTitle == "." || userTitle == ".." {
|
2023-04-19 23:20:10 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
webPath := UserTitleToWebPath("", userTitle)
|
|
|
|
gitPath := WebPathToGitPath(webPath)
|
|
|
|
|
|
|
|
webPath1, _ := GitPathToWebPath(gitPath)
|
|
|
|
_, userTitle1 := WebPathToUserTitle(webPath1)
|
|
|
|
gitPath1 := WebPathToGitPath(webPath1)
|
|
|
|
|
|
|
|
assert.EqualValues(t, userTitle, userTitle1, "UserTitle for userTitle: %q", userTitle)
|
|
|
|
assert.EqualValues(t, webPath, webPath1, "WebPath for userTitle: %q", userTitle)
|
|
|
|
assert.EqualValues(t, gitPath, gitPath1, "GitPath for userTitle: %q", userTitle)
|
2020-01-07 23:57:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_InitWiki(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
unittest.PrepareTestEnv(t)
|
2020-01-07 23:57:36 +05:30
|
|
|
// repo1 already has a wiki
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-01-20 04:56:57 +05:30
|
|
|
assert.NoError(t, InitWiki(git.DefaultContext, repo1))
|
2020-01-07 23:57:36 +05:30
|
|
|
|
|
|
|
// repo2 does not already have a wiki
|
2022-08-16 07:52:25 +05:30
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2022-01-20 04:56:57 +05:30
|
|
|
assert.NoError(t, InitWiki(git.DefaultContext, repo2))
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.True(t, repo2.HasWiki())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_AddWikiPage(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-01-07 23:57:36 +05:30
|
|
|
const wikiContent = "This is the wiki content"
|
|
|
|
const commitMsg = "Commit message"
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-04-19 23:20:10 +05:30
|
|
|
for _, userTitle := range []string{
|
2020-01-07 23:57:36 +05:30
|
|
|
"Another page",
|
|
|
|
"Here's a <tag> and a/slash",
|
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
t.Run("test wiki exist: "+userTitle, func(t *testing.T) {
|
|
|
|
webPath := UserTitleToWebPath("", userTitle)
|
|
|
|
assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, webPath, wikiContent, commitMsg))
|
2020-01-07 23:57:36 +05:30
|
|
|
// Now need to show that the page has been added:
|
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
|
|
|
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
|
2023-09-20 06:21:36 +05:30
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
2020-01-07 23:57:36 +05:30
|
|
|
defer gitRepo.Close()
|
[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
|
|
|
masterTree, err := gitRepo.GetTree("master")
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
gitPath := WebPathToGitPath(webPath)
|
|
|
|
entry, err := masterTree.GetTreeEntryByPath(gitPath)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, gitPath, entry.Name(), "%s not added correctly", userTitle)
|
2020-01-07 23:57:36 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("check wiki already exist", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// test for already-existing wiki name
|
2022-01-20 04:56:57 +05:30
|
|
|
err := AddWikiPage(git.DefaultContext, doer, repo, "Home", wikiContent, commitMsg)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.Error(t, err)
|
2022-08-25 08:01:57 +05:30
|
|
|
assert.True(t, repo_model.IsErrWikiAlreadyExist(err))
|
2020-01-07 23:57:36 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check wiki reserved name", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// test for reserved wiki name
|
2022-01-20 04:56:57 +05:30
|
|
|
err := AddWikiPage(git.DefaultContext, doer, repo, "_edit", wikiContent, commitMsg)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.Error(t, err)
|
2022-08-25 08:01:57 +05:30
|
|
|
assert.True(t, repo_model.IsErrWikiReservedName(err))
|
2020-01-07 23:57:36 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_EditWikiPage(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-05-12 09:43:42 +05:30
|
|
|
|
2020-01-07 23:57:36 +05:30
|
|
|
const newWikiContent = "This is the new content"
|
|
|
|
const commitMsg = "Commit message"
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2020-01-07 23:57:36 +05:30
|
|
|
for _, newWikiName := range []string{
|
|
|
|
"Home", // same name as before
|
|
|
|
"New home",
|
|
|
|
"New/name/with/slashes",
|
|
|
|
} {
|
2023-04-19 23:20:10 +05:30
|
|
|
webPath := UserTitleToWebPath("", newWikiName)
|
2021-11-12 20:06:47 +05:30
|
|
|
unittest.PrepareTestEnv(t)
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", webPath, newWikiContent, commitMsg))
|
2020-01-07 23:57:36 +05:30
|
|
|
|
|
|
|
// Now need to show that the page has been added:
|
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
|
|
|
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, 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
|
|
|
masterTree, err := gitRepo.GetTree("master")
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
gitPath := WebPathToGitPath(webPath)
|
|
|
|
entry, err := masterTree.GetTreeEntryByPath(gitPath)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, gitPath, entry.Name(), "%s not edited correctly", newWikiName)
|
2020-01-07 23:57:36 +05:30
|
|
|
|
|
|
|
if newWikiName != "Home" {
|
|
|
|
_, err := masterTree.GetTreeEntryByPath("Home.md")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
gitRepo.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_DeleteWikiPage(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
unittest.PrepareTestEnv(t)
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2022-01-20 04:56:57 +05:30
|
|
|
assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home"))
|
2020-01-07 23:57:36 +05:30
|
|
|
|
|
|
|
// Now need to show that the page has been added:
|
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
|
|
|
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
|
2023-09-20 06:21:36 +05:30
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
2020-01-07 23:57:36 +05:30
|
|
|
defer gitRepo.Close()
|
[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
|
|
|
masterTree, err := gitRepo.GetTree("master")
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
gitPath := WebPathToGitPath("Home")
|
|
|
|
_, err = masterTree.GetTreeEntryByPath(gitPath)
|
2020-01-07 23:57:36 +05:30
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2021-07-20 18:46:20 +05:30
|
|
|
|
|
|
|
func TestPrepareWikiFileName(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
unittest.PrepareTestEnv(t)
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
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
|
|
|
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
|
2023-09-20 06:21:36 +05:30
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-19 20:15:44 +05:30
|
|
|
defer gitRepo.Close()
|
2021-07-20 18:46:20 +05:30
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
arg string
|
|
|
|
existence bool
|
|
|
|
wikiPath string
|
|
|
|
wantErr bool
|
|
|
|
}{{
|
|
|
|
name: "add suffix",
|
|
|
|
arg: "Home",
|
|
|
|
existence: true,
|
|
|
|
wikiPath: "Home.md",
|
|
|
|
wantErr: false,
|
|
|
|
}, {
|
|
|
|
name: "test special chars",
|
|
|
|
arg: "home of and & or wiki page!",
|
|
|
|
existence: false,
|
|
|
|
wikiPath: "home-of-and-%26-or-wiki-page%21.md",
|
|
|
|
wantErr: false,
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-04-19 23:20:10 +05:30
|
|
|
webPath := UserTitleToWebPath("", tt.arg)
|
[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
|
|
|
existence, newWikiPath, err := prepareGitPath(gitRepo, "master", webPath)
|
2021-07-20 18:46:20 +05:30
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if existence != tt.existence {
|
|
|
|
if existence {
|
|
|
|
t.Errorf("expect to find no escaped file but we detect one")
|
|
|
|
} else {
|
|
|
|
t.Errorf("expect to find an escaped file but we could not detect one")
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, tt.wikiPath, newWikiPath)
|
2021-07-20 18:46:20 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 22:34:32 +05:30
|
|
|
|
|
|
|
func TestPrepareWikiFileName_FirstPage(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
unittest.PrepareTestEnv(t)
|
2021-08-01 22:34:32 +05:30
|
|
|
|
|
|
|
// Now create a temporaryDirectory
|
2022-09-04 20:44:53 +05:30
|
|
|
tmpDir := t.TempDir()
|
2021-08-01 22:34:32 +05:30
|
|
|
|
2023-12-17 17:26:08 +05:30
|
|
|
err := git.InitRepository(git.DefaultContext, tmpDir, true, git.Sha1ObjectFormat.Name())
|
2021-08-01 22:34:32 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err := git.OpenRepository(git.DefaultContext, tmpDir)
|
2023-09-20 06:21:36 +05:30
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-19 20:15:44 +05:30
|
|
|
defer gitRepo.Close()
|
2021-08-01 22:34:32 +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
|
|
|
existence, newWikiPath, err := prepareGitPath(gitRepo, "master", "Home")
|
2021-08-01 22:34:32 +05:30
|
|
|
assert.False(t, existence)
|
|
|
|
assert.NoError(t, err)
|
2023-04-19 23:20:10 +05:30
|
|
|
assert.EqualValues(t, "Home.md", newWikiPath)
|
2021-08-01 22:34:32 +05:30
|
|
|
}
|
2023-08-03 07:07:48 +05:30
|
|
|
|
|
|
|
func TestWebPathConversion(t *testing.T) {
|
|
|
|
assert.Equal(t, "path/wiki", WebPathToURLPath(WebPath("path/wiki")))
|
|
|
|
assert.Equal(t, "wiki", WebPathToURLPath(WebPath("wiki")))
|
|
|
|
assert.Equal(t, "", WebPathToURLPath(WebPath("")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebPathFromRequest(t *testing.T) {
|
|
|
|
assert.Equal(t, WebPath("a%2Fb"), WebPathFromRequest("a/b"))
|
|
|
|
assert.Equal(t, WebPath("a"), WebPathFromRequest("a"))
|
|
|
|
assert.Equal(t, WebPath("b"), WebPathFromRequest("a/../b"))
|
|
|
|
}
|