aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/files
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-01-28 04:09:51 +0800
committerGitHub <noreply@github.com>2024-01-27 21:09:51 +0100
commit5f82ead13cb7706d3f660271d94de6101cef4119 (patch)
treec10dc0ec9b98992629847f2b98554f3d1c3713ed /services/repository/files
parent60e4a98ab07dcf3bd86cf630c79e6433c3ef3e84 (diff)
downloadgitea-5f82ead13cb7706d3f660271d94de6101cef4119.tar.gz
gitea-5f82ead13cb7706d3f660271d94de6101cef4119.zip
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. --------- Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'services/repository/files')
-rw-r--r--services/repository/files/commit.go3
-rw-r--r--services/repository/files/content.go5
-rw-r--r--services/repository/files/content_test.go5
-rw-r--r--services/repository/files/file_test.go4
-rw-r--r--services/repository/files/patch.go3
-rw-r--r--services/repository/files/update.go3
6 files changed, 13 insertions, 10 deletions
diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go
index 048e41e6fd..16a15e06a7 100644
--- a/services/repository/files/commit.go
+++ b/services/repository/files/commit.go
@@ -12,6 +12,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/automerge"
)
@@ -23,7 +24,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
repoPath := repo.RepoPath()
// confirm that commit is exist
- gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return fmt.Errorf("OpenRepository[%s]: %w", repoPath, err)
}
diff --git a/services/repository/files/content.go b/services/repository/files/content.go
index 30d62fbcdf..c278d7f835 100644
--- a/services/repository/files/content.go
+++ b/services/repository/files/content.go
@@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
@@ -58,7 +59,7 @@ func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePat
}
treePath = cleanTreePath
- gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return nil, err
}
@@ -133,7 +134,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
}
treePath = cleanTreePath
- gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return nil, err
}
diff --git a/services/repository/files/content_test.go b/services/repository/files/content_test.go
index 3ad3e3ab98..d50847789a 100644
--- a/services/repository/files/content_test.go
+++ b/services/repository/files/content_test.go
@@ -6,10 +6,9 @@ package files
import (
"testing"
- repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/contexttest"
- "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
api "code.gitea.io/gitea/modules/structs"
_ "code.gitea.io/gitea/models/actions"
@@ -235,7 +234,7 @@ func TestGetBlobBySHA(t *testing.T) {
ctx.SetParams(":id", "1")
ctx.SetParams(":sha", sha)
- gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name))
+ gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
if err != nil {
t.Fail()
}
diff --git a/services/repository/files/file_test.go b/services/repository/files/file_test.go
index 4e67ad1410..675ddbddb3 100644
--- a/services/repository/files/file_test.go
+++ b/services/repository/files/file_test.go
@@ -8,7 +8,7 @@ import (
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/contexttest"
- "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -109,7 +109,7 @@ func TestGetFileResponseFromCommit(t *testing.T) {
repo := ctx.Repo.Repository
branch := repo.DefaultBranch
treePath := "README.md"
- gitRepo, _ := git.OpenRepository(ctx, repo.RepoPath())
+ gitRepo, _ := gitrepo.OpenRepository(ctx, repo)
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(branch)
expectedFileResponse := getExpectedFileResponse()
diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go
index 14f8caaa8c..f6d5643dc9 100644
--- a/services/repository/files/patch.go
+++ b/services/repository/files/patch.go
@@ -13,6 +13,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs"
asymkey_service "code.gitea.io/gitea/services/asymkey"
@@ -42,7 +43,7 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode
opts.NewBranch = opts.OldBranch
}
- gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return err
}
diff --git a/services/repository/files/update.go b/services/repository/files/update.go
index 1892043304..f223daf3a9 100644
--- a/services/repository/files/update.go
+++ b/services/repository/files/update.go
@@ -16,6 +16,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -78,7 +79,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
opts.NewBranch = opts.OldBranch
}
- gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return nil, err
}