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

Unified · Split

services/pull/pull.go+9−8
@@ -23,6 +23,7 @@ import ( · 22 unmodified lines
2323 "code.gitea.io/gitea/modules/container"
2424 gitea_context "code.gitea.io/gitea/modules/context"
2525 "code.gitea.io/gitea/modules/git"
26+ "code.gitea.io/gitea/modules/gitrepo"
2627 "code.gitea.io/gitea/modules/graceful"
2728 "code.gitea.io/gitea/modules/json"
2829 "code.gitea.io/gitea/modules/log"
@@ -62,7 +63,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *iss · 33 unmodified lines
6263 assigneeCommentMap := make(map[int64]*issues_model.Comment)
6364
6465 // add first push codes comment
65− baseGitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
66+ baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
6667 if err != nil {
6768 return err
6869 }
@@ -269,9 +270,9 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest · 200 unmodified lines
269270 if err != nil {
270271 return fmt.Errorf("GetRepositoryByIDCtx: %w", err)
271272 }
272− gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
273+ gitRepo, err := gitrepo.OpenRepository(ctx, repo)
273274 if err != nil {
274− return fmt.Errorf("git.OpenRepository: %w", err)
275+ return fmt.Errorf("gitrepo.OpenRepository: %w", err)
275276 }
276277 go func() {
277278 // FIXME: graceful: We need to tell the manager we're doing something...
@@ -614,7 +615,7 @@ func CloseBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, · 336 unmodified lines
614615
615616 // CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository
616617 func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) error {
617− branches, _, err := git.GetBranchesByPath(ctx, repo.RepoPath(), 0, 0)
618+ branches, _, err := gitrepo.GetBranchesByPath(ctx, repo, 0, 0)
618619 if err != nil {
619620 return err
620621 }
@@ -671,7 +672,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ · 50 unmodified lines
671672 }
672673 }
673674
674− gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath())
675+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo)
675676 if err != nil {
676677 log.Error("Unable to open head repository: Error: %v", err)
677678 return ""
@@ -845,7 +846,7 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues issues_model.IssueList · 167 unmodified lines
845846 }
846847 gitRepo, ok := gitRepos[issue.RepoID]
847848 if !ok {
848− gitRepo, err = git.OpenRepository(ctx, issue.Repo.RepoPath())
849+ gitRepo, err = gitrepo.OpenRepository(ctx, issue.Repo)
849850 if err != nil {
850851 log.Error("Cannot open git repository %-v for issue #%d[%d]. Error: %v", issue.Repo, issue.Index, issue.ID, err)
851852 continue
@@ -882,7 +883,7 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *issues_model.PullRequest, br · 30 unmodified lines
882883 if err = pr.LoadBaseRepo(ctx); err != nil {
883884 return false, err
884885 }
885− baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath())
886+ baseGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo)
886887 if err != nil {
887888 return false, err
888889 }
@@ -902,7 +903,7 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *issues_model.PullRequest, br · 13 unmodified lines
902903 } else {
903904 var closer io.Closer
904905
905− headGitRepo, closer, err = git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath())
906+ headGitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo)
906907 if err != nil {
907908 return false, err
908909 }