summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkzwin <kz@example.org>2017-08-28 20:55:08 -0400
committerkzwin <kz@example.org>2017-08-28 20:58:14 -0400
commitec06f57d26e1cfd363324a32767370d4cf03cf47 (patch)
treee2adf696c03a1d9846ea00772a27044e670a5086
parent8c30db84a92f995cb6cf6d83f4964dffdd28ad1c (diff)
downloadchef-ec06f57d26e1cfd363324a32767370d4cf03cf47.tar.gz
handle apple git
Signed-off-by: kzwin <k.z.win@happyw.info>
-rw-r--r--lib/chef/provider/git.rb13
-rw-r--r--spec/unit/provider/git_spec.rb30
2 files changed, 40 insertions, 3 deletions
diff --git a/lib/chef/provider/git.rb b/lib/chef/provider/git.rb
index db83e0aea6..7999a16b31 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,11 @@ class Chef
end
end
- def git_minor_version
- @git_minor_version ||= Gem::Version.new( git("--version").stdout.split.last )
+ def git_gem_version
+ output = git('--version').stdout
+ match = GIT_VERSION_PATTERN.match(output)
+ match or raise ArgumentError, "unparsable git version number #{output}"
+ @git_gem_version ||= Gem::Version.new(match[1])
end
def existing_git_clone?
@@ -142,7 +147,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_gem_version >= Gem::Version.new("1.7.10")
clone_cmd << "\"#{new_resource.repository}\""
clone_cmd << "\"#{cwd}\""
@@ -285,6 +290,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..9c0bd8796a 100644
--- a/spec/unit/provider/git_spec.rb
+++ b/spec/unit/provider/git_spec.rb
@@ -83,6 +83,36 @@ 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 raise_error(ArgumentError, /unparsable git version/)
+ end
+ end
+
context "resolving revisions to a SHA" do
before do