annotate lib/redmine/scm/adapters/abstract_adapter.rb @ 1295:622f24f53b42 redmine-2.3

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