Mercurial > hg > soundsoftware-site
comparison .svn/pristine/dc/dce2b25d87da51cebea469c60089fcd76b121f3d.svn-base @ 1517:dffacf8a6908 redmine-2.5
Update to Redmine SVN revision 13367 on 2.5-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:29:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1516:b450a9d58aed | 1517:dffacf8a6908 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 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 # | |
9 # 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 # | |
14 # 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 'cgi' | |
19 require 'redmine/scm/adapters' | |
20 | |
21 if RUBY_VERSION < '1.9' | |
22 require 'iconv' | |
23 end | |
24 | |
25 module Redmine | |
26 module Scm | |
27 module Adapters | |
28 class AbstractAdapter #:nodoc: | |
29 | |
30 # raised if scm command exited with error, e.g. unknown revision. | |
31 class ScmCommandAborted < CommandFailed; end | |
32 | |
33 class << self | |
34 def client_command | |
35 "" | |
36 end | |
37 | |
38 def shell_quote_command | |
39 if Redmine::Platform.mswin? && RUBY_PLATFORM == 'java' | |
40 client_command | |
41 else | |
42 shell_quote(client_command) | |
43 end | |
44 end | |
45 | |
46 # Returns the version of the scm client | |
47 # Eg: [1, 5, 0] or [] if unknown | |
48 def client_version | |
49 [] | |
50 end | |
51 | |
52 # Returns the version string of the scm client | |
53 # Eg: '1.5.0' or 'Unknown version' if unknown | |
54 def client_version_string | |
55 v = client_version || 'Unknown version' | |
56 v.is_a?(Array) ? v.join('.') : v.to_s | |
57 end | |
58 | |
59 # Returns true if the current client version is above | |
60 # or equals the given one | |
61 # If option is :unknown is set to true, it will return | |
62 # true if the client version is unknown | |
63 def client_version_above?(v, options={}) | |
64 ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown]) | |
65 end | |
66 | |
67 def client_available | |
68 true | |
69 end | |
70 | |
71 def shell_quote(str) | |
72 if Redmine::Platform.mswin? | |
73 '"' + str.gsub(/"/, '\\"') + '"' | |
74 else | |
75 "'" + str.gsub(/'/, "'\"'\"'") + "'" | |
76 end | |
77 end | |
78 end | |
79 | |
80 def initialize(url, root_url=nil, login=nil, password=nil, | |
81 path_encoding=nil) | |
82 @url = url | |
83 @login = login if login && !login.empty? | |
84 @password = (password || "") if @login | |
85 @root_url = root_url.blank? ? retrieve_root_url : root_url | |
86 end | |
87 | |
88 def adapter_name | |
89 'Abstract' | |
90 end | |
91 | |
92 def supports_cat? | |
93 true | |
94 end | |
95 | |
96 def supports_annotate? | |
97 respond_to?('annotate') | |
98 end | |
99 | |
100 def root_url | |
101 @root_url | |
102 end | |
103 | |
104 def url | |
105 @url | |
106 end | |
107 | |
108 def path_encoding | |
109 nil | |
110 end | |
111 | |
112 # get info about the svn repository | |
113 def info | |
114 return nil | |
115 end | |
116 | |
117 # Returns the entry identified by path and revision identifier | |
118 # or nil if entry doesn't exist in the repository | |
119 def entry(path=nil, identifier=nil) | |
120 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?} | |
121 search_path = parts[0..-2].join('/') | |
122 search_name = parts[-1] | |
123 if search_path.blank? && search_name.blank? | |
124 # Root entry | |
125 Entry.new(:path => '', :kind => 'dir') | |
126 else | |
127 # Search for the entry in the parent directory | |
128 es = entries(search_path, identifier) | |
129 es ? es.detect {|e| e.name == search_name} : nil | |
130 end | |
131 end | |
132 | |
133 # Returns an Entries collection | |
134 # or nil if the given path doesn't exist in the repository | |
135 def entries(path=nil, identifier=nil, options={}) | |
136 return nil | |
137 end | |
138 | |
139 def branches | |
140 return nil | |
141 end | |
142 | |
143 def tags | |
144 return nil | |
145 end | |
146 | |
147 def default_branch | |
148 return nil | |
149 end | |
150 | |
151 def properties(path, identifier=nil) | |
152 return nil | |
153 end | |
154 | |
155 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) | |
156 return nil | |
157 end | |
158 | |
159 def diff(path, identifier_from, identifier_to=nil) | |
160 return nil | |
161 end | |
162 | |
163 def cat(path, identifier=nil) | |
164 return nil | |
165 end | |
166 | |
167 def with_leading_slash(path) | |
168 path ||= '' | |
169 (path[0,1]!="/") ? "/#{path}" : path | |
170 end | |
171 | |
172 def with_trailling_slash(path) | |
173 path ||= '' | |
174 (path[-1,1] == "/") ? path : "#{path}/" | |
175 end | |
176 | |
177 def without_leading_slash(path) | |
178 path ||= '' | |
179 path.gsub(%r{^/+}, '') | |
180 end | |
181 | |
182 def without_trailling_slash(path) | |
183 path ||= '' | |
184 (path[-1,1] == "/") ? path[0..-2] : path | |
185 end | |
186 | |
187 def shell_quote(str) | |
188 self.class.shell_quote(str) | |
189 end | |
190 | |
191 private | |
192 def retrieve_root_url | |
193 info = self.info | |
194 info ? info.root_url : nil | |
195 end | |
196 | |
197 def target(path, sq=true) | |
198 path ||= '' | |
199 base = path.match(/^\//) ? root_url : url | |
200 str = "#{base}/#{path}".gsub(/[?<>\*]/, '') | |
201 if sq | |
202 str = shell_quote(str) | |
203 end | |
204 str | |
205 end | |
206 | |
207 def logger | |
208 self.class.logger | |
209 end | |
210 | |
211 def shellout(cmd, options = {}, &block) | |
212 self.class.shellout(cmd, options, &block) | |
213 end | |
214 | |
215 def self.logger | |
216 Rails.logger | |
217 end | |
218 | |
219 # Path to the file where scm stderr output is logged | |
220 # Returns nil if the log file is not writable | |
221 def self.stderr_log_file | |
222 if @stderr_log_file.nil? | |
223 writable = false | |
224 path = Redmine::Configuration['scm_stderr_log_file'].presence | |
225 path ||= Rails.root.join("log/#{Rails.env}.scm.stderr.log").to_s | |
226 if File.exists?(path) | |
227 if File.file?(path) && File.writable?(path) | |
228 writable = true | |
229 else | |
230 logger.warn("SCM log file (#{path}) is not writable") | |
231 end | |
232 else | |
233 begin | |
234 File.open(path, "w") {} | |
235 writable = true | |
236 rescue => e | |
237 logger.warn("SCM log file (#{path}) cannot be created: #{e.message}") | |
238 end | |
239 end | |
240 @stderr_log_file = writable ? path : false | |
241 end | |
242 @stderr_log_file || nil | |
243 end | |
244 | |
245 def self.shellout(cmd, options = {}, &block) | |
246 if logger && logger.debug? | |
247 logger.debug "Shelling out: #{strip_credential(cmd)}" | |
248 # Capture stderr in a log file | |
249 if stderr_log_file | |
250 cmd = "#{cmd} 2>>#{shell_quote(stderr_log_file)}" | |
251 end | |
252 end | |
253 begin | |
254 mode = "r+" | |
255 IO.popen(cmd, mode) do |io| | |
256 io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding) | |
257 io.close_write unless options[:write_stdin] | |
258 block.call(io) if block_given? | |
259 end | |
260 ## If scm command does not exist, | |
261 ## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException | |
262 ## in production environment. | |
263 # rescue Errno::ENOENT => e | |
264 rescue Exception => e | |
265 msg = strip_credential(e.message) | |
266 # The command failed, log it and re-raise | |
267 logmsg = "SCM command failed, " | |
268 logmsg += "make sure that your SCM command (e.g. svn) is " | |
269 logmsg += "in PATH (#{ENV['PATH']})\n" | |
270 logmsg += "You can configure your scm commands in config/configuration.yml.\n" | |
271 logmsg += "#{strip_credential(cmd)}\n" | |
272 logmsg += "with: #{msg}" | |
273 logger.error(logmsg) | |
274 raise CommandFailed.new(msg) | |
275 end | |
276 end | |
277 | |
278 # Hides username/password in a given command | |
279 def self.strip_credential(cmd) | |
280 q = (Redmine::Platform.mswin? ? '"' : "'") | |
281 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx') | |
282 end | |
283 | |
284 def strip_credential(cmd) | |
285 self.class.strip_credential(cmd) | |
286 end | |
287 | |
288 def scm_iconv(to, from, str) | |
289 return nil if str.nil? | |
290 return str if to == from | |
291 if str.respond_to?(:force_encoding) | |
292 str.force_encoding(from) | |
293 begin | |
294 str.encode(to) | |
295 rescue Exception => err | |
296 logger.error("failed to convert from #{from} to #{to}. #{err}") | |
297 nil | |
298 end | |
299 else | |
300 begin | |
301 Iconv.conv(to, from, str) | |
302 rescue Iconv::Failure => err | |
303 logger.error("failed to convert from #{from} to #{to}. #{err}") | |
304 nil | |
305 end | |
306 end | |
307 end | |
308 | |
309 def parse_xml(xml) | |
310 if RUBY_PLATFORM == 'java' | |
311 xml = xml.sub(%r{<\?xml[^>]*\?>}, '') | |
312 end | |
313 ActiveSupport::XmlMini.parse(xml) | |
314 end | |
315 end | |
316 | |
317 class Entries < Array | |
318 def sort_by_name | |
319 dup.sort! {|x,y| | |
320 if x.kind == y.kind | |
321 x.name.to_s <=> y.name.to_s | |
322 else | |
323 x.kind <=> y.kind | |
324 end | |
325 } | |
326 end | |
327 | |
328 def revisions | |
329 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact) | |
330 end | |
331 end | |
332 | |
333 class Info | |
334 attr_accessor :root_url, :lastrev | |
335 def initialize(attributes={}) | |
336 self.root_url = attributes[:root_url] if attributes[:root_url] | |
337 self.lastrev = attributes[:lastrev] | |
338 end | |
339 end | |
340 | |
341 class Entry | |
342 attr_accessor :name, :path, :kind, :size, :lastrev, :changeset | |
343 | |
344 def initialize(attributes={}) | |
345 self.name = attributes[:name] if attributes[:name] | |
346 self.path = attributes[:path] if attributes[:path] | |
347 self.kind = attributes[:kind] if attributes[:kind] | |
348 self.size = attributes[:size].to_i if attributes[:size] | |
349 self.lastrev = attributes[:lastrev] | |
350 end | |
351 | |
352 def is_file? | |
353 'file' == self.kind | |
354 end | |
355 | |
356 def is_dir? | |
357 'dir' == self.kind | |
358 end | |
359 | |
360 def is_text? | |
361 Redmine::MimeType.is_type?('text', name) | |
362 end | |
363 | |
364 def author | |
365 if changeset | |
366 changeset.author.to_s | |
367 elsif lastrev | |
368 Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first) | |
369 end | |
370 end | |
371 end | |
372 | |
373 class Revisions < Array | |
374 def latest | |
375 sort {|x,y| | |
376 unless x.time.nil? or y.time.nil? | |
377 x.time <=> y.time | |
378 else | |
379 0 | |
380 end | |
381 }.last | |
382 end | |
383 end | |
384 | |
385 class Revision | |
386 attr_accessor :scmid, :name, :author, :time, :message, | |
387 :paths, :revision, :branch, :identifier, | |
388 :parents | |
389 | |
390 def initialize(attributes={}) | |
391 self.identifier = attributes[:identifier] | |
392 self.scmid = attributes[:scmid] | |
393 self.name = attributes[:name] || self.identifier | |
394 self.author = attributes[:author] | |
395 self.time = attributes[:time] | |
396 self.message = attributes[:message] || "" | |
397 self.paths = attributes[:paths] | |
398 self.revision = attributes[:revision] | |
399 self.branch = attributes[:branch] | |
400 self.parents = attributes[:parents] | |
401 end | |
402 | |
403 # Returns the readable identifier. | |
404 def format_identifier | |
405 self.identifier.to_s | |
406 end | |
407 | |
408 def ==(other) | |
409 if other.nil? | |
410 false | |
411 elsif scmid.present? | |
412 scmid == other.scmid | |
413 elsif identifier.present? | |
414 identifier == other.identifier | |
415 elsif revision.present? | |
416 revision == other.revision | |
417 end | |
418 end | |
419 end | |
420 | |
421 class Annotate | |
422 attr_reader :lines, :revisions | |
423 | |
424 def initialize | |
425 @lines = [] | |
426 @revisions = [] | |
427 end | |
428 | |
429 def add_line(line, revision) | |
430 @lines << line | |
431 @revisions << revision | |
432 end | |
433 | |
434 def content | |
435 content = lines.join("\n") | |
436 end | |
437 | |
438 def empty? | |
439 lines.empty? | |
440 end | |
441 end | |
442 | |
443 class Branch < String | |
444 attr_accessor :revision, :scmid | |
445 end | |
446 end | |
447 end | |
448 end |