1 require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
3 Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) do
4 desc "Supports Git repositories"
6 ##TODO modify the commands below so that the su - is included
7 optional_commands :git => 'git',
9 has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user
12 if !@resource.value(:source)
13 init_repository(@resource.value(:path))
15 clone_repository(@resource.value(:source), @resource.value(:path))
16 if @resource.value(:revision)
17 if @resource.value(:ensure) == :bare
18 notice "Ignoring revision for bare repository"
23 if @resource.value(:ensure) != :bare
27 update_owner_and_excludes
31 FileUtils.rm_rf(@resource.value(:path))
36 return self.revision == self.latest
43 return get_revision("#{@resource.value(:remote)}/HEAD")
44 elsif branch == '(no branch)'
45 return get_revision('HEAD')
47 return get_revision("#{@resource.value(:remote)}/%s" % branch)
53 current = at_path { git_with_identity('rev-parse', 'HEAD').chomp }
54 return current unless @resource.value(:revision)
56 if tag_revision?(@resource.value(:revision))
57 canonical = at_path { git_with_identity('show', @resource.value(:revision)).scan(/commit (.*)/).to_s }
59 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp }
62 if current == canonical
63 @resource.value(:revision)
69 def revision=(desired)
71 if local_branch_revision?(desired)
72 # reset instead of pull to avoid merge conflicts. assuming remote is
74 # might be worthwhile to have an allow_local_changes param to decide
75 # whether to reset or pull when we're ensuring latest.
76 at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") }
78 if @resource.value(:ensure) != :bare
81 update_owner_and_excludes
85 bare_git_config_exists? && !working_copy_exists?
88 def working_copy_exists?
89 File.directory?(File.join(@resource.value(:path), '.git'))
93 working_copy_exists? || bare_exists?
98 git_with_identity('fetch', @resource.value(:remote))
99 git_with_identity('fetch', '--tags', @resource.value(:remote))
100 update_owner_and_excludes
106 def bare_git_config_exists?
107 File.exist?(File.join(@resource.value(:path), 'config'))
110 def clone_repository(source, path)
113 if @resource.value(:ensure) == :bare
116 if !File.exist?(File.join(@resource.value(:path), '.git'))
117 args.push(source, path)
119 git_with_identity(*args)
122 notice "Repo has already been cloned"
128 if @resource.value(:force)
129 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
132 raise Puppet::Error, "Could not create repository (non-repository at path)"
137 def init_repository(path)
139 if @resource.value(:ensure) == :bare && working_copy_exists?
140 convert_working_copy_to_bare
141 elsif @resource.value(:ensure) == :present && bare_exists?
142 convert_bare_to_working_copy
145 FileUtils.mkdir(@resource.value(:path))
146 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
148 if @resource.value(:ensure) == :bare
152 git_with_identity(*args)
157 # Convert working copy to bare
163 def convert_working_copy_to_bare
164 notice "Converting working copy repository to bare repository"
165 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
166 FileUtils.rm_rf(@resource.value(:path))
167 FileUtils.mv(tempdir, @resource.value(:path))
170 # Convert bare to working copy
176 def convert_bare_to_working_copy
177 notice "Converting bare repository to working copy repository"
178 FileUtils.mv(@resource.value(:path), tempdir)
179 FileUtils.mkdir(@resource.value(:path))
180 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
181 if commits_in?(File.join(@resource.value(:path), '.git'))
183 git_with_identity('checkout', '-f')
184 update_owner_and_excludes
188 def commits_in?(dot_git)
189 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
190 return true unless %w(. ..).include?(File::basename(e))
195 def checkout(revision = @resource.value(:revision))
196 if !local_branch_revision? && remote_branch_revision?
197 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
199 at_path { git_with_identity('checkout', '--force', revision) }
205 git_with_identity('reset', '--hard', desired)
209 def update_submodules
211 git_with_identity('submodule', 'init')
212 git_with_identity('submodule', 'update')
213 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'init')
214 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'update')
218 def remote_branch_revision?(revision = @resource.value(:revision))
219 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
220 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
221 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
227 def local_branch_revision?(revision = @resource.value(:revision))
228 at_path { branches.include?(revision) }
231 def tag_revision?(revision = @resource.value(:revision))
232 at_path { tags.include?(revision) }
236 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
240 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
244 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
248 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
251 def get_revision(rev)
252 if !working_copy_exists?
256 git_with_identity('fetch', @resource.value(:remote))
257 git_with_identity('fetch', '--tags', @resource.value(:remote))
259 current = at_path { git_with_identity('rev-parse', rev).strip }
260 if @resource.value(:revision)
261 if local_branch_revision?
262 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
263 elsif remote_branch_revision?
264 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
266 current = @resource.value(:revision) if current == canonical
268 update_owner_and_excludes
272 def update_owner_and_excludes
273 if @resource.value(:owner) or @resource.value(:group)
276 if @resource.value(:excludes)
281 def git_with_identity(*args)
282 if @resource.value(:identity)
283 Tempfile.open('git-helper') do |f|
285 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -i #{@resource.value(:identity)} $*"
288 FileUtils.chmod(0755, f.path)
289 env_save = ENV['GIT_SSH']
290 ENV['GIT_SSH'] = f.path
294 ENV['GIT_SSH'] = env_save
298 elsif @resource.value(:user)
299 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )