annotate lib/redmine/scm/adapters/cvs_adapter.rb @ 853:969bb872d4bf feature_142

Close obsolete branch feature_142
author Chris Cannam
date Thu, 14 Jul 2011 14:26:44 +0100
parents 753f1380d6bc
children 5e80956cc792
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 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 'redmine/scm/adapters/abstract_adapter'
Chris@0 19
Chris@0 20 module Redmine
Chris@0 21 module Scm
Chris@0 22 module Adapters
Chris@0 23 class CvsAdapter < AbstractAdapter
Chris@0 24
Chris@0 25 # CVS executable name
Chris@210 26 CVS_BIN = Redmine::Configuration['scm_cvs_command'] || "cvs"
Chris@245 27
Chris@441 28 # raised if scm command exited with error, e.g. unknown revision.
Chris@441 29 class ScmCommandAborted < CommandFailed; end
Chris@441 30
Chris@245 31 class << self
Chris@245 32 def client_command
Chris@245 33 @@bin ||= CVS_BIN
Chris@245 34 end
Chris@245 35
Chris@245 36 def sq_bin
Chris@245 37 @@sq_bin ||= shell_quote(CVS_BIN)
Chris@245 38 end
Chris@245 39
Chris@245 40 def client_version
Chris@245 41 @@client_version ||= (scm_command_version || [])
Chris@245 42 end
Chris@245 43
Chris@245 44 def client_available
Chris@245 45 client_version_above?([1, 12])
Chris@245 46 end
Chris@245 47
Chris@245 48 def scm_command_version
Chris@245 49 scm_version = scm_version_from_command_line.dup
Chris@245 50 if scm_version.respond_to?(:force_encoding)
Chris@245 51 scm_version.force_encoding('ASCII-8BIT')
Chris@245 52 end
Chris@245 53 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}m)
Chris@245 54 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@245 55 end
Chris@245 56 end
Chris@245 57
Chris@245 58 def scm_version_from_command_line
Chris@245 59 shellout("#{sq_bin} --version") { |io| io.read }.to_s
Chris@245 60 end
Chris@245 61 end
Chris@245 62
Chris@0 63 # Guidelines for the input:
Chris@441 64 # url -> the project-path, relative to the cvsroot (eg. module name)
Chris@0 65 # root_url -> the good old, sometimes damned, CVSROOT
Chris@441 66 # login -> unnecessary
Chris@0 67 # password -> unnecessary too
Chris@245 68 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@245 69 path_encoding=nil)
Chris@441 70 @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
Chris@441 71 @url = url
Chris@441 72 # TODO: better Exception here (IllegalArgumentException)
Chris@441 73 raise CommandFailed if root_url.blank?
Chris@441 74 @root_url = root_url
Chris@441 75
Chris@441 76 # These are unused.
Chris@441 77 @login = login if login && !login.empty?
Chris@0 78 @password = (password || "") if @login
Chris@0 79 end
Chris@245 80
Chris@441 81 def path_encoding
Chris@441 82 @path_encoding
Chris@0 83 end
Chris@245 84
Chris@0 85 def info
Chris@0 86 logger.debug "<cvs> info"
Chris@0 87 Info.new({:root_url => @root_url, :lastrev => nil})
Chris@0 88 end
Chris@245 89
Chris@0 90 def get_previous_revision(revision)
Chris@0 91 CvsRevisionHelper.new(revision).prevRev
Chris@0 92 end
Chris@245 93
Chris@0 94 # Returns an Entries collection
Chris@0 95 # or nil if the given path doesn't exist in the repository
Chris@0 96 # this method is used by the repository-browser (aka LIST)
Chris@441 97 def entries(path=nil, identifier=nil, options={})
Chris@0 98 logger.debug "<cvs> entries '#{path}' with identifier '#{identifier}'"
Chris@441 99 path_locale = scm_iconv(@path_encoding, 'UTF-8', path)
Chris@441 100 path_locale.force_encoding("ASCII-8BIT") if path_locale.respond_to?(:force_encoding)
Chris@0 101 entries = Entries.new
Chris@441 102 cmd_args = %w|-q rls -e|
Chris@441 103 cmd_args << "-D" << time_to_cvstime_rlog(identifier) if identifier
Chris@441 104 cmd_args << path_with_proj(path)
Chris@441 105 scm_cmd(*cmd_args) do |io|
Chris@441 106 io.each_line() do |line|
Chris@441 107 fields = line.chop.split('/',-1)
Chris@0 108 logger.debug(">>InspectLine #{fields.inspect}")
Chris@0 109 if fields[0]!="D"
Chris@441 110 time = nil
Chris@441 111 # Thu Dec 13 16:27:22 2007
Chris@441 112 time_l = fields[-3].split(' ')
Chris@441 113 if time_l.size == 5 && time_l[4].length == 4
Chris@441 114 begin
Chris@441 115 time = Time.parse(
Chris@441 116 "#{time_l[1]} #{time_l[2]} #{time_l[3]} GMT #{time_l[4]}")
Chris@441 117 rescue
Chris@441 118 end
Chris@441 119 end
Chris@441 120 entries << Entry.new(
Chris@441 121 {
Chris@441 122 :name => scm_iconv('UTF-8', @path_encoding, fields[-5]),
Chris@0 123 #:path => fields[-4].include?(path)?fields[-4]:(path + "/"+ fields[-4]),
Chris@441 124 :path => scm_iconv('UTF-8', @path_encoding, "#{path_locale}/#{fields[-5]}"),
Chris@0 125 :kind => 'file',
Chris@0 126 :size => nil,
Chris@441 127 :lastrev => Revision.new(
Chris@441 128 {
Chris@441 129 :revision => fields[-4],
Chris@441 130 :name => scm_iconv('UTF-8', @path_encoding, fields[-4]),
Chris@441 131 :time => time,
Chris@441 132 :author => ''
Chris@441 133 })
Chris@0 134 })
Chris@0 135 else
Chris@441 136 entries << Entry.new(
Chris@441 137 {
Chris@441 138 :name => scm_iconv('UTF-8', @path_encoding, fields[1]),
Chris@441 139 :path => scm_iconv('UTF-8', @path_encoding, "#{path_locale}/#{fields[1]}"),
Chris@441 140 :kind => 'dir',
Chris@441 141 :size => nil,
Chris@0 142 :lastrev => nil
Chris@441 143 })
Chris@0 144 end
Chris@441 145 end
Chris@0 146 end
Chris@0 147 entries.sort_by_name
Chris@441 148 rescue ScmCommandAborted
Chris@441 149 nil
Chris@245 150 end
Chris@0 151
Chris@0 152 STARTLOG="----------------------------"
Chris@0 153 ENDLOG ="============================================================================="
Chris@245 154
Chris@0 155 # Returns all revisions found between identifier_from and identifier_to
Chris@0 156 # in the repository. both identifier have to be dates or nil.
Chris@0 157 # these method returns nothing but yield every result in block
Chris@0 158 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}, &block)
Chris@441 159 path_with_project_utf8 = path_with_proj(path)
Chris@441 160 path_with_project_locale = scm_iconv(@path_encoding, 'UTF-8', path_with_project_utf8)
Chris@441 161 logger.debug "<cvs> revisions path:" +
Chris@441 162 "'#{path}',identifier_from #{identifier_from}, identifier_to #{identifier_to}"
Chris@441 163 cmd_args = %w|-q rlog|
Chris@441 164 cmd_args << "-d" << ">#{time_to_cvstime_rlog(identifier_from)}" if identifier_from
Chris@441 165 cmd_args << path_with_project_utf8
Chris@441 166 scm_cmd(*cmd_args) do |io|
Chris@441 167 state = "entry_start"
Chris@441 168 commit_log = String.new
Chris@441 169 revision = nil
Chris@441 170 date = nil
Chris@441 171 author = nil
Chris@441 172 entry_path = nil
Chris@441 173 entry_name = nil
Chris@441 174 file_state = nil
Chris@441 175 branch_map = nil
Chris@245 176 io.each_line() do |line|
Chris@441 177 if state != "revision" && /^#{ENDLOG}/ =~ line
Chris@441 178 commit_log = String.new
Chris@441 179 revision = nil
Chris@441 180 state = "entry_start"
Chris@0 181 end
Chris@441 182 if state == "entry_start"
Chris@441 183 branch_map = Hash.new
Chris@441 184 if /^RCS file: #{Regexp.escape(root_url_path)}\/#{Regexp.escape(path_with_project_locale)}(.+),v$/ =~ line
Chris@0 185 entry_path = normalize_cvs_path($1)
Chris@0 186 entry_name = normalize_path(File.basename($1))
Chris@0 187 logger.debug("Path #{entry_path} <=> Name #{entry_name}")
Chris@0 188 elsif /^head: (.+)$/ =~ line
Chris@0 189 entry_headRev = $1 #unless entry.nil?
Chris@0 190 elsif /^symbolic names:/ =~ line
Chris@441 191 state = "symbolic" #unless entry.nil?
Chris@0 192 elsif /^#{STARTLOG}/ =~ line
Chris@441 193 commit_log = String.new
Chris@441 194 state = "revision"
Chris@441 195 end
Chris@0 196 next
Chris@441 197 elsif state == "symbolic"
Chris@441 198 if /^(.*):\s(.*)/ =~ (line.strip)
Chris@441 199 branch_map[$1] = $2
Chris@0 200 else
Chris@441 201 state = "tags"
Chris@0 202 next
Chris@441 203 end
Chris@441 204 elsif state == "tags"
Chris@0 205 if /^#{STARTLOG}/ =~ line
Chris@0 206 commit_log = ""
Chris@441 207 state = "revision"
Chris@0 208 elsif /^#{ENDLOG}/ =~ line
Chris@441 209 state = "head"
Chris@0 210 end
Chris@0 211 next
Chris@441 212 elsif state == "revision"
Chris@245 213 if /^#{ENDLOG}/ =~ line || /^#{STARTLOG}/ =~ line
Chris@0 214 if revision
Chris@441 215 revHelper = CvsRevisionHelper.new(revision)
Chris@441 216 revBranch = "HEAD"
Chris@441 217 branch_map.each() do |branch_name, branch_point|
Chris@0 218 if revHelper.is_in_branch_with_symbol(branch_point)
Chris@441 219 revBranch = branch_name
Chris@0 220 end
Chris@0 221 end
Chris@0 222 logger.debug("********** YIELD Revision #{revision}::#{revBranch}")
Chris@245 223 yield Revision.new({
Chris@441 224 :time => date,
Chris@441 225 :author => author,
Chris@441 226 :message => commit_log.chomp,
Chris@0 227 :paths => [{
Chris@0 228 :revision => revision,
Chris@441 229 :branch => revBranch,
Chris@441 230 :path => scm_iconv('UTF-8', @path_encoding, entry_path),
Chris@441 231 :name => scm_iconv('UTF-8', @path_encoding, entry_name),
Chris@441 232 :kind => 'file',
Chris@441 233 :action => file_state
Chris@441 234 }]
Chris@441 235 })
Chris@0 236 end
Chris@441 237 commit_log = String.new
Chris@441 238 revision = nil
Chris@0 239 if /^#{ENDLOG}/ =~ line
Chris@441 240 state = "entry_start"
Chris@0 241 end
Chris@0 242 next
Chris@0 243 end
Chris@245 244
Chris@0 245 if /^branches: (.+)$/ =~ line
Chris@441 246 # TODO: version.branch = $1
Chris@0 247 elsif /^revision (\d+(?:\.\d+)+).*$/ =~ line
Chris@441 248 revision = $1
Chris@0 249 elsif /^date:\s+(\d+.\d+.\d+\s+\d+:\d+:\d+)/ =~ line
Chris@441 250 date = Time.parse($1)
Chris@441 251 line_utf8 = scm_iconv('UTF-8', options[:log_encoding], line)
Chris@441 252 author_utf8 = /author: ([^;]+)/.match(line_utf8)[1]
Chris@441 253 author = scm_iconv(options[:log_encoding], 'UTF-8', author_utf8)
Chris@441 254 file_state = /state: ([^;]+)/.match(line)[1]
Chris@441 255 # TODO:
Chris@441 256 # linechanges only available in CVS....
Chris@441 257 # maybe a feature our SVN implementation.
Chris@441 258 # I'm sure, they are useful for stats or something else
Chris@0 259 # linechanges =/lines: \+(\d+) -(\d+)/.match(line)
Chris@0 260 # unless linechanges.nil?
Chris@0 261 # version.line_plus = linechanges[1]
Chris@0 262 # version.line_minus = linechanges[2]
Chris@0 263 # else
Chris@0 264 # version.line_plus = 0
Chris@245 265 # version.line_minus = 0
Chris@245 266 # end
Chris@245 267 else
Chris@0 268 commit_log << line unless line =~ /^\*\*\* empty log message \*\*\*/
Chris@245 269 end
Chris@245 270 end
Chris@0 271 end
Chris@0 272 end
Chris@441 273 rescue ScmCommandAborted
Chris@441 274 Revisions.new
Chris@245 275 end
Chris@245 276
Chris@0 277 def diff(path, identifier_from, identifier_to=nil)
Chris@441 278 logger.debug "<cvs> diff path:'#{path}'" +
Chris@441 279 ",identifier_from #{identifier_from}, identifier_to #{identifier_to}"
Chris@441 280 cmd_args = %w|rdiff -u|
Chris@441 281 cmd_args << "-r#{identifier_to}"
Chris@441 282 cmd_args << "-r#{identifier_from}"
Chris@441 283 cmd_args << path_with_proj(path)
Chris@0 284 diff = []
Chris@441 285 scm_cmd(*cmd_args) do |io|
Chris@0 286 io.each_line do |line|
Chris@0 287 diff << line
Chris@0 288 end
Chris@0 289 end
Chris@0 290 diff
Chris@441 291 rescue ScmCommandAborted
Chris@441 292 nil
Chris@245 293 end
Chris@245 294
Chris@0 295 def cat(path, identifier=nil)
Chris@0 296 identifier = (identifier) ? identifier : "HEAD"
Chris@0 297 logger.debug "<cvs> cat path:'#{path}',identifier #{identifier}"
Chris@441 298 cmd_args = %w|-q co|
Chris@441 299 cmd_args << "-D" << time_to_cvstime(identifier) if identifier
Chris@441 300 cmd_args << "-p" << path_with_proj(path)
Chris@0 301 cat = nil
Chris@441 302 scm_cmd(*cmd_args) do |io|
Chris@245 303 io.binmode
Chris@0 304 cat = io.read
Chris@0 305 end
Chris@0 306 cat
Chris@441 307 rescue ScmCommandAborted
Chris@441 308 nil
Chris@245 309 end
Chris@0 310
Chris@0 311 def annotate(path, identifier=nil)
Chris@441 312 identifier = (identifier) ? identifier : "HEAD"
Chris@0 313 logger.debug "<cvs> annotate path:'#{path}',identifier #{identifier}"
Chris@441 314 cmd_args = %w|rannotate|
Chris@441 315 cmd_args << "-D" << time_to_cvstime(identifier) if identifier
Chris@441 316 cmd_args << path_with_proj(path)
Chris@0 317 blame = Annotate.new
Chris@441 318 scm_cmd(*cmd_args) do |io|
Chris@0 319 io.each_line do |line|
Chris@0 320 next unless line =~ %r{^([\d\.]+)\s+\(([^\)]+)\s+[^\)]+\):\s(.*)$}
Chris@441 321 blame.add_line(
Chris@441 322 $3.rstrip,
Chris@441 323 Revision.new(
Chris@441 324 :revision => $1,
Chris@441 325 :identifier => nil,
Chris@441 326 :author => $2.strip
Chris@441 327 ))
Chris@0 328 end
Chris@0 329 end
Chris@0 330 blame
Chris@441 331 rescue ScmCommandAborted
Chris@441 332 Annotate.new
Chris@0 333 end
Chris@245 334
Chris@0 335 private
Chris@245 336
Chris@0 337 # Returns the root url without the connexion string
Chris@0 338 # :pserver:anonymous@foo.bar:/path => /path
Chris@0 339 # :ext:cvsservername:/path => /path
Chris@0 340 def root_url_path
Chris@0 341 root_url.to_s.gsub(/^:.+:\d*/, '')
Chris@0 342 end
Chris@0 343
Chris@0 344 # convert a date/time into the CVS-format
Chris@0 345 def time_to_cvstime(time)
Chris@0 346 return nil if time.nil?
Chris@441 347 time = Time.now if time == 'HEAD'
Chris@441 348
Chris@0 349 unless time.kind_of? Time
Chris@0 350 time = Time.parse(time)
Chris@0 351 end
Chris@441 352 return time_to_cvstime_rlog(time)
Chris@0 353 end
Chris@210 354
Chris@210 355 def time_to_cvstime_rlog(time)
Chris@210 356 return nil if time.nil?
Chris@210 357 t1 = time.clone.localtime
Chris@210 358 return t1.strftime("%Y-%m-%d %H:%M:%S")
Chris@210 359 end
Chris@441 360
Chris@0 361 def normalize_cvs_path(path)
Chris@0 362 normalize_path(path.gsub(/Attic\//,''))
Chris@0 363 end
Chris@441 364
Chris@0 365 def normalize_path(path)
Chris@0 366 path.sub(/^(\/)*(.*)/,'\2').sub(/(.*)(,v)+/,'\1')
Chris@441 367 end
Chris@441 368
Chris@441 369 def path_with_proj(path)
Chris@441 370 "#{url}#{with_leading_slash(path)}"
Chris@441 371 end
Chris@441 372 private :path_with_proj
Chris@441 373
Chris@441 374 class Revision < Redmine::Scm::Adapters::Revision
Chris@441 375 # Returns the readable identifier
Chris@441 376 def format_identifier
Chris@441 377 revision.to_s
Chris@441 378 end
Chris@441 379 end
Chris@441 380
Chris@441 381 def scm_cmd(*args, &block)
Chris@441 382 full_args = [CVS_BIN, '-d', root_url]
Chris@441 383 full_args += args
Chris@441 384 full_args_locale = []
Chris@441 385 full_args.map do |e|
Chris@441 386 full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e)
Chris@441 387 end
Chris@441 388 ret = shellout(full_args_locale.map { |e| shell_quote e.to_s }.join(' '), &block)
Chris@441 389 if $? && $?.exitstatus != 0
Chris@441 390 raise ScmCommandAborted, "cvs exited with non-zero status: #{$?.exitstatus}"
Chris@441 391 end
Chris@441 392 ret
Chris@441 393 end
Chris@441 394 private :scm_cmd
Chris@441 395 end
Chris@441 396
Chris@0 397 class CvsRevisionHelper
Chris@0 398 attr_accessor :complete_rev, :revision, :base, :branchid
Chris@441 399
Chris@0 400 def initialize(complete_rev)
Chris@0 401 @complete_rev = complete_rev
Chris@0 402 parseRevision()
Chris@0 403 end
Chris@441 404
Chris@0 405 def branchPoint
Chris@0 406 return @base
Chris@0 407 end
Chris@441 408
Chris@0 409 def branchVersion
Chris@0 410 if isBranchRevision
Chris@0 411 return @base+"."+@branchid
Chris@0 412 end
Chris@0 413 return @base
Chris@0 414 end
Chris@441 415
Chris@0 416 def isBranchRevision
Chris@0 417 !@branchid.nil?
Chris@0 418 end
Chris@441 419
Chris@0 420 def prevRev
Chris@441 421 unless @revision == 0
Chris@441 422 return buildRevision( @revision - 1 )
Chris@0 423 end
Chris@441 424 return buildRevision( @revision )
Chris@0 425 end
Chris@441 426
Chris@0 427 def is_in_branch_with_symbol(branch_symbol)
Chris@441 428 bpieces = branch_symbol.split(".")
Chris@441 429 branch_start = "#{bpieces[0..-3].join(".")}.#{bpieces[-1]}"
Chris@441 430 return ( branchVersion == branch_start )
Chris@0 431 end
Chris@441 432
Chris@0 433 private
Chris@0 434 def buildRevision(rev)
Chris@441 435 if rev == 0
Chris@245 436 if @branchid.nil?
Chris@441 437 @base + ".0"
Chris@245 438 else
Chris@245 439 @base
Chris@245 440 end
Chris@441 441 elsif @branchid.nil?
Chris@441 442 @base + "." + rev.to_s
Chris@0 443 else
Chris@441 444 @base + "." + @branchid + "." + rev.to_s
Chris@0 445 end
Chris@0 446 end
Chris@441 447
Chris@0 448 # Interpretiert die cvs revisionsnummern wie z.b. 1.14 oder 1.3.0.15
Chris@0 449 def parseRevision()
Chris@441 450 pieces = @complete_rev.split(".")
Chris@441 451 @revision = pieces.last.to_i
Chris@441 452 baseSize = 1
Chris@441 453 baseSize += (pieces.size / 2)
Chris@441 454 @base = pieces[0..-baseSize].join(".")
Chris@0 455 if baseSize > 2
Chris@441 456 @branchid = pieces[-2]
Chris@441 457 end
Chris@0 458 end
Chris@0 459 end
Chris@0 460 end
Chris@0 461 end
Chris@0 462 end