annotate lib/redmine/scm/adapters/abstract_adapter.rb @ 1613:90bed4e10cc8 deploy

Download file link
author Chris Cannam
date Wed, 30 Aug 2017 17:24:37 +0100
parents a1bdbf8a87d5
children
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@922 290 # bug 446: non-utf8 paths in repositories blow up repo viewer and reposman
Chris@922 291 # -- Remove this short-circuit: we want the conversion to
Chris@922 292 # happen always, so we can trap the error here if the
Chris@922 293 # source text happens not to be in the advertised
Chris@922 294 # encoding (instead of having the database blow up later)
Chris@922 295 # return str if to == from
Chris@1115 296 if str.respond_to?(:force_encoding)
Chris@1115 297 str.force_encoding(from)
Chris@1115 298 begin
Chris@1115 299 str.encode(to)
Chris@1115 300 rescue Exception => err
Chris@1115 301 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@1115 302 nil
Chris@1115 303 end
Chris@1115 304 else
Chris@1115 305 begin
Chris@1115 306 Iconv.conv(to, from, str)
Chris@1115 307 rescue Iconv::Failure => err
Chris@1115 308 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@1115 309 nil
Chris@1115 310 end
Chris@245 311 end
Chris@245 312 end
Chris@1115 313
Chris@1115 314 def parse_xml(xml)
Chris@1115 315 if RUBY_PLATFORM == 'java'
Chris@1115 316 xml = xml.sub(%r{<\?xml[^>]*\?>}, '')
Chris@1115 317 end
Chris@1115 318 ActiveSupport::XmlMini.parse(xml)
Chris@1115 319 end
Chris@0 320 end
Chris@245 321
Chris@0 322 class Entries < Array
Chris@0 323 def sort_by_name
Chris@1115 324 dup.sort! {|x,y|
Chris@0 325 if x.kind == y.kind
Chris@0 326 x.name.to_s <=> y.name.to_s
Chris@0 327 else
Chris@0 328 x.kind <=> y.kind
Chris@0 329 end
Chris@245 330 }
Chris@0 331 end
Chris@441 332
Chris@0 333 def revisions
Chris@0 334 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
Chris@0 335 end
Chris@0 336 end
Chris@441 337
Chris@0 338 class Info
Chris@0 339 attr_accessor :root_url, :lastrev
Chris@0 340 def initialize(attributes={})
Chris@0 341 self.root_url = attributes[:root_url] if attributes[:root_url]
Chris@0 342 self.lastrev = attributes[:lastrev]
Chris@0 343 end
Chris@0 344 end
Chris@441 345
Chris@0 346 class Entry
Chris@1115 347 attr_accessor :name, :path, :kind, :size, :lastrev, :changeset
Chris@1115 348
Chris@0 349 def initialize(attributes={})
Chris@0 350 self.name = attributes[:name] if attributes[:name]
Chris@0 351 self.path = attributes[:path] if attributes[:path]
Chris@0 352 self.kind = attributes[:kind] if attributes[:kind]
Chris@0 353 self.size = attributes[:size].to_i if attributes[:size]
Chris@0 354 self.lastrev = attributes[:lastrev]
Chris@0 355 end
Chris@441 356
Chris@0 357 def is_file?
Chris@0 358 'file' == self.kind
Chris@0 359 end
Chris@441 360
Chris@0 361 def is_dir?
Chris@0 362 'dir' == self.kind
Chris@0 363 end
Chris@441 364
Chris@0 365 def is_text?
Chris@0 366 Redmine::MimeType.is_type?('text', name)
Chris@0 367 end
Chris@1115 368
Chris@1115 369 def author
Chris@1115 370 if changeset
Chris@1115 371 changeset.author.to_s
Chris@1115 372 elsif lastrev
Chris@1115 373 Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first)
Chris@1115 374 end
Chris@1115 375 end
Chris@0 376 end
Chris@441 377
Chris@0 378 class Revisions < Array
Chris@0 379 def latest
Chris@0 380 sort {|x,y|
Chris@0 381 unless x.time.nil? or y.time.nil?
Chris@0 382 x.time <=> y.time
Chris@0 383 else
Chris@0 384 0
Chris@0 385 end
Chris@0 386 }.last
Chris@441 387 end
Chris@0 388 end
Chris@441 389
Chris@0 390 class Revision
Chris@441 391 attr_accessor :scmid, :name, :author, :time, :message,
Chris@909 392 :paths, :revision, :branch, :identifier,
Chris@909 393 :parents
Chris@0 394
Chris@0 395 def initialize(attributes={})
Chris@0 396 self.identifier = attributes[:identifier]
Chris@441 397 self.scmid = attributes[:scmid]
Chris@441 398 self.name = attributes[:name] || self.identifier
Chris@441 399 self.author = attributes[:author]
Chris@441 400 self.time = attributes[:time]
Chris@441 401 self.message = attributes[:message] || ""
Chris@441 402 self.paths = attributes[:paths]
Chris@441 403 self.revision = attributes[:revision]
Chris@441 404 self.branch = attributes[:branch]
Chris@909 405 self.parents = attributes[:parents]
Chris@117 406 end
Chris@117 407
Chris@117 408 # Returns the readable identifier.
Chris@117 409 def format_identifier
Chris@441 410 self.identifier.to_s
Chris@117 411 end
Chris@1115 412
Chris@1115 413 def ==(other)
Chris@1115 414 if other.nil?
Chris@1115 415 false
Chris@1115 416 elsif scmid.present?
Chris@1115 417 scmid == other.scmid
Chris@1115 418 elsif identifier.present?
Chris@1115 419 identifier == other.identifier
Chris@1115 420 elsif revision.present?
Chris@1115 421 revision == other.revision
Chris@1115 422 end
Chris@1115 423 end
Chris@245 424 end
Chris@117 425
Chris@0 426 class Annotate
Chris@0 427 attr_reader :lines, :revisions
Chris@441 428
Chris@0 429 def initialize
Chris@0 430 @lines = []
Chris@0 431 @revisions = []
Chris@0 432 end
Chris@441 433
Chris@0 434 def add_line(line, revision)
Chris@0 435 @lines << line
Chris@0 436 @revisions << revision
Chris@0 437 end
Chris@441 438
Chris@0 439 def content
Chris@0 440 content = lines.join("\n")
Chris@0 441 end
Chris@441 442
Chris@0 443 def empty?
Chris@0 444 lines.empty?
Chris@0 445 end
Chris@0 446 end
Chris@909 447
Chris@909 448 class Branch < String
Chris@909 449 attr_accessor :revision, :scmid
Chris@909 450 end
Chris@0 451 end
Chris@0 452 end
Chris@0 453 end