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, :depth
12 if @resource.value(:revision) and @resource.value(:ensure) == :bare
13 fail("Cannot set a revision (#{@resource.value(:revision)}) on a bare repository")
15 if !@resource.value(:source)
16 init_repository(@resource.value(:path))
18 clone_repository(@resource.value(:source), @resource.value(:path))
19 if @resource.value(:revision)
22 if @resource.value(:ensure) != :bare
26 update_owner_and_excludes
30 FileUtils.rm_rf(@resource.value(:path))
35 return self.revision == self.latest
42 return get_revision('HEAD')
44 return get_revision("#{@resource.value(:remote)}/#{branch}")
50 current = at_path { git_with_identity('rev-parse', 'HEAD').chomp }
51 return current unless @resource.value(:revision)
53 if tag_revision?(@resource.value(:revision))
54 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp }
56 # if it's not a tag, look for it as a local ref
57 canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp }
59 # git rev-parse executed properly but didn't find the ref;
60 # look for it in the remote
61 remote_ref = at_path { git_with_identity('ls-remote', '--heads', '--tags', @resource.value(:remote), @resource.value(:revision)).chomp }
63 fail("#{@resource.value(:revision)} is not a local or remote ref")
66 # $ git ls-remote --heads --tags origin feature/cvs
67 # 7d4244b35e72904e30130cad6d2258f901c16f1a refs/heads/feature/cvs
68 canonical = remote_ref.split.first
72 if current == canonical
73 @resource.value(:revision)
79 def revision=(desired)
81 if local_branch_revision?
82 # reset instead of pull to avoid merge conflicts. assuming remote is
84 # might be worthwhile to have an allow_local_changes param to decide
85 # whether to reset or pull when we're ensuring latest.
87 git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}")
89 git_with_identity('checkout', "#{@resource.value(:revision)}")
90 git_with_identity('pull')
94 if @resource.value(:ensure) != :bare
97 update_owner_and_excludes
101 bare_git_config_exists? && !working_copy_exists?
104 def working_copy_exists?
105 File.directory?(File.join(@resource.value(:path), '.git'))
109 working_copy_exists? || bare_exists?
112 def update_remote_origin_url
113 current = git_with_identity('config', "remote.#{@resource.value(:remote)}.url")
114 unless @resource.value(:source).nil?
115 if current.nil? or current.strip != @resource.value(:source)
116 git_with_identity('config', "remote.#{@resource.value(:remote)}.url", @resource.value(:source))
121 def update_references
123 update_remote_origin_url
124 git_with_identity('fetch', @resource.value(:remote))
125 git_with_identity('fetch', '--tags', @resource.value(:remote))
126 update_owner_and_excludes
132 def bare_git_config_exists?
133 File.exist?(File.join(@resource.value(:path), 'config'))
136 def clone_repository(source, path)
139 if @resource.value(:depth) and @resource.value(:depth).to_i > 0
140 args.push('--depth', @resource.value(:depth).to_s)
142 if @resource.value(:ensure) == :bare
145 if @resource.value(:remote) != 'origin'
146 args.push('--origin', @resource.value(:remote))
148 if !working_copy_exists?
149 args.push(source, path)
151 git_with_identity(*args)
154 notice "Repo has already been cloned"
159 if path_exists? and not path_empty?
160 if @resource.value(:force)
161 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
164 raise Puppet::Error, "Could not create repository (non-repository at path)"
169 def init_repository(path)
171 if @resource.value(:ensure) == :bare && working_copy_exists?
172 convert_working_copy_to_bare
173 elsif @resource.value(:ensure) == :present && bare_exists?
174 convert_bare_to_working_copy
177 FileUtils.mkdir(@resource.value(:path))
178 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
180 if @resource.value(:ensure) == :bare
184 git_with_identity(*args)
189 # Convert working copy to bare
195 def convert_working_copy_to_bare
196 notice "Converting working copy repository to bare repository"
197 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
198 FileUtils.rm_rf(@resource.value(:path))
199 FileUtils.mv(tempdir, @resource.value(:path))
202 # Convert bare to working copy
208 def convert_bare_to_working_copy
209 notice "Converting bare repository to working copy repository"
210 FileUtils.mv(@resource.value(:path), tempdir)
211 FileUtils.mkdir(@resource.value(:path))
212 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
213 if commits_in?(File.join(@resource.value(:path), '.git'))
215 git_with_identity('checkout', '--force')
216 update_owner_and_excludes
220 def commits_in?(dot_git)
221 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
222 return true unless %w(. ..).include?(File::basename(e))
227 def checkout(revision = @resource.value(:revision))
228 if !local_branch_revision? && remote_branch_revision?
229 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
231 at_path { git_with_identity('checkout', '--force', revision) }
237 git_with_identity('reset', '--hard', desired)
241 def update_submodules
243 git_with_identity('submodule', 'update', '--init', '--recursive')
247 def remote_branch_revision?(revision = @resource.value(:revision))
248 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
249 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
250 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
251 branch unless branch.empty?
254 def local_branch_revision?(revision = @resource.value(:revision))
255 at_path { branches.include?(revision) }
258 def tag_revision?(revision = @resource.value(:revision))
259 at_path { tags.include?(revision) }
263 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
268 matches = git_with_identity('branch', '-a').match /\*\s+(.*)/
269 matches[1] unless matches[1].match /detached/
275 git_with_identity('branch', '-a').match /\*\s+\(detached from.*\)/
280 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
284 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
287 def get_revision(rev)
288 if @resource.value(:force) && working_copy_exists?
291 if !working_copy_exists?
295 update_remote_origin_url
296 git_with_identity('fetch', @resource.value(:remote))
297 git_with_identity('fetch', '--tags', @resource.value(:remote))
299 current = at_path { git_with_identity('rev-parse', rev).strip }
300 if @resource.value(:revision)
301 if local_branch_revision?
302 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
303 elsif remote_branch_revision?
304 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
306 current = @resource.value(:revision) if current == canonical
308 update_owner_and_excludes
312 def update_owner_and_excludes
313 if @resource.value(:owner) or @resource.value(:group)
316 if @resource.value(:excludes)
321 def git_with_identity(*args)
322 if @resource.value(:identity)
323 Tempfile.open('git-helper') do |f|
325 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
328 FileUtils.chmod(0755, f.path)
329 env_save = ENV['GIT_SSH']
330 ENV['GIT_SSH'] = f.path
334 ENV['GIT_SSH'] = env_save
338 elsif @resource.value(:user) and @resource.value(:user) != Facter['id'].value
339 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )