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(: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('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 canonical = at_path { git_with_identity('show', @resource.value(:revision)).scan(/^commit (.*)/).to_s }
57 # if it's not a tag, look for it as a local ref
58 canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp }
60 # git rev-parse executed properly but didn't find the ref;
61 # look for it in the remote
62 remote_ref = at_path { git_with_identity('ls-remote', '--heads', '--tags', @resource.value(:remote), @resource.value(:revision)).chomp }
64 fail("#{@resource.value(:revision)} is not a local or remote ref")
67 # $ git ls-remote --heads --tags origin feature/cvs
68 # 7d4244b35e72904e30130cad6d2258f901c16f1a refs/heads/feature/cvs
69 canonical = remote_ref.split.first
73 if current == canonical
74 @resource.value(:revision)
80 def revision=(desired)
82 if local_branch_revision?(desired)
83 # reset instead of pull to avoid merge conflicts. assuming remote is
85 # might be worthwhile to have an allow_local_changes param to decide
86 # whether to reset or pull when we're ensuring latest.
87 at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") }
89 if @resource.value(:ensure) != :bare
92 update_owner_and_excludes
96 bare_git_config_exists? && !working_copy_exists?
99 def working_copy_exists?
100 File.directory?(File.join(@resource.value(:path), '.git'))
104 working_copy_exists? || bare_exists?
107 def update_remote_origin_url
108 current = git_with_identity('config', "remote.#{@resource.value(:remote)}.url")
109 unless @resource.value(:source).nil?
110 if current.nil? or current.strip != @resource.value(:source)
111 git_with_identity('config', "remote.#{@resource.value(:remote)}.url", @resource.value(:source))
116 def update_references
118 update_remote_origin_url
119 git_with_identity('fetch', @resource.value(:remote))
120 git_with_identity('fetch', '--tags', @resource.value(:remote))
121 update_owner_and_excludes
127 def bare_git_config_exists?
128 File.exist?(File.join(@resource.value(:path), 'config'))
131 def clone_repository(source, path)
134 if @resource.value(:depth) and @resource.value(:depth).to_i > 0
135 args.push('--depth', @resource.value(:depth).to_s)
137 if @resource.value(:ensure) == :bare
140 if @resource.value(:remote) != 'origin'
141 args.push('--origin', @resource.value(:remote))
143 if !File.exist?(File.join(@resource.value(:path), '.git'))
144 args.push(source, path)
146 git_with_identity(*args)
149 notice "Repo has already been cloned"
154 if path_exists? and not path_empty?
155 if @resource.value(:force)
156 notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
159 raise Puppet::Error, "Could not create repository (non-repository at path)"
164 def init_repository(path)
166 if @resource.value(:ensure) == :bare && working_copy_exists?
167 convert_working_copy_to_bare
168 elsif @resource.value(:ensure) == :present && bare_exists?
169 convert_bare_to_working_copy
172 FileUtils.mkdir(@resource.value(:path))
173 FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
175 if @resource.value(:ensure) == :bare
179 git_with_identity(*args)
184 # Convert working copy to bare
190 def convert_working_copy_to_bare
191 notice "Converting working copy repository to bare repository"
192 FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
193 FileUtils.rm_rf(@resource.value(:path))
194 FileUtils.mv(tempdir, @resource.value(:path))
197 # Convert bare to working copy
203 def convert_bare_to_working_copy
204 notice "Converting bare repository to working copy repository"
205 FileUtils.mv(@resource.value(:path), tempdir)
206 FileUtils.mkdir(@resource.value(:path))
207 FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
208 if commits_in?(File.join(@resource.value(:path), '.git'))
210 git_with_identity('checkout', '--force')
211 update_owner_and_excludes
215 def commits_in?(dot_git)
216 Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
217 return true unless %w(. ..).include?(File::basename(e))
222 def checkout(revision = @resource.value(:revision))
223 if !local_branch_revision? && remote_branch_revision?
224 at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
226 at_path { git_with_identity('checkout', '--force', revision) }
232 git_with_identity('reset', '--hard', desired)
236 def update_submodules
238 git_with_identity('submodule', 'update', '--init', '--recursive')
242 def remote_branch_revision?(revision = @resource.value(:revision))
243 # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
244 # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
245 branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
246 branch unless branch.empty?
249 def local_branch_revision?(revision = @resource.value(:revision))
250 at_path { branches.include?(revision) }
253 def tag_revision?(revision = @resource.value(:revision))
254 at_path { tags.include?(revision) }
258 at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
262 at_path { git_with_identity('rev-parse', '--abbrev-ref', 'HEAD').chomp }
266 at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
270 at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
273 def get_revision(rev)
274 if !working_copy_exists?
278 update_remote_origin_url
279 git_with_identity('fetch', @resource.value(:remote))
280 git_with_identity('fetch', '--tags', @resource.value(:remote))
282 current = at_path { git_with_identity('rev-parse', rev).strip }
283 if @resource.value(:revision)
284 if local_branch_revision?
285 canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
286 elsif remote_branch_revision?
287 canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
289 current = @resource.value(:revision) if current == canonical
291 update_owner_and_excludes
295 def update_owner_and_excludes
296 if @resource.value(:owner) or @resource.value(:group)
299 if @resource.value(:excludes)
304 def git_with_identity(*args)
305 if @resource.value(:identity)
306 Tempfile.open('git-helper') do |f|
308 f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
311 FileUtils.chmod(0755, f.path)
312 env_save = ENV['GIT_SSH']
313 ENV['GIT_SSH'] = f.path
317 ENV['GIT_SSH'] = env_save
321 elsif @resource.value(:user)
322 su(@resource.value(:user), '-c', "git #{args.join(' ')}" )