annotate lib/redmine/scm/adapters/cvs_adapter.rb @ 1516:b450a9d58aed redmine-2.4

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