refactor: correct git repo design and fix some legacy problems (#38512)

This commit is contained in:
wxiaoguang
2026-07-18 21:28:14 +08:00
committed by GitHub
parent 7786df34cf
commit 66e3849a70
41 changed files with 233 additions and 233 deletions
+14 -38
View File
@@ -9,9 +9,7 @@ package git
import (
"path/filepath"
gitealog "gitea.dev/modules/log"
"gitea.dev/modules/setting"
"gitea.dev/modules/util"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
@@ -30,25 +28,13 @@ type Repository struct {
gogitStorage *filesystem.Storage
}
func OpenRepository(repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
}
exist, err := util.IsDir(repoPath)
if err != nil {
return nil, err
}
if !exist {
return nil, util.NewNotExistErrorf("no such file or directory")
}
fs := osfs.New(repoPath)
_, err = fs.Stat(".git")
func openRepositoryInternal(gitRepo *Repository) error {
fs := osfs.New(gitRepo.Path)
_, err := fs.Stat(".git")
if err == nil {
fs, err = fs.Chroot(".git")
if err != nil {
return nil, err
return err
}
}
// the "clone --shared" repo doesn't work well with go-git AlternativeFS, https://github.com/go-git/go-git/issues/1006
@@ -59,33 +45,23 @@ func OpenRepository(repoPath string) (*Repository, error) {
} else {
altFs = osfs.New("/")
}
storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{KeepDescriptors: true, LargeObjectThreshold: setting.Git.LargeObjectThreshold, AlternatesFS: altFs})
gogitRepo, err := gogit.Open(storage, fs)
gitRepo.objectFormatCache = ParseGogitHash(plumbing.ZeroHash).Type()
gitRepo.gogitStorage = filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{KeepDescriptors: true, LargeObjectThreshold: setting.Git.LargeObjectThreshold, AlternatesFS: altFs})
gitRepo.gogitRepo, err = gogit.Open(gitRepo.gogitStorage, fs)
if err != nil {
return nil, err
_ = gitRepo.gogitStorage.Close()
return err
}
repo := &Repository{
RepositoryBase: prepareRepositoryBase(repoPath),
gogitRepo: gogitRepo,
gogitStorage: storage,
}
repo.objectFormatCache = ParseGogitHash(plumbing.ZeroHash).Type()
return repo, nil
return nil
}
// Close this repository, in particular close the underlying gogitStorage if this is not nil
func (repo *Repository) Close() error {
if repo == nil || repo.gogitStorage == nil {
func (repo *Repository) closeInternal() error {
if repo.gogitStorage == nil {
return nil
}
if err := repo.gogitStorage.Close(); err != nil {
gitealog.Error("Error closing storage: %v", err)
}
err := repo.gogitStorage.Close()
repo.gogitStorage = nil
repo.LastCommitCache = nil
repo.tagCache = nil
return nil
return err
}
// GoGitRepo gets the go-git repo representation