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