annotate .svn/pristine/d9/d9ae3cb5600a5eb2525edf835bf114f2fc4ccc54.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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