annotate .svn/pristine/1d/1d23ef14041c621865426d5030ff2b1bdf3b4a55.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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