annotate .svn/pristine/0b/0bba8bba58df7fc70a5a93a0d2973b34736d639d.svn-base @ 1464:261b3d9a4903 redmine-2.4

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