mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-16 22:12:07 +02:00
refactor: decouple git.Repository(ctx) from git.Commit & git.Tree (#38464)
1. Storing "ctx" in a long-living object is wrong 2. Make the commit & tree cacheable (for the future performance optimization) 3. Also fix some bad designs like `// FIXME: bad design, this field can be nil if the commit is from "last commit cache"` ref: * #33893
This commit is contained in:
+7
-17
@@ -6,31 +6,22 @@ package git
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/modules/git/gitcmd"
|
||||
)
|
||||
|
||||
type TreeCommon struct {
|
||||
ID ObjectID
|
||||
ResolvedID ObjectID
|
||||
|
||||
repo *Repository
|
||||
ptree *Tree // parent tree
|
||||
ID ObjectID
|
||||
}
|
||||
|
||||
// NewTree create a new tree according the repository and tree id
|
||||
func NewTree(repo *Repository, id ObjectID) *Tree {
|
||||
return &Tree{
|
||||
TreeCommon: TreeCommon{
|
||||
ID: id,
|
||||
repo: repo,
|
||||
},
|
||||
}
|
||||
func newTree(id ObjectID) *Tree {
|
||||
return &Tree{TreeCommon: TreeCommon{ID: id}}
|
||||
}
|
||||
|
||||
// SubTree get a subtree by the sub dir path
|
||||
func (t *Tree) SubTree(rpath string) (*Tree, error) {
|
||||
func (t *Tree) SubTree(ctx context.Context, gitRepo *Repository, rpath string) (*Tree, error) {
|
||||
if len(rpath) == 0 {
|
||||
return t, nil
|
||||
}
|
||||
@@ -43,16 +34,15 @@ func (t *Tree) SubTree(rpath string) (*Tree, error) {
|
||||
te *TreeEntry
|
||||
)
|
||||
for _, name := range paths {
|
||||
te, err = p.GetTreeEntryByPath(name)
|
||||
te, err = p.GetTreeEntryByPath(ctx, gitRepo, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g, err = t.repo.getTree(te.ID)
|
||||
g, err = gitRepo.getTree(te.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.ptree = p
|
||||
p = g
|
||||
}
|
||||
return g, nil
|
||||
|
||||
Reference in New Issue
Block a user