refactor: remove Path field from git.Repository (#38552)

The "path" details should be hidden to other packages

By the way, fix a resource leaking in gogit's CommitNodeIndex
(the file was not closed in CacheCommit)
This commit is contained in:
wxiaoguang
2026-07-21 02:03:28 +08:00
committed by GitHub
parent 9dc04289aa
commit a4526d5a82
28 changed files with 146 additions and 107 deletions
+8 -7
View File
@@ -25,11 +25,6 @@ import (
type RepositoryFacade = gitcmd.RepositoryFacade
type RepositoryBase struct {
// TODO: refactor it to a private field "localPath" in the future
// * for repo accessing purpose, in most causes, use "WithRepo", or RepoLocalPath(repo) if the local path must be used
// * for error handling & logging purpose, it needs to introduce a new function "git.RepoLogName()" to handle various cases
Path string
LastCommitCache *LastCommitCache
repoFacade RepositoryFacade
@@ -51,6 +46,10 @@ func (repo *Repository) GitRepoLocation() string {
return repo.repoFacade.GitRepoLocation()
}
func (repo *Repository) LogString() string {
return repo.repoFacade.LogString()
}
func OpenRepository(repo RepositoryFacade) (*Repository, error) {
repoPath := gitcmd.RepoLocalPath(repo)
exist, err := util.IsDir(repoPath)
@@ -61,7 +60,7 @@ func OpenRepository(repo RepositoryFacade) (*Repository, error) {
return nil, util.NewNotExistErrorf("no such file or directory")
}
gitRepo := &Repository{
RepositoryBase: RepositoryBase{Path: repoPath, tagCache: newObjectCache[*Tag](), repoFacade: repo},
RepositoryBase: RepositoryBase{tagCache: newObjectCache[*Tag](), repoFacade: repo},
}
if err = openRepositoryInternal(gitRepo); err != nil {
return nil, err
@@ -69,6 +68,8 @@ func OpenRepository(repo RepositoryFacade) (*Repository, error) {
return gitRepo, nil
}
// OpenRepositoryLocal opens a local repository that is not managed by Gitea
// If the path is relative, it will be converted to an absolute path using filepath.Abs (base on current working path)
func OpenRepositoryLocal(localPath string) (_ *Repository, err error) {
if !filepath.IsAbs(localPath) {
localPath, err = filepath.Abs(localPath)
@@ -129,7 +130,7 @@ func InitRepositoryLocal(ctx context.Context, repoPath string, bare bool, object
// IsEmpty Check if repository is empty.
func (repo *Repository) IsEmpty(ctx context.Context) (bool, error) {
stdout, _, err := gitcmd.NewCommand().
AddOptionFormat("--git-dir=%s", repo.Path).
AddOptionFormat("--git-dir=%s", gitcmd.RepoLocalPath(repo)). // TODO: all git commands should use "--git-dir" or "GIT_DIR=..."
AddArguments("rev-list", "-n", "1", "--all").
WithRepo(repo).
RunStdString(ctx)