##// END OF EJS Templates
scm: cvs: use localtime at cvs rlog -d option (#996, #3761)....
Toshi MARUYAMA -
r4674:10ca61a13bd0
parent child
Show More
@@ -1,364 +1,370
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/abstract_adapter'
19 19
20 20 module Redmine
21 21 module Scm
22 22 module Adapters
23 23 class CvsAdapter < AbstractAdapter
24 24
25 25 # CVS executable name
26 26 CVS_BIN = "cvs"
27 27
28 28 # Guidelines for the input:
29 29 # url -> the project-path, relative to the cvsroot (eg. module name)
30 30 # root_url -> the good old, sometimes damned, CVSROOT
31 31 # login -> unnecessary
32 32 # password -> unnecessary too
33 33 def initialize(url, root_url=nil, login=nil, password=nil)
34 34 @url = url
35 35 @login = login if login && !login.empty?
36 36 @password = (password || "") if @login
37 37 #TODO: better Exception here (IllegalArgumentException)
38 38 raise CommandFailed if root_url.blank?
39 39 @root_url = root_url
40 40 end
41 41
42 42 def root_url
43 43 @root_url
44 44 end
45 45
46 46 def url
47 47 @url
48 48 end
49 49
50 50 def info
51 51 logger.debug "<cvs> info"
52 52 Info.new({:root_url => @root_url, :lastrev => nil})
53 53 end
54 54
55 55 def get_previous_revision(revision)
56 56 CvsRevisionHelper.new(revision).prevRev
57 57 end
58 58
59 59 # Returns an Entries collection
60 60 # or nil if the given path doesn't exist in the repository
61 61 # this method is used by the repository-browser (aka LIST)
62 62 def entries(path=nil, identifier=nil)
63 63 logger.debug "<cvs> entries '#{path}' with identifier '#{identifier}'"
64 64 path_with_project="#{url}#{with_leading_slash(path)}"
65 65 entries = Entries.new
66 66 cmd = "#{CVS_BIN} -d #{shell_quote root_url} rls -e"
67 67 cmd << " -D \"#{time_to_cvstime(identifier)}\"" if identifier
68 68 cmd << " #{shell_quote path_with_project}"
69 69 shellout(cmd) do |io|
70 70 io.each_line(){|line|
71 71 fields=line.chop.split('/',-1)
72 72 logger.debug(">>InspectLine #{fields.inspect}")
73 73
74 74 if fields[0]!="D"
75 75 entries << Entry.new({:name => fields[-5],
76 76 #:path => fields[-4].include?(path)?fields[-4]:(path + "/"+ fields[-4]),
77 77 :path => "#{path}/#{fields[-5]}",
78 78 :kind => 'file',
79 79 :size => nil,
80 80 :lastrev => Revision.new({
81 81 :revision => fields[-4],
82 82 :name => fields[-4],
83 83 :time => Time.parse(fields[-3]),
84 84 :author => ''
85 85 })
86 86 })
87 87 else
88 88 entries << Entry.new({:name => fields[1],
89 89 :path => "#{path}/#{fields[1]}",
90 90 :kind => 'dir',
91 91 :size => nil,
92 92 :lastrev => nil
93 93 })
94 94 end
95 95 }
96 96 end
97 97 return nil if $? && $?.exitstatus != 0
98 98 entries.sort_by_name
99 99 end
100 100
101 101 STARTLOG="----------------------------"
102 102 ENDLOG ="============================================================================="
103 103
104 104 # Returns all revisions found between identifier_from and identifier_to
105 105 # in the repository. both identifier have to be dates or nil.
106 106 # these method returns nothing but yield every result in block
107 107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}, &block)
108 108 logger.debug "<cvs> revisions path:'#{path}',identifier_from #{identifier_from}, identifier_to #{identifier_to}"
109 109
110 110 path_with_project="#{url}#{with_leading_slash(path)}"
111 111 cmd = "#{CVS_BIN} -d #{shell_quote root_url} rlog"
112 cmd << " -d\">#{time_to_cvstime(identifier_from)}\"" if identifier_from
112 cmd << " -d\">#{time_to_cvstime_rlog(identifier_from)}\"" if identifier_from
113 113 cmd << " #{shell_quote path_with_project}"
114 114 shellout(cmd) do |io|
115 115 state="entry_start"
116 116
117 117 commit_log=String.new
118 118 revision=nil
119 119 date=nil
120 120 author=nil
121 121 entry_path=nil
122 122 entry_name=nil
123 123 file_state=nil
124 124 branch_map=nil
125 125
126 126 io.each_line() do |line|
127 127
128 128 if state!="revision" && /^#{ENDLOG}/ =~ line
129 129 commit_log=String.new
130 130 revision=nil
131 131 state="entry_start"
132 132 end
133 133
134 134 if state=="entry_start"
135 135 branch_map=Hash.new
136 136 if /^RCS file: #{Regexp.escape(root_url_path)}\/#{Regexp.escape(path_with_project)}(.+),v$/ =~ line
137 137 entry_path = normalize_cvs_path($1)
138 138 entry_name = normalize_path(File.basename($1))
139 139 logger.debug("Path #{entry_path} <=> Name #{entry_name}")
140 140 elsif /^head: (.+)$/ =~ line
141 141 entry_headRev = $1 #unless entry.nil?
142 142 elsif /^symbolic names:/ =~ line
143 143 state="symbolic" #unless entry.nil?
144 144 elsif /^#{STARTLOG}/ =~ line
145 145 commit_log=String.new
146 146 state="revision"
147 147 end
148 148 next
149 149 elsif state=="symbolic"
150 150 if /^(.*):\s(.*)/ =~ (line.strip)
151 151 branch_map[$1]=$2
152 152 else
153 153 state="tags"
154 154 next
155 155 end
156 156 elsif state=="tags"
157 157 if /^#{STARTLOG}/ =~ line
158 158 commit_log = ""
159 159 state="revision"
160 160 elsif /^#{ENDLOG}/ =~ line
161 161 state="head"
162 162 end
163 163 next
164 164 elsif state=="revision"
165 165 if /^#{ENDLOG}/ =~ line || /^#{STARTLOG}/ =~ line
166 166 if revision
167 167
168 168 revHelper=CvsRevisionHelper.new(revision)
169 169 revBranch="HEAD"
170 170
171 171 branch_map.each() do |branch_name,branch_point|
172 172 if revHelper.is_in_branch_with_symbol(branch_point)
173 173 revBranch=branch_name
174 174 end
175 175 end
176 176
177 177 logger.debug("********** YIELD Revision #{revision}::#{revBranch}")
178 178
179 179 yield Revision.new({
180 180 :time => date,
181 181 :author => author,
182 182 :message=>commit_log.chomp,
183 183 :paths => [{
184 184 :revision => revision,
185 185 :branch=> revBranch,
186 186 :path=>entry_path,
187 187 :name=>entry_name,
188 188 :kind=>'file',
189 189 :action=>file_state
190 190 }]
191 191 })
192 192 end
193 193
194 194 commit_log=String.new
195 195 revision=nil
196 196
197 197 if /^#{ENDLOG}/ =~ line
198 198 state="entry_start"
199 199 end
200 200 next
201 201 end
202 202
203 203 if /^branches: (.+)$/ =~ line
204 204 #TODO: version.branch = $1
205 205 elsif /^revision (\d+(?:\.\d+)+).*$/ =~ line
206 206 revision = $1
207 207 elsif /^date:\s+(\d+.\d+.\d+\s+\d+:\d+:\d+)/ =~ line
208 208 date = Time.parse($1)
209 209 author = /author: ([^;]+)/.match(line)[1]
210 210 file_state = /state: ([^;]+)/.match(line)[1]
211 211 #TODO: linechanges only available in CVS.... maybe a feature our SVN implementation. i'm sure, they are
212 212 # useful for stats or something else
213 213 # linechanges =/lines: \+(\d+) -(\d+)/.match(line)
214 214 # unless linechanges.nil?
215 215 # version.line_plus = linechanges[1]
216 216 # version.line_minus = linechanges[2]
217 217 # else
218 218 # version.line_plus = 0
219 219 # version.line_minus = 0
220 220 # end
221 221 else
222 222 commit_log << line unless line =~ /^\*\*\* empty log message \*\*\*/
223 223 end
224 224 end
225 225 end
226 226 end
227 227 end
228 228
229 229 def diff(path, identifier_from, identifier_to=nil)
230 230 logger.debug "<cvs> diff path:'#{path}',identifier_from #{identifier_from}, identifier_to #{identifier_to}"
231 231 path_with_project="#{url}#{with_leading_slash(path)}"
232 232 cmd = "#{CVS_BIN} -d #{shell_quote root_url} rdiff -u -r#{identifier_to} -r#{identifier_from} #{shell_quote path_with_project}"
233 233 diff = []
234 234 shellout(cmd) do |io|
235 235 io.each_line do |line|
236 236 diff << line
237 237 end
238 238 end
239 239 return nil if $? && $?.exitstatus != 0
240 240 diff
241 241 end
242 242
243 243 def cat(path, identifier=nil)
244 244 identifier = (identifier) ? identifier : "HEAD"
245 245 logger.debug "<cvs> cat path:'#{path}',identifier #{identifier}"
246 246 path_with_project="#{url}#{with_leading_slash(path)}"
247 247 cmd = "#{CVS_BIN} -d #{shell_quote root_url} co"
248 248 cmd << " -D \"#{time_to_cvstime(identifier)}\"" if identifier
249 249 cmd << " -p #{shell_quote path_with_project}"
250 250 cat = nil
251 251 shellout(cmd) do |io|
252 252 cat = io.read
253 253 end
254 254 return nil if $? && $?.exitstatus != 0
255 255 cat
256 256 end
257 257
258 258 def annotate(path, identifier=nil)
259 259 identifier = (identifier) ? identifier.to_i : "HEAD"
260 260 logger.debug "<cvs> annotate path:'#{path}',identifier #{identifier}"
261 261 path_with_project="#{url}#{with_leading_slash(path)}"
262 262 cmd = "#{CVS_BIN} -d #{shell_quote root_url} rannotate -r#{identifier} #{shell_quote path_with_project}"
263 263 blame = Annotate.new
264 264 shellout(cmd) do |io|
265 265 io.each_line do |line|
266 266 next unless line =~ %r{^([\d\.]+)\s+\(([^\)]+)\s+[^\)]+\):\s(.*)$}
267 267 blame.add_line($3.rstrip, Revision.new(:revision => $1, :author => $2.strip))
268 268 end
269 269 end
270 270 return nil if $? && $?.exitstatus != 0
271 271 blame
272 272 end
273 273
274 274 private
275 275
276 276 # Returns the root url without the connexion string
277 277 # :pserver:anonymous@foo.bar:/path => /path
278 278 # :ext:cvsservername:/path => /path
279 279 def root_url_path
280 280 root_url.to_s.gsub(/^:.+:\d*/, '')
281 281 end
282 282
283 283 # convert a date/time into the CVS-format
284 284 def time_to_cvstime(time)
285 285 return nil if time.nil?
286 286 return Time.now if time == 'HEAD'
287 287
288 288 unless time.kind_of? Time
289 289 time = Time.parse(time)
290 290 end
291 291 return time.strftime("%Y-%m-%d %H:%M:%S")
292 292 end
293 293
294 def time_to_cvstime_rlog(time)
295 return nil if time.nil?
296 t1 = time.clone.localtime
297 return t1.strftime("%Y-%m-%d %H:%M:%S")
298 end
299
294 300 def normalize_cvs_path(path)
295 301 normalize_path(path.gsub(/Attic\//,''))
296 302 end
297 303
298 304 def normalize_path(path)
299 305 path.sub(/^(\/)*(.*)/,'\2').sub(/(.*)(,v)+/,'\1')
300 306 end
301 307 end
302 308
303 309 class CvsRevisionHelper
304 310 attr_accessor :complete_rev, :revision, :base, :branchid
305 311
306 312 def initialize(complete_rev)
307 313 @complete_rev = complete_rev
308 314 parseRevision()
309 315 end
310 316
311 317 def branchPoint
312 318 return @base
313 319 end
314 320
315 321 def branchVersion
316 322 if isBranchRevision
317 323 return @base+"."+@branchid
318 324 end
319 325 return @base
320 326 end
321 327
322 328 def isBranchRevision
323 329 !@branchid.nil?
324 330 end
325 331
326 332 def prevRev
327 333 unless @revision==0
328 334 return buildRevision(@revision-1)
329 335 end
330 336 return buildRevision(@revision)
331 337 end
332 338
333 339 def is_in_branch_with_symbol(branch_symbol)
334 340 bpieces=branch_symbol.split(".")
335 341 branch_start="#{bpieces[0..-3].join(".")}.#{bpieces[-1]}"
336 342 return (branchVersion==branch_start)
337 343 end
338 344
339 345 private
340 346 def buildRevision(rev)
341 347 if rev== 0
342 348 @base
343 349 elsif @branchid.nil?
344 350 @base+"."+rev.to_s
345 351 else
346 352 @base+"."+@branchid+"."+rev.to_s
347 353 end
348 354 end
349 355
350 356 # Interpretiert die cvs revisionsnummern wie z.b. 1.14 oder 1.3.0.15
351 357 def parseRevision()
352 358 pieces=@complete_rev.split(".")
353 359 @revision=pieces.last.to_i
354 360 baseSize=1
355 361 baseSize+=(pieces.size/2)
356 362 @base=pieces[0..-baseSize].join(".")
357 363 if baseSize > 2
358 364 @branchid=pieces[-2]
359 365 end
360 366 end
361 367 end
362 368 end
363 369 end
364 370 end
@@ -1,33 +1,42
1 1 require File.expand_path('../../../../../../test_helper', __FILE__)
2 2 begin
3 3 require 'mocha'
4 4
5 5 class CvsAdapterTest < ActiveSupport::TestCase
6 6
7 7 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/cvs_repository'
8 8 MODULE_NAME = 'test'
9 9
10 10 if File.directory?(REPOSITORY_PATH)
11 11 def setup
12 12 @adapter = Redmine::Scm::Adapters::CvsAdapter.new(MODULE_NAME, REPOSITORY_PATH)
13 13 end
14 14
15 15 def test_revisions_all
16 16 cnt = 0
17 17 @adapter.revisions('', nil, nil, :with_paths => true) do |revision|
18 18 cnt += 1
19 19 end
20 20 assert_equal 14, cnt
21 21 end
22
23 def test_revisions_from_rev3
24 rev3_committed_on = Time.gm(2007, 12, 13, 16, 27, 22)
25 cnt = 0
26 @adapter.revisions('', rev3_committed_on, nil, :with_paths => true) do |revision|
27 cnt += 1
28 end
29 assert_equal 2, cnt
30 end
22 31 else
23 32 puts "Cvs test repository NOT FOUND. Skipping unit tests !!!"
24 33 def test_fake; assert true end
25 34 end
26 35 end
27 36
28 37 rescue LoadError
29 38 class CvsMochaFake < ActiveSupport::TestCase
30 39 def test_fake; assert(false, "Requires mocha to run those tests") end
31 40 end
32 41 end
33 42
General Comments 0
You need to be logged in to leave comments. Login now