To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / scm / adapters / .svn / text-base / abstract_adapter.rb.svn-base @ 441:cbce1fd3b1b7
History | View | Annotate | Download (9.4 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software |
|---|---|---|---|
| 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
||
| 3 | 0:513646585e45 | Chris | # |
| 4 | # This program is free software; you can redistribute it and/or |
||
| 5 | # modify it under the terms of the GNU General Public License |
||
| 6 | # as published by the Free Software Foundation; either version 2 |
||
| 7 | # of the License, or (at your option) any later version. |
||
| 8 | 441:cbce1fd3b1b7 | Chris | # |
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | # GNU General Public License for more details. |
||
| 13 | 441:cbce1fd3b1b7 | Chris | # |
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program; if not, write to the Free Software |
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
| 17 | |||
| 18 | require 'cgi' |
||
| 19 | |||
| 20 | module Redmine |
||
| 21 | module Scm |
||
| 22 | 245:051f544170fe | Chris | module Adapters |
| 23 | 0:513646585e45 | Chris | class CommandFailed < StandardError #:nodoc: |
| 24 | end |
||
| 25 | 245:051f544170fe | Chris | |
| 26 | 0:513646585e45 | Chris | class AbstractAdapter #:nodoc: |
| 27 | class << self |
||
| 28 | 245:051f544170fe | Chris | def client_command |
| 29 | "" |
||
| 30 | end |
||
| 31 | |||
| 32 | 0:513646585e45 | Chris | # Returns the version of the scm client |
| 33 | # Eg: [1, 5, 0] or [] if unknown |
||
| 34 | def client_version |
||
| 35 | [] |
||
| 36 | end |
||
| 37 | 245:051f544170fe | Chris | |
| 38 | 0:513646585e45 | Chris | # Returns the version string of the scm client |
| 39 | # Eg: '1.5.0' or 'Unknown version' if unknown |
||
| 40 | def client_version_string |
||
| 41 | v = client_version || 'Unknown version' |
||
| 42 | v.is_a?(Array) ? v.join('.') : v.to_s
|
||
| 43 | end |
||
| 44 | 245:051f544170fe | Chris | |
| 45 | 0:513646585e45 | Chris | # Returns true if the current client version is above |
| 46 | # or equals the given one |
||
| 47 | # If option is :unknown is set to true, it will return |
||
| 48 | # true if the client version is unknown |
||
| 49 | def client_version_above?(v, options={})
|
||
| 50 | ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown]) |
||
| 51 | end |
||
| 52 | 245:051f544170fe | Chris | |
| 53 | def client_available |
||
| 54 | true |
||
| 55 | end |
||
| 56 | |||
| 57 | def shell_quote(str) |
||
| 58 | if Redmine::Platform.mswin? |
||
| 59 | '"' + str.gsub(/"/, '\\"') + '"' |
||
| 60 | else |
||
| 61 | "'" + str.gsub(/'/, "'\"'\"'") + "'" |
||
| 62 | end |
||
| 63 | end |
||
| 64 | 0:513646585e45 | Chris | end |
| 65 | 245:051f544170fe | Chris | |
| 66 | def initialize(url, root_url=nil, login=nil, password=nil, |
||
| 67 | path_encoding=nil) |
||
| 68 | 0:513646585e45 | Chris | @url = url |
| 69 | @login = login if login && !login.empty? |
||
| 70 | @password = (password || "") if @login |
||
| 71 | @root_url = root_url.blank? ? retrieve_root_url : root_url |
||
| 72 | end |
||
| 73 | 245:051f544170fe | Chris | |
| 74 | 0:513646585e45 | Chris | def adapter_name |
| 75 | 'Abstract' |
||
| 76 | end |
||
| 77 | 245:051f544170fe | Chris | |
| 78 | 0:513646585e45 | Chris | def supports_cat? |
| 79 | true |
||
| 80 | end |
||
| 81 | |||
| 82 | def supports_annotate? |
||
| 83 | respond_to?('annotate')
|
||
| 84 | end |
||
| 85 | 245:051f544170fe | Chris | |
| 86 | 0:513646585e45 | Chris | def root_url |
| 87 | @root_url |
||
| 88 | end |
||
| 89 | 245:051f544170fe | Chris | |
| 90 | 0:513646585e45 | Chris | def url |
| 91 | @url |
||
| 92 | end |
||
| 93 | 441:cbce1fd3b1b7 | Chris | |
| 94 | def path_encoding |
||
| 95 | nil |
||
| 96 | end |
||
| 97 | |||
| 98 | 0:513646585e45 | Chris | # get info about the svn repository |
| 99 | def info |
||
| 100 | return nil |
||
| 101 | end |
||
| 102 | 441:cbce1fd3b1b7 | Chris | |
| 103 | 0:513646585e45 | Chris | # Returns the entry identified by path and revision identifier |
| 104 | # or nil if entry doesn't exist in the repository |
||
| 105 | def entry(path=nil, identifier=nil) |
||
| 106 | parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
|
||
| 107 | search_path = parts[0..-2].join('/')
|
||
| 108 | search_name = parts[-1] |
||
| 109 | if search_path.blank? && search_name.blank? |
||
| 110 | # Root entry |
||
| 111 | Entry.new(:path => '', :kind => 'dir') |
||
| 112 | else |
||
| 113 | # Search for the entry in the parent directory |
||
| 114 | es = entries(search_path, identifier) |
||
| 115 | es ? es.detect {|e| e.name == search_name} : nil
|
||
| 116 | end |
||
| 117 | end |
||
| 118 | 441:cbce1fd3b1b7 | Chris | |
| 119 | 0:513646585e45 | Chris | # Returns an Entries collection |
| 120 | # or nil if the given path doesn't exist in the repository |
||
| 121 | 441:cbce1fd3b1b7 | Chris | def entries(path=nil, identifier=nil, options={})
|
| 122 | 0:513646585e45 | Chris | return nil |
| 123 | end |
||
| 124 | |||
| 125 | def branches |
||
| 126 | return nil |
||
| 127 | end |
||
| 128 | |||
| 129 | 441:cbce1fd3b1b7 | Chris | def tags |
| 130 | 0:513646585e45 | Chris | return nil |
| 131 | end |
||
| 132 | |||
| 133 | def default_branch |
||
| 134 | return nil |
||
| 135 | end |
||
| 136 | 441:cbce1fd3b1b7 | Chris | |
| 137 | 0:513646585e45 | Chris | def properties(path, identifier=nil) |
| 138 | return nil |
||
| 139 | end |
||
| 140 | 441:cbce1fd3b1b7 | Chris | |
| 141 | 0:513646585e45 | Chris | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
| 142 | return nil |
||
| 143 | end |
||
| 144 | 441:cbce1fd3b1b7 | Chris | |
| 145 | 0:513646585e45 | Chris | def diff(path, identifier_from, identifier_to=nil) |
| 146 | return nil |
||
| 147 | end |
||
| 148 | 441:cbce1fd3b1b7 | Chris | |
| 149 | 0:513646585e45 | Chris | def cat(path, identifier=nil) |
| 150 | return nil |
||
| 151 | end |
||
| 152 | 441:cbce1fd3b1b7 | Chris | |
| 153 | 0:513646585e45 | Chris | def with_leading_slash(path) |
| 154 | path ||= '' |
||
| 155 | (path[0,1]!="/") ? "/#{path}" : path
|
||
| 156 | end |
||
| 157 | |||
| 158 | def with_trailling_slash(path) |
||
| 159 | path ||= '' |
||
| 160 | (path[-1,1] == "/") ? path : "#{path}/"
|
||
| 161 | end |
||
| 162 | 245:051f544170fe | Chris | |
| 163 | 0:513646585e45 | Chris | def without_leading_slash(path) |
| 164 | path ||= '' |
||
| 165 | path.gsub(%r{^/+}, '')
|
||
| 166 | end |
||
| 167 | |||
| 168 | def without_trailling_slash(path) |
||
| 169 | path ||= '' |
||
| 170 | (path[-1,1] == "/") ? path[0..-2] : path |
||
| 171 | end |
||
| 172 | 245:051f544170fe | Chris | |
| 173 | 0:513646585e45 | Chris | def shell_quote(str) |
| 174 | 245:051f544170fe | Chris | self.class.shell_quote(str) |
| 175 | 0:513646585e45 | Chris | end |
| 176 | |||
| 177 | private |
||
| 178 | def retrieve_root_url |
||
| 179 | info = self.info |
||
| 180 | info ? info.root_url : nil |
||
| 181 | end |
||
| 182 | 441:cbce1fd3b1b7 | Chris | |
| 183 | 0:513646585e45 | Chris | def target(path) |
| 184 | path ||= '' |
||
| 185 | base = path.match(/^\//) ? root_url : url |
||
| 186 | shell_quote("#{base}/#{path}".gsub(/[?<>\*]/, ''))
|
||
| 187 | end |
||
| 188 | 245:051f544170fe | Chris | |
| 189 | 0:513646585e45 | Chris | def logger |
| 190 | self.class.logger |
||
| 191 | end |
||
| 192 | 245:051f544170fe | Chris | |
| 193 | 0:513646585e45 | Chris | def shellout(cmd, &block) |
| 194 | self.class.shellout(cmd, &block) |
||
| 195 | end |
||
| 196 | 245:051f544170fe | Chris | |
| 197 | 0:513646585e45 | Chris | def self.logger |
| 198 | RAILS_DEFAULT_LOGGER |
||
| 199 | end |
||
| 200 | 245:051f544170fe | Chris | |
| 201 | 0:513646585e45 | Chris | def self.shellout(cmd, &block) |
| 202 | logger.debug "Shelling out: #{strip_credential(cmd)}" if logger && logger.debug?
|
||
| 203 | if Rails.env == 'development' |
||
| 204 | # Capture stderr when running in dev environment |
||
| 205 | cmd = "#{cmd} 2>>#{RAILS_ROOT}/log/scm.stderr.log"
|
||
| 206 | end |
||
| 207 | begin |
||
| 208 | 245:051f544170fe | Chris | if RUBY_VERSION < '1.9' |
| 209 | mode = "r+" |
||
| 210 | else |
||
| 211 | mode = "r+:ASCII-8BIT" |
||
| 212 | end |
||
| 213 | IO.popen(cmd, mode) do |io| |
||
| 214 | 0:513646585e45 | Chris | io.close_write |
| 215 | block.call(io) if block_given? |
||
| 216 | end |
||
| 217 | rescue Errno::ENOENT => e |
||
| 218 | msg = strip_credential(e.message) |
||
| 219 | # The command failed, log it and re-raise |
||
| 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}")
|
||
| 221 | raise CommandFailed.new(msg) |
||
| 222 | end |
||
| 223 | 245:051f544170fe | Chris | end |
| 224 | |||
| 225 | 0:513646585e45 | Chris | # Hides username/password in a given command |
| 226 | def self.strip_credential(cmd) |
||
| 227 | q = (Redmine::Platform.mswin? ? '"' : "'") |
||
| 228 | cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
|
||
| 229 | end |
||
| 230 | 441:cbce1fd3b1b7 | Chris | |
| 231 | 0:513646585e45 | Chris | def strip_credential(cmd) |
| 232 | self.class.strip_credential(cmd) |
||
| 233 | end |
||
| 234 | 245:051f544170fe | Chris | |
| 235 | def scm_iconv(to, from, str) |
||
| 236 | return nil if str.nil? |
||
| 237 | return str if to == from |
||
| 238 | begin |
||
| 239 | Iconv.conv(to, from, str) |
||
| 240 | rescue Iconv::Failure => err |
||
| 241 | logger.error("failed to convert from #{from} to #{to}. #{err}")
|
||
| 242 | nil |
||
| 243 | end |
||
| 244 | end |
||
| 245 | 0:513646585e45 | Chris | end |
| 246 | 245:051f544170fe | Chris | |
| 247 | 0:513646585e45 | Chris | class Entries < Array |
| 248 | def sort_by_name |
||
| 249 | 441:cbce1fd3b1b7 | Chris | sort {|x,y|
|
| 250 | 0:513646585e45 | Chris | if x.kind == y.kind |
| 251 | x.name.to_s <=> y.name.to_s |
||
| 252 | else |
||
| 253 | x.kind <=> y.kind |
||
| 254 | end |
||
| 255 | 245:051f544170fe | Chris | } |
| 256 | 0:513646585e45 | Chris | end |
| 257 | 441:cbce1fd3b1b7 | Chris | |
| 258 | 0:513646585e45 | Chris | def revisions |
| 259 | revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
|
||
| 260 | end |
||
| 261 | end |
||
| 262 | 441:cbce1fd3b1b7 | Chris | |
| 263 | 0:513646585e45 | Chris | class Info |
| 264 | attr_accessor :root_url, :lastrev |
||
| 265 | def initialize(attributes={})
|
||
| 266 | self.root_url = attributes[:root_url] if attributes[:root_url] |
||
| 267 | self.lastrev = attributes[:lastrev] |
||
| 268 | end |
||
| 269 | end |
||
| 270 | 441:cbce1fd3b1b7 | Chris | |
| 271 | 0:513646585e45 | Chris | class Entry |
| 272 | attr_accessor :name, :path, :kind, :size, :lastrev |
||
| 273 | def initialize(attributes={})
|
||
| 274 | self.name = attributes[:name] if attributes[:name] |
||
| 275 | self.path = attributes[:path] if attributes[:path] |
||
| 276 | self.kind = attributes[:kind] if attributes[:kind] |
||
| 277 | self.size = attributes[:size].to_i if attributes[:size] |
||
| 278 | self.lastrev = attributes[:lastrev] |
||
| 279 | end |
||
| 280 | 441:cbce1fd3b1b7 | Chris | |
| 281 | 0:513646585e45 | Chris | def is_file? |
| 282 | 'file' == self.kind |
||
| 283 | end |
||
| 284 | 441:cbce1fd3b1b7 | Chris | |
| 285 | 0:513646585e45 | Chris | def is_dir? |
| 286 | 'dir' == self.kind |
||
| 287 | end |
||
| 288 | 441:cbce1fd3b1b7 | Chris | |
| 289 | 0:513646585e45 | Chris | def is_text? |
| 290 | Redmine::MimeType.is_type?('text', name)
|
||
| 291 | end |
||
| 292 | end |
||
| 293 | 441:cbce1fd3b1b7 | Chris | |
| 294 | 0:513646585e45 | Chris | class Revisions < Array |
| 295 | def latest |
||
| 296 | sort {|x,y|
|
||
| 297 | unless x.time.nil? or y.time.nil? |
||
| 298 | x.time <=> y.time |
||
| 299 | else |
||
| 300 | 0 |
||
| 301 | end |
||
| 302 | }.last |
||
| 303 | 441:cbce1fd3b1b7 | Chris | end |
| 304 | 0:513646585e45 | Chris | end |
| 305 | 441:cbce1fd3b1b7 | Chris | |
| 306 | 0:513646585e45 | Chris | class Revision |
| 307 | 441:cbce1fd3b1b7 | Chris | attr_accessor :scmid, :name, :author, :time, :message, |
| 308 | :paths, :revision, :branch, :identifier |
||
| 309 | 0:513646585e45 | Chris | |
| 310 | def initialize(attributes={})
|
||
| 311 | self.identifier = attributes[:identifier] |
||
| 312 | 441:cbce1fd3b1b7 | Chris | self.scmid = attributes[:scmid] |
| 313 | self.name = attributes[:name] || self.identifier |
||
| 314 | self.author = attributes[:author] |
||
| 315 | self.time = attributes[:time] |
||
| 316 | self.message = attributes[:message] || "" |
||
| 317 | self.paths = attributes[:paths] |
||
| 318 | self.revision = attributes[:revision] |
||
| 319 | self.branch = attributes[:branch] |
||
| 320 | 119:8661b858af72 | Chris | end |
| 321 | |||
| 322 | # Returns the readable identifier. |
||
| 323 | def format_identifier |
||
| 324 | 441:cbce1fd3b1b7 | Chris | self.identifier.to_s |
| 325 | 119:8661b858af72 | Chris | end |
| 326 | 245:051f544170fe | Chris | end |
| 327 | 119:8661b858af72 | Chris | |
| 328 | 0:513646585e45 | Chris | class Annotate |
| 329 | attr_reader :lines, :revisions |
||
| 330 | 441:cbce1fd3b1b7 | Chris | |
| 331 | 0:513646585e45 | Chris | def initialize |
| 332 | @lines = [] |
||
| 333 | @revisions = [] |
||
| 334 | end |
||
| 335 | 441:cbce1fd3b1b7 | Chris | |
| 336 | 0:513646585e45 | Chris | def add_line(line, revision) |
| 337 | @lines << line |
||
| 338 | @revisions << revision |
||
| 339 | end |
||
| 340 | 441:cbce1fd3b1b7 | Chris | |
| 341 | 0:513646585e45 | Chris | def content |
| 342 | content = lines.join("\n")
|
||
| 343 | end |
||
| 344 | 441:cbce1fd3b1b7 | Chris | |
| 345 | 0:513646585e45 | Chris | def empty? |
| 346 | lines.empty? |
||
| 347 | end |
||
| 348 | end |
||
| 349 | end |
||
| 350 | end |
||
| 351 | end |