annotate lib/redmine/scm/adapters/abstract_adapter.rb @ 1480:75fd8eace091 issue_556

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