Merge pull request #31 from ghoneycutt/noticket/master/missing_trailing_comma
[puppet_vcsrepo.git] / lib / puppet / provider / vcsrepo / bzr.rb
1 require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
2
3 Puppet::Type.type(:vcsrepo).provide(:bzr, :parent => Puppet::Provider::Vcsrepo) do
4   desc "Supports Bazaar repositories"
5
6   optional_commands   :bzr => 'bzr'
7   defaultfor :bzr => :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   end
17
18   def working_copy_exists?
19     File.directory?(File.join(@resource.value(:path), '.bzr'))
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 revision
31     at_path do
32       current_revid = bzr('version-info')[/^revision-id:\s+(\S+)/, 1]
33       desired = @resource.value(:revision)
34       begin
35         desired_revid = bzr('revision-info', desired).strip.split(/\s+/).last
36       rescue Puppet::ExecutionFailure
37         # Possible revid available during update (but definitely not current)
38         desired_revid = nil
39       end
40       if current_revid == desired_revid
41         desired
42       else
43         current_revid
44       end
45     end
46   end
47
48   def revision=(desired)
49     bzr('update', '-r', desired, @resource.value(:path))
50   end
51
52   private
53
54   def create_repository(path)
55     bzr('init', path)
56   end
57
58   def clone_repository(revision)
59     args = ['branch']
60     if revision
61       args.push('-r', revision)
62     end
63     args.push(@resource.value(:source),
64               @resource.value(:path))
65     bzr(*args)
66   end
67
68 end