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 # if it's not a tag, look for it as a local ref
60 canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp }
62 # git rev-parse executed properly but didn't find the ref;
63 # look for it in the remote
64 remote_ref = at_path { git_with_identity('ls-remote', '--heads', '--tags', @resource.value(:remote), @resource.value(:revision)).chomp }
66 fail("#{@resource.value(:revision)} is not a local or remote ref")
69 # $ git ls-remote --heads --tags origin feature/cvs
70 # 7d4244b35e72904e30130cad6d2258f901c16f1a refs/heads/feature/cvs
71 canonical = remote_ref.split.first
75 if current == canonical
76 @resource.value(:revision)
82 def revision=(desired)
84 if local_branch_revision?(desired)
85 # reset instead of pull to avoid merge conflicts. assuming remote is
87 # might be worthwhile to have an allow_local_changes param to decide
88 # whether to reset or pull when we're ensuring latest.
89 at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") }
91 if @resource.value(:ensure) != :bare
94 update_owner_and_excludes
98 bare_git_config_exists? && !working_copy_exists?
101 def working_copy_exists?
102 File.directory?(File.join(@resource.value(:path), '.git'))
106 working_copy_exists? || bare_exists?
109 def update_remote_origin_url
110 current = git_with_identity('config', 'remote.origin.url')
111 unless @resource.value(:source).nil?
112 if current.nil? or current.strip != @resource.value(:source)
113 git_with_identity('config', 'remote.origin.url', @resource.value(:source))
118 def update_references
120 update_remote_origin_url
121 git_with_identity('fetch', @resource.value(:remote))
122 git_with_identity('fetch', '--tags', @resource.value(:remote))
123 update_owner_and_excludes
129 def bare_git_config_exists?
130 File.exist?(File.join(@resource.value(:path), 'config'))
133 def clone_repository(source, path)
136 if @resource.value(:ensure) == :bare
139 if !File.exist?(File.join(@resource.value(:path), '.git'))
140 args.push(source, path)
142 git_with_identity(*args)
145 notice "Repo has already been cloned"
151 if @resource.value(:force)
152 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
155 raise Puppet::Error, "Could not create repository (non-repository at path)"
160 def init_repository(path)
162 if @resource.value(:ensure) == :bare && working_copy_exists?
163 convert_working_copy_to_bare
164 elsif @resource.value(:ensure) == :present && bare_exists?
165 convert_bare_to_working_copy
168 FileUtils.mkdir(@resource.value(:path))
169 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
171 if @resource.value(:ensure) == :bare
175 git_with_identity(*args)
180 # Convert working copy to bare
186 def convert_working_copy_to_bare
187 notice "Converting working copy repository to bare repository"
188 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
189 FileUtils.rm_rf(@resource.value(:path))
190 FileUtils.mv(tempdir, @resource.value(:path))
193 # Convert bare to working copy
199 def convert_bare_to_working_copy
200 notice "Converting bare repository to working copy repository"
201 FileUtils.mv(@resource.value(:path), tempdir)
202 FileUtils.mkdir(@resource.value(:path))
203 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
204 if commits_in?(File.join(@resource.value(:path), '.git'))
206 git_with_identity('checkout', '--force')
207 update_owner_and_excludes
211 def commits_in?(dot_git)
212 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
213 return true unless %w(. ..).include?(File::basename(e))
218 def checkout(revision = @resource.value(:revision))
219 if !local_branch_revision? && remote_branch_revision?
220 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
222 at_path { git_with_identity('checkout', '--force', revision) }
228 git_with_identity('reset', '--hard', desired)
232 def update_submodules
234 git_with_identity('submodule', 'update', '--init', '--recursive')
238 def remote_branch_revision?(revision = @resource.value(:revision))
239 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
240 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
241 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
247 def local_branch_revision?(revision = @resource.value(:revision))
248 at_path { branches.include?(revision) }
251 def tag_revision?(revision = @resource.value(:revision))
252 at_path { tags.include?(revision) }
256 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
260 at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
264 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
268 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
271 def get_revision(rev)
272 if !working_copy_exists?
276 update_remote_origin_url
277 git_with_identity('fetch', @resource.value(:remote))
278 git_with_identity('fetch', '--tags', @resource.value(:remote))
280 current = at_path { git_with_identity('rev-parse', rev).strip }
281 if @resource.value(:revision)
282 if local_branch_revision?
283 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
284 elsif remote_branch_revision?
285 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
287 current = @resource.value(:revision) if current == canonical
289 update_owner_and_excludes
293 def update_owner_and_excludes
294 if @resource.value(:owner) or @resource.value(:group)
297 if @resource.value(:excludes)
302 def git_with_identity(*args)
303 if @resource.value(:identity)
304 Tempfile.open('git-helper') do |f|
306 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
309 FileUtils.chmod(0755, f.path)
310 env_save = ENV['GIT_SSH']
311 ENV['GIT_SSH'] = f.path
315 ENV['GIT_SSH'] = env_save
319 elsif @resource.value(:user)
320 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )