annotate .svn/pristine/df/dfb3acb9226878b28e389fb4105ce64bd182e491.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 'cgi'
Chris@909 19
Chris@909 20 module Redmine
Chris@909 21 module Scm
Chris@909 22 module Adapters
Chris@909 23 class CommandFailed < StandardError #:nodoc:
Chris@909 24 end
Chris@909 25
Chris@909 26 class AbstractAdapter #:nodoc:
Chris@909 27
Chris@909 28 # raised if scm command exited with error, e.g. unknown revision.
Chris@909 29 class ScmCommandAborted < CommandFailed; end
Chris@909 30
Chris@909 31 class << self
Chris@909 32 def client_command
Chris@909 33 ""
Chris@909 34 end
Chris@909 35
Chris@909 36 def shell_quote_command
Chris@909 37 if Redmine::Platform.mswin? && RUBY_PLATFORM == 'java'
Chris@909 38 client_command
Chris@909 39 else
Chris@909 40 shell_quote(client_command)
Chris@909 41 end
Chris@909 42 end
Chris@909 43
Chris@909 44 # Returns the version of the scm client
Chris@909 45 # Eg: [1, 5, 0] or [] if unknown
Chris@909 46 def client_version
Chris@909 47 []
Chris@909 48 end
Chris@909 49
Chris@909 50 # Returns the version string of the scm client
Chris@909 51 # Eg: '1.5.0' or 'Unknown version' if unknown
Chris@909 52 def client_version_string
Chris@909 53 v = client_version || 'Unknown version'
Chris@909 54 v.is_a?(Array) ? v.join('.') : v.to_s
Chris@909 55 end
Chris@909 56
Chris@909 57 # Returns true if the current client version is above
Chris@909 58 # or equals the given one
Chris@909 59 # If option is :unknown is set to true, it will return
Chris@909 60 # true if the client version is unknown
Chris@909 61 def client_version_above?(v, options={})
Chris@909 62 ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
Chris@909 63 end
Chris@909 64
Chris@909 65 def client_available
Chris@909 66 true
Chris@909 67 end
Chris@909 68
Chris@909 69 def shell_quote(str)
Chris@909 70 if Redmine::Platform.mswin?
Chris@909 71 '"' + str.gsub(/"/, '\\"') + '"'
Chris@909 72 else
Chris@909 73 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@909 74 end
Chris@909 75 end
Chris@909 76 end
Chris@909 77
Chris@909 78 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@909 79 path_encoding=nil)
Chris@909 80 @url = url
Chris@909 81 @login = login if login && !login.empty?
Chris@909 82 @password = (password || "") if @login
Chris@909 83 @root_url = root_url.blank? ? retrieve_root_url : root_url
Chris@909 84 end
Chris@909 85
Chris@909 86 def adapter_name
Chris@909 87 'Abstract'
Chris@909 88 end
Chris@909 89
Chris@909 90 def supports_cat?
Chris@909 91 true
Chris@909 92 end
Chris@909 93
Chris@909 94 def supports_annotate?
Chris@909 95 respond_to?('annotate')
Chris@909 96 end
Chris@909 97
Chris@909 98 def root_url
Chris@909 99 @root_url
Chris@909 100 end
Chris@909 101
Chris@909 102 def url
Chris@909 103 @url
Chris@909 104 end
Chris@909 105
Chris@909 106 def path_encoding
Chris@909 107 nil
Chris@909 108 end
Chris@909 109
Chris@909 110 # get info about the svn repository
Chris@909 111 def info
Chris@909 112 return nil
Chris@909 113 end
Chris@909 114
Chris@909 115 # Returns the entry identified by path and revision identifier
Chris@909 116 # or nil if entry doesn't exist in the repository
Chris@909 117 def entry(path=nil, identifier=nil)
Chris@909 118 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
Chris@909 119 search_path = parts[0..-2].join('/')
Chris@909 120 search_name = parts[-1]
Chris@909 121 if search_path.blank? && search_name.blank?
Chris@909 122 # Root entry
Chris@909 123 Entry.new(:path => '', :kind => 'dir')
Chris@909 124 else
Chris@909 125 # Search for the entry in the parent directory
Chris@909 126 es = entries(search_path, identifier)
Chris@909 127 es ? es.detect {|e| e.name == search_name} : nil
Chris@909 128 end
Chris@909 129 end
Chris@909 130
Chris@909 131 # Returns an Entries collection
Chris@909 132 # or nil if the given path doesn't exist in the repository
Chris@909 133 def entries(path=nil, identifier=nil, options={})
Chris@909 134 return nil
Chris@909 135 end
Chris@909 136
Chris@909 137 def branches
Chris@909 138 return nil
Chris@909 139 end
Chris@909 140
Chris@909 141 def tags
Chris@909 142 return nil
Chris@909 143 end
Chris@909 144
Chris@909 145 def default_branch
Chris@909 146 return nil
Chris@909 147 end
Chris@909 148
Chris@909 149 def properties(path, identifier=nil)
Chris@909 150 return nil
Chris@909 151 end
Chris@909 152
Chris@909 153 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@909 154 return nil
Chris@909 155 end
Chris@909 156
Chris@909 157 def diff(path, identifier_from, identifier_to=nil)
Chris@909 158 return nil
Chris@909 159 end
Chris@909 160
Chris@909 161 def cat(path, identifier=nil)
Chris@909 162 return nil
Chris@909 163 end
Chris@909 164
Chris@909 165 def with_leading_slash(path)
Chris@909 166 path ||= ''
Chris@909 167 (path[0,1]!="/") ? "/#{path}" : path
Chris@909 168 end
Chris@909 169
Chris@909 170 def with_trailling_slash(path)
Chris@909 171 path ||= ''
Chris@909 172 (path[-1,1] == "/") ? path : "#{path}/"
Chris@909 173 end
Chris@909 174
Chris@909 175 def without_leading_slash(path)
Chris@909 176 path ||= ''
Chris@909 177 path.gsub(%r{^/+}, '')
Chris@909 178 end
Chris@909 179
Chris@909 180 def without_trailling_slash(path)
Chris@909 181 path ||= ''
Chris@909 182 (path[-1,1] == "/") ? path[0..-2] : path
Chris@909 183 end
Chris@909 184
Chris@909 185 def shell_quote(str)
Chris@909 186 self.class.shell_quote(str)
Chris@909 187 end
Chris@909 188
Chris@909 189 private
Chris@909 190 def retrieve_root_url
Chris@909 191 info = self.info
Chris@909 192 info ? info.root_url : nil
Chris@909 193 end
Chris@909 194
Chris@909 195 def target(path, sq=true)
Chris@909 196 path ||= ''
Chris@909 197 base = path.match(/^\//) ? root_url : url
Chris@909 198 str = "#{base}/#{path}".gsub(/[?<>\*]/, '')
Chris@909 199 if sq
Chris@909 200 str = shell_quote(str)
Chris@909 201 end
Chris@909 202 str
Chris@909 203 end
Chris@909 204
Chris@909 205 def logger
Chris@909 206 self.class.logger
Chris@909 207 end
Chris@909 208
Chris@909 209 def shellout(cmd, &block)
Chris@909 210 self.class.shellout(cmd, &block)
Chris@909 211 end
Chris@909 212
Chris@909 213 def self.logger
Chris@909 214 Rails.logger
Chris@909 215 end
Chris@909 216
Chris@909 217 def self.shellout(cmd, &block)
Chris@909 218 if logger && logger.debug?
Chris@909 219 logger.debug "Shelling out: #{strip_credential(cmd)}"
Chris@909 220 end
Chris@909 221 if Rails.env == 'development'
Chris@909 222 # Capture stderr when running in dev environment
Chris@909 223 cmd = "#{cmd} 2>>#{Rails.root}/log/scm.stderr.log"
Chris@909 224 end
Chris@909 225 begin
Chris@909 226 if RUBY_VERSION < '1.9'
Chris@909 227 mode = "r+"
Chris@909 228 else
Chris@909 229 mode = "r+:ASCII-8BIT"
Chris@909 230 end
Chris@909 231 IO.popen(cmd, mode) do |io|
Chris@909 232 io.close_write
Chris@909 233 block.call(io) if block_given?
Chris@909 234 end
Chris@909 235 ## If scm command does not exist,
Chris@909 236 ## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException
Chris@909 237 ## in production environment.
Chris@909 238 # rescue Errno::ENOENT => e
Chris@909 239 rescue Exception => e
Chris@909 240 msg = strip_credential(e.message)
Chris@909 241 # The command failed, log it and re-raise
Chris@909 242 logmsg = "SCM command failed, "
Chris@909 243 logmsg += "make sure that your SCM command (e.g. svn) is "
Chris@909 244 logmsg += "in PATH (#{ENV['PATH']})\n"
Chris@909 245 logmsg += "You can configure your scm commands in config/configuration.yml.\n"
Chris@909 246 logmsg += "#{strip_credential(cmd)}\n"
Chris@909 247 logmsg += "with: #{msg}"
Chris@909 248 logger.error(logmsg)
Chris@909 249 raise CommandFailed.new(msg)
Chris@909 250 end
Chris@909 251 end
Chris@909 252
Chris@909 253 # Hides username/password in a given command
Chris@909 254 def self.strip_credential(cmd)
Chris@909 255 q = (Redmine::Platform.mswin? ? '"' : "'")
Chris@909 256 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
Chris@909 257 end
Chris@909 258
Chris@909 259 def strip_credential(cmd)
Chris@909 260 self.class.strip_credential(cmd)
Chris@909 261 end
Chris@909 262
Chris@909 263 def scm_iconv(to, from, str)
Chris@909 264 return nil if str.nil?
Chris@909 265 return str if to == from
Chris@909 266 begin
Chris@909 267 Iconv.conv(to, from, str)
Chris@909 268 rescue Iconv::Failure => err
Chris@909 269 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@909 270 nil
Chris@909 271 end
Chris@909 272 end
Chris@909 273 end
Chris@909 274
Chris@909 275 class Entries < Array
Chris@909 276 def sort_by_name
Chris@909 277 sort {|x,y|
Chris@909 278 if x.kind == y.kind
Chris@909 279 x.name.to_s <=> y.name.to_s
Chris@909 280 else
Chris@909 281 x.kind <=> y.kind
Chris@909 282 end
Chris@909 283 }
Chris@909 284 end
Chris@909 285
Chris@909 286 def revisions
Chris@909 287 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
Chris@909 288 end
Chris@909 289 end
Chris@909 290
Chris@909 291 class Info
Chris@909 292 attr_accessor :root_url, :lastrev
Chris@909 293 def initialize(attributes={})
Chris@909 294 self.root_url = attributes[:root_url] if attributes[:root_url]
Chris@909 295 self.lastrev = attributes[:lastrev]
Chris@909 296 end
Chris@909 297 end
Chris@909 298
Chris@909 299 class Entry
Chris@909 300 attr_accessor :name, :path, :kind, :size, :lastrev
Chris@909 301 def initialize(attributes={})
Chris@909 302 self.name = attributes[:name] if attributes[:name]
Chris@909 303 self.path = attributes[:path] if attributes[:path]
Chris@909 304 self.kind = attributes[:kind] if attributes[:kind]
Chris@909 305 self.size = attributes[:size].to_i if attributes[:size]
Chris@909 306 self.lastrev = attributes[:lastrev]
Chris@909 307 end
Chris@909 308
Chris@909 309 def is_file?
Chris@909 310 'file' == self.kind
Chris@909 311 end
Chris@909 312
Chris@909 313 def is_dir?
Chris@909 314 'dir' == self.kind
Chris@909 315 end
Chris@909 316
Chris@909 317 def is_text?
Chris@909 318 Redmine::MimeType.is_type?('text', name)
Chris@909 319 end
Chris@909 320 end
Chris@909 321
Chris@909 322 class Revisions < Array
Chris@909 323 def latest
Chris@909 324 sort {|x,y|
Chris@909 325 unless x.time.nil? or y.time.nil?
Chris@909 326 x.time <=> y.time
Chris@909 327 else
Chris@909 328 0
Chris@909 329 end
Chris@909 330 }.last
Chris@909 331 end
Chris@909 332 end
Chris@909 333
Chris@909 334 class Revision
Chris@909 335 attr_accessor :scmid, :name, :author, :time, :message,
Chris@909 336 :paths, :revision, :branch, :identifier,
Chris@909 337 :parents
Chris@909 338
Chris@909 339 def initialize(attributes={})
Chris@909 340 self.identifier = attributes[:identifier]
Chris@909 341 self.scmid = attributes[:scmid]
Chris@909 342 self.name = attributes[:name] || self.identifier
Chris@909 343 self.author = attributes[:author]
Chris@909 344 self.time = attributes[:time]
Chris@909 345 self.message = attributes[:message] || ""
Chris@909 346 self.paths = attributes[:paths]
Chris@909 347 self.revision = attributes[:revision]
Chris@909 348 self.branch = attributes[:branch]
Chris@909 349 self.parents = attributes[:parents]
Chris@909 350 end
Chris@909 351
Chris@909 352 # Returns the readable identifier.
Chris@909 353 def format_identifier
Chris@909 354 self.identifier.to_s
Chris@909 355 end
Chris@909 356 end
Chris@909 357
Chris@909 358 class Annotate
Chris@909 359 attr_reader :lines, :revisions
Chris@909 360
Chris@909 361 def initialize
Chris@909 362 @lines = []
Chris@909 363 @revisions = []
Chris@909 364 end
Chris@909 365
Chris@909 366 def add_line(line, revision)
Chris@909 367 @lines << line
Chris@909 368 @revisions << revision
Chris@909 369 end
Chris@909 370
Chris@909 371 def content
Chris@909 372 content = lines.join("\n")
Chris@909 373 end
Chris@909 374
Chris@909 375 def empty?
Chris@909 376 lines.empty?
Chris@909 377 end
Chris@909 378 end
Chris@909 379
Chris@909 380 class Branch < String
Chris@909 381 attr_accessor :revision, :scmid
Chris@909 382 end
Chris@909 383 end
Chris@909 384 end
Chris@909 385 end