Skip to content

Commit fe3a850

Browse files
authored
Merge pull request glennsarti#38 from beechtom/bug/gclone
Support using commits for `GClone` spec searcher, correctly print errors
2 parents d3c27fd + 24cff33 commit fe3a850

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

lib/puppetfile-resolver/spec_searchers/git/gclone.rb

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module PuppetfileResolver
1212
module SpecSearchers
1313
module Git
1414
module GClone
15+
METADATA_FILE = 'metadata.json'
16+
1517
# @summary clones the remote url and reads the metadata file
1618
# @returns [String] the content of the metadata file
1719
def self.metadata(puppetfile_module, resolver_ui, config)
@@ -25,8 +27,6 @@ def self.metadata(puppetfile_module, resolver_ui, config)
2527
return nil if repo_url.nil?
2628
return nil unless valid_http_url?(repo_url)
2729

28-
metadata_file = 'metadata.json'
29-
3030
ref = puppetfile_module.ref ||
3131
puppetfile_module.tag ||
3232
puppetfile_module.commit ||
@@ -35,7 +35,7 @@ def self.metadata(puppetfile_module, resolver_ui, config)
3535

3636
resolver_ui.debug { "Querying git repository #{repo_url}" }
3737

38-
clone_and_read_file(repo_url, ref, metadata_file, config)
38+
clone_and_read_file(repo_url, ref, config)
3939
end
4040

4141
# @summary clones the git url and reads the file at the given ref
@@ -44,24 +44,37 @@ def self.metadata(puppetfile_module, resolver_ui, config)
4444
# @param ref [String] the git ref, branch, commit, tag
4545
# @param file [String] the file you wish to read
4646
# @returns [String] the content of the file
47-
def self.clone_and_read_file(url, ref, file, config)
48-
clone_cmd = ['git', 'clone', '--bare', '--depth=1', '--single-branch']
49-
err_msg = ''
50-
if config.proxy
51-
err_msg += " with proxy #{config.proxy}: "
52-
proxy = "--config \"http.proxy=#{config.proxy}\" --config \"https.proxy=#{config.proxy}\""
53-
clone_cmd.push(proxy)
54-
end
55-
47+
def self.clone_and_read_file(url, ref, config)
5648
Dir.mktmpdir(nil, config.clone_dir) do |dir|
57-
clone_cmd.push("--branch=#{ref}") if ref != 'HEAD'
58-
clone_cmd.push(url, dir)
59-
out, err_out, process = ::PuppetfileResolver::Util.run_command(clone_cmd)
60-
err_msg += out
61-
raise err_msg unless process.success?
49+
clone = ['git', 'clone', url, dir]
50+
clone += ['--config', "http.proxy=#{config.proxy}", '--config', "https.proxy=#{config.proxy}"] if config.proxy
51+
52+
bare_clone = clone + ['--bare', '--depth=1']
53+
bare_clone.push("--branch=#{ref}") unless ref == 'HEAD'
54+
55+
# Try to clone a bare repository. If that fails, fall back to a full clone.
56+
# Cloning might fail because the repo does not exist or is otherwise
57+
# inaccessible, but it can also fail because cloning a bare repository from
58+
# a commit/SHA1 fails. Falling back to a full clone ensures that we support
59+
# commits/SHA1s like Puppetfile does.
60+
_stdout, _stderr, process = ::PuppetfileResolver::Util.run_command(bare_clone)
61+
62+
unless process.success?
63+
_stdout, stderr, process = ::PuppetfileResolver::Util.run_command(clone)
64+
65+
unless process.success?
66+
msg = if config.proxy
67+
"Cloning #{url} with proxy #{config.proxy} failed: #{stderr}"
68+
else
69+
"Cloning #{url} failed: #{stderr}"
70+
end
71+
raise msg
72+
end
73+
end
74+
6275
Dir.chdir(dir) do
63-
content, err_out, process = ::PuppetfileResolver::Util.run_command(['git', 'show', "#{ref}:#{file}"])
64-
raise 'InvalidContent' unless process.success? && content.length > 2
76+
content, stderr, process = ::PuppetfileResolver::Util.run_command(['git', 'show', "#{ref}:#{METADATA_FILE}"])
77+
raise "Could not find #{METADATA_FILE} for ref #{ref} at #{url}: #{stderr}" unless process.success?
6578
return content
6679
end
6780
end

spec/unit/puppetfile-resolver/spec_searchers/git/gclone_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
expect(JSON.parse(content)['name']).to eq('puppetlabs-powershell')
2424
end
2525

26-
context 'with ref' do
27-
26+
context 'with tag' do
2827
let(:puppetfile_module) do
2928
PuppetfileModule.new(remote: url, ref: '2.1.2')
3029
end
@@ -34,6 +33,29 @@
3433
expect(JSON.parse(content)['name']).to eq('puppetlabs-powershell')
3534
end
3635
end
36+
37+
context 'with commit' do
38+
let(:puppetfile_module) do
39+
PuppetfileModule.new(remote: url, ref: '9276de695798097e8471b877a18df27f764eecda')
40+
end
41+
42+
it 'reads metadata' do
43+
content = subject.metadata(puppetfile_module, Logger.new(STDERR), config)
44+
expect(JSON.parse(content)['name']).to eq('puppetlabs-powershell')
45+
end
46+
end
47+
48+
context 'with invalid ref' do
49+
let(:puppetfile_module) do
50+
PuppetfileModule.new(remote: url, ref: '8f7d5ea3ef49dadc5e166d5d802d091abc4b02bc')
51+
end
52+
53+
it 'errors gracefully' do
54+
expect { subject.metadata(puppetfile_module, Logger.new(STDERR), config) }.to raise_error(
55+
/Could not find metadata\.json for ref .* at .*/
56+
)
57+
end
58+
end
3759
end
3860

3961
context 'invalid url' do

0 commit comments

Comments
 (0)