Skip to content

Commit 1bcceef

Browse files
feat: cache tree objects (#81)
Co-authored-by: Joe Chen <[email protected]>
1 parent a2567c6 commit 1bcceef

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

repo.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Repository struct {
2323

2424
cachedCommits *objectCache
2525
cachedTags *objectCache
26+
cachedTrees *objectCache
2627
}
2728

2829
// Path returns the path of the repository.
@@ -98,6 +99,7 @@ func Open(repoPath string) (*Repository, error) {
9899
path: repoPath,
99100
cachedCommits: newObjectCache(),
100101
cachedTags: newObjectCache(),
102+
cachedTrees: newObjectCache(),
101103
}, nil
102104
}
103105

repo_tree.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,32 @@ type LsTreeOptions struct {
9696
CommandOptions
9797
}
9898

99-
// LsTree returns the tree object in the repository by given revision.
100-
func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) {
99+
// LsTree returns the tree object in the repository by given tree ID.
100+
func (r *Repository) LsTree(treeID string, opts ...LsTreeOptions) (*Tree, error) {
101101
var opt LsTreeOptions
102102
if len(opts) > 0 {
103103
opt = opts[0]
104104
}
105105

106+
cache, ok := r.cachedTrees.Get(treeID)
107+
if ok {
108+
log("Cached tree hit: %s", treeID)
109+
return cache.(*Tree), nil
110+
}
111+
106112
var err error
107-
rev, err = r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint
113+
treeID, err = r.RevParse(treeID, RevParseOptions{Timeout: opt.Timeout}) //nolint
108114
if err != nil {
109115
return nil, err
110116
}
111117
t := &Tree{
112-
id: MustIDFromString(rev),
118+
id: MustIDFromString(treeID),
113119
repo: r,
114120
}
115121

116122
stdout, err := NewCommand("ls-tree").
117123
AddOptions(opt.CommandOptions).
118-
AddArgs(rev).
124+
AddArgs(treeID).
119125
RunInDirWithTimeout(opt.Timeout, r.path)
120126
if err != nil {
121127
return nil, err
@@ -126,5 +132,6 @@ func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) {
126132
return nil, err
127133
}
128134

135+
r.cachedTrees.Set(treeID, t)
129136
return t, nil
130137
}

0 commit comments

Comments
 (0)