annotate lib/redmine/scm/adapters/abstract_adapter.rb @ 1474:c4436fec34bf bug_494

Close obsolete branch bug_494
author Chris Cannam
date Sat, 10 Nov 2012 13:57:53 +0000
parents 18beae6cb226
children bb32da3bea34
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@0 209 def shellout(cmd, &block)
Chris@0 210 self.class.shellout(cmd, &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@0 217 def self.shellout(cmd, &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@909 223 cmd = "#{cmd} 2>>#{Rails.root}/log/scm.stderr.log"
Chris@0 224 end
Chris@0 225 begin
Chris@245 226 if RUBY_VERSION < '1.9'
Chris@245 227 mode = "r+"
Chris@245 228 else
Chris@245 229 mode = "r+:ASCII-8BIT"
Chris@245 230 end
Chris@245 231 IO.popen(cmd, mode) do |io|
Chris@0 232 io.close_write
Chris@0 233 block.call(io) if block_given?
Chris@0 234 end
Chris@909 235 ## If scm command does not exist,
Chris@909 236 ## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException
Chris@909 237 ## in production environment.
Chris@909 238 # rescue Errno::ENOENT => e
Chris@909 239 rescue Exception => e
Chris@0 240 msg = strip_credential(e.message)
Chris@0 241 # The command failed, log it and re-raise
Chris@507 242 logmsg = "SCM command failed, "
Chris@507 243 logmsg += "make sure that your SCM command (e.g. svn) is "
Chris@507 244 logmsg += "in PATH (#{ENV['PATH']})\n"
Chris@507 245 logmsg += "You can configure your scm commands in config/configuration.yml.\n"
Chris@507 246 logmsg += "#{strip_credential(cmd)}\n"
Chris@507 247 logmsg += "with: #{msg}"
Chris@507 248 logger.error(logmsg)
Chris@0 249 raise CommandFailed.new(msg)
Chris@0 250 end
Chris@245 251 end
Chris@245 252
Chris@0 253 # Hides username/password in a given command
Chris@0 254 def self.strip_credential(cmd)
Chris@0 255 q = (Redmine::Platform.mswin? ? '"' : "'")
Chris@0 256 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
Chris@0 257 end
Chris@441 258
Chris@0 259 def strip_credential(cmd)
Chris@0 260 self.class.strip_credential(cmd)
Chris@0 261 end
Chris@245 262
Chris@245 263 def scm_iconv(to, from, str)
Chris@245 264 return nil if str.nil?
Chris@922 265 # bug 446: non-utf8 paths in repositories blow up repo viewer and reposman
Chris@922 266 # -- Remove this short-circuit: we want the conversion to
Chris@922 267 # happen always, so we can trap the error here if the
Chris@922 268 # source text happens not to be in the advertised
Chris@922 269 # encoding (instead of having the database blow up later)
Chris@922 270 # return str if to == from
Chris@245 271 begin
Chris@245 272 Iconv.conv(to, from, str)
Chris@245 273 rescue Iconv::Failure => err
Chris@245 274 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@245 275 nil
Chris@245 276 end
Chris@245 277 end
Chris@0 278 end
Chris@245 279
Chris@0 280 class Entries < Array
Chris@0 281 def sort_by_name
Chris@441 282 sort {|x,y|
Chris@0 283 if x.kind == y.kind
Chris@0 284 x.name.to_s <=> y.name.to_s
Chris@0 285 else
Chris@0 286 x.kind <=> y.kind
Chris@0 287 end
Chris@245 288 }
Chris@0 289 end
Chris@441 290
Chris@0 291 def revisions
Chris@0 292 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
Chris@0 293 end
Chris@0 294 end
Chris@441 295
Chris@0 296 class Info
Chris@0 297 attr_accessor :root_url, :lastrev
Chris@0 298 def initialize(attributes={})
Chris@0 299 self.root_url = attributes[:root_url] if attributes[:root_url]
Chris@0 300 self.lastrev = attributes[:lastrev]
Chris@0 301 end
Chris@0 302 end
Chris@441 303
Chris@0 304 class Entry
Chris@0 305 attr_accessor :name, :path, :kind, :size, :lastrev
Chris@0 306 def initialize(attributes={})
Chris@0 307 self.name = attributes[:name] if attributes[:name]
Chris@0 308 self.path = attributes[:path] if attributes[:path]
Chris@0 309 self.kind = attributes[:kind] if attributes[:kind]
Chris@0 310 self.size = attributes[:size].to_i if attributes[:size]
Chris@0 311 self.lastrev = attributes[:lastrev]
Chris@0 312 end
Chris@441 313
Chris@0 314 def is_file?
Chris@0 315 'file' == self.kind
Chris@0 316 end
Chris@441 317
Chris@0 318 def is_dir?
Chris@0 319 'dir' == self.kind
Chris@0 320 end
Chris@441 321
Chris@0 322 def is_text?
Chris@0 323 Redmine::MimeType.is_type?('text', name)
Chris@0 324 end
Chris@0 325 end
Chris@441 326
Chris@0 327 class Revisions < Array
Chris@0 328 def latest
Chris@0 329 sort {|x,y|
Chris@0 330 unless x.time.nil? or y.time.nil?
Chris@0 331 x.time <=> y.time
Chris@0 332 else
Chris@0 333 0
Chris@0 334 end
Chris@0 335 }.last
Chris@441 336 end
Chris@0 337 end
Chris@441 338
Chris@0 339 class Revision
Chris@441 340 attr_accessor :scmid, :name, :author, :time, :message,
Chris@909 341 :paths, :revision, :branch, :identifier,
Chris@909 342 :parents
Chris@0 343
Chris@0 344 def initialize(attributes={})
Chris@0 345 self.identifier = attributes[:identifier]
Chris@441 346 self.scmid = attributes[:scmid]
Chris@441 347 self.name = attributes[:name] || self.identifier
Chris@441 348 self.author = attributes[:author]
Chris@441 349 self.time = attributes[:time]
Chris@441 350 self.message = attributes[:message] || ""
Chris@441 351 self.paths = attributes[:paths]
Chris@441 352 self.revision = attributes[:revision]
Chris@441 353 self.branch = attributes[:branch]
Chris@909 354 self.parents = attributes[:parents]
Chris@117 355 end
Chris@117 356
Chris@117 357 # Returns the readable identifier.
Chris@117 358 def format_identifier
Chris@441 359 self.identifier.to_s
Chris@117 360 end
Chris@245 361 end
Chris@117 362
Chris@0 363 class Annotate
Chris@0 364 attr_reader :lines, :revisions
Chris@441 365
Chris@0 366 def initialize
Chris@0 367 @lines = []
Chris@0 368 @revisions = []
Chris@0 369 end
Chris@441 370
Chris@0 371 def add_line(line, revision)
Chris@0 372 @lines << line
Chris@0 373 @revisions << revision
Chris@0 374 end
Chris@441 375
Chris@0 376 def content
Chris@0 377 content = lines.join("\n")
Chris@0 378 end
Chris@441 379
Chris@0 380 def empty?
Chris@0 381 lines.empty?
Chris@0 382 end
Chris@0 383 end
Chris@909 384
Chris@909 385 class Branch < String
Chris@909 386 attr_accessor :revision, :scmid
Chris@909 387 end
Chris@0 388 end
Chris@0 389 end
Chris@0 390 end