annotate .svn/pristine/b7/b75d5f0ff60be5f80b39e65c645dcf825ec9984b.svn-base @ 1296:038ba2d95de8 redmine-2.2

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