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