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
23 "code.gitea.io/gitea/modules/container"23 "code.gitea.io/gitea/modules/container"
24 gitea_context "code.gitea.io/gitea/modules/context"24 gitea_context "code.gitea.io/gitea/modules/context"
25 "code.gitea.io/gitea/modules/git"25 "code.gitea.io/gitea/modules/git"
26 "code.gitea.io/gitea/modules/gitrepo"
26 "code.gitea.io/gitea/modules/graceful"27 "code.gitea.io/gitea/modules/graceful"
27 "code.gitea.io/gitea/modules/json"28 "code.gitea.io/gitea/modules/json"
28 "code.gitea.io/gitea/modules/log"29 "code.gitea.io/gitea/modules/log"
@@ -62,7 +63,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *iss · 33 unmodified lines
62 assigneeCommentMap := make(map[int64]*issues_model.Comment)63 assigneeCommentMap := make(map[int64]*issues_model.Comment)
63 64
64 // add first push codes comment65 // add first push codes comment
65 baseGitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())66 baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
66 if err != nil {67 if err != nil {
67 return err68 return err
68 }69 }
@@ -269,9 +270,9 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest · 200 unmodified lines
269 if err != nil {270 if err != nil {
270 return fmt.Errorf("GetRepositoryByIDCtx: %w", err)271 return fmt.Errorf("GetRepositoryByIDCtx: %w", err)
271 }272 }
272 gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())273 gitRepo, err := gitrepo.OpenRepository(ctx, repo)
273 if err != nil {274 if err != nil {
274 return fmt.Errorf("git.OpenRepository: %w", err)275 return fmt.Errorf("gitrepo.OpenRepository: %w", err)
275 }276 }
276 go func() {277 go func() {
277 // FIXME: graceful: We need to tell the manager we're doing something...278 // 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
614 615
615// CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository616// CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository
616func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) error {617func 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)
618 if err != nil {619 if err != nil {
619 return err620 return err
620 }621 }
@@ -671,7 +672,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ · 50 unmodified lines
671 }672 }
672 }673 }
673 674
674 gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath())675 gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo)
675 if err != nil {676 if err != nil {
676 log.Error("Unable to open head repository: Error: %v", err)677 log.Error("Unable to open head repository: Error: %v", err)
677 return ""678 return ""
@@ -845,7 +846,7 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues issues_model.IssueList · 167 unmodified lines
845 }846 }
846 gitRepo, ok := gitRepos[issue.RepoID]847 gitRepo, ok := gitRepos[issue.RepoID]
847 if !ok {848 if !ok {
848 gitRepo, err = git.OpenRepository(ctx, issue.Repo.RepoPath())849 gitRepo, err = gitrepo.OpenRepository(ctx, issue.Repo)
849 if err != nil {850 if err != nil {
850 log.Error("Cannot open git repository %-v for issue #%d[%d]. Error: %v", issue.Repo, issue.Index, issue.ID, err)851 log.Error("Cannot open git repository %-v for issue #%d[%d]. Error: %v", issue.Repo, issue.Index, issue.ID, err)
851 continue852 continue
@@ -882,7 +883,7 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *issues_model.PullRequest, br · 30 unmodified lines
882 if err = pr.LoadBaseRepo(ctx); err != nil {883 if err = pr.LoadBaseRepo(ctx); err != nil {
883 return false, err884 return false, err
884 }885 }
885 baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath())886 baseGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo)
886 if err != nil {887 if err != nil {
887 return false, err888 return false, err
888 }889 }
@@ -902,7 +903,7 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *issues_model.PullRequest, br · 13 unmodified lines
902 } else {903 } else {
903 var closer io.Closer904 var closer io.Closer
904 905
905 headGitRepo, closer, err = git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath())906 headGitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo)
906 if err != nil {907 if err != nil {
907 return false, err908 return false, err
908 }909 }