Skip to content

Commit

Permalink
Merge pull request #223 from adsr/git_memo
Browse files Browse the repository at this point in the history
Memoize git shell commands
  • Loading branch information
nikhil2611 authored Sep 11, 2023
2 parents b5fdd8d + f67c7ef commit 70a0cce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
27 changes: 25 additions & 2 deletions lib/chef-cli/cookbook_profiler/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ class Git

include Helpers

@@git_memo = {}

attr_reader :cookbook_path

def self.uncache
@@git_memo = {}
end

def initialize(cookbook_path)
@cookbook_path = cookbook_path
@repo_path = nil
@unborn_branch = nil
@unborn_branch_ref = nil
end
Expand Down Expand Up @@ -111,8 +118,24 @@ def git!(subcommand, options = {})
end

def git(subcommand, options = {})
options = { cwd: cookbook_path }.merge(options)
system_command("git #{subcommand}", options)
# memoize commands per-repo
repo_path = get_repo_path
memo_key = [repo_path, subcommand, options]
if @@git_memo.key?(memo_key)
rv = @@git_memo[memo_key]
else
options = { cwd: cookbook_path }.merge(options)
rv = system_command("git #{subcommand}", options)
@@git_memo[memo_key] = rv
end
rv
end

def get_repo_path
unless @repo_path
@repo_path = system_command("git rev-parse --show-toplevel", { cwd: cookbook_path }).stdout.strip
end
@repo_path
end

def detect_current_branch
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/cookbook_profiler/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

include ChefCLI::Helpers

let(:git_profiler) do
let!(:git_profiler) do
ChefCLI::CookbookProfiler::Git.uncache
ChefCLI::CookbookProfiler::Git.new(cookbook_path)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/unit/policyfile_lock_build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def expect_hash_equal(actual, expected)
ChefCLI::Policyfile::StorageConfig.new( cache_path: cache_path, relative_paths_root: relative_paths_root )
end

let!(:git_memo) do
ChefCLI::CookbookProfiler::Git.uncache
end

context "when a cached cookbook omits the cache key" do

let(:policyfile_lock) do
Expand Down

0 comments on commit 70a0cce

Please sign in to comment.