annotate .svn/pristine/f5/f5ada035c7d5ce2734e191045efdfc2d9fd551ae.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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