Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Repository struct {

cachedCommits *objectCache
cachedTags *objectCache
cachedTrees *objectCache
}

// Path returns the path of the repository.
Expand Down Expand Up @@ -98,6 +99,7 @@ func Open(repoPath string) (*Repository, error) {
path: repoPath,
cachedCommits: newObjectCache(),
cachedTags: newObjectCache(),
cachedTrees: newObjectCache(),
}, nil
}

Expand Down
14 changes: 10 additions & 4 deletions repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,30 @@ type LsTreeOptions struct {
}

// LsTree returns the tree object in the repository by given revision.
func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) {
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
Expand All @@ -126,5 +131,6 @@ func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) {
return nil, err
}

r.cachedTrees.Set(treeID, t)
return t, nil
}