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/context (3 of 84 files). Show all

Unified · Split

3 files
  1. modules/context/api.go +4 −5
  2. modules/context/context.go +2 −2
  3. modules/context/repo.go +5 −5
modules/context/api.go+4−5
@@ -11,11 +11,11 @@ import ( · 10 unmodified lines
1111 "net/url"
1212 "strings"
1313
14− repo_model "code.gitea.io/gitea/models/repo"
1514 "code.gitea.io/gitea/models/unit"
1615 user_model "code.gitea.io/gitea/models/user"
1716 mc "code.gitea.io/gitea/modules/cache"
1817 "code.gitea.io/gitea/modules/git"
18+ "code.gitea.io/gitea/modules/gitrepo"
1919 "code.gitea.io/gitea/modules/httpcache"
2020 "code.gitea.io/gitea/modules/log"
2121 "code.gitea.io/gitea/modules/setting"
@@ -224,7 +224,7 @@ func APIContexter() func(http.Handler) http.Handler { · 202 unmodified lines
224224 defer baseCleanUp()
225225
226226 ctx.Base.AppendContextValue(apiContextKey, ctx)
227− ctx.Base.AppendContextValueFunc(git.RepositoryContextKey, func() any { return ctx.Repo.GitRepo })
227+ ctx.Base.AppendContextValueFunc(gitrepo.RepositoryContextKey, func() any { return ctx.Repo.GitRepo })
228228
229229 // If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
230230 if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
@@ -278,10 +278,9 @@ func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) (cancel context · 47 unmodified lines
278278
279279 // For API calls.
280280 if ctx.Repo.GitRepo == nil {
281− repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
282− gitRepo, err := git.OpenRepository(ctx, repoPath)
281+ gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
283282 if err != nil {
284− ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
283+ ctx.Error(http.StatusInternalServerError, fmt.Sprintf("Open Repository %v failed", ctx.Repo.Repository.FullName()), err)
285284 return cancel
286285 }
287286 ctx.Repo.GitRepo = gitRepo
modules/context/context.go+2−2
@@ -17,7 +17,7 @@ import ( · 16 unmodified lines
1717 "code.gitea.io/gitea/models/unit"
1818 user_model "code.gitea.io/gitea/models/user"
1919 mc "code.gitea.io/gitea/modules/cache"
20− "code.gitea.io/gitea/modules/git"
20+ "code.gitea.io/gitea/modules/gitrepo"
2121 "code.gitea.io/gitea/modules/httpcache"
2222 "code.gitea.io/gitea/modules/setting"
2323 "code.gitea.io/gitea/modules/templates"
@@ -163,7 +163,7 @@ func Contexter() func(next http.Handler) http.Handler { · 139 unmodified lines
163163 ctx.Data["PageData"] = ctx.PageData
164164
165165 ctx.Base.AppendContextValue(WebContextKey, ctx)
166− ctx.Base.AppendContextValueFunc(git.RepositoryContextKey, func() any { return ctx.Repo.GitRepo })
166+ ctx.Base.AppendContextValueFunc(gitrepo.RepositoryContextKey, func() any { return ctx.Repo.GitRepo })
167167
168168 ctx.Csrf = PrepareCSRFProtector(csrfOpts, ctx)
169169
modules/context/repo.go+5−5
@@ -23,6 +23,7 @@ import ( · 22 unmodified lines
2323 user_model "code.gitea.io/gitea/models/user"
2424 "code.gitea.io/gitea/modules/cache"
2525 "code.gitea.io/gitea/modules/git"
26+ "code.gitea.io/gitea/modules/gitrepo"
2627 code_indexer "code.gitea.io/gitea/modules/indexer/code"
2728 "code.gitea.io/gitea/modules/log"
2829 repo_module "code.gitea.io/gitea/modules/repository"
@@ -633,7 +634,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { · 604 unmodified lines
633634 return nil
634635 }
635636
636− gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(userName, repoName))
637+ gitRepo, err := gitrepo.OpenRepository(ctx, repo)
637638 if err != nil {
638639 if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") {
639640 log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err)
@@ -645,7 +646,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { · 5 unmodified lines
645646 }
646647 return nil
647648 }
648− ctx.ServerError("RepoAssignment Invalid repo "+repo_model.RepoPath(userName, repoName), err)
649+ ctx.ServerError("RepoAssignment Invalid repo "+repo.FullName(), err)
649650 return nil
650651 }
651652 if ctx.Repo.GitRepo != nil {
@@ -920,10 +921,9 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context · 268 unmodified lines
920921 )
921922
922923 if ctx.Repo.GitRepo == nil {
923− repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
924− ctx.Repo.GitRepo, err = git.OpenRepository(ctx, repoPath)
924+ ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
925925 if err != nil {
926− ctx.ServerError("RepoRef Invalid repo "+repoPath, err)
926+ ctx.ServerError(fmt.Sprintf("Open Repository %v failed", ctx.Repo.Repository.FullName()), err)
927927 return nil
928928 }
929929 // We opened it, we should close it