Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'cgi'
|
Chris@0
|
19
|
Chris@0
|
20 module Redmine
|
Chris@0
|
21 module Scm
|
Chris@245
|
22 module Adapters
|
Chris@0
|
23 class CommandFailed < StandardError #:nodoc:
|
Chris@0
|
24 end
|
Chris@245
|
25
|
Chris@0
|
26 class AbstractAdapter #:nodoc:
|
Chris@0
|
27 class << self
|
Chris@245
|
28 def client_command
|
Chris@245
|
29 ""
|
Chris@245
|
30 end
|
Chris@245
|
31
|
Chris@0
|
32 # Returns the version of the scm client
|
Chris@0
|
33 # Eg: [1, 5, 0] or [] if unknown
|
Chris@0
|
34 def client_version
|
Chris@0
|
35 []
|
Chris@0
|
36 end
|
Chris@245
|
37
|
Chris@0
|
38 # Returns the version string of the scm client
|
Chris@0
|
39 # Eg: '1.5.0' or 'Unknown version' if unknown
|
Chris@0
|
40 def client_version_string
|
Chris@0
|
41 v = client_version || 'Unknown version'
|
Chris@0
|
42 v.is_a?(Array) ? v.join('.') : v.to_s
|
Chris@0
|
43 end
|
Chris@245
|
44
|
Chris@0
|
45 # Returns true if the current client version is above
|
Chris@0
|
46 # or equals the given one
|
Chris@0
|
47 # If option is :unknown is set to true, it will return
|
Chris@0
|
48 # true if the client version is unknown
|
Chris@0
|
49 def client_version_above?(v, options={})
|
Chris@0
|
50 ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
|
Chris@0
|
51 end
|
Chris@245
|
52
|
Chris@245
|
53 def client_available
|
Chris@245
|
54 true
|
Chris@245
|
55 end
|
Chris@245
|
56
|
Chris@245
|
57 def shell_quote(str)
|
Chris@245
|
58 if Redmine::Platform.mswin?
|
Chris@245
|
59 '"' + str.gsub(/"/, '\\"') + '"'
|
Chris@245
|
60 else
|
Chris@245
|
61 "'" + str.gsub(/'/, "'\"'\"'") + "'"
|
Chris@245
|
62 end
|
Chris@245
|
63 end
|
Chris@0
|
64 end
|
Chris@245
|
65
|
Chris@245
|
66 def initialize(url, root_url=nil, login=nil, password=nil,
|
Chris@245
|
67 path_encoding=nil)
|
Chris@0
|
68 @url = url
|
Chris@0
|
69 @login = login if login && !login.empty?
|
Chris@0
|
70 @password = (password || "") if @login
|
Chris@0
|
71 @root_url = root_url.blank? ? retrieve_root_url : root_url
|
Chris@0
|
72 end
|
Chris@245
|
73
|
Chris@0
|
74 def adapter_name
|
Chris@0
|
75 'Abstract'
|
Chris@0
|
76 end
|
Chris@245
|
77
|
Chris@0
|
78 def supports_cat?
|
Chris@0
|
79 true
|
Chris@0
|
80 end
|
Chris@0
|
81
|
Chris@0
|
82 def supports_annotate?
|
Chris@0
|
83 respond_to?('annotate')
|
Chris@0
|
84 end
|
Chris@245
|
85
|
Chris@0
|
86 def root_url
|
Chris@0
|
87 @root_url
|
Chris@0
|
88 end
|
Chris@245
|
89
|
Chris@0
|
90 def url
|
Chris@0
|
91 @url
|
Chris@0
|
92 end
|
Chris@0
|
93
|
Chris@0
|
94 # get info about the svn repository
|
Chris@0
|
95 def info
|
Chris@0
|
96 return nil
|
Chris@0
|
97 end
|
Chris@0
|
98
|
Chris@0
|
99 # Returns the entry identified by path and revision identifier
|
Chris@0
|
100 # or nil if entry doesn't exist in the repository
|
Chris@0
|
101 def entry(path=nil, identifier=nil)
|
Chris@0
|
102 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
|
Chris@0
|
103 search_path = parts[0..-2].join('/')
|
Chris@0
|
104 search_name = parts[-1]
|
Chris@0
|
105 if search_path.blank? && search_name.blank?
|
Chris@0
|
106 # Root entry
|
Chris@0
|
107 Entry.new(:path => '', :kind => 'dir')
|
Chris@0
|
108 else
|
Chris@0
|
109 # Search for the entry in the parent directory
|
Chris@0
|
110 es = entries(search_path, identifier)
|
Chris@0
|
111 es ? es.detect {|e| e.name == search_name} : nil
|
Chris@0
|
112 end
|
Chris@0
|
113 end
|
Chris@0
|
114
|
Chris@0
|
115 # Returns an Entries collection
|
Chris@0
|
116 # or nil if the given path doesn't exist in the repository
|
Chris@0
|
117 def entries(path=nil, identifier=nil)
|
Chris@0
|
118 return nil
|
Chris@0
|
119 end
|
Chris@0
|
120
|
Chris@0
|
121 def branches
|
Chris@0
|
122 return nil
|
Chris@0
|
123 end
|
Chris@0
|
124
|
Chris@0
|
125 def tags
|
Chris@0
|
126 return nil
|
Chris@0
|
127 end
|
Chris@0
|
128
|
Chris@0
|
129 def default_branch
|
Chris@0
|
130 return nil
|
Chris@0
|
131 end
|
Chris@0
|
132
|
Chris@0
|
133 def properties(path, identifier=nil)
|
Chris@0
|
134 return nil
|
Chris@0
|
135 end
|
Chris@0
|
136
|
Chris@0
|
137 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
Chris@0
|
138 return nil
|
Chris@0
|
139 end
|
Chris@0
|
140
|
Chris@0
|
141 def diff(path, identifier_from, identifier_to=nil)
|
Chris@0
|
142 return nil
|
Chris@0
|
143 end
|
Chris@0
|
144
|
Chris@0
|
145 def cat(path, identifier=nil)
|
Chris@0
|
146 return nil
|
Chris@0
|
147 end
|
Chris@0
|
148
|
Chris@0
|
149 def with_leading_slash(path)
|
Chris@0
|
150 path ||= ''
|
Chris@0
|
151 (path[0,1]!="/") ? "/#{path}" : path
|
Chris@0
|
152 end
|
Chris@0
|
153
|
Chris@0
|
154 def with_trailling_slash(path)
|
Chris@0
|
155 path ||= ''
|
Chris@0
|
156 (path[-1,1] == "/") ? path : "#{path}/"
|
Chris@0
|
157 end
|
Chris@245
|
158
|
Chris@0
|
159 def without_leading_slash(path)
|
Chris@0
|
160 path ||= ''
|
Chris@0
|
161 path.gsub(%r{^/+}, '')
|
Chris@0
|
162 end
|
Chris@0
|
163
|
Chris@0
|
164 def without_trailling_slash(path)
|
Chris@0
|
165 path ||= ''
|
Chris@0
|
166 (path[-1,1] == "/") ? path[0..-2] : path
|
Chris@0
|
167 end
|
Chris@245
|
168
|
Chris@0
|
169 def shell_quote(str)
|
Chris@245
|
170 self.class.shell_quote(str)
|
Chris@0
|
171 end
|
Chris@0
|
172
|
Chris@0
|
173 private
|
Chris@0
|
174 def retrieve_root_url
|
Chris@0
|
175 info = self.info
|
Chris@0
|
176 info ? info.root_url : nil
|
Chris@0
|
177 end
|
Chris@0
|
178
|
Chris@0
|
179 def target(path)
|
Chris@0
|
180 path ||= ''
|
Chris@0
|
181 base = path.match(/^\//) ? root_url : url
|
Chris@0
|
182 shell_quote("#{base}/#{path}".gsub(/[?<>\*]/, ''))
|
Chris@0
|
183 end
|
Chris@245
|
184
|
Chris@0
|
185 def logger
|
Chris@0
|
186 self.class.logger
|
Chris@0
|
187 end
|
Chris@245
|
188
|
Chris@0
|
189 def shellout(cmd, &block)
|
Chris@0
|
190 self.class.shellout(cmd, &block)
|
Chris@0
|
191 end
|
Chris@245
|
192
|
Chris@0
|
193 def self.logger
|
Chris@0
|
194 RAILS_DEFAULT_LOGGER
|
Chris@0
|
195 end
|
Chris@245
|
196
|
Chris@0
|
197 def self.shellout(cmd, &block)
|
Chris@0
|
198 logger.debug "Shelling out: #{strip_credential(cmd)}" if logger && logger.debug?
|
Chris@0
|
199 if Rails.env == 'development'
|
Chris@0
|
200 # Capture stderr when running in dev environment
|
Chris@0
|
201 cmd = "#{cmd} 2>>#{RAILS_ROOT}/log/scm.stderr.log"
|
Chris@0
|
202 end
|
Chris@0
|
203 begin
|
Chris@245
|
204 if RUBY_VERSION < '1.9'
|
Chris@245
|
205 mode = "r+"
|
Chris@245
|
206 else
|
Chris@245
|
207 mode = "r+:ASCII-8BIT"
|
Chris@245
|
208 end
|
Chris@245
|
209 IO.popen(cmd, mode) do |io|
|
Chris@0
|
210 io.close_write
|
Chris@0
|
211 block.call(io) if block_given?
|
Chris@0
|
212 end
|
Chris@0
|
213 rescue Errno::ENOENT => e
|
Chris@0
|
214 msg = strip_credential(e.message)
|
Chris@0
|
215 # The command failed, log it and re-raise
|
Chris@0
|
216 logger.error("SCM command failed, make sure that your SCM binary (eg. svn) is in PATH (#{ENV['PATH']}): #{strip_credential(cmd)}\n with: #{msg}")
|
Chris@0
|
217 raise CommandFailed.new(msg)
|
Chris@0
|
218 end
|
Chris@245
|
219 end
|
Chris@245
|
220
|
Chris@0
|
221 # Hides username/password in a given command
|
Chris@0
|
222 def self.strip_credential(cmd)
|
Chris@0
|
223 q = (Redmine::Platform.mswin? ? '"' : "'")
|
Chris@0
|
224 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
|
Chris@0
|
225 end
|
Chris@0
|
226
|
Chris@0
|
227 def strip_credential(cmd)
|
Chris@0
|
228 self.class.strip_credential(cmd)
|
Chris@0
|
229 end
|
Chris@245
|
230
|
Chris@245
|
231 def scm_iconv(to, from, str)
|
Chris@245
|
232 return nil if str.nil?
|
Chris@245
|
233 return str if to == from
|
Chris@245
|
234 begin
|
Chris@245
|
235 Iconv.conv(to, from, str)
|
Chris@245
|
236 rescue Iconv::Failure => err
|
Chris@245
|
237 logger.error("failed to convert from #{from} to #{to}. #{err}")
|
Chris@245
|
238 nil
|
Chris@245
|
239 end
|
Chris@245
|
240 end
|
Chris@0
|
241 end
|
Chris@245
|
242
|
Chris@0
|
243 class Entries < Array
|
Chris@0
|
244 def sort_by_name
|
Chris@0
|
245 sort {|x,y|
|
Chris@0
|
246 if x.kind == y.kind
|
Chris@0
|
247 x.name.to_s <=> y.name.to_s
|
Chris@0
|
248 else
|
Chris@0
|
249 x.kind <=> y.kind
|
Chris@0
|
250 end
|
Chris@245
|
251 }
|
Chris@0
|
252 end
|
Chris@0
|
253
|
Chris@0
|
254 def revisions
|
Chris@0
|
255 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
|
Chris@0
|
256 end
|
Chris@0
|
257 end
|
Chris@0
|
258
|
Chris@0
|
259 class Info
|
Chris@0
|
260 attr_accessor :root_url, :lastrev
|
Chris@0
|
261 def initialize(attributes={})
|
Chris@0
|
262 self.root_url = attributes[:root_url] if attributes[:root_url]
|
Chris@0
|
263 self.lastrev = attributes[:lastrev]
|
Chris@0
|
264 end
|
Chris@0
|
265 end
|
Chris@0
|
266
|
Chris@0
|
267 class Entry
|
Chris@0
|
268 attr_accessor :name, :path, :kind, :size, :lastrev
|
Chris@0
|
269 def initialize(attributes={})
|
Chris@0
|
270 self.name = attributes[:name] if attributes[:name]
|
Chris@0
|
271 self.path = attributes[:path] if attributes[:path]
|
Chris@0
|
272 self.kind = attributes[:kind] if attributes[:kind]
|
Chris@0
|
273 self.size = attributes[:size].to_i if attributes[:size]
|
Chris@0
|
274 self.lastrev = attributes[:lastrev]
|
Chris@0
|
275 end
|
Chris@0
|
276
|
Chris@0
|
277 def is_file?
|
Chris@0
|
278 'file' == self.kind
|
Chris@0
|
279 end
|
Chris@0
|
280
|
Chris@0
|
281 def is_dir?
|
Chris@0
|
282 'dir' == self.kind
|
Chris@0
|
283 end
|
Chris@0
|
284
|
Chris@0
|
285 def is_text?
|
Chris@0
|
286 Redmine::MimeType.is_type?('text', name)
|
Chris@0
|
287 end
|
Chris@0
|
288 end
|
Chris@0
|
289
|
Chris@0
|
290 class Revisions < Array
|
Chris@0
|
291 def latest
|
Chris@0
|
292 sort {|x,y|
|
Chris@0
|
293 unless x.time.nil? or y.time.nil?
|
Chris@0
|
294 x.time <=> y.time
|
Chris@0
|
295 else
|
Chris@0
|
296 0
|
Chris@0
|
297 end
|
Chris@0
|
298 }.last
|
Chris@0
|
299 end
|
Chris@0
|
300 end
|
Chris@0
|
301
|
Chris@0
|
302 class Revision
|
Chris@117
|
303 attr_accessor :scmid, :name, :author, :time, :message, :paths, :revision, :branch
|
Chris@117
|
304 attr_writer :identifier
|
Chris@0
|
305
|
Chris@0
|
306 def initialize(attributes={})
|
Chris@0
|
307 self.identifier = attributes[:identifier]
|
Chris@0
|
308 self.scmid = attributes[:scmid]
|
Chris@0
|
309 self.name = attributes[:name] || self.identifier
|
Chris@0
|
310 self.author = attributes[:author]
|
Chris@0
|
311 self.time = attributes[:time]
|
Chris@0
|
312 self.message = attributes[:message] || ""
|
Chris@0
|
313 self.paths = attributes[:paths]
|
Chris@0
|
314 self.revision = attributes[:revision]
|
Chris@0
|
315 self.branch = attributes[:branch]
|
Chris@0
|
316 end
|
Chris@0
|
317
|
Chris@117
|
318 # Returns the identifier of this revision; see also Changeset model
|
Chris@117
|
319 def identifier
|
Chris@117
|
320 (@identifier || revision).to_s
|
Chris@117
|
321 end
|
Chris@117
|
322
|
Chris@117
|
323 # Returns the readable identifier.
|
Chris@117
|
324 def format_identifier
|
Chris@117
|
325 identifier
|
Chris@117
|
326 end
|
Chris@245
|
327 end
|
Chris@117
|
328
|
Chris@0
|
329 class Annotate
|
Chris@0
|
330 attr_reader :lines, :revisions
|
Chris@0
|
331
|
Chris@0
|
332 def initialize
|
Chris@0
|
333 @lines = []
|
Chris@0
|
334 @revisions = []
|
Chris@0
|
335 end
|
Chris@0
|
336
|
Chris@0
|
337 def add_line(line, revision)
|
Chris@0
|
338 @lines << line
|
Chris@0
|
339 @revisions << revision
|
Chris@0
|
340 end
|
Chris@0
|
341
|
Chris@0
|
342 def content
|
Chris@0
|
343 content = lines.join("\n")
|
Chris@0
|
344 end
|
Chris@0
|
345
|
Chris@0
|
346 def empty?
|
Chris@0
|
347 lines.empty?
|
Chris@0
|
348 end
|
Chris@0
|
349 end
|
Chris@0
|
350 end
|
Chris@0
|
351 end
|
Chris@0
|
352 end
|