Class: Chef::Provider::Git
Instance Attribute Summary
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Instance Method Summary
collapse
action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?
#provided_as, #provides, #provides?
#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited
#descendants, #include, #included
#notifying_block, #subcontext_block
#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context
#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!
#enforce_path_sanity, #sanitized_path
#powershell_out, #powershell_out!
#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory
#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family
Constructor Details
This class inherits a constructor from Chef::Provider
Instance Method Details
#action_checkout ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/chef/provider/git.rb', line 72
def action_checkout
if target_dir_non_existent_or_empty?
clone
if new_resource.enable_checkout
checkout
end
enable_submodules
add_remotes
else
Chef::Log.debug "#{new_resource} checkout destination #{cwd} already exists or is a non-empty directory"
end
end
|
#action_export ⇒ Object
85
86
87
88
89
90
|
# File 'lib/chef/provider/git.rb', line 85
def action_export
action_checkout
converge_by("complete the export by removing #{cwd}.git after checkout") do
FileUtils.rm_rf(::File.join(cwd, ".git"))
end
end
|
#action_sync ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/chef/provider/git.rb', line 92
def action_sync
if existing_git_clone?
Chef::Log.debug "#{new_resource} current revision: #{current_resource.revision} target revision: #{target_revision}"
unless current_revision_matches_target_revision?
fetch_updates
enable_submodules
Chef::Log.info "#{new_resource} updated to revision #{target_revision}"
end
add_remotes
else
action_checkout
end
end
|
#add_remotes ⇒ Object
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/chef/provider/git.rb', line 127
def add_remotes
if new_resource.additional_remotes.length > 0
new_resource.additional_remotes.each_pair do |remote_name, remote_url|
converge_by("add remote #{remote_name} from #{remote_url}") do
Chef::Log.info "#{new_resource} adding git remote #{remote_name} = #{remote_url}"
setup_remote_tracking_branches(remote_name, remote_url)
end
end
end
end
|
#checkout ⇒ Object
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/chef/provider/git.rb', line 154
def checkout
sha_ref = target_revision
converge_by("checkout ref #{sha_ref} branch #{new_resource.revision}") do
git("branch", "-f", new_resource.checkout_branch, sha_ref, cwd: cwd)
git("checkout", new_resource.checkout_branch, cwd: cwd)
Chef::Log.info "#{new_resource} checked out branch: #{new_resource.revision} onto: #{new_resource.checkout_branch} reference: #{sha_ref}"
end
end
|
#clone ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/chef/provider/git.rb', line 138
def clone
converge_by("clone from #{new_resource.repository} into #{cwd}") do
remote = new_resource.remote
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 << "\"#{new_resource.repository}\""
clone_cmd << "\"#{cwd}\""
Chef::Log.info "#{new_resource} cloning repo #{new_resource.repository} to #{cwd}"
git clone_cmd
end
end
|
#current_revision_matches_target_revision? ⇒ Boolean
217
218
219
|
# File 'lib/chef/provider/git.rb', line 217
def current_revision_matches_target_revision?
(!current_resource.revision.nil?) && (target_revision.strip.to_i(16) == current_resource.revision.strip.to_i(16))
end
|
#define_resource_requirements ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/chef/provider/git.rb', line 41
def define_resource_requirements
requirements.assert(:checkout, :sync) do |a|
dirname = ::File.dirname(cwd)
a.assertion { ::File.directory?(dirname) }
a.whyrun("Directory #{dirname} does not exist, this run will fail unless it has been previously created. Assuming it would have been created.")
a.failure_message(Chef::Exceptions::MissingParentDirectory,
"Cannot clone #{new_resource} to #{cwd}, the enclosing directory #{dirname} does not exist")
end
requirements.assert(:all_actions) do |a|
a.assertion { !(new_resource.revision =~ /^origin\//) }
a.failure_message Chef::Exceptions::InvalidRemoteGitReference,
"Deploying remote branches is not supported. " +
"Specify the remote branch as a local branch for " +
"the git repository you're deploying from " +
"(ie: '#{new_resource.revision.gsub('origin/', '')}' rather than '#{new_resource.revision}')."
end
requirements.assert(:all_actions) do |a|
a.assertion { !target_revision.nil? }
a.failure_message Chef::Exceptions::UnresolvableGitReference,
"Unable to parse SHA reference for '#{new_resource.revision}' in repository '#{new_resource.repository}'. " +
"Verify your (case-sensitive) repository URL and revision.\n" +
"`git ls-remote '#{new_resource.repository}' '#{rev_search_pattern}'` output: #{@resolved_reference}"
end
end
|
#enable_submodules ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/chef/provider/git.rb', line 165
def enable_submodules
if new_resource.enable_submodules
converge_by("enable git submodules for #{new_resource}") do
Chef::Log.info "#{new_resource} synchronizing git submodules"
git("submodule", "sync", cwd: cwd)
Chef::Log.info "#{new_resource} enabling git submodules"
git("submodule", "update", "--init", "--recursive", cwd: cwd)
end
end
end
|
#existing_git_clone? ⇒ Boolean
110
111
112
|
# File 'lib/chef/provider/git.rb', line 110
def existing_git_clone?
::File.exist?(::File.join(cwd, ".git"))
end
|
#fetch_updates ⇒ Object
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/chef/provider/git.rb', line 177
def fetch_updates
setup_remote_tracking_branches(new_resource.remote, new_resource.repository)
converge_by("fetch updates for #{new_resource.remote}") do
Chef::Log.debug "Fetching updates from #{new_resource.remote} and resetting to revision #{target_revision}"
git("fetch", new_resource.remote, cwd: cwd)
git("fetch", new_resource.remote, "--tags", cwd: cwd)
git("reset", "--hard", target_revision, cwd: cwd)
end
end
|
#find_current_revision ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/chef/provider/git.rb', line 118
def find_current_revision
Chef::Log.debug("#{new_resource} finding current git revision")
if ::File.exist?(::File.join(cwd, ".git"))
result = git("rev-parse", "HEAD", cwd: cwd, returns: [0, 128]).stdout.strip
end
sha_hash?(result) ? result : nil
end
|
#find_revision(refs, revision, suffix = "") ⇒ Object
257
258
259
260
261
262
|
# File 'lib/chef/provider/git.rb', line 257
def find_revision(refs, revision, suffix = "")
found = refs_search(refs, rev_match_pattern("refs/tags/", revision) + suffix)
found = refs_search(refs, rev_match_pattern("refs/heads/", revision) + suffix) if found.empty?
found = refs_search(refs, revision + suffix) if found.empty?
found
end
|
#git_ls_remote(rev_pattern) ⇒ Object
280
281
282
|
# File 'lib/chef/provider/git.rb', line 280
def git_ls_remote(rev_pattern)
git("ls-remote", "\"#{new_resource.repository}\"", "\"#{rev_pattern}\"").stdout
end
|
#git_minor_version ⇒ Object
106
107
108
|
# File 'lib/chef/provider/git.rb', line 106
def git_minor_version
@git_minor_version ||= Gem::Version.new( git("--version").stdout.split.last )
end
|
#load_current_resource ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/chef/provider/git.rb', line 33
def load_current_resource
@resolved_reference = nil
@current_resource = Chef::Resource::Git.new(new_resource.name)
if current_revision = find_current_revision
current_resource.revision current_revision
end
end
|
#multiple_remotes?(check_remote_command_result) ⇒ Boolean
209
210
211
|
# File 'lib/chef/provider/git.rb', line 209
def multiple_remotes?(check_remote_command_result)
check_remote_command_result.exitstatus == 2
end
|
#refs_search(refs, pattern) ⇒ Object
284
285
286
|
# File 'lib/chef/provider/git.rb', line 284
def refs_search(refs, pattern)
refs.find_all { |m| m[1] == pattern }
end
|
#remote_matches?(remote_url, check_remote_command_result) ⇒ Boolean
213
214
215
|
# File 'lib/chef/provider/git.rb', line 213
def remote_matches?(remote_url, check_remote_command_result)
check_remote_command_result.stdout.strip.eql?(remote_url)
end
|
#remote_resolve_reference ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/chef/provider/git.rb', line 233
def remote_resolve_reference
Chef::Log.debug("#{new_resource} resolving remote reference")
@resolved_reference = git_ls_remote(rev_search_pattern)
refs = @resolved_reference.split("\n").map { |line| line.split("\t") }
if rev_search_pattern != "HEAD"
found = find_revision(refs, new_resource.revision, "^{}")
else
found = refs_search(refs, "HEAD")
end
found = find_revision(refs, new_resource.revision) if found.empty?
found.size == 1 ? found.first[0] : nil
end
|
#rev_match_pattern(prefix, revision) ⇒ Object
264
265
266
267
268
269
270
|
# File 'lib/chef/provider/git.rb', line 264
def rev_match_pattern(prefix, revision)
if revision.start_with?(prefix)
revision
else
prefix + revision
end
end
|
#rev_search_pattern ⇒ Object
272
273
274
275
276
277
278
|
# File 'lib/chef/provider/git.rb', line 272
def rev_search_pattern
if ["", "HEAD"].include? new_resource.revision
"HEAD"
else
new_resource.revision + "*"
end
end
|
#setup_remote_tracking_branches(remote_name, remote_url) ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/chef/provider/git.rb', line 188
def setup_remote_tracking_branches(remote_name, remote_url)
converge_by("set up remote tracking branches for #{remote_url} at #{remote_name}") do
Chef::Log.debug "#{new_resource} configuring remote tracking branches for repository #{remote_url} " + "at remote #{remote_name}"
check_remote_command = ["config", "--get", "remote.#{remote_name}.url"]
remote_status = git(check_remote_command, cwd: cwd, returns: [0, 1, 2])
case remote_status.exitstatus
when 0, 2
if multiple_remotes?(remote_status) || !remote_matches?(remote_url, remote_status)
git("config", "--replace-all", "remote.#{remote_name}.url", remote_url, cwd: cwd)
end
when 1
git("remote", "add", remote_name, remote_url, cwd: cwd)
end
end
end
|
#target_dir_non_existent_or_empty? ⇒ Boolean
114
115
116
|
# File 'lib/chef/provider/git.rb', line 114
def target_dir_non_existent_or_empty?
!::File.exist?(cwd) || Dir.entries(cwd).sort == [".", ".."]
end
|
#target_revision ⇒ Object
Also known as:
revision_slug
221
222
223
224
225
226
227
228
229
|
# File 'lib/chef/provider/git.rb', line 221
def target_revision
@target_revision ||= begin
if sha_hash?(new_resource.revision)
@target_revision = new_resource.revision
else
@target_revision = remote_resolve_reference
end
end
end
|