]> git.openstreetmap.org Git - chef.git/blob - cookbooks/chef/libraries/subversion.rb
Merge remote-tracking branch 'github/pull/440'
[chef.git] / cookbooks / chef / libraries / subversion.rb
1 require "chef/mixin/shell_out"
2
3 class Chef
4   class Provider
5     class Subversion
6       extend Chef::Mixin::ShellOut
7
8       def sync_command
9         if current_repository_matches_target_repository?
10           c = scm :update, new_resource.svn_arguments, verbose, authentication, proxy, "-r#{revision_int}", new_resource.destination
11           Chef::Log.debug "#{new_resource} updated working copy #{new_resource.destination} to revision #{new_resource.revision}"
12         else
13           c = scm :switch, new_resource.svn_arguments, verbose, authentication, proxy, "-r#{revision_int}", new_resource.repository, new_resource.destination
14           Chef::Log.debug "#{new_resource} updated working copy #{new_resource.destination} to #{new_resource.repository} revision #{new_resource.revision}"
15         end
16         c
17       end
18
19       def current_repository
20         @current_repository ||= repo_attrs["URL"]
21       end
22
23       def current_repository_matches_target_repository?
24         !current_repository.nil? && (new_resource.repository == current_repository)
25       end
26
27       def repo_attrs
28         return {} unless ::File.exist?(::File.join(new_resource.destination, ".svn"))
29
30         @repo_attrs ||= svn_info.lines.each_with_object({}) do |line, attrs|
31           next unless line =~ SVN_INFO_PATTERN
32
33           property = Regexp.last_match[1]
34           value = Regexp.last_match[2]
35           attrs[property] = value
36         end
37
38         raise "Could not parse `svn info` data: #{svn_info}" if @repo_attrs.empty?
39
40         @repo_attrs
41       end
42
43       def svn_info
44         command = scm(:info)
45         shell_out!(command, run_options(:cwd => cwd, :returns => [0, 1])).stdout
46       end
47
48       def revision_int
49         @revision_int ||= if new_resource.revision =~ /^\d+$/
50                             new_resource.revision
51                           else
52                             command = scm(:info, new_resource.repository, new_resource.svn_info_args, authentication, "-r#{new_resource.revision}")
53                             svn_info = shell_out!(command, run_options(:returns => [0, 1])).stdout
54
55                             extract_revision_info(svn_info)
56                           end
57       end
58     end
59   end
60 end