diff --git a/lib/chef-cli/cookbook_profiler/git.rb b/lib/chef-cli/cookbook_profiler/git.rb index ba0a52f2..65893e19 100644 --- a/lib/chef-cli/cookbook_profiler/git.rb +++ b/lib/chef-cli/cookbook_profiler/git.rb @@ -23,8 +23,14 @@ class Git include Helpers + @@git_memo = {} + attr_reader :cookbook_path + def self.uncache + @@git_memo = {} + end + def initialize(cookbook_path) @cookbook_path = cookbook_path @unborn_branch = nil @@ -111,8 +117,15 @@ def git!(subcommand, options = {}) end def git(subcommand, options = {}) - options = { cwd: cookbook_path }.merge(options) - system_command("git #{subcommand}", options) + memo_key = [subcommand, options] + if @@git_memo.has_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 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