annotate lib/redmine/scm/adapters/.svn/text-base/abstract_adapter.rb.svn-base @ 853:969bb872d4bf feature_142

Close obsolete branch feature_142
author Chris Cannam
date Thu, 14 Jul 2011 14:26:44 +0100
parents cbce1fd3b1b7
children 0c939c159af4
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@0 27 class << self
Chris@245 28 def client_command
Chris@245 29 ""
Chris@245 30 end
Chris@245 31
Chris@0 32 # Returns the version of the scm client
Chris@0 33 # Eg: [1, 5, 0] or [] if unknown
Chris@0 34 def client_version
Chris@0 35 []
Chris@0 36 end
Chris@245 37
Chris@0 38 # Returns the version string of the scm client
Chris@0 39 # Eg: '1.5.0' or 'Unknown version' if unknown
Chris@0 40 def client_version_string
Chris@0 41 v = client_version || 'Unknown version'
Chris@0 42 v.is_a?(Array) ? v.join('.') : v.to_s
Chris@0 43 end
Chris@245 44
Chris@0 45 # Returns true if the current client version is above
Chris@0 46 # or equals the given one
Chris@0 47 # If option is :unknown is set to true, it will return
Chris@0 48 # true if the client version is unknown
Chris@0 49 def client_version_above?(v, options={})
Chris@0 50 ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
Chris@0 51 end
Chris@245 52
Chris@245 53 def client_available
Chris@245 54 true
Chris@245 55 end
Chris@245 56
Chris@245 57 def shell_quote(str)
Chris@245 58 if Redmine::Platform.mswin?
Chris@245 59 '"' + str.gsub(/"/, '\\"') + '"'
Chris@245 60 else
Chris@245 61 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@245 62 end
Chris@245 63 end
Chris@0 64 end
Chris@245 65
Chris@245 66 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@245 67 path_encoding=nil)
Chris@0 68 @url = url
Chris@0 69 @login = login if login && !login.empty?
Chris@0 70 @password = (password || "") if @login
Chris@0 71 @root_url = root_url.blank? ? retrieve_root_url : root_url
Chris@0 72 end
Chris@245 73
Chris@0 74 def adapter_name
Chris@0 75 'Abstract'
Chris@0 76 end
Chris@245 77
Chris@0 78 def supports_cat?
Chris@0 79 true
Chris@0 80 end
Chris@0 81
Chris@0 82 def supports_annotate?
Chris@0 83 respond_to?('annotate')
Chris@0 84 end
Chris@245 85
Chris@0 86 def root_url
Chris@0 87 @root_url
Chris@0 88 end
Chris@245 89
Chris@0 90 def url
Chris@0 91 @url
Chris@0 92 end
Chris@441 93
Chris@441 94 def path_encoding
Chris@441 95 nil
Chris@441 96 end
Chris@441 97
Chris@0 98 # get info about the svn repository
Chris@0 99 def info
Chris@0 100 return nil
Chris@0 101 end
Chris@441 102
Chris@0 103 # Returns the entry identified by path and revision identifier
Chris@0 104 # or nil if entry doesn't exist in the repository
Chris@0 105 def entry(path=nil, identifier=nil)
Chris@0 106 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
Chris@0 107 search_path = parts[0..-2].join('/')
Chris@0 108 search_name = parts[-1]
Chris@0 109 if search_path.blank? && search_name.blank?
Chris@0 110 # Root entry
Chris@0 111 Entry.new(:path => '', :kind => 'dir')
Chris@0 112 else
Chris@0 113 # Search for the entry in the parent directory
Chris@0 114 es = entries(search_path, identifier)
Chris@0 115 es ? es.detect {|e| e.name == search_name} : nil
Chris@0 116 end
Chris@0 117 end
Chris@441 118
Chris@0 119 # Returns an Entries collection
Chris@0 120 # or nil if the given path doesn't exist in the repository
Chris@441 121 def entries(path=nil, identifier=nil, options={})
Chris@0 122 return nil
Chris@0 123 end
Chris@0 124
Chris@0 125 def branches
Chris@0 126 return nil
Chris@0 127 end
Chris@0 128
Chris@441 129 def tags
Chris@0 130 return nil
Chris@0 131 end
Chris@0 132
Chris@0 133 def default_branch
Chris@0 134 return nil
Chris@0 135 end
Chris@441 136
Chris@0 137 def properties(path, identifier=nil)
Chris@0 138 return nil
Chris@0 139 end
Chris@441 140
Chris@0 141 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@0 142 return nil
Chris@0 143 end
Chris@441 144
Chris@0 145 def diff(path, identifier_from, identifier_to=nil)
Chris@0 146 return nil
Chris@0 147 end
Chris@441 148
Chris@0 149 def cat(path, identifier=nil)
Chris@0 150 return nil
Chris@0 151 end
Chris@441 152
Chris@0 153 def with_leading_slash(path)
Chris@0 154 path ||= ''
Chris@0 155 (path[0,1]!="/") ? "/#{path}" : path
Chris@0 156 end
Chris@0 157
Chris@0 158 def with_trailling_slash(path)
Chris@0 159 path ||= ''
Chris@0 160 (path[-1,1] == "/") ? path : "#{path}/"
Chris@0 161 end
Chris@245 162
Chris@0 163 def without_leading_slash(path)
Chris@0 164 path ||= ''
Chris@0 165 path.gsub(%r{^/+}, '')
Chris@0 166 end
Chris@0 167
Chris@0 168 def without_trailling_slash(path)
Chris@0 169 path ||= ''
Chris@0 170 (path[-1,1] == "/") ? path[0..-2] : path
Chris@0 171 end
Chris@245 172
Chris@0 173 def shell_quote(str)
Chris@245 174 self.class.shell_quote(str)
Chris@0 175 end
Chris@0 176
Chris@0 177 private
Chris@0 178 def retrieve_root_url
Chris@0 179 info = self.info
Chris@0 180 info ? info.root_url : nil
Chris@0 181 end
Chris@441 182
Chris@0 183 def target(path)
Chris@0 184 path ||= ''
Chris@0 185 base = path.match(/^\//) ? root_url : url
Chris@0 186 shell_quote("#{base}/#{path}".gsub(/[?<>\*]/, ''))
Chris@0 187 end
Chris@245 188
Chris@0 189 def logger
Chris@0 190 self.class.logger
Chris@0 191 end
Chris@245 192
Chris@0 193 def shellout(cmd, &block)
Chris@0 194 self.class.shellout(cmd, &block)
Chris@0 195 end
Chris@245 196
Chris@0 197 def self.logger
Chris@0 198 RAILS_DEFAULT_LOGGER
Chris@0 199 end
Chris@245 200
Chris@0 201 def self.shellout(cmd, &block)
Chris@0 202 logger.debug "Shelling out: #{strip_credential(cmd)}" if logger && logger.debug?
Chris@0 203 if Rails.env == 'development'
Chris@0 204 # Capture stderr when running in dev environment
Chris@0 205 cmd = "#{cmd} 2>>#{RAILS_ROOT}/log/scm.stderr.log"
Chris@0 206 end
Chris@0 207 begin
Chris@245 208 if RUBY_VERSION < '1.9'
Chris@245 209 mode = "r+"
Chris@245 210 else
Chris@245 211 mode = "r+:ASCII-8BIT"
Chris@245 212 end
Chris@245 213 IO.popen(cmd, mode) do |io|
Chris@0 214 io.close_write
Chris@0 215 block.call(io) if block_given?
Chris@0 216 end
Chris@0 217 rescue Errno::ENOENT => e
Chris@0 218 msg = strip_credential(e.message)
Chris@0 219 # The command failed, log it and re-raise
Chris@0 220 logger.error("SCM command failed, make sure that your SCM binary (eg. svn) is in PATH (#{ENV['PATH']}): #{strip_credential(cmd)}\n with: #{msg}")
Chris@0 221 raise CommandFailed.new(msg)
Chris@0 222 end
Chris@245 223 end
Chris@245 224
Chris@0 225 # Hides username/password in a given command
Chris@0 226 def self.strip_credential(cmd)
Chris@0 227 q = (Redmine::Platform.mswin? ? '"' : "'")
Chris@0 228 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
Chris@0 229 end
Chris@441 230
Chris@0 231 def strip_credential(cmd)
Chris@0 232 self.class.strip_credential(cmd)
Chris@0 233 end
Chris@245 234
Chris@245 235 def scm_iconv(to, from, str)
Chris@245 236 return nil if str.nil?
Chris@245 237 return str if to == from
Chris@245 238 begin
Chris@245 239 Iconv.conv(to, from, str)
Chris@245 240 rescue Iconv::Failure => err
Chris@245 241 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@245 242 nil
Chris@245 243 end
Chris@245 244 end
Chris@0 245 end
Chris@245 246
Chris@0 247 class Entries < Array
Chris@0 248 def sort_by_name
Chris@441 249 sort {|x,y|
Chris@0 250 if x.kind == y.kind
Chris@0 251 x.name.to_s <=> y.name.to_s
Chris@0 252 else
Chris@0 253 x.kind <=> y.kind
Chris@0 254 end
Chris@245 255 }
Chris@0 256 end
Chris@441 257
Chris@0 258 def revisions
Chris@0 259 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
Chris@0 260 end
Chris@0 261 end
Chris@441 262
Chris@0 263 class Info
Chris@0 264 attr_accessor :root_url, :lastrev
Chris@0 265 def initialize(attributes={})
Chris@0 266 self.root_url = attributes[:root_url] if attributes[:root_url]
Chris@0 267 self.lastrev = attributes[:lastrev]
Chris@0 268 end
Chris@0 269 end
Chris@441 270
Chris@0 271 class Entry
Chris@0 272 attr_accessor :name, :path, :kind, :size, :lastrev
Chris@0 273 def initialize(attributes={})
Chris@0 274 self.name = attributes[:name] if attributes[:name]
Chris@0 275 self.path = attributes[:path] if attributes[:path]
Chris@0 276 self.kind = attributes[:kind] if attributes[:kind]
Chris@0 277 self.size = attributes[:size].to_i if attributes[:size]
Chris@0 278 self.lastrev = attributes[:lastrev]
Chris@0 279 end
Chris@441 280
Chris@0 281 def is_file?
Chris@0 282 'file' == self.kind
Chris@0 283 end
Chris@441 284
Chris@0 285 def is_dir?
Chris@0 286 'dir' == self.kind
Chris@0 287 end
Chris@441 288
Chris@0 289 def is_text?
Chris@0 290 Redmine::MimeType.is_type?('text', name)
Chris@0 291 end
Chris@0 292 end
Chris@441 293
Chris@0 294 class Revisions < Array
Chris@0 295 def latest
Chris@0 296 sort {|x,y|
Chris@0 297 unless x.time.nil? or y.time.nil?
Chris@0 298 x.time <=> y.time
Chris@0 299 else
Chris@0 300 0
Chris@0 301 end
Chris@0 302 }.last
Chris@441 303 end
Chris@0 304 end
Chris@441 305
Chris@0 306 class Revision
Chris@441 307 attr_accessor :scmid, :name, :author, :time, :message,
Chris@441 308 :paths, :revision, :branch, :identifier
Chris@0 309
Chris@0 310 def initialize(attributes={})
Chris@0 311 self.identifier = attributes[:identifier]
Chris@441 312 self.scmid = attributes[:scmid]
Chris@441 313 self.name = attributes[:name] || self.identifier
Chris@441 314 self.author = attributes[:author]
Chris@441 315 self.time = attributes[:time]
Chris@441 316 self.message = attributes[:message] || ""
Chris@441 317 self.paths = attributes[:paths]
Chris@441 318 self.revision = attributes[:revision]
Chris@441 319 self.branch = attributes[:branch]
Chris@119 320 end
Chris@119 321
Chris@119 322 # Returns the readable identifier.
Chris@119 323 def format_identifier
Chris@441 324 self.identifier.to_s
Chris@119 325 end
Chris@245 326 end
Chris@119 327
Chris@0 328 class Annotate
Chris@0 329 attr_reader :lines, :revisions
Chris@441 330
Chris@0 331 def initialize
Chris@0 332 @lines = []
Chris@0 333 @revisions = []
Chris@0 334 end
Chris@441 335
Chris@0 336 def add_line(line, revision)
Chris@0 337 @lines << line
Chris@0 338 @revisions << revision
Chris@0 339 end
Chris@441 340
Chris@0 341 def content
Chris@0 342 content = lines.join("\n")
Chris@0 343 end
Chris@441 344
Chris@0 345 def empty?
Chris@0 346 lines.empty?
Chris@0 347 end
Chris@0 348 end
Chris@0 349 end
Chris@0 350 end
Chris@0 351 end