2010-03-13 15:19:39 -08:00
|
|
|
require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
|
|
|
|
|
|
|
|
Puppet::Type.type(:vcsrepo).provide(:hg, :parent => Puppet::Provider::Vcsrepo) do
|
|
|
|
desc "Supports Mercurial repositories"
|
|
|
|
|
2011-10-06 23:35:41 +02:00
|
|
|
optional_commands :hg => 'hg'
|
2010-03-13 15:19:39 -08:00
|
|
|
defaultfor :hg => :exists
|
2010-03-15 11:16:22 -07:00
|
|
|
has_features :reference_tracking
|
2010-03-13 15:19:39 -08:00
|
|
|
|
|
|
|
def create
|
|
|
|
if !@resource.value(:source)
|
|
|
|
create_repository(@resource.value(:path))
|
|
|
|
else
|
|
|
|
clone_repository(@resource.value(:revision))
|
|
|
|
end
|
2010-12-15 02:21:42 +08:00
|
|
|
update_owner
|
2010-03-13 15:19:39 -08:00
|
|
|
end
|
|
|
|
|
2010-12-14 09:25:34 +08:00
|
|
|
def working_copy_exists?
|
2010-03-13 15:19:39 -08:00
|
|
|
File.directory?(File.join(@resource.value(:path), '.hg'))
|
|
|
|
end
|
|
|
|
|
2010-12-14 09:25:34 +08:00
|
|
|
def exists?
|
|
|
|
working_copy_exists?
|
|
|
|
end
|
|
|
|
|
2010-03-13 15:19:39 -08:00
|
|
|
def destroy
|
|
|
|
FileUtils.rm_rf(@resource.value(:path))
|
|
|
|
end
|
2010-12-14 09:25:34 +08:00
|
|
|
|
2010-12-15 01:42:30 +08:00
|
|
|
def latest?
|
|
|
|
at_path do
|
|
|
|
return self.revision == self.latest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest
|
|
|
|
at_path do
|
|
|
|
begin
|
|
|
|
hg('incoming', '--branch', '.', '--newest-first', '--limit', '1')[/^changeset:\s+(?:-?\d+):(\S+)/m, 1]
|
|
|
|
rescue Puppet::ExecutionFailure
|
|
|
|
# If there are no new changesets, return the current nodeid
|
|
|
|
self.revision
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-13 15:19:39 -08:00
|
|
|
def revision
|
|
|
|
at_path do
|
2010-03-13 17:32:17 -08:00
|
|
|
current = hg('parents')[/^changeset:\s+(?:-?\d+):(\S+)/m, 1]
|
|
|
|
desired = @resource.value(:revision)
|
2010-12-15 01:43:52 +08:00
|
|
|
if desired
|
|
|
|
# Return the tag name if it maps to the current nodeid
|
2010-03-13 17:32:17 -08:00
|
|
|
mapped = hg('tags')[/^#{Regexp.quote(desired)}\s+\d+:(\S+)/m, 1]
|
2010-12-15 01:43:52 +08:00
|
|
|
if current == mapped
|
|
|
|
desired
|
2010-03-13 17:32:17 -08:00
|
|
|
else
|
|
|
|
current
|
|
|
|
end
|
2010-12-15 01:43:52 +08:00
|
|
|
else
|
|
|
|
current
|
2010-03-13 17:32:17 -08:00
|
|
|
end
|
2010-03-13 15:19:39 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revision=(desired)
|
|
|
|
at_path do
|
2012-03-18 17:26:54 +01:00
|
|
|
begin
|
|
|
|
hg('pull')
|
|
|
|
rescue
|
|
|
|
end
|
2010-03-13 15:19:39 -08:00
|
|
|
begin
|
|
|
|
hg('merge')
|
|
|
|
rescue Puppet::ExecutionFailure
|
|
|
|
# If there's nothing to merge, just skip
|
|
|
|
end
|
|
|
|
hg('update', '--clean', '-r', desired)
|
|
|
|
end
|
2010-12-15 02:21:42 +08:00
|
|
|
update_owner
|
2010-03-13 15:19:39 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_repository(path)
|
|
|
|
hg('init', path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clone_repository(revision)
|
|
|
|
args = ['clone']
|
|
|
|
if revision
|
|
|
|
args.push('-u', revision)
|
|
|
|
end
|
|
|
|
args.push(@resource.value(:source),
|
|
|
|
@resource.value(:path))
|
|
|
|
hg(*args)
|
|
|
|
end
|
|
|
|
|
2010-12-15 02:21:42 +08:00
|
|
|
def update_owner
|
|
|
|
if @resource.value(:owner) or @resource.value(:group)
|
|
|
|
set_ownership
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-13 15:19:39 -08:00
|
|
|
end
|