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(:source)
14 init_repository(@resource.value(:path))
16 clone_repository(@resource.value(:source), @resource.value(:path))
17 if @resource.value(:revision)
18 if @resource.value(:ensure) == :bare
19 notice "Ignoring revision for bare repository"
24 if @resource.value(:ensure) != :bare
28 update_owner_and_excludes
32 FileUtils.rm_rf(@resource.value(:path))
37 return self.revision == self.latest
44 return get_revision('HEAD')
46 return get_revision("#{@resource.value(:remote)}/#{branch}")
52 current = at_path { git_with_identity('rev-parse', 'HEAD').chomp }
53 return current unless @resource.value(:revision)
55 if tag_revision?(@resource.value(:revision))
56 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp }
58 # if it's not a tag, look for it as a local ref
59 canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp }
61 # git rev-parse executed properly but didn't find the ref;
62 # look for it in the remote
63 remote_ref = at_path { git_with_identity('ls-remote', '--heads', '--tags', @resource.value(:remote), @resource.value(:revision)).chomp }
65 fail("#{@resource.value(:revision)} is not a local or remote ref")
68 # $ git ls-remote --heads --tags origin feature/cvs
69 # 7d4244b35e72904e30130cad6d2258f901c16f1a refs/heads/feature/cvs
70 canonical = remote_ref.split.first
74 if current == canonical
75 @resource.value(:revision)
81 def revision=(desired)
83 if local_branch_revision?
84 # reset instead of pull to avoid merge conflicts. assuming remote is
86 # might be worthwhile to have an allow_local_changes param to decide
87 # whether to reset or pull when we're ensuring latest.
89 git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}")
91 git_with_identity('checkout', "#{@resource.value(:revision)}")
92 git_with_identity('pull')
96 if @resource.value(:ensure) != :bare
99 update_owner_and_excludes
103 bare_git_config_exists? && !working_copy_exists?
106 def working_copy_exists?
107 File.directory?(File.join(@resource.value(:path), '.git'))
111 working_copy_exists? || bare_exists?
114 def update_remote_origin_url
115 current = git_with_identity('config', "remote.#{@resource.value(:remote)}.url")
116 unless @resource.value(:source).nil?
117 if current.nil? or current.strip != @resource.value(:source)
118 git_with_identity('config', "remote.#{@resource.value(:remote)}.url", @resource.value(:source))
123 def update_references
125 update_remote_origin_url
126 git_with_identity('fetch', @resource.value(:remote))
127 git_with_identity('fetch', '--tags', @resource.value(:remote))
128 update_owner_and_excludes
134 def bare_git_config_exists?
135 File.exist?(File.join(@resource.value(:path), 'config'))
138 def clone_repository(source, path)
141 if @resource.value(:depth) and @resource.value(:depth).to_i > 0
142 args.push('--depth', @resource.value(:depth).to_s)
144 if @resource.value(:ensure) == :bare
147 if @resource.value(:remote) != 'origin'
148 args.push('--origin', @resource.value(:remote))
150 if !working_copy_exists?
151 args.push(source, path)
153 git_with_identity(*args)
156 notice "Repo has already been cloned"
161 if path_exists? and not path_empty?
162 if @resource.value(:force)
163 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
166 raise Puppet::Error, "Could not create repository (non-repository at path)"
171 def init_repository(path)
173 if @resource.value(:ensure) == :bare && working_copy_exists?
174 convert_working_copy_to_bare
175 elsif @resource.value(:ensure) == :present && bare_exists?
176 convert_bare_to_working_copy
179 FileUtils.mkdir(@resource.value(:path))
180 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
182 if @resource.value(:ensure) == :bare
186 git_with_identity(*args)
191 # Convert working copy to bare
197 def convert_working_copy_to_bare
198 notice "Converting working copy repository to bare repository"
199 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
200 FileUtils.rm_rf(@resource.value(:path))
201 FileUtils.mv(tempdir, @resource.value(:path))
204 # Convert bare to working copy
210 def convert_bare_to_working_copy
211 notice "Converting bare repository to working copy repository"
212 FileUtils.mv(@resource.value(:path), tempdir)
213 FileUtils.mkdir(@resource.value(:path))
214 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
215 if commits_in?(File.join(@resource.value(:path), '.git'))
217 git_with_identity('checkout', '--force')
218 update_owner_and_excludes
222 def commits_in?(dot_git)
223 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
224 return true unless %w(. ..).include?(File::basename(e))
229 def checkout(revision = @resource.value(:revision))
230 if !local_branch_revision? && remote_branch_revision?
231 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
233 at_path { git_with_identity('checkout', '--force', revision) }
239 git_with_identity('reset', '--hard', desired)
243 def update_submodules
245 git_with_identity('submodule', 'update', '--init', '--recursive')
249 def remote_branch_revision?(revision = @resource.value(:revision))
250 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
251 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
252 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
253 branch unless branch.empty?
256 def local_branch_revision?(revision = @resource.value(:revision))
257 at_path { branches.include?(revision) }
260 def tag_revision?(revision = @resource.value(:revision))
261 at_path { tags.include?(revision) }
265 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
270 matches = git_with_identity('branch', '-a').match /\*\s+(.*)/
271 matches[1] unless matches[1].match /detached/
277 git_with_identity('branch', '-a').match /\*\s+\(detached from.*\)/
282 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
286 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
289 def get_revision(rev)
290 if @resource.value(:force) && working_copy_exists?
293 if !working_copy_exists?
297 update_remote_origin_url
298 git_with_identity('fetch', @resource.value(:remote))
299 git_with_identity('fetch', '--tags', @resource.value(:remote))
301 current = at_path { git_with_identity('rev-parse', rev).strip }
302 if @resource.value(:revision)
303 if local_branch_revision?
304 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
305 elsif remote_branch_revision?
306 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
308 current = @resource.value(:revision) if current == canonical
310 update_owner_and_excludes
314 def update_owner_and_excludes
315 if @resource.value(:owner) or @resource.value(:group)
318 if @resource.value(:excludes)
323 def git_with_identity(*args)
324 if @resource.value(:identity)
325 Tempfile.open('git-helper') do |f|
327 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
330 FileUtils.chmod(0755, f.path)
331 env_save = ENV['GIT_SSH']
332 ENV['GIT_SSH'] = f.path
336 ENV['GIT_SSH'] = env_save
340 elsif @resource.value(:user) and @resource.value(:user) != Facter['id'].value
341 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )