annotate lib/redmine/scm/adapters/abstract_adapter.rb @ 1517:dffacf8a6908 redmine-2.5

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