annotate .svn/pristine/1e/1ea62f0251b7b445e6492159a303f580aeb22b77.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require 'redmine/scm/adapters/abstract_adapter'
Chris@1494 19 require 'uri'
Chris@1494 20
Chris@1494 21 module Redmine
Chris@1494 22 module Scm
Chris@1494 23 module Adapters
Chris@1494 24 class SubversionAdapter < AbstractAdapter
Chris@1494 25
Chris@1494 26 # SVN executable name
Chris@1494 27 SVN_BIN = Redmine::Configuration['scm_subversion_command'] || "svn"
Chris@1494 28
Chris@1494 29 class << self
Chris@1494 30 def client_command
Chris@1494 31 @@bin ||= SVN_BIN
Chris@1494 32 end
Chris@1494 33
Chris@1494 34 def sq_bin
Chris@1494 35 @@sq_bin ||= shell_quote_command
Chris@1494 36 end
Chris@1494 37
Chris@1494 38 def client_version
Chris@1494 39 @@client_version ||= (svn_binary_version || [])
Chris@1494 40 end
Chris@1494 41
Chris@1494 42 def client_available
Chris@1494 43 # --xml options are introduced in 1.3.
Chris@1494 44 # http://subversion.apache.org/docs/release-notes/1.3.html
Chris@1494 45 client_version_above?([1, 3])
Chris@1494 46 end
Chris@1494 47
Chris@1494 48 def svn_binary_version
Chris@1494 49 scm_version = scm_version_from_command_line.dup
Chris@1494 50 if scm_version.respond_to?(:force_encoding)
Chris@1494 51 scm_version.force_encoding('ASCII-8BIT')
Chris@1494 52 end
Chris@1494 53 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
Chris@1494 54 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@1494 55 end
Chris@1494 56 end
Chris@1494 57
Chris@1494 58 def scm_version_from_command_line
Chris@1494 59 shellout("#{sq_bin} --version") { |io| io.read }.to_s
Chris@1494 60 end
Chris@1494 61 end
Chris@1494 62
Chris@1494 63 # Get info about the svn repository
Chris@1494 64 def info
Chris@1494 65 cmd = "#{self.class.sq_bin} info --xml #{target}"
Chris@1494 66 cmd << credentials_string
Chris@1494 67 info = nil
Chris@1494 68 shellout(cmd) do |io|
Chris@1494 69 output = io.read
Chris@1494 70 if output.respond_to?(:force_encoding)
Chris@1494 71 output.force_encoding('UTF-8')
Chris@1494 72 end
Chris@1494 73 begin
Chris@1494 74 doc = parse_xml(output)
Chris@1494 75 # root_url = doc.elements["info/entry/repository/root"].text
Chris@1494 76 info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'],
Chris@1494 77 :lastrev => Revision.new({
Chris@1494 78 :identifier => doc['info']['entry']['commit']['revision'],
Chris@1494 79 :time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime,
Chris@1494 80 :author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "")
Chris@1494 81 })
Chris@1494 82 })
Chris@1494 83 rescue
Chris@1494 84 end
Chris@1494 85 end
Chris@1494 86 return nil if $? && $?.exitstatus != 0
Chris@1494 87 info
Chris@1494 88 rescue CommandFailed
Chris@1494 89 return nil
Chris@1494 90 end
Chris@1494 91
Chris@1494 92 # Returns an Entries collection
Chris@1494 93 # or nil if the given path doesn't exist in the repository
Chris@1494 94 def entries(path=nil, identifier=nil, options={})
Chris@1494 95 path ||= ''
Chris@1494 96 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
Chris@1494 97 entries = Entries.new
Chris@1494 98 cmd = "#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}"
Chris@1494 99 cmd << credentials_string
Chris@1494 100 shellout(cmd) do |io|
Chris@1494 101 output = io.read
Chris@1494 102 if output.respond_to?(:force_encoding)
Chris@1494 103 output.force_encoding('UTF-8')
Chris@1494 104 end
Chris@1494 105 begin
Chris@1494 106 doc = parse_xml(output)
Chris@1494 107 each_xml_element(doc['lists']['list'], 'entry') do |entry|
Chris@1494 108 commit = entry['commit']
Chris@1494 109 commit_date = commit['date']
Chris@1494 110 # Skip directory if there is no commit date (usually that
Chris@1494 111 # means that we don't have read access to it)
Chris@1494 112 next if entry['kind'] == 'dir' && commit_date.nil?
Chris@1494 113 name = entry['name']['__content__']
Chris@1494 114 entries << Entry.new({:name => URI.unescape(name),
Chris@1494 115 :path => ((path.empty? ? "" : "#{path}/") + name),
Chris@1494 116 :kind => entry['kind'],
Chris@1494 117 :size => ((s = entry['size']) ? s['__content__'].to_i : nil),
Chris@1494 118 :lastrev => Revision.new({
Chris@1494 119 :identifier => commit['revision'],
Chris@1494 120 :time => Time.parse(commit_date['__content__'].to_s).localtime,
Chris@1494 121 :author => ((a = commit['author']) ? a['__content__'] : nil)
Chris@1494 122 })
Chris@1494 123 })
Chris@1494 124 end
Chris@1494 125 rescue Exception => e
Chris@1494 126 logger.error("Error parsing svn output: #{e.message}")
Chris@1494 127 logger.error("Output was:\n #{output}")
Chris@1494 128 end
Chris@1494 129 end
Chris@1494 130 return nil if $? && $?.exitstatus != 0
Chris@1494 131 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
Chris@1494 132 entries.sort_by_name
Chris@1494 133 end
Chris@1494 134
Chris@1494 135 def properties(path, identifier=nil)
Chris@1494 136 # proplist xml output supported in svn 1.5.0 and higher
Chris@1494 137 return nil unless self.class.client_version_above?([1, 5, 0])
Chris@1494 138
Chris@1494 139 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
Chris@1494 140 cmd = "#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}"
Chris@1494 141 cmd << credentials_string
Chris@1494 142 properties = {}
Chris@1494 143 shellout(cmd) do |io|
Chris@1494 144 output = io.read
Chris@1494 145 if output.respond_to?(:force_encoding)
Chris@1494 146 output.force_encoding('UTF-8')
Chris@1494 147 end
Chris@1494 148 begin
Chris@1494 149 doc = parse_xml(output)
Chris@1494 150 each_xml_element(doc['properties']['target'], 'property') do |property|
Chris@1494 151 properties[ property['name'] ] = property['__content__'].to_s
Chris@1494 152 end
Chris@1494 153 rescue
Chris@1494 154 end
Chris@1494 155 end
Chris@1494 156 return nil if $? && $?.exitstatus != 0
Chris@1494 157 properties
Chris@1494 158 end
Chris@1494 159
Chris@1494 160 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@1494 161 path ||= ''
Chris@1494 162 identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
Chris@1494 163 identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1
Chris@1494 164 revisions = Revisions.new
Chris@1494 165 cmd = "#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}"
Chris@1494 166 cmd << credentials_string
Chris@1494 167 cmd << " --verbose " if options[:with_paths]
Chris@1494 168 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
Chris@1494 169 cmd << ' ' + target(path)
Chris@1494 170 shellout(cmd) do |io|
Chris@1494 171 output = io.read
Chris@1494 172 if output.respond_to?(:force_encoding)
Chris@1494 173 output.force_encoding('UTF-8')
Chris@1494 174 end
Chris@1494 175 begin
Chris@1494 176 doc = parse_xml(output)
Chris@1494 177 each_xml_element(doc['log'], 'logentry') do |logentry|
Chris@1494 178 paths = []
Chris@1494 179 each_xml_element(logentry['paths'], 'path') do |path|
Chris@1494 180 paths << {:action => path['action'],
Chris@1494 181 :path => path['__content__'],
Chris@1494 182 :from_path => path['copyfrom-path'],
Chris@1494 183 :from_revision => path['copyfrom-rev']
Chris@1494 184 }
Chris@1494 185 end if logentry['paths'] && logentry['paths']['path']
Chris@1494 186 paths.sort! { |x,y| x[:path] <=> y[:path] }
Chris@1494 187
Chris@1494 188 revisions << Revision.new({:identifier => logentry['revision'],
Chris@1494 189 :author => (logentry['author'] ? logentry['author']['__content__'] : ""),
Chris@1494 190 :time => Time.parse(logentry['date']['__content__'].to_s).localtime,
Chris@1494 191 :message => logentry['msg']['__content__'],
Chris@1494 192 :paths => paths
Chris@1494 193 })
Chris@1494 194 end
Chris@1494 195 rescue
Chris@1494 196 end
Chris@1494 197 end
Chris@1494 198 return nil if $? && $?.exitstatus != 0
Chris@1494 199 revisions
Chris@1494 200 end
Chris@1494 201
Chris@1494 202 def diff(path, identifier_from, identifier_to=nil)
Chris@1494 203 path ||= ''
Chris@1494 204 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
Chris@1494 205
Chris@1494 206 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
Chris@1494 207
Chris@1494 208 cmd = "#{self.class.sq_bin} diff -r "
Chris@1494 209 cmd << "#{identifier_to}:"
Chris@1494 210 cmd << "#{identifier_from}"
Chris@1494 211 cmd << " #{target(path)}@#{identifier_from}"
Chris@1494 212 cmd << credentials_string
Chris@1494 213 diff = []
Chris@1494 214 shellout(cmd) do |io|
Chris@1494 215 io.each_line do |line|
Chris@1494 216 diff << line
Chris@1494 217 end
Chris@1494 218 end
Chris@1494 219 return nil if $? && $?.exitstatus != 0
Chris@1494 220 diff
Chris@1494 221 end
Chris@1494 222
Chris@1494 223 def cat(path, identifier=nil)
Chris@1494 224 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
Chris@1494 225 cmd = "#{self.class.sq_bin} cat #{target(path)}@#{identifier}"
Chris@1494 226 cmd << credentials_string
Chris@1494 227 cat = nil
Chris@1494 228 shellout(cmd) do |io|
Chris@1494 229 io.binmode
Chris@1494 230 cat = io.read
Chris@1494 231 end
Chris@1494 232 return nil if $? && $?.exitstatus != 0
Chris@1494 233 cat
Chris@1494 234 end
Chris@1494 235
Chris@1494 236 def annotate(path, identifier=nil)
Chris@1494 237 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
Chris@1494 238 cmd = "#{self.class.sq_bin} blame #{target(path)}@#{identifier}"
Chris@1494 239 cmd << credentials_string
Chris@1494 240 blame = Annotate.new
Chris@1494 241 shellout(cmd) do |io|
Chris@1494 242 io.each_line do |line|
Chris@1494 243 next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
Chris@1494 244 rev = $1
Chris@1494 245 blame.add_line($3.rstrip,
Chris@1494 246 Revision.new(
Chris@1494 247 :identifier => rev,
Chris@1494 248 :revision => rev,
Chris@1494 249 :author => $2.strip
Chris@1494 250 ))
Chris@1494 251 end
Chris@1494 252 end
Chris@1494 253 return nil if $? && $?.exitstatus != 0
Chris@1494 254 blame
Chris@1494 255 end
Chris@1494 256
Chris@1494 257 private
Chris@1494 258
Chris@1494 259 def credentials_string
Chris@1494 260 str = ''
Chris@1494 261 str << " --username #{shell_quote(@login)}" unless @login.blank?
Chris@1494 262 str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
Chris@1494 263 str << " --no-auth-cache --non-interactive"
Chris@1494 264 str
Chris@1494 265 end
Chris@1494 266
Chris@1494 267 # Helper that iterates over the child elements of a xml node
Chris@1494 268 # MiniXml returns a hash when a single child is found
Chris@1494 269 # or an array of hashes for multiple children
Chris@1494 270 def each_xml_element(node, name)
Chris@1494 271 if node && node[name]
Chris@1494 272 if node[name].is_a?(Hash)
Chris@1494 273 yield node[name]
Chris@1494 274 else
Chris@1494 275 node[name].each do |element|
Chris@1494 276 yield element
Chris@1494 277 end
Chris@1494 278 end
Chris@1494 279 end
Chris@1494 280 end
Chris@1494 281
Chris@1494 282 def target(path = '')
Chris@1494 283 base = path.match(/^\//) ? root_url : url
Chris@1494 284 uri = "#{base}/#{path}"
Chris@1494 285 uri = URI.escape(URI.escape(uri), '[]')
Chris@1494 286 shell_quote(uri.gsub(/[?<>\*]/, ''))
Chris@1494 287 end
Chris@1494 288 end
Chris@1494 289 end
Chris@1494 290 end
Chris@1494 291 end