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)
118 git_with_identity(*args)
120 notice "Repo has already been cloned"
126 if @resource.value(:force)
127 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
130 raise Puppet::Error, "Could not create repository (non-repository at path)"
135 def init_repository(path)
137 if @resource.value(:ensure) == :bare && working_copy_exists?
138 convert_working_copy_to_bare
139 elsif @resource.value(:ensure) == :present && bare_exists?
140 convert_bare_to_working_copy
143 FileUtils.mkdir(@resource.value(:path))
145 if @resource.value(:ensure) == :bare
149 git_with_identity(*args)
154 # Convert working copy to bare
160 def convert_working_copy_to_bare
161 notice "Converting working copy repository to bare repository"
162 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
163 FileUtils.rm_rf(@resource.value(:path))
164 FileUtils.mv(tempdir, @resource.value(:path))
167 # Convert bare to working copy
173 def convert_bare_to_working_copy
174 notice "Converting bare repository to working copy repository"
175 FileUtils.mv(@resource.value(:path), tempdir)
176 FileUtils.mkdir(@resource.value(:path))
177 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
178 if commits_in?(File.join(@resource.value(:path), '.git'))
180 git_with_identity('checkout', '-f')
181 update_owner_and_excludes
185 def commits_in?(dot_git)
186 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
187 return true unless %w(. ..).include?(File::basename(e))
192 def checkout(revision = @resource.value(:revision))
193 if !local_branch_revision? && remote_branch_revision?
194 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
196 at_path { git_with_identity('checkout', '--force', revision) }
202 git_with_identity('reset', '--hard', desired)
206 def update_submodules
208 git_with_identity('submodule', 'update', '--init', '--recursive')
212 def remote_branch_revision?(revision = @resource.value(:revision))
213 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
214 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
215 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
221 def local_branch_revision?(revision = @resource.value(:revision))
222 at_path { branches.include?(revision) }
225 def tag_revision?(revision = @resource.value(:revision))
226 at_path { tags.include?(revision) }
230 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
234 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
238 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
242 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
245 def get_revision(rev)
246 if !working_copy_exists?
250 git_with_identity('fetch', @resource.value(:remote))
251 git_with_identity('fetch', '--tags', @resource.value(:remote))
253 current = at_path { git_with_identity('rev-parse', rev).strip }
254 if @resource.value(:revision)
255 if local_branch_revision?
256 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
257 elsif remote_branch_revision?
258 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
260 current = @resource.value(:revision) if current == canonical
262 update_owner_and_excludes
266 def update_owner_and_excludes
267 if @resource.value(:owner) or @resource.value(:group)
270 if @resource.value(:excludes)
275 def git_with_identity(*args)
276 if @resource.value(:identity)
277 Tempfile.open('git-helper') do |f|
279 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -i #{@resource.value(:identity)} $*"
282 FileUtils.chmod(0755, f.path)
283 env_save = ENV['GIT_SSH']
284 ENV['GIT_SSH'] = f.path
288 ENV['GIT_SSH'] = env_save
292 elsif @resource.value(:user)
293 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )