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

Unified · Split

3 files
  1. modules/repository/branch.go +3 −2
  2. modules/repository/generate.go +2 −1
  3. modules/repository/repo.go +2 −1
modules/repository/branch.go+3−2
@@ -11,6 +11,7 @@ import ( · 10 unmodified lines
1111 repo_model "code.gitea.io/gitea/models/repo"
1212 "code.gitea.io/gitea/modules/container"
1313 "code.gitea.io/gitea/modules/git"
14+ "code.gitea.io/gitea/modules/gitrepo"
1415 "code.gitea.io/gitea/modules/log"
1516 "code.gitea.io/gitea/modules/timeutil"
1617 )
@@ -24,9 +25,9 @@ func SyncRepoBranches(ctx context.Context, repoID, doerID int64) (int64, error) · 7 unmodified lines
2425
2526 log.Debug("SyncRepoBranches: in Repo[%d:%s]", repo.ID, repo.FullName())
2627
27− gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
28+ gitRepo, err := gitrepo.OpenRepository(ctx, repo)
2829 if err != nil {
29− log.Error("OpenRepository[%s]: %w", repo.RepoPath(), err)
30+ log.Error("OpenRepository[%s]: %w", repo.FullName(), err)
3031 return 0, err
3132 }
3233 defer gitRepo.Close()
modules/repository/generate.go+2−1
@@ -19,6 +19,7 @@ import ( · 18 unmodified lines
1919 repo_model "code.gitea.io/gitea/models/repo"
2020 user_model "code.gitea.io/gitea/models/user"
2121 "code.gitea.io/gitea/modules/git"
22+ "code.gitea.io/gitea/modules/gitrepo"
2223 "code.gitea.io/gitea/modules/log"
2324 "code.gitea.io/gitea/modules/util"
2425
@@ -271,7 +272,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r · 246 unmodified lines
271272 repo.DefaultBranch = templateRepo.DefaultBranch
272273 }
273274
274− gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
275+ gitRepo, err := gitrepo.OpenRepository(ctx, repo)
275276 if err != nil {
276277 return fmt.Errorf("openRepository: %w", err)
277278 }
modules/repository/repo.go+2−1
@@ -19,6 +19,7 @@ import ( · 18 unmodified lines
1919 user_model "code.gitea.io/gitea/models/user"
2020 "code.gitea.io/gitea/modules/container"
2121 "code.gitea.io/gitea/modules/git"
22+ "code.gitea.io/gitea/modules/gitrepo"
2223 "code.gitea.io/gitea/modules/lfs"
2324 "code.gitea.io/gitea/modules/log"
2425 "code.gitea.io/gitea/modules/migration"
@@ -291,7 +292,7 @@ func SyncRepoTags(ctx context.Context, repoID int64) error { · 266 unmodified lines
291292 return err
292293 }
293294
294− gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
295+ gitRepo, err := gitrepo.OpenRepository(ctx, repo)
295296 if err != nil {
296297 return err
297298 }