annotate .svn/pristine/a8/a8fc14f9eb6b10ebe9da4200ebea010b2daee352.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 require 'cgi'
Chris@1464 19
Chris@1464 20 if RUBY_VERSION < '1.9'
Chris@1464 21 require 'iconv'
Chris@1464 22 end
Chris@1464 23
Chris@1464 24 module Redmine
Chris@1464 25 module Scm
Chris@1464 26 module Adapters
Chris@1464 27 class CommandFailed < StandardError #:nodoc:
Chris@1464 28 end
Chris@1464 29
Chris@1464 30 class AbstractAdapter #:nodoc:
Chris@1464 31
Chris@1464 32 # raised if scm command exited with error, e.g. unknown revision.
Chris@1464 33 class ScmCommandAborted < CommandFailed; end
Chris@1464 34
Chris@1464 35 class << self
Chris@1464 36 def client_command
Chris@1464 37 ""
Chris@1464 38 end
Chris@1464 39
Chris@1464 40 def shell_quote_command
Chris@1464 41 if Redmine::Platform.mswin? && RUBY_PLATFORM == 'java'
Chris@1464 42 client_command
Chris@1464 43 else
Chris@1464 44 shell_quote(client_command)
Chris@1464 45 end
Chris@1464 46 end
Chris@1464 47
Chris@1464 48 # Returns the version of the scm client
Chris@1464 49 # Eg: [1, 5, 0] or [] if unknown
Chris@1464 50 def client_version
Chris@1464 51 []
Chris@1464 52 end
Chris@1464 53
Chris@1464 54 # Returns the version string of the scm client
Chris@1464 55 # Eg: '1.5.0' or 'Unknown version' if unknown
Chris@1464 56 def client_version_string
Chris@1464 57 v = client_version || 'Unknown version'
Chris@1464 58 v.is_a?(Array) ? v.join('.') : v.to_s
Chris@1464 59 end
Chris@1464 60
Chris@1464 61 # Returns true if the current client version is above
Chris@1464 62 # or equals the given one
Chris@1464 63 # If option is :unknown is set to true, it will return
Chris@1464 64 # true if the client version is unknown
Chris@1464 65 def client_version_above?(v, options={})
Chris@1464 66 ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
Chris@1464 67 end
Chris@1464 68
Chris@1464 69 def client_available
Chris@1464 70 true
Chris@1464 71 end
Chris@1464 72
Chris@1464 73 def shell_quote(str)
Chris@1464 74 if Redmine::Platform.mswin?
Chris@1464 75 '"' + str.gsub(/"/, '\\"') + '"'
Chris@1464 76 else
Chris@1464 77 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@1464 78 end
Chris@1464 79 end
Chris@1464 80 end
Chris@1464 81
Chris@1464 82 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@1464 83 path_encoding=nil)
Chris@1464 84 @url = url
Chris@1464 85 @login = login if login && !login.empty?
Chris@1464 86 @password = (password || "") if @login
Chris@1464 87 @root_url = root_url.blank? ? retrieve_root_url : root_url
Chris@1464 88 end
Chris@1464 89
Chris@1464 90 def adapter_name
Chris@1464 91 'Abstract'
Chris@1464 92 end
Chris@1464 93
Chris@1464 94 def supports_cat?
Chris@1464 95 true
Chris@1464 96 end
Chris@1464 97
Chris@1464 98 def supports_annotate?
Chris@1464 99 respond_to?('annotate')
Chris@1464 100 end
Chris@1464 101
Chris@1464 102 def root_url
Chris@1464 103 @root_url
Chris@1464 104 end
Chris@1464 105
Chris@1464 106 def url
Chris@1464 107 @url
Chris@1464 108 end
Chris@1464 109
Chris@1464 110 def path_encoding
Chris@1464 111 nil
Chris@1464 112 end
Chris@1464 113
Chris@1464 114 # get info about the svn repository
Chris@1464 115 def info
Chris@1464 116 return nil
Chris@1464 117 end
Chris@1464 118
Chris@1464 119 # Returns the entry identified by path and revision identifier
Chris@1464 120 # or nil if entry doesn't exist in the repository
Chris@1464 121 def entry(path=nil, identifier=nil)
Chris@1464 122 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
Chris@1464 123 search_path = parts[0..-2].join('/')
Chris@1464 124 search_name = parts[-1]
Chris@1464 125 if search_path.blank? && search_name.blank?
Chris@1464 126 # Root entry
Chris@1464 127 Entry.new(:path => '', :kind => 'dir')
Chris@1464 128 else
Chris@1464 129 # Search for the entry in the parent directory
Chris@1464 130 es = entries(search_path, identifier)
Chris@1464 131 es ? es.detect {|e| e.name == search_name} : nil
Chris@1464 132 end
Chris@1464 133 end
Chris@1464 134
Chris@1464 135 # Returns an Entries collection
Chris@1464 136 # or nil if the given path doesn't exist in the repository
Chris@1464 137 def entries(path=nil, identifier=nil, options={})
Chris@1464 138 return nil
Chris@1464 139 end
Chris@1464 140
Chris@1464 141 def branches
Chris@1464 142 return nil
Chris@1464 143 end
Chris@1464 144
Chris@1464 145 def tags
Chris@1464 146 return nil
Chris@1464 147 end
Chris@1464 148
Chris@1464 149 def default_branch
Chris@1464 150 return nil
Chris@1464 151 end
Chris@1464 152
Chris@1464 153 def properties(path, identifier=nil)
Chris@1464 154 return nil
Chris@1464 155 end
Chris@1464 156
Chris@1464 157 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@1464 158 return nil
Chris@1464 159 end
Chris@1464 160
Chris@1464 161 def diff(path, identifier_from, identifier_to=nil)
Chris@1464 162 return nil
Chris@1464 163 end
Chris@1464 164
Chris@1464 165 def cat(path, identifier=nil)
Chris@1464 166 return nil
Chris@1464 167 end
Chris@1464 168
Chris@1464 169 def with_leading_slash(path)
Chris@1464 170 path ||= ''
Chris@1464 171 (path[0,1]!="/") ? "/#{path}" : path
Chris@1464 172 end
Chris@1464 173
Chris@1464 174 def with_trailling_slash(path)
Chris@1464 175 path ||= ''
Chris@1464 176 (path[-1,1] == "/") ? path : "#{path}/"
Chris@1464 177 end
Chris@1464 178
Chris@1464 179 def without_leading_slash(path)
Chris@1464 180 path ||= ''
Chris@1464 181 path.gsub(%r{^/+}, '')
Chris@1464 182 end
Chris@1464 183
Chris@1464 184 def without_trailling_slash(path)
Chris@1464 185 path ||= ''
Chris@1464 186 (path[-1,1] == "/") ? path[0..-2] : path
Chris@1464 187 end
Chris@1464 188
Chris@1464 189 def shell_quote(str)
Chris@1464 190 self.class.shell_quote(str)
Chris@1464 191 end
Chris@1464 192
Chris@1464 193 private
Chris@1464 194 def retrieve_root_url
Chris@1464 195 info = self.info
Chris@1464 196 info ? info.root_url : nil
Chris@1464 197 end
Chris@1464 198
Chris@1464 199 def target(path, sq=true)
Chris@1464 200 path ||= ''
Chris@1464 201 base = path.match(/^\//) ? root_url : url
Chris@1464 202 str = "#{base}/#{path}".gsub(/[?<>\*]/, '')
Chris@1464 203 if sq
Chris@1464 204 str = shell_quote(str)
Chris@1464 205 end
Chris@1464 206 str
Chris@1464 207 end
Chris@1464 208
Chris@1464 209 def logger
Chris@1464 210 self.class.logger
Chris@1464 211 end
Chris@1464 212
Chris@1464 213 def shellout(cmd, options = {}, &block)
Chris@1464 214 self.class.shellout(cmd, options, &block)
Chris@1464 215 end
Chris@1464 216
Chris@1464 217 def self.logger
Chris@1464 218 Rails.logger
Chris@1464 219 end
Chris@1464 220
Chris@1464 221 # Path to the file where scm stderr output is logged
Chris@1464 222 # Returns nil if the log file is not writable
Chris@1464 223 def self.stderr_log_file
Chris@1464 224 if @stderr_log_file.nil?
Chris@1464 225 writable = false
Chris@1464 226 path = Redmine::Configuration['scm_stderr_log_file'].presence
Chris@1464 227 path ||= Rails.root.join("log/#{Rails.env}.scm.stderr.log").to_s
Chris@1464 228 if File.exists?(path)
Chris@1464 229 if File.file?(path) && File.writable?(path)
Chris@1464 230 writable = true
Chris@1464 231 else
Chris@1464 232 logger.warn("SCM log file (#{path}) is not writable")
Chris@1464 233 end
Chris@1464 234 else
Chris@1464 235 begin
Chris@1464 236 File.open(path, "w") {}
Chris@1464 237 writable = true
Chris@1464 238 rescue => e
Chris@1464 239 logger.warn("SCM log file (#{path}) cannot be created: #{e.message}")
Chris@1464 240 end
Chris@1464 241 end
Chris@1464 242 @stderr_log_file = writable ? path : false
Chris@1464 243 end
Chris@1464 244 @stderr_log_file || nil
Chris@1464 245 end
Chris@1464 246
Chris@1464 247 def self.shellout(cmd, options = {}, &block)
Chris@1464 248 if logger && logger.debug?
Chris@1464 249 logger.debug "Shelling out: #{strip_credential(cmd)}"
Chris@1464 250 # Capture stderr in a log file
Chris@1464 251 if stderr_log_file
Chris@1464 252 cmd = "#{cmd} 2>>#{shell_quote(stderr_log_file)}"
Chris@1464 253 end
Chris@1464 254 end
Chris@1464 255 begin
Chris@1464 256 mode = "r+"
Chris@1464 257 IO.popen(cmd, mode) do |io|
Chris@1464 258 io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
Chris@1464 259 io.close_write unless options[:write_stdin]
Chris@1464 260 block.call(io) if block_given?
Chris@1464 261 end
Chris@1464 262 ## If scm command does not exist,
Chris@1464 263 ## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException
Chris@1464 264 ## in production environment.
Chris@1464 265 # rescue Errno::ENOENT => e
Chris@1464 266 rescue Exception => e
Chris@1464 267 msg = strip_credential(e.message)
Chris@1464 268 # The command failed, log it and re-raise
Chris@1464 269 logmsg = "SCM command failed, "
Chris@1464 270 logmsg += "make sure that your SCM command (e.g. svn) is "
Chris@1464 271 logmsg += "in PATH (#{ENV['PATH']})\n"
Chris@1464 272 logmsg += "You can configure your scm commands in config/configuration.yml.\n"
Chris@1464 273 logmsg += "#{strip_credential(cmd)}\n"
Chris@1464 274 logmsg += "with: #{msg}"
Chris@1464 275 logger.error(logmsg)
Chris@1464 276 raise CommandFailed.new(msg)
Chris@1464 277 end
Chris@1464 278 end
Chris@1464 279
Chris@1464 280 # Hides username/password in a given command
Chris@1464 281 def self.strip_credential(cmd)
Chris@1464 282 q = (Redmine::Platform.mswin? ? '"' : "'")
Chris@1464 283 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
Chris@1464 284 end
Chris@1464 285
Chris@1464 286 def strip_credential(cmd)
Chris@1464 287 self.class.strip_credential(cmd)
Chris@1464 288 end
Chris@1464 289
Chris@1464 290 def scm_iconv(to, from, str)
Chris@1464 291 return nil if str.nil?
Chris@1464 292 return str if to == from
Chris@1464 293 if str.respond_to?(:force_encoding)
Chris@1464 294 str.force_encoding(from)
Chris@1464 295 begin
Chris@1464 296 str.encode(to)
Chris@1464 297 rescue Exception => err
Chris@1464 298 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@1464 299 nil
Chris@1464 300 end
Chris@1464 301 else
Chris@1464 302 begin
Chris@1464 303 Iconv.conv(to, from, str)
Chris@1464 304 rescue Iconv::Failure => err
Chris@1464 305 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@1464 306 nil
Chris@1464 307 end
Chris@1464 308 end
Chris@1464 309 end
Chris@1464 310
Chris@1464 311 def parse_xml(xml)
Chris@1464 312 if RUBY_PLATFORM == 'java'
Chris@1464 313 xml = xml.sub(%r{<\?xml[^>]*\?>}, '')
Chris@1464 314 end
Chris@1464 315 ActiveSupport::XmlMini.parse(xml)
Chris@1464 316 end
Chris@1464 317 end
Chris@1464 318
Chris@1464 319 class Entries < Array
Chris@1464 320 def sort_by_name
Chris@1464 321 dup.sort! {|x,y|
Chris@1464 322 if x.kind == y.kind
Chris@1464 323 x.name.to_s <=> y.name.to_s
Chris@1464 324 else
Chris@1464 325 x.kind <=> y.kind
Chris@1464 326 end
Chris@1464 327 }
Chris@1464 328 end
Chris@1464 329
Chris@1464 330 def revisions
Chris@1464 331 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
Chris@1464 332 end
Chris@1464 333 end
Chris@1464 334
Chris@1464 335 class Info
Chris@1464 336 attr_accessor :root_url, :lastrev
Chris@1464 337 def initialize(attributes={})
Chris@1464 338 self.root_url = attributes[:root_url] if attributes[:root_url]
Chris@1464 339 self.lastrev = attributes[:lastrev]
Chris@1464 340 end
Chris@1464 341 end
Chris@1464 342
Chris@1464 343 class Entry
Chris@1464 344 attr_accessor :name, :path, :kind, :size, :lastrev, :changeset
Chris@1464 345
Chris@1464 346 def initialize(attributes={})
Chris@1464 347 self.name = attributes[:name] if attributes[:name]
Chris@1464 348 self.path = attributes[:path] if attributes[:path]
Chris@1464 349 self.kind = attributes[:kind] if attributes[:kind]
Chris@1464 350 self.size = attributes[:size].to_i if attributes[:size]
Chris@1464 351 self.lastrev = attributes[:lastrev]
Chris@1464 352 end
Chris@1464 353
Chris@1464 354 def is_file?
Chris@1464 355 'file' == self.kind
Chris@1464 356 end
Chris@1464 357
Chris@1464 358 def is_dir?
Chris@1464 359 'dir' == self.kind
Chris@1464 360 end
Chris@1464 361
Chris@1464 362 def is_text?
Chris@1464 363 Redmine::MimeType.is_type?('text', name)
Chris@1464 364 end
Chris@1464 365
Chris@1464 366 def author
Chris@1464 367 if changeset
Chris@1464 368 changeset.author.to_s
Chris@1464 369 elsif lastrev
Chris@1464 370 Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first)
Chris@1464 371 end
Chris@1464 372 end
Chris@1464 373 end
Chris@1464 374
Chris@1464 375 class Revisions < Array
Chris@1464 376 def latest
Chris@1464 377 sort {|x,y|
Chris@1464 378 unless x.time.nil? or y.time.nil?
Chris@1464 379 x.time <=> y.time
Chris@1464 380 else
Chris@1464 381 0
Chris@1464 382 end
Chris@1464 383 }.last
Chris@1464 384 end
Chris@1464 385 end
Chris@1464 386
Chris@1464 387 class Revision
Chris@1464 388 attr_accessor :scmid, :name, :author, :time, :message,
Chris@1464 389 :paths, :revision, :branch, :identifier,
Chris@1464 390 :parents
Chris@1464 391
Chris@1464 392 def initialize(attributes={})
Chris@1464 393 self.identifier = attributes[:identifier]
Chris@1464 394 self.scmid = attributes[:scmid]
Chris@1464 395 self.name = attributes[:name] || self.identifier
Chris@1464 396 self.author = attributes[:author]
Chris@1464 397 self.time = attributes[:time]
Chris@1464 398 self.message = attributes[:message] || ""
Chris@1464 399 self.paths = attributes[:paths]
Chris@1464 400 self.revision = attributes[:revision]
Chris@1464 401 self.branch = attributes[:branch]
Chris@1464 402 self.parents = attributes[:parents]
Chris@1464 403 end
Chris@1464 404
Chris@1464 405 # Returns the readable identifier.
Chris@1464 406 def format_identifier
Chris@1464 407 self.identifier.to_s
Chris@1464 408 end
Chris@1464 409
Chris@1464 410 def ==(other)
Chris@1464 411 if other.nil?
Chris@1464 412 false
Chris@1464 413 elsif scmid.present?
Chris@1464 414 scmid == other.scmid
Chris@1464 415 elsif identifier.present?
Chris@1464 416 identifier == other.identifier
Chris@1464 417 elsif revision.present?
Chris@1464 418 revision == other.revision
Chris@1464 419 end
Chris@1464 420 end
Chris@1464 421 end
Chris@1464 422
Chris@1464 423 class Annotate
Chris@1464 424 attr_reader :lines, :revisions
Chris@1464 425
Chris@1464 426 def initialize
Chris@1464 427 @lines = []
Chris@1464 428 @revisions = []
Chris@1464 429 end
Chris@1464 430
Chris@1464 431 def add_line(line, revision)
Chris@1464 432 @lines << line
Chris@1464 433 @revisions << revision
Chris@1464 434 end
Chris@1464 435
Chris@1464 436 def content
Chris@1464 437 content = lines.join("\n")
Chris@1464 438 end
Chris@1464 439
Chris@1464 440 def empty?
Chris@1464 441 lines.empty?
Chris@1464 442 end
Chris@1464 443 end
Chris@1464 444
Chris@1464 445 class Branch < String
Chris@1464 446 attr_accessor :revision, :scmid
Chris@1464 447 end
Chris@1464 448 end
Chris@1464 449 end
Chris@1464 450 end