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 defaultfor :git => :exists
10 has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user
13 if !@resource.value(:source)
14 init_repository(@resource.value(:path))
16 clone_repository(@resource.value(:source), @resource.value(:path))
17 if @resource.value(:revision)
18 if @resource.value(:ensure) == :bare
19 notice "Ignoring revision for bare repository"
24 if @resource.value(:ensure) != :bare
28 update_owner_and_excludes
32 FileUtils.rm_rf(@resource.value(:path))
37 return self.revision == self.latest
44 return get_revision("#{@resource.value(:remote)}/HEAD")
45 elsif branch == '(no branch)'
46 return get_revision('HEAD')
48 return get_revision("#{@resource.value(:remote)}/%s" % branch)
54 current = at_path { git_with_identity('rev-parse', 'HEAD').chomp }
55 return current unless @resource.value(:revision)
57 if tag_revision?(@resource.value(:revision))
58 canonical = at_path { git_with_identity('show', @resource.value(:revision)).scan(/commit (.*)/).to_s }
60 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp }
63 if current == canonical
64 @resource.value(:revision)
70 def revision=(desired)
72 if local_branch_revision?(desired)
73 # reset instead of pull to avoid merge conflicts. assuming remote is
75 # might be worthwhile to have an allow_local_changes param to decide
76 # whether to reset or pull when we're ensuring latest.
77 at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") }
79 if @resource.value(:ensure) != :bare
82 update_owner_and_excludes
86 bare_git_config_exists? && !working_copy_exists?
89 def working_copy_exists?
90 File.directory?(File.join(@resource.value(:path), '.git'))
94 working_copy_exists? || bare_exists?
100 git_with_identity('pull', @resource.value(:remote))
101 update_owner_and_excludes
107 def bare_git_config_exists?
108 File.exist?(File.join(@resource.value(:path), 'config'))
111 def clone_repository(source, path)
114 if @resource.value(:ensure) == :bare
117 if !File.exist?(File.join(@resource.value(:path), '.git'))
118 args.push(source, path)
119 git_with_identity(*args)
121 notice "Repo has already been cloned"
127 if @resource.value(:force)
128 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
131 raise Puppet::Error, "Could not create repository (non-repository at path)"
136 def init_repository(path)
138 if @resource.value(:ensure) == :bare && working_copy_exists?
139 convert_working_copy_to_bare
140 elsif @resource.value(:ensure) == :present && bare_exists?
141 convert_bare_to_working_copy
144 FileUtils.mkdir(@resource.value(:path))
146 if @resource.value(:ensure) == :bare
150 git_with_identity(*args)
155 # Convert working copy to bare
161 def convert_working_copy_to_bare
162 notice "Converting working copy repository to bare repository"
163 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
164 FileUtils.rm_rf(@resource.value(:path))
165 FileUtils.mv(tempdir, @resource.value(:path))
168 # Convert bare to working copy
174 def convert_bare_to_working_copy
175 notice "Converting bare repository to working copy repository"
176 FileUtils.mv(@resource.value(:path), tempdir)
177 FileUtils.mkdir(@resource.value(:path))
178 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
179 if commits_in?(File.join(@resource.value(:path), '.git'))
181 git_with_identity('checkout', '-f')
182 update_owner_and_excludes
186 def commits_in?(dot_git)
187 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
188 return true unless %w(. ..).include?(File::basename(e))
193 def checkout(revision = @resource.value(:revision))
194 if !local_branch_revision? && remote_branch_revision?
195 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
197 at_path { git_with_identity('checkout', '--force', revision) }
203 git_with_identity('reset', '--hard', desired)
207 def update_submodules
209 git_with_identity('submodule', 'init')
210 git_with_identity('submodule', 'update')
211 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'init')
212 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'update')
216 def remote_branch_revision?(revision = @resource.value(:revision))
217 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
218 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
219 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
225 def local_branch_revision?(revision = @resource.value(:revision))
226 at_path { branches.include?(revision) }
229 def tag_revision?(revision = @resource.value(:revision))
230 at_path { tags.include?(revision) }
234 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
238 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
242 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
246 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
249 def get_revision(rev)
250 if !working_copy_exists?
254 git_with_identity('fetch', @resource.value(:remote))
255 git_with_identity('fetch', '--tags', @resource.value(:remote))
257 current = at_path { git_with_identity('rev-parse', rev).strip }
258 if @resource.value(:revision)
259 if local_branch_revision?
260 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
261 elsif remote_branch_revision?
262 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
264 current = @resource.value(:revision) if current == canonical
266 update_owner_and_excludes
270 def update_owner_and_excludes
271 if @resource.value(:owner) or @resource.value(:group)
274 if @resource.value(:excludes)
279 def git_with_identity(*args)
280 if @resource.value(:identity)
281 Tempfile.open('git-helper') do |f|
283 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -i #{@resource.value(:identity)} $*"
286 FileUtils.chmod(0755, f.path)
287 env_save = ENV['GIT_SSH']
288 ENV['GIT_SSH'] = f.path
292 ENV['GIT_SSH'] = env_save
296 elsif @resource.value(:user)
297 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )