Skip to content

feat: cache tree objects #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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
17 changes: 12 additions & 5 deletions repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}