Class: Chef::Provider::Git
Constant Summary
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary
#current_resource, #new_resource, #run_context
Instance Method Summary
collapse
#run_command_compatible_options, #shell_out, #shell_out!
#action_nothing, build_from_file, #cookbook_name, #initialize, #node, #resource_collection
#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename
#enforce_ownership_and_permissions
#method_missing
#data_bag, #data_bag_item, #platform?, #platform_family?, #search, #value_for_platform, #value_for_platform_family
Constructor Details
This class inherits a constructor from Chef::Provider
Instance Method Details
#action_checkout ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/chef/provider/git.rb', line 39
def action_checkout
assert_target_directory_valid!
if target_dir_non_existent_or_empty?
clone
checkout
enable_submodules
add_remotes
@new_resource.updated_by_last_action(true)
else
Chef::Log.debug "#{@new_resource} checkout destination #{@new_resource.destination} already exists or is a non-empty directory"
end
end
|
#action_export ⇒ Object
53
54
55
56
57
|
# File 'lib/chef/provider/git.rb', line 53
def action_export
action_checkout
FileUtils.rm_rf(::File.join(@new_resource.destination,".git"))
@new_resource.updated_by_last_action(true)
end
|
#action_sync ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/chef/provider/git.rb', line 59
def action_sync
assert_target_directory_valid!
if existing_git_clone?
current_rev = find_current_revision
Chef::Log.debug "#{@new_resource} current revision: #{current_rev} target revision: #{target_revision}"
unless current_revision_matches_target_revision?
fetch_updates
enable_submodules
Chef::Log.info "#{@new_resource} updated to revision #{target_revision}"
@new_resource.updated_by_last_action(true)
end
add_remotes
else
action_checkout
@new_resource.updated_by_last_action(true)
end
end
|
#add_remotes ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/chef/provider/git.rb', line 103
def add_remotes
if (@new_resource.additional_remotes.length > 0)
@new_resource.additional_remotes.each_pair do |remote_name, remote_url|
Chef::Log.info "#{@new_resource} adding git remote #{remote_name} = #{remote_url}"
command = "git remote add #{remote_name} #{remote_url}"
if shell_out(command, run_options(:cwd => @new_resource.destination, :log_level => :info)).exitstatus != 0
@new_resource.updated_by_last_action(true)
end
end
end
end
|
#assert_target_directory_valid! ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/chef/provider/git.rb', line 78
def assert_target_directory_valid!
target_parent_directory = ::File.dirname(@new_resource.destination)
unless ::File.directory?(target_parent_directory)
msg = "Cannot clone #{@new_resource} to #{@new_resource.destination}, the enclosing directory #{target_parent_directory} does not exist"
raise Chef::Exceptions::MissingParentDirectory, msg
end
end
|
128
129
130
131
132
133
|
# File 'lib/chef/provider/git.rb', line 128
def checkout
sha_ref = target_revision
shell_out!("git checkout -b deploy #{sha_ref}", run_options(:cwd => @new_resource.destination))
Chef::Log.info "#{@new_resource} checked out branch: #{@new_resource.revision} reference: #{sha_ref}"
end
|
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/chef/provider/git.rb', line 115
def clone
remote = @new_resource.remote
args = []
args << "-o #{remote}" unless remote == 'origin'
args << "--depth #{@new_resource.depth}" if @new_resource.depth
Chef::Log.info "#{@new_resource} cloning repo #{@new_resource.repository} to #{@new_resource.destination}"
clone_cmd = "git clone #{args.join(' ')} #{@new_resource.repository} #{Shellwords.escape @new_resource.destination}"
shell_out!(clone_cmd, run_options(:log_level => :info))
end
|
#current_revision_matches_target_revision? ⇒ Boolean
169
170
171
|
# File 'lib/chef/provider/git.rb', line 169
def current_revision_matches_target_revision?
(!@current_resource.revision.nil?) && (target_revision.strip.to_i(16) == @current_resource.revision.strip.to_i(16))
end
|
#enable_submodules ⇒ Object
135
136
137
138
139
140
141
142
|
# File 'lib/chef/provider/git.rb', line 135
def enable_submodules
if @new_resource.enable_submodules
Chef::Log.info "#{@new_resource} enabling git submodules"
command = "git submodule update --init --recursive"
shell_out!(command, run_options(:cwd => @new_resource.destination, :log_level => :info))
end
end
|
#existing_git_clone? ⇒ Boolean
86
87
88
|
# File 'lib/chef/provider/git.rb', line 86
def existing_git_clone?
::File.exist?(::File.join(@new_resource.destination, ".git"))
end
|
#fetch_updates ⇒ Object
144
145
146
147
148
149
150
151
|
# File 'lib/chef/provider/git.rb', line 144
def fetch_updates
setup_remote_tracking_branches if @new_resource.remote != 'origin'
fetch_command = "git fetch #{@new_resource.remote} && git fetch #{@new_resource.remote} --tags && git reset --hard #{target_revision}"
Chef::Log.debug "Fetching updates from #{new_resource.remote} and resetting to revision #{target_revision}"
shell_out!(fetch_command, run_options(:cwd => @new_resource.destination))
end
|
#find_current_revision ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/chef/provider/git.rb', line 94
def find_current_revision
Chef::Log.debug("#{@new_resource} finding current git revision")
if ::File.exist?(::File.join(cwd, ".git"))
result = shell_out!('git rev-parse HEAD', :cwd => cwd, :returns => [0,128]).stdout.strip
end
sha_hash?(result) ? result : nil
end
|
#load_current_resource ⇒ Object
32
33
34
35
36
37
|
# File 'lib/chef/provider/git.rb', line 32
def load_current_resource
@current_resource = Chef::Resource::Git.new(@new_resource.name)
if current_revision = find_current_revision
@current_resource.revision current_revision
end
end
|
#remote_resolve_reference ⇒ Object
188
189
190
191
192
|
# File 'lib/chef/provider/git.rb', line 188
def remote_resolve_reference
Chef::Log.debug("#{@new_resource} resolving remote reference")
command = git('ls-remote', @new_resource.repository, @new_resource.revision)
shell_out!(command, run_options).stdout
end
|
#setup_remote_tracking_branches ⇒ Object
Use git-config to setup a remote tracking branches. Could use git-remote but it complains when a remote of the same name already exists, git-config will just silenty overwrite the setting every time. This could cause wierd-ness in the remote cache if the url changes between calls, but as long as the repositories are all based from each other it should still work fine.
159
160
161
162
163
164
165
166
167
|
# File 'lib/chef/provider/git.rb', line 159
def setup_remote_tracking_branches
command = []
Chef::Log.debug "#{@new_resource} configuring remote tracking branches for repository #{@new_resource.repository} "+
"at remote #{@new_resource.remote}"
command << "git config remote.#{@new_resource.remote}.url #{@new_resource.repository}"
command << "git config remote.#{@new_resource.remote}.fetch +refs/heads/*:refs/remotes/#{@new_resource.remote}/*"
shell_out!(command.join(" && "), run_options(:cwd => @new_resource.destination))
end
|
#target_dir_non_existent_or_empty? ⇒ Boolean
90
91
92
|
# File 'lib/chef/provider/git.rb', line 90
def target_dir_non_existent_or_empty?
!::File.exist?(@new_resource.destination) || Dir.entries(@new_resource.destination).sort == ['.','..']
end
|
#target_revision ⇒ Object
Also known as:
revision_slug
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/chef/provider/git.rb', line 173
def target_revision
@target_revision ||= begin
assert_revision_not_remote
if sha_hash?(@new_resource.revision)
@target_revision = @new_resource.revision
else
resolved_reference = remote_resolve_reference
@target_revision = (resolved_reference)
end
end
end
|