diff options
author | Thom May <thom@may.lt> | 2017-09-04 10:22:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-04 10:22:39 +0100 |
commit | 38361e70e83c044e9706cf02e9bc39b3d1617930 (patch) | |
tree | e775e0ecde4fd8ff8a912d78eda425fb3d1d758f | |
parent | 88b3dc1f889a82111b879061273f3e684100beaf (diff) | |
parent | 236f47a154f297cdf7ed42edcd494d850567a4f6 (diff) | |
download | chef-38361e70e83c044e9706cf02e9bc39b3d1617930.tar.gz |
Merge pull request #6359 from kzw/issue.6350
handle apple git
-rw-r--r-- | lib/chef/provider/git.rb | 23 | ||||
-rw-r--r-- | spec/unit/provider/git_spec.rb | 55 |
2 files changed, 75 insertions, 3 deletions
diff --git a/lib/chef/provider/git.rb b/lib/chef/provider/git.rb index db83e0aea6..bdbeca5941 100644 --- a/lib/chef/provider/git.rb +++ b/lib/chef/provider/git.rb @@ -28,6 +28,8 @@ class Chef extend Forwardable provides :git + GIT_VERSION_PATTERN = Regexp.compile('git version (\d+\.\d+.\d+)') + def_delegator :new_resource, :destination, :cwd def load_current_resource @@ -103,8 +105,21 @@ class Chef end end - def git_minor_version - @git_minor_version ||= Gem::Version.new( git("--version").stdout.split.last ) + def git_has_single_branch_option? + @git_has_single_branch_option ||= !git_gem_version.nil? && git_gem_version >= Gem::Version.new("1.7.10") + end + + def git_gem_version + return @git_gem_version if defined?(@git_gem_version) + output = git("--version").stdout + match = GIT_VERSION_PATTERN.match(output) + if match + @git_gem_version = Gem::Version.new(match[1]) + else + Chef::Log.warn "Unable to parse git version from '#{output}'" + @git_gem_version = nil + end + @git_gem_version end def existing_git_clone? @@ -142,7 +157,7 @@ class Chef clone_cmd = ["clone"] clone_cmd << "-o #{remote}" unless remote == "origin" clone_cmd << "--depth #{new_resource.depth}" if new_resource.depth - clone_cmd << "--no-single-branch" if new_resource.depth && git_minor_version >= Gem::Version.new("1.7.10") + clone_cmd << "--no-single-branch" if new_resource.depth && git_has_single_branch_option? clone_cmd << "\"#{new_resource.repository}\"" clone_cmd << "\"#{cwd}\"" @@ -285,6 +300,8 @@ class Chef refs.find_all { |m| m[1] == pattern } end + alias git_minor_version git_gem_version + private def run_options(run_opts = {}) diff --git a/spec/unit/provider/git_spec.rb b/spec/unit/provider/git_spec.rb index a60c1b44c3..cacee0baaa 100644 --- a/spec/unit/provider/git_spec.rb +++ b/spec/unit/provider/git_spec.rb @@ -83,6 +83,61 @@ describe Chef::Provider::Git do expect(@provider.new_resource).to equal(@resource) end + context "cast git version into gem version object" do + it "returns correct version with standard git" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version 2.14.1")) + expect(@provider.git_gem_version).to eq Gem::Version.new("2.14.1") + end + + it "returns correct version with Apple git" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version 2.11.0 (Apple Git-81)")) + expect(@provider.git_gem_version).to eq Gem::Version.new("2.11.0") + end + + it "maintains deprecated method name" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version 1.2.3")) + expect(@provider.git_minor_version).to eq Gem::Version.new("1.2.3") + end + + it "does not know how to handle other version" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version home-grown-git-99")) + expect(@provider.git_gem_version).to be_nil + end + + it "determines single branch option when it fails to parse git version" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version home-grown-git-99")) + expect(@provider.git_has_single_branch_option?).to be false + end + + it "determines single branch option as true when it parses git version and version is large" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version 1.8.0")) + expect(@provider.git_has_single_branch_option?).to be true + end + + it "determines single branch option as false when it parses git version and version is small" do + expect(@provider).to receive(:shell_out!) + .with("git --version", log_tag: "git[web2.0 app]") + .and_return(double("ShellOut result", stdout: "git version 1.7.4")) + expect(@provider.git_has_single_branch_option?).to be false + end + + it "is compatible with git in travis" do + expect(@provider.git_gem_version).to be > Gem::Version.new("1.0") + end + end + context "resolving revisions to a SHA" do before do |