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'
8 defaultfor :git => :exists
9 has_features :bare_repositories, :reference_tracking, :ssh_identity
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('origin/HEAD')
44 elsif branch == '(no branch)'
45 return get_revision('HEAD')
47 return get_revision('origin/%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)
70 checkout_or_reset(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', "origin/#{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', '--tags', 'origin')
104 def bare_git_config_exists?
105 File.exist?(File.join(@resource.value(:path), 'config'))
108 def clone_repository(source, path)
111 if @resource.value(:ensure) == :bare
114 if !File.exist?(File.join(@resource.value(:path), '.git'))
115 args.push(source, path)
116 git_with_identity(*args)
118 notice "Repo has already been cloned"
124 if @resource.value(:force)
125 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
128 raise Puppet::Error, "Could not create repository (non-repository at path)"
133 def init_repository(path)
135 if @resource.value(:ensure) == :bare && working_copy_exists?
136 convert_working_copy_to_bare
137 elsif @resource.value(:ensure) == :present && bare_exists?
138 convert_bare_to_working_copy
141 FileUtils.mkdir(@resource.value(:path))
143 if @resource.value(:ensure) == :bare
147 git_with_identity(*args)
152 # Convert working copy to bare
158 def convert_working_copy_to_bare
159 notice "Converting working copy repository to bare repository"
160 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
161 FileUtils.rm_rf(@resource.value(:path))
162 FileUtils.mv(tempdir, @resource.value(:path))
165 # Convert bare to working copy
171 def convert_bare_to_working_copy
172 notice "Converting bare repository to working copy repository"
173 FileUtils.mv(@resource.value(:path), tempdir)
174 FileUtils.mkdir(@resource.value(:path))
175 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
176 if commits_in?(File.join(@resource.value(:path), '.git'))
178 git_with_identity('checkout', '-f')
179 update_owner_and_excludes
183 def commits_in?(dot_git)
184 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
185 return true unless %w(. ..).include?(File::basename(e))
190 def checkout_or_reset(revision = @resource.value(:revision))
191 if local_branch_revision?
194 at_path { git_with_identity('checkout', revision) }
195 elsif remote_branch_revision?
196 at_path { git_with_identity('checkout', '-b', revision, '--track', "origin/#{revision}") }
202 git_with_identity('reset', '--hard', desired)
206 def update_submodules
208 git_with_identity('submodule', 'init')
209 git_with_identity('submodule', 'update')
210 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'init')
211 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'update')
215 def remote_branch_revision?(revision = @resource.value(:revision))
216 # git < 1.6 returns 'origin/#{revision}'
217 # git 1.6+ returns 'remotes/origin/#{revision}'
218 branch = at_path { branches.grep /(remotes\/)?origin\/#{revision}/ }
224 def local_branch_revision?(revision = @resource.value(:revision))
225 at_path { branches.include?(revision) }
228 def tag_revision?(revision = @resource.value(:revision))
229 at_path { tags.include?(revision) }
233 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
237 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
241 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
245 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
248 def get_revision(rev)
249 if !working_copy_exists?
253 git_with_identity('fetch', 'origin')
254 git_with_identity('fetch', '--tags', 'origin')
256 current = at_path { git_with_identity('rev-parse', rev).strip }
257 if @resource.value(:revision)
258 if local_branch_revision?
259 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
260 elsif remote_branch_revision?
261 canonical = at_path { git_with_identity('rev-parse', 'origin/' + @resource.value(:revision)).strip }
263 current = @resource.value(:revision) if current == canonical
268 def update_owner_and_excludes
269 if @resource.value(:owner) or @resource.value(:group)
272 if @resource.value(:excludes)
277 def git_with_identity(*args)
278 if @resource.value(:identity)
279 Tempfile.open('git-helper') do |f|
281 f.puts "exec ssh -i #{@resource.value(:identity)} $*"
284 FileUtils.chmod(0755, f.path)
285 env_save = ENV['GIT_SSH']
286 ENV['GIT_SSH'] = f.path
290 ENV['GIT_SSH'] = env_save