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
9 issues_model "code.gitea.io/gitea/models/issues"9 issues_model "code.gitea.io/gitea/models/issues"
10 repo_model "code.gitea.io/gitea/models/repo"10 repo_model "code.gitea.io/gitea/models/repo"
11 user_model "code.gitea.io/gitea/models/user"11 user_model "code.gitea.io/gitea/models/user"
12 "code.gitea.io/gitea/modules/git"12 "code.gitea.io/gitea/modules/gitrepo"
13 "code.gitea.io/gitea/modules/json"13 "code.gitea.io/gitea/modules/json"
14)14)
15 15
@@ -17,8 +17,7 @@ import ( · 1 unmodified line
17// isForcePush will be true if oldCommit isn't on the branch17// isForcePush will be true if oldCommit isn't on the branch
18// Commit on baseBranch will skip18// Commit on baseBranch will skip
19func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {19func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
20 repoPath := repo.RepoPath()20 gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
21 gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath)
22 if err != nil {21 if err != nil {
23 return nil, false, err22 return nil, false, err
24 }23 }

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