Merge pull request #78 from jhoblitt/git_provider_checkout_remote_refs
[puppet_vcsrepo.git] / lib / puppet / provider / vcsrepo / svn.rb
1 require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
2
3 Puppet::Type.type(:vcsrepo).provide(:svn, :parent => Puppet::Provider::Vcsrepo) do
4   desc "Supports Subversion repositories"
5
6   optional_commands :svn      => 'svn',
7                     :svnadmin => 'svnadmin'
8
9   has_features :filesystem_types, :reference_tracking, :basic_auth
10
11   def create
12     if !@resource.value(:source)
13       create_repository(@resource.value(:path))
14     else
15       checkout_repository(@resource.value(:source),
16                           @resource.value(:path),
17                           @resource.value(:revision))
18     end
19     update_owner
20   end
21
22   def working_copy_exists?
23     File.directory?(File.join(@resource.value(:path), '.svn'))
24   end
25
26   def exists?
27     working_copy_exists?
28   end
29
30   def destroy
31     FileUtils.rm_rf(@resource.value(:path))
32   end
33
34   def latest?
35     at_path do
36       if self.revision < self.latest then
37         return false
38       else
39         return true
40       end
41     end
42   end
43
44   def buildargs
45     args = ['--non-interactive']
46     if @resource.value(:basic_auth_username) && @resource.value(:basic_auth_password)
47       args.push('--username', @resource.value(:basic_auth_username))
48       args.push('--password', @resource.value(:basic_auth_password))
49       args.push('--no-auth-cache')
50     end
51
52     if @resource.value(:force)
53       args.push('--force')
54     end
55
56     return args
57   end
58
59   def latest
60     args = buildargs.push('info', '-r', 'HEAD')
61     at_path do
62       svn(*args)[/^Last Changed Rev:\s+(\d+)/m, 1]
63     end
64   end
65
66   def revision
67     args = buildargs.push('info')
68     at_path do
69       svn(*args)[/^Last Changed Rev:\s+(\d+)/m, 1]
70     end
71   end
72
73   def revision=(desired)
74     args = buildargs.push('update', '-r', desired)
75     at_path do
76       svn(*args)
77     end
78     update_owner
79   end
80
81   private
82
83   def checkout_repository(source, path, revision)
84     args = buildargs.push('checkout')
85     if revision
86       args.push('-r', revision)
87     end
88     args.push(source, path)
89     svn(*args)
90   end
91
92   def create_repository(path)
93     args = ['create']
94     if @resource.value(:fstype)
95       args.push('--fs-type', @resource.value(:fstype))
96     end
97     args << path
98     svnadmin(*args)
99   end
100
101   def update_owner
102     if @resource.value(:owner) or @resource.value(:group)
103       set_ownership
104     end
105   end
106 end