To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / scm / adapters / cvs_adapter.rb @ 912:5e80956cc792

History | View | Annotate | Download (16.3 KB)

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