@@ -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
0 commit comments