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
8 optional_commands :su => 'su'
10 has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth
13 if @resource.value(:revision) and @resource.value(:ensure) == :bare
14 fail("Cannot set a revision (#{@resource.value(:revision)}) on a bare repository")
16 if !@resource.value(:source)
17 init_repository(@resource.value(:path))
19 clone_repository(@resource.value(:source), @resource.value(:path))
20 if @resource.value(:revision)
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('HEAD')
45 return get_revision("#{@resource.value(:remote)}/#{branch}")
51 current = at_path { git_with_identity('rev-parse', 'HEAD').chomp }
52 return current unless @resource.value(:revision)
54 if tag_revision?(@resource.value(:revision))
55 # git-rev-parse will give you the hash of the tag object itself rather than the commit it points to by default.
56 # Using tag^0 will return the actual commit.
57 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:revision)}^0").chomp }
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?
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.
90 git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}")
92 git_with_identity('checkout', "#{@resource.value(:revision)}")
93 git_with_identity('pull')
97 if @resource.value(:ensure) != :bare
100 update_owner_and_excludes
104 bare_git_config_exists? && !working_copy_exists?
107 def working_copy_exists?
108 File.directory?(File.join(@resource.value(:path), '.git'))
112 working_copy_exists? || bare_exists?
115 def update_remote_origin_url
116 current = git_with_identity('config', "remote.#{@resource.value(:remote)}.url")
117 unless @resource.value(:source).nil?
118 if current.nil? or current.strip != @resource.value(:source)
119 git_with_identity('config', "remote.#{@resource.value(:remote)}.url", @resource.value(:source))
124 def update_references
126 update_remote_origin_url
127 git_with_identity('fetch', @resource.value(:remote))
128 git_with_identity('fetch', '--tags', @resource.value(:remote))
129 update_owner_and_excludes
135 def bare_git_config_exists?
136 File.exist?(File.join(@resource.value(:path), 'config'))
139 def clone_repository(source, path)
142 if @resource.value(:depth) and @resource.value(:depth).to_i > 0
143 args.push('--depth', @resource.value(:depth).to_s)
145 if @resource.value(:ensure) == :bare
148 if @resource.value(:remote) != 'origin'
149 args.push('--origin', @resource.value(:remote))
151 if !working_copy_exists?
152 args.push(source, path)
154 git_with_identity(*args)
157 notice "Repo has already been cloned"
162 if path_exists? and not path_empty?
163 if @resource.value(:force)
164 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
167 raise Puppet::Error, "Could not create repository (non-repository at path)"
172 def init_repository(path)
174 if @resource.value(:ensure) == :bare && working_copy_exists?
175 convert_working_copy_to_bare
176 elsif @resource.value(:ensure) == :present && bare_exists?
177 convert_bare_to_working_copy
180 FileUtils.mkdir(@resource.value(:path))
181 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
183 if @resource.value(:ensure) == :bare
187 git_with_identity(*args)
192 # Convert working copy to bare
198 def convert_working_copy_to_bare
199 notice "Converting working copy repository to bare repository"
200 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
201 FileUtils.rm_rf(@resource.value(:path))
202 FileUtils.mv(tempdir, @resource.value(:path))
205 # Convert bare to working copy
211 def convert_bare_to_working_copy
212 notice "Converting bare repository to working copy repository"
213 FileUtils.mv(@resource.value(:path), tempdir)
214 FileUtils.mkdir(@resource.value(:path))
215 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
216 if commits_in?(File.join(@resource.value(:path), '.git'))
218 git_with_identity('checkout', '--force')
219 update_owner_and_excludes
223 def commits_in?(dot_git)
224 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
225 return true unless %w(. ..).include?(File::basename(e))
230 def checkout(revision = @resource.value(:revision))
231 if !local_branch_revision? && remote_branch_revision?
232 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
234 at_path { git_with_identity('checkout', '--force', revision) }
240 git_with_identity('reset', '--hard', desired)
244 def update_submodules
246 git_with_identity('submodule', 'update', '--init', '--recursive')
250 def remote_branch_revision?(revision = @resource.value(:revision))
251 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
252 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
253 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
254 branch unless branch.empty?
257 def local_branch_revision?(revision = @resource.value(:revision))
258 at_path { branches.include?(revision) }
261 def tag_revision?(revision = @resource.value(:revision))
262 at_path { tags.include?(revision) }
266 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
271 matches = git_with_identity('branch', '-a').match /\*\s+(.*)/
272 matches[1] unless matches[1].match /detached/
278 git_with_identity('branch', '-a').match /\*\s+\(detached from.*\)/
283 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
287 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
290 def get_revision(rev)
291 if @resource.value(:force) && working_copy_exists?
294 if !working_copy_exists?
298 update_remote_origin_url
299 git_with_identity('fetch', @resource.value(:remote))
300 git_with_identity('fetch', '--tags', @resource.value(:remote))
302 current = at_path { git_with_identity('rev-parse', rev).strip }
303 if @resource.value(:revision)
304 if local_branch_revision?
305 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
306 elsif remote_branch_revision?
307 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
309 current = @resource.value(:revision) if current == canonical
311 update_owner_and_excludes
315 def update_owner_and_excludes
316 if @resource.value(:owner) or @resource.value(:group)
319 if @resource.value(:excludes)
324 def git_with_identity(*args)
325 if @resource.value(:identity)
326 Tempfile.open('git-helper') do |f|
328 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
331 FileUtils.chmod(0755, f.path)
332 env_save = ENV['GIT_SSH']
333 ENV['GIT_SSH'] = f.path
337 ENV['GIT_SSH'] = env_save
341 elsif @resource.value(:user) and @resource.value(:user) != Facter['id'].value
342 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )