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, :multiple_remotes
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', '--tags', @resource.value(:remote))
99 update_owner_and_excludes
105 def bare_git_config_exists?
106 File.exist?(File.join(@resource.value(:path), 'config'))
109 def clone_repository(source, path)
112 if @resource.value(:ensure) == :bare
115 if !File.exist?(File.join(@resource.value(:path), '.git'))
116 args.push(source, path)
117 git_with_identity(*args)
119 notice "Repo has already been cloned"
125 if @resource.value(:force)
126 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
129 raise Puppet::Error, "Could not create repository (non-repository at path)"
134 def init_repository(path)
136 if @resource.value(:ensure) == :bare && working_copy_exists?
137 convert_working_copy_to_bare
138 elsif @resource.value(:ensure) == :present && bare_exists?
139 convert_bare_to_working_copy
142 FileUtils.mkdir(@resource.value(:path))
144 if @resource.value(:ensure) == :bare
148 git_with_identity(*args)
153 # Convert working copy to bare
159 def convert_working_copy_to_bare
160 notice "Converting working copy repository to bare repository"
161 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
162 FileUtils.rm_rf(@resource.value(:path))
163 FileUtils.mv(tempdir, @resource.value(:path))
166 # Convert bare to working copy
172 def convert_bare_to_working_copy
173 notice "Converting bare repository to working copy repository"
174 FileUtils.mv(@resource.value(:path), tempdir)
175 FileUtils.mkdir(@resource.value(:path))
176 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
177 if commits_in?(File.join(@resource.value(:path), '.git'))
179 git_with_identity('checkout', '-f')
180 update_owner_and_excludes
184 def commits_in?(dot_git)
185 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
186 return true unless %w(. ..).include?(File::basename(e))
191 def checkout(revision = @resource.value(:revision))
192 if !local_branch_revision? && remote_branch_revision?
193 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
195 at_path { git_with_identity('checkout', '--force', revision) }
201 git_with_identity('reset', '--hard', desired)
205 def update_submodules
207 git_with_identity('submodule', 'init')
208 git_with_identity('submodule', 'update')
209 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'init')
210 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'update')
214 def remote_branch_revision?(revision = @resource.value(:revision))
215 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
216 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
217 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
223 def local_branch_revision?(revision = @resource.value(:revision))
224 at_path { branches.include?(revision) }
227 def tag_revision?(revision = @resource.value(:revision))
228 at_path { tags.include?(revision) }
232 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
236 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
240 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
244 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
247 def get_revision(rev)
248 if !working_copy_exists?
252 git_with_identity('fetch', @resource.value(:remote))
253 git_with_identity('fetch', '--tags', @resource.value(:remote))
255 current = at_path { git_with_identity('rev-parse', rev).strip }
256 if @resource.value(:revision)
257 if local_branch_revision?
258 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
259 elsif remote_branch_revision?
260 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
262 current = @resource.value(:revision) if current == canonical
264 update_owner_and_excludes
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