refactor: git repo and relative path handling (#38522)

1. simplify "StorageRepo" code
2. simplify git.OpenRepository code
3. by the way, clean up some unused files in git test fixtures.
This commit is contained in:
wxiaoguang
2026-07-19 15:32:00 +08:00
committed by GitHub
parent ae176cd649
commit b06002f449
100 changed files with 315 additions and 290 deletions
-14
View File
@@ -1,14 +0,0 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
import (
"context"
"gitea.dev/modules/git"
)
func NewBatch(ctx context.Context, repo Repository) (git.CatFileBatchCloser, error) {
return git.NewBatch(ctx, repoPath(repo))
}
+2
View File
@@ -9,6 +9,8 @@ import (
"gitea.dev/modules/git/gitcmd"
)
// TODO: all wrappers can be removed in next PR, because cmd now can accept Repository directly.
func RunCmd(ctx context.Context, repo Repository, cmd *gitcmd.Command) error {
return cmd.WithRepo(repo).WithParentCallerInfo().Run(ctx)
}
-14
View File
@@ -1,14 +0,0 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
import (
"context"
"gitea.dev/modules/git"
)
func WriteCommitGraph(ctx context.Context, repo Repository) error {
return git.WriteCommitGraph(ctx, repoPath(repo))
}
+7 -9
View File
@@ -19,16 +19,14 @@ import (
type Repository = gitcmd.RepositoryFacade
var repoPath = gitcmd.RepoLocalPath
// OpenRepository opens the repository at the given relative path with the provided context.
func OpenRepository(repo Repository) (*git.Repository, error) {
return git.OpenRepository(repoPath(repo))
}
var (
repoPath = gitcmd.RepoLocalPath
OpenRepository = git.OpenRepository
)
// contextKey is a value for use with context.WithValue.
type contextKey struct {
uniqueID string
key string
}
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
@@ -46,11 +44,11 @@ func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Rep
// RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context.
// Caller shouldn't close the git repo manually, the git repo will be automatically closed when the request context is done.
func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Repository) (*git.Repository, error) {
ck := contextKey{uniqueID: repo.GitRepoUniqueID()}
ck := contextKey{key: repo.GitRepoLocation()}
if gitRepo, ok := ctx.Value(ck).(*git.Repository); ok {
return gitRepo, nil
}
gitRepo, err := git.OpenRepository(repoPath(repo))
gitRepo, err := git.OpenRepository(repo)
if err != nil {
return nil, err
}
+3 -3
View File
@@ -7,17 +7,17 @@ import (
"path/filepath"
"testing"
"gitea.dev/models/repo"
"gitea.dev/modules/git"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/setting"
)
func mockRepository(repoPath string) repo.StorageRepo {
func mockRepository(repoPath string) gitcmd.RepositoryFacade {
if !filepath.IsAbs(repoPath) {
// resolve repository path relative to the unit test fixture directory
repoPath = filepath.Join(setting.GetGiteaTestSourceRoot(), "modules/git/tests/repos", repoPath)
}
return repo.StorageRepo(repoPath)
return gitcmd.RepositoryManaged(repoPath, repoPath)
}
func TestMain(m *testing.M) {