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

Unified · Split

services/pull/commit_status.go+2−1
@@ -11,6 +11,7 @@ import ( · 10 unmodified lines
1111 git_model "code.gitea.io/gitea/models/git"
1212 issues_model "code.gitea.io/gitea/models/issues"
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/structs"
1617
@@ -116,7 +117,7 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullR · 99 unmodified lines
116117 }
117118
118119 // check if all required status checks are successful
119− headGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath())
120+ headGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo)
120121 if err != nil {
121122 return "", errors.Wrap(err, "OpenRepository")
122123 }