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
+24 -6
View File
@@ -28,15 +28,23 @@ type RepositoryBase struct {
LastCommitCache *LastCommitCache
repoFacade RepositoryFacade
tagCache *ObjectCache[*Tag]
objectFormatCache ObjectFormat
}
func OpenRepository(repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
}
var _ gitcmd.RepositoryFacade = (*Repository)(nil)
func (repo *Repository) GitRepoManagedID() string {
return repo.repoFacade.GitRepoManagedID()
}
func (repo *Repository) GitRepoLocation() string {
return repo.repoFacade.GitRepoLocation()
}
func OpenRepository(repo RepositoryFacade) (*Repository, error) {
repoPath := gitcmd.RepoLocalPath(repo)
exist, err := util.IsDir(repoPath)
if err != nil {
return nil, err
@@ -45,7 +53,7 @@ func OpenRepository(repoPath string) (*Repository, error) {
return nil, util.NewNotExistErrorf("no such file or directory")
}
gitRepo := &Repository{
RepositoryBase: RepositoryBase{Path: repoPath, tagCache: newObjectCache[*Tag]()},
RepositoryBase: RepositoryBase{Path: repoPath, tagCache: newObjectCache[*Tag](), repoFacade: repo},
}
if err = openRepositoryInternal(gitRepo); err != nil {
return nil, err
@@ -53,6 +61,16 @@ func OpenRepository(repoPath string) (*Repository, error) {
return gitRepo, nil
}
func OpenRepositoryLocal(localPath string) (_ *Repository, err error) {
if !filepath.IsAbs(localPath) {
localPath, err = filepath.Abs(localPath)
if err != nil {
return nil, err
}
}
return OpenRepository(gitcmd.RepositoryUnmanaged(localPath))
}
func (repo *Repository) Close() error {
if repo == nil {
setting.PanicInDevOrTesting("don't close a nil repository")