mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-29 12:04:17 +00:00
5f82ead13c
## 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>
105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
|
"code.gitea.io/gitea/modules/migration"
|
|
"code.gitea.io/gitea/modules/repository"
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
|
release_service "code.gitea.io/gitea/services/release"
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
|
"code.gitea.io/gitea/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMirrorPull(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
repoPath := repo_model.RepoPath(user.Name, repo.Name)
|
|
|
|
opts := migration.MigrateOptions{
|
|
RepoName: "test_mirror",
|
|
Description: "Test mirror",
|
|
Private: false,
|
|
Mirror: true,
|
|
CloneAddr: repoPath,
|
|
Wiki: true,
|
|
Releases: false,
|
|
}
|
|
|
|
mirrorRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
|
|
Name: opts.RepoName,
|
|
Description: opts.Description,
|
|
IsPrivate: opts.Private,
|
|
IsMirror: opts.Mirror,
|
|
Status: repo_model.RepositoryBeingMigrated,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.True(t, mirrorRepo.IsMirror, "expected pull-mirror repo to be marked as a mirror immediately after its creation")
|
|
|
|
ctx := context.Background()
|
|
|
|
mirror, err := repository.MigrateRepositoryGitData(ctx, user, mirrorRepo, opts, nil)
|
|
assert.NoError(t, err)
|
|
|
|
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo)
|
|
assert.NoError(t, err)
|
|
defer gitRepo.Close()
|
|
|
|
findOptions := repo_model.FindReleasesOptions{
|
|
IncludeDrafts: true,
|
|
IncludeTags: true,
|
|
RepoID: mirror.ID,
|
|
}
|
|
initCount, err := db.Count[repo_model.Release](db.DefaultContext, findOptions)
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, release_service.CreateRelease(gitRepo, &repo_model.Release{
|
|
RepoID: repo.ID,
|
|
Repo: repo,
|
|
PublisherID: user.ID,
|
|
Publisher: user,
|
|
TagName: "v0.2",
|
|
Target: "master",
|
|
Title: "v0.2 is released",
|
|
Note: "v0.2 is released",
|
|
IsDraft: false,
|
|
IsPrerelease: false,
|
|
IsTag: true,
|
|
}, nil, ""))
|
|
|
|
_, err = repo_model.GetMirrorByRepoID(ctx, mirror.ID)
|
|
assert.NoError(t, err)
|
|
|
|
ok := mirror_service.SyncPullMirror(ctx, mirror.ID)
|
|
assert.True(t, ok)
|
|
|
|
count, err := db.Count[repo_model.Release](db.DefaultContext, findOptions)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, initCount+1, count)
|
|
|
|
release, err := repo_model.GetRelease(db.DefaultContext, repo.ID, "v0.2")
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, release_service.DeleteReleaseByID(ctx, repo, release, user, true))
|
|
|
|
ok = mirror_service.SyncPullMirror(ctx, mirror.ID)
|
|
assert.True(t, ok)
|
|
|
|
count, err = db.Count[repo_model.Release](db.DefaultContext, findOptions)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, initCount, count)
|
|
}
|