diff --git a/lib/chef-cli/cookbook_profiler/git.rb b/lib/chef-cli/cookbook_profiler/git.rb index ba0a52f2..449fea9a 100644 --- a/lib/chef-cli/cookbook_profiler/git.rb +++ b/lib/chef-cli/cookbook_profiler/git.rb @@ -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 @@ -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 diff --git a/spec/unit/cookbook_profiler/git_spec.rb b/spec/unit/cookbook_profiler/git_spec.rb index 1a45f6b3..9a960b00 100644 --- a/spec/unit/cookbook_profiler/git_spec.rb +++ b/spec/unit/cookbook_profiler/git_spec.rb @@ -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 diff --git a/spec/unit/policyfile_lock_build_spec.rb b/spec/unit/policyfile_lock_build_spec.rb index cd008ee8..a500cb6e 100644 --- a/spec/unit/policyfile_lock_build_spec.rb +++ b/spec/unit/policyfile_lock_build_spec.rb @@ -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