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 modules/contexttest/context_tests.go (1 of 84 files). Show all

Unified · Split

modules/contexttest/context_tests.go+3−3
@@ -18,7 +18,7 @@ import ( · 17 unmodified lines
18 "code.gitea.io/gitea/models/unittest"18 "code.gitea.io/gitea/models/unittest"
19 user_model "code.gitea.io/gitea/models/user"19 user_model "code.gitea.io/gitea/models/user"
20 "code.gitea.io/gitea/modules/context"20 "code.gitea.io/gitea/modules/context"
21 "code.gitea.io/gitea/modules/git"21 "code.gitea.io/gitea/modules/gitrepo"
22 "code.gitea.io/gitea/modules/templates"22 "code.gitea.io/gitea/modules/templates"
23 "code.gitea.io/gitea/modules/translation"23 "code.gitea.io/gitea/modules/translation"
24 "code.gitea.io/gitea/modules/web/middleware"24 "code.gitea.io/gitea/modules/web/middleware"
@@ -107,7 +107,7 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) { · 82 unmodified lines
107 assert.FailNow(t, "context is not *context.Context or *context.APIContext")107 assert.FailNow(t, "context is not *context.Context or *context.APIContext")
108 }108 }
109 109
110 gitRepo, err := git.OpenRepository(ctx, repo.Repository.RepoPath())110 gitRepo, err := gitrepo.OpenRepository(ctx, repo.Repository)
111 assert.NoError(t, err)111 assert.NoError(t, err)
112 defer gitRepo.Close()112 defer gitRepo.Close()
113 branch, err := gitRepo.GetHEADBranch()113 branch, err := gitRepo.GetHEADBranch()
@@ -137,7 +137,7 @@ func LoadUser(t *testing.T, ctx gocontext.Context, userID int64) { · 23 unmodified lines
137func LoadGitRepo(t *testing.T, ctx *context.Context) {137func LoadGitRepo(t *testing.T, ctx *context.Context) {
138 assert.NoError(t, ctx.Repo.Repository.LoadOwner(ctx))138 assert.NoError(t, ctx.Repo.Repository.LoadOwner(ctx))
139 var err error139 var err error
140 ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())140 ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
141 assert.NoError(t, err)141 assert.NoError(t, err)
142}142}
143 143