diff --git a/repo.go b/repo.go index d54cebac..effebcf8 100644 --- a/repo.go +++ b/repo.go @@ -23,6 +23,7 @@ type Repository struct { cachedCommits *objectCache cachedTags *objectCache + cachedTrees *objectCache } // Path returns the path of the repository. @@ -98,6 +99,7 @@ func Open(repoPath string) (*Repository, error) { path: repoPath, cachedCommits: newObjectCache(), cachedTags: newObjectCache(), + cachedTrees: newObjectCache(), }, nil } diff --git a/repo_tree.go b/repo_tree.go index fe98bb39..394d7b0b 100644 --- a/repo_tree.go +++ b/repo_tree.go @@ -96,26 +96,32 @@ type LsTreeOptions struct { CommandOptions } -// LsTree returns the tree object in the repository by given revision. -func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) { +// LsTree returns the tree object in the repository by given tree ID. +func (r *Repository) LsTree(treeID string, opts ...LsTreeOptions) (*Tree, error) { var opt LsTreeOptions if len(opts) > 0 { opt = opts[0] } + cache, ok := r.cachedTrees.Get(treeID) + if ok { + log("Cached tree hit: %s", treeID) + return cache.(*Tree), nil + } + var err error - rev, err = r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint + treeID, err = r.RevParse(treeID, RevParseOptions{Timeout: opt.Timeout}) //nolint if err != nil { return nil, err } t := &Tree{ - id: MustIDFromString(rev), + id: MustIDFromString(treeID), repo: r, } stdout, err := NewCommand("ls-tree"). AddOptions(opt.CommandOptions). - AddArgs(rev). + AddArgs(treeID). RunInDirWithTimeout(opt.Timeout, r.path) if err != nil { return nil, err @@ -126,5 +132,6 @@ func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) { return nil, err } + r.cachedTrees.Set(treeID, t) return t, nil }