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?
96 def update_remote_origin_url
97 current = git_with_identity('config', 'remote.origin.url')
98 unless @resource.value(:source).nil?
99 if current.nil? or current.strip != @resource.value(:source)
100 git_with_identity('config', 'remote.origin.url', @resource.value(:source))
105 def update_references
107 update_remote_origin_url
108 git_with_identity('fetch', @resource.value(:remote))
109 git_with_identity('fetch', '--tags', @resource.value(:remote))
110 update_owner_and_excludes
116 def bare_git_config_exists?
117 File.exist?(File.join(@resource.value(:path), 'config'))
120 def clone_repository(source, path)
123 if @resource.value(:ensure) == :bare
126 if !File.exist?(File.join(@resource.value(:path), '.git'))
127 args.push(source, path)
129 git_with_identity(*args)
132 notice "Repo has already been cloned"
138 if @resource.value(:force)
139 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
142 raise Puppet::Error, "Could not create repository (non-repository at path)"
147 def init_repository(path)
149 if @resource.value(:ensure) == :bare && working_copy_exists?
150 convert_working_copy_to_bare
151 elsif @resource.value(:ensure) == :present && bare_exists?
152 convert_bare_to_working_copy
155 FileUtils.mkdir(@resource.value(:path))
156 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
158 if @resource.value(:ensure) == :bare
162 git_with_identity(*args)
167 # Convert working copy to bare
173 def convert_working_copy_to_bare
174 notice "Converting working copy repository to bare repository"
175 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
176 FileUtils.rm_rf(@resource.value(:path))
177 FileUtils.mv(tempdir, @resource.value(:path))
180 # Convert bare to working copy
186 def convert_bare_to_working_copy
187 notice "Converting bare repository to working copy repository"
188 FileUtils.mv(@resource.value(:path), tempdir)
189 FileUtils.mkdir(@resource.value(:path))
190 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
191 if commits_in?(File.join(@resource.value(:path), '.git'))
193 git_with_identity('checkout', '-f')
194 update_owner_and_excludes
198 def commits_in?(dot_git)
199 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
200 return true unless %w(. ..).include?(File::basename(e))
205 def checkout(revision = @resource.value(:revision))
206 if !local_branch_revision? && remote_branch_revision?
207 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
209 at_path { git_with_identity('checkout', '--force', revision) }
215 git_with_identity('reset', '--hard', desired)
219 def update_submodules
221 git_with_identity('submodule', 'init')
222 git_with_identity('submodule', 'update')
223 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'init')
224 git_with_identity('submodule', 'foreach', 'git', 'submodule', 'update')
228 def remote_branch_revision?(revision = @resource.value(:revision))
229 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
230 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
231 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
237 def local_branch_revision?(revision = @resource.value(:revision))
238 at_path { branches.include?(revision) }
241 def tag_revision?(revision = @resource.value(:revision))
242 at_path { tags.include?(revision) }
246 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
250 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
254 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
258 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
261 def get_revision(rev)
262 if !working_copy_exists?
266 update_remote_origin_url
267 git_with_identity('fetch', @resource.value(:remote))
268 git_with_identity('fetch', '--tags', @resource.value(:remote))
270 current = at_path { git_with_identity('rev-parse', rev).strip }
271 if @resource.value(:revision)
272 if local_branch_revision?
273 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
274 elsif remote_branch_revision?
275 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
277 current = @resource.value(:revision) if current == canonical
279 update_owner_and_excludes
283 def update_owner_and_excludes
284 if @resource.value(:owner) or @resource.value(:group)
287 if @resource.value(:excludes)
292 def git_with_identity(*args)
293 if @resource.value(:identity)
294 Tempfile.open('git-helper') do |f|
296 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
299 FileUtils.chmod(0755, f.path)
300 env_save = ENV['GIT_SSH']
301 ENV['GIT_SSH'] = f.path
305 ENV['GIT_SSH'] = env_save
309 elsif @resource.value(:user)
310 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )