History

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. ---------

Lunny Xiao committed 2 years ago

Commit 5f82ead1 parent 60e4a9884 files · +426 −273Browse files

Showing the changes to services/pull/pull_test.go (1 of 84 files). Show all

Unified · Split

services/pull/pull_test.go+3−2
@@ -13,6 +13,7 @@ import ( · 12 unmodified lines
1313 "code.gitea.io/gitea/models/unit"
1414 "code.gitea.io/gitea/models/unittest"
1515 "code.gitea.io/gitea/modules/git"
16+ "code.gitea.io/gitea/modules/gitrepo"
1617
1718 "github.com/stretchr/testify/assert"
1819 )
@@ -41,7 +42,7 @@ func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { · 22 unmodified lines
4142 pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
4243
4344 assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
44− gitRepo, err := git.OpenRepository(git.DefaultContext, pr.BaseRepo.RepoPath())
45+ gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, pr.BaseRepo)
4546 assert.NoError(t, err)
4647 defer gitRepo.Close()
4748
@@ -71,7 +72,7 @@ func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) { · 23 unmodified lines
7172 pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2, BaseRepo: baseRepo})
7273
7374 assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
74− gitRepo, err := git.OpenRepository(git.DefaultContext, pr.BaseRepo.RepoPath())
75+ gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, pr.BaseRepo)
7576 assert.NoError(t, err)
7677 defer gitRepo.Close()
7778