Merge remote-tracking branch 'github/pull/470'
[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 shell_out!(*args, **options)
9         options = args.pop if options.empty? && args.last.is_a?(Hash)
10
11         super(*args, **options)
12       end
13
14       def sync_command
15         if current_repository_matches_target_repository?
16           c = scm :update, new_resource.svn_arguments, verbose, authentication, proxy, "-r#{revision_int}", new_resource.destination
17           Chef::Log.debug "#{new_resource} updated working copy #{new_resource.destination} to revision #{new_resource.revision}"
18         else
19           c = scm :switch, new_resource.svn_arguments, verbose, authentication, proxy, "-r#{revision_int}", new_resource.repository, new_resource.destination
20           Chef::Log.debug "#{new_resource} updated working copy #{new_resource.destination} to #{new_resource.repository} revision #{new_resource.revision}"
21         end
22         c
23       end
24
25       def current_repository
26         @current_repository ||= repo_attrs["URL"]
27       end
28
29       def current_repository_matches_target_repository?
30         !current_repository.nil? && (new_resource.repository == current_repository)
31       end
32
33       def repo_attrs
34         return {} unless ::File.exist?(::File.join(new_resource.destination, ".svn"))
35
36         @repo_attrs ||= svn_info.lines.each_with_object({}) do |line, attrs|
37           next unless line =~ SVN_INFO_PATTERN
38
39           property = Regexp.last_match[1]
40           value = Regexp.last_match[2]
41           attrs[property] = value
42         end
43
44         raise "Could not parse `svn info` data: #{svn_info}" if @repo_attrs.empty?
45
46         @repo_attrs
47       end
48
49       def svn_info
50         command = scm(:info)
51         shell_out!(command, **run_options(:cwd => cwd, :returns => [0, 1])).stdout
52       end
53
54       def revision_int
55         @revision_int ||= if new_resource.revision =~ /^\d+$/
56                             new_resource.revision
57                           else
58                             command = scm(:info, new_resource.repository, new_resource.svn_info_args, authentication, "-r#{new_resource.revision}")
59                             svn_info = shell_out!(command, **run_options(:returns => [0, 1])).stdout
60
61                             extract_revision_info(svn_info)
62                           end
63       end
64     end
65   end
66 end