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

Unified · Split

services/pull/comment.go+2−3
@@ -9,7 +9,7 @@ import ( · 8 unmodified lines
99 issues_model "code.gitea.io/gitea/models/issues"
1010 repo_model "code.gitea.io/gitea/models/repo"
1111 user_model "code.gitea.io/gitea/models/user"
12− "code.gitea.io/gitea/modules/git"
12+ "code.gitea.io/gitea/modules/gitrepo"
1313 "code.gitea.io/gitea/modules/json"
1414 )
1515
@@ -17,8 +17,7 @@ import ( · 1 unmodified line
1717 // isForcePush will be true if oldCommit isn't on the branch
1818 // Commit on baseBranch will skip
1919 func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
20− repoPath := repo.RepoPath()
21− gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath)
20+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
2221 if err != nil {
2322 return nil, false, err
2423 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t