Merge pull request #35 from sodabrew/dummy-provider
[puppet_vcsrepo.git] / lib / puppet / provider / vcsrepo / hg.rb
1 require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
2
3 Puppet::Type.type(:vcsrepo).provide(:hg, :parent => Puppet::Provider::Vcsrepo) do
4   desc "Supports Mercurial repositories"
5
6   optional_commands   :hg => 'hg'
7   has_features :reference_tracking
8
9   def create
10     if !@resource.value(:source)
11       create_repository(@resource.value(:path))
12     else
13       clone_repository(@resource.value(:revision))
14     end
15     update_owner
16   end
17
18   def working_copy_exists?
19     File.directory?(File.join(@resource.value(:path), '.hg'))
20   end
21
22   def exists?
23     working_copy_exists?
24   end
25
26   def destroy
27     FileUtils.rm_rf(@resource.value(:path))
28   end
29
30   def latest?
31     at_path do
32       return self.revision == self.latest
33     end
34   end
35
36   def latest
37     at_path do
38       begin
39         hg('incoming', '--branch', '.', '--newest-first', '--limit', '1')[/^changeset:\s+(?:-?\d+):(\S+)/m, 1]
40       rescue Puppet::ExecutionFailure
41         # If there are no new changesets, return the current nodeid
42         self.revision
43       end
44     end
45   end
46
47   def revision
48     at_path do
49       current = hg('parents')[/^changeset:\s+(?:-?\d+):(\S+)/m, 1]
50       desired = @resource.value(:revision)
51       if desired
52         # Return the tag name if it maps to the current nodeid
53         mapped = hg('tags')[/^#{Regexp.quote(desired)}\s+\d+:(\S+)/m, 1]
54         if current == mapped
55           desired
56         else
57           current
58         end
59       else
60         current
61       end
62     end
63   end
64
65   def revision=(desired)
66     at_path do
67       begin
68         hg('pull')
69       rescue
70       end
71       begin
72         hg('merge')
73       rescue Puppet::ExecutionFailure
74         # If there's nothing to merge, just skip
75       end
76       hg('update', '--clean', '-r', desired)
77     end
78     update_owner
79   end
80
81   private
82
83   def create_repository(path)
84     hg('init', path)
85   end
86
87   def clone_repository(revision)
88     args = ['clone']
89     if revision
90       args.push('-u', revision)
91     end
92     args.push(@resource.value(:source),
93               @resource.value(:path))
94     hg(*args)
95   end
96
97   def update_owner
98     if @resource.value(:owner) or @resource.value(:group)
99       set_ownership
100     end
101   end
102
103 end