From c86392dc81d64987625a88f82e84705f33625f01 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 14 Jun 2022 13:46:04 -0400 Subject: [PATCH 1/2] feat: cache tree objects --- repo.go | 2 ++ repo_tree.go | 14 ++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) 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..73a33c33 100644 --- a/repo_tree.go +++ b/repo_tree.go @@ -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 @@ -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 } From 7430a96f48ea2f6b196444cfc793743db6de80a2 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Tue, 21 Jun 2022 21:15:50 +0800 Subject: [PATCH 2/2] Apply suggestions from code review --- repo_tree.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repo_tree.go b/repo_tree.go index 73a33c33..394d7b0b 100644 --- a/repo_tree.go +++ b/repo_tree.go @@ -96,7 +96,7 @@ type LsTreeOptions struct { CommandOptions } -// LsTree returns the tree object in the repository by given revision. +// 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 { @@ -108,6 +108,7 @@ func (r *Repository) LsTree(treeID string, opts ...LsTreeOptions) (*Tree, error) log("Cached tree hit: %s", treeID) return cache.(*Tree), nil } + var err error treeID, err = r.RevParse(treeID, RevParseOptions{Timeout: opt.Timeout}) //nolint if err != nil {