##// END OF EJS Templates
scm: subversion: use "shell_quote_command" method at adapter for JRuby + Windows command name (#8825)....
Toshi MARUYAMA -
r6154:ce1c6209caf1
parent child
Show More
@@ -1,291 +1,291
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 require 'uri'
20 20
21 21 module Redmine
22 22 module Scm
23 23 module Adapters
24 24 class SubversionAdapter < AbstractAdapter
25 25
26 26 # SVN executable name
27 27 SVN_BIN = Redmine::Configuration['scm_subversion_command'] || "svn"
28 28
29 29 class << self
30 30 def client_command
31 31 @@bin ||= SVN_BIN
32 32 end
33 33
34 34 def sq_bin
35 @@sq_bin ||= shell_quote(SVN_BIN)
35 @@sq_bin ||= shell_quote_command
36 36 end
37 37
38 38 def client_version
39 39 @@client_version ||= (svn_binary_version || [])
40 40 end
41 41
42 42 def client_available
43 43 # --xml options are introduced in 1.3.
44 44 # http://subversion.apache.org/docs/release-notes/1.3.html
45 45 client_version_above?([1, 3])
46 46 end
47 47
48 48 def svn_binary_version
49 49 scm_version = scm_version_from_command_line.dup
50 50 if scm_version.respond_to?(:force_encoding)
51 51 scm_version.force_encoding('ASCII-8BIT')
52 52 end
53 53 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
54 54 m[2].scan(%r{\d+}).collect(&:to_i)
55 55 end
56 56 end
57 57
58 58 def scm_version_from_command_line
59 59 shellout("#{sq_bin} --version") { |io| io.read }.to_s
60 60 end
61 61 end
62 62
63 63 # Get info about the svn repository
64 64 def info
65 65 cmd = "#{self.class.sq_bin} info --xml #{target}"
66 66 cmd << credentials_string
67 67 info = nil
68 68 shellout(cmd) do |io|
69 69 output = io.read
70 70 if output.respond_to?(:force_encoding)
71 71 output.force_encoding('UTF-8')
72 72 end
73 73 begin
74 74 doc = ActiveSupport::XmlMini.parse(output)
75 75 # root_url = doc.elements["info/entry/repository/root"].text
76 76 info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'],
77 77 :lastrev => Revision.new({
78 78 :identifier => doc['info']['entry']['commit']['revision'],
79 79 :time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime,
80 80 :author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "")
81 81 })
82 82 })
83 83 rescue
84 84 end
85 85 end
86 86 return nil if $? && $?.exitstatus != 0
87 87 info
88 88 rescue CommandFailed
89 89 return nil
90 90 end
91 91
92 92 # Returns an Entries collection
93 93 # or nil if the given path doesn't exist in the repository
94 94 def entries(path=nil, identifier=nil, options={})
95 95 path ||= ''
96 96 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
97 97 entries = Entries.new
98 98 cmd = "#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}"
99 99 cmd << credentials_string
100 100 shellout(cmd) do |io|
101 101 output = io.read
102 102 if output.respond_to?(:force_encoding)
103 103 output.force_encoding('UTF-8')
104 104 end
105 105 begin
106 106 doc = ActiveSupport::XmlMini.parse(output)
107 107 each_xml_element(doc['lists']['list'], 'entry') do |entry|
108 108 commit = entry['commit']
109 109 commit_date = commit['date']
110 110 # Skip directory if there is no commit date (usually that
111 111 # means that we don't have read access to it)
112 112 next if entry['kind'] == 'dir' && commit_date.nil?
113 113 name = entry['name']['__content__']
114 114 entries << Entry.new({:name => URI.unescape(name),
115 115 :path => ((path.empty? ? "" : "#{path}/") + name),
116 116 :kind => entry['kind'],
117 117 :size => ((s = entry['size']) ? s['__content__'].to_i : nil),
118 118 :lastrev => Revision.new({
119 119 :identifier => commit['revision'],
120 120 :time => Time.parse(commit_date['__content__'].to_s).localtime,
121 121 :author => ((a = commit['author']) ? a['__content__'] : nil)
122 122 })
123 123 })
124 124 end
125 125 rescue Exception => e
126 126 logger.error("Error parsing svn output: #{e.message}")
127 127 logger.error("Output was:\n #{output}")
128 128 end
129 129 end
130 130 return nil if $? && $?.exitstatus != 0
131 131 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
132 132 entries.sort_by_name
133 133 end
134 134
135 135 def properties(path, identifier=nil)
136 136 # proplist xml output supported in svn 1.5.0 and higher
137 137 return nil unless self.class.client_version_above?([1, 5, 0])
138 138
139 139 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
140 140 cmd = "#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}"
141 141 cmd << credentials_string
142 142 properties = {}
143 143 shellout(cmd) do |io|
144 144 output = io.read
145 145 if output.respond_to?(:force_encoding)
146 146 output.force_encoding('UTF-8')
147 147 end
148 148 begin
149 149 doc = ActiveSupport::XmlMini.parse(output)
150 150 each_xml_element(doc['properties']['target'], 'property') do |property|
151 151 properties[ property['name'] ] = property['__content__'].to_s
152 152 end
153 153 rescue
154 154 end
155 155 end
156 156 return nil if $? && $?.exitstatus != 0
157 157 properties
158 158 end
159 159
160 160 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
161 161 path ||= ''
162 162 identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
163 163 identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1
164 164 revisions = Revisions.new
165 165 cmd = "#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}"
166 166 cmd << credentials_string
167 167 cmd << " --verbose " if options[:with_paths]
168 168 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
169 169 cmd << ' ' + target(path)
170 170 shellout(cmd) do |io|
171 171 output = io.read
172 172 if output.respond_to?(:force_encoding)
173 173 output.force_encoding('UTF-8')
174 174 end
175 175 begin
176 176 doc = ActiveSupport::XmlMini.parse(output)
177 177 each_xml_element(doc['log'], 'logentry') do |logentry|
178 178 paths = []
179 179 each_xml_element(logentry['paths'], 'path') do |path|
180 180 paths << {:action => path['action'],
181 181 :path => path['__content__'],
182 182 :from_path => path['copyfrom-path'],
183 183 :from_revision => path['copyfrom-rev']
184 184 }
185 185 end if logentry['paths'] && logentry['paths']['path']
186 186 paths.sort! { |x,y| x[:path] <=> y[:path] }
187 187
188 188 revisions << Revision.new({:identifier => logentry['revision'],
189 189 :author => (logentry['author'] ? logentry['author']['__content__'] : ""),
190 190 :time => Time.parse(logentry['date']['__content__'].to_s).localtime,
191 191 :message => logentry['msg']['__content__'],
192 192 :paths => paths
193 193 })
194 194 end
195 195 rescue
196 196 end
197 197 end
198 198 return nil if $? && $?.exitstatus != 0
199 199 revisions
200 200 end
201 201
202 202 def diff(path, identifier_from, identifier_to=nil, type="inline")
203 203 path ||= ''
204 204 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
205 205
206 206 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
207 207
208 208 cmd = "#{self.class.sq_bin} diff -r "
209 209 cmd << "#{identifier_to}:"
210 210 cmd << "#{identifier_from}"
211 211 cmd << " #{target(path)}@#{identifier_from}"
212 212 cmd << credentials_string
213 213 diff = []
214 214 shellout(cmd) do |io|
215 215 io.each_line do |line|
216 216 diff << line
217 217 end
218 218 end
219 219 return nil if $? && $?.exitstatus != 0
220 220 diff
221 221 end
222 222
223 223 def cat(path, identifier=nil)
224 224 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
225 225 cmd = "#{self.class.sq_bin} cat #{target(path)}@#{identifier}"
226 226 cmd << credentials_string
227 227 cat = nil
228 228 shellout(cmd) do |io|
229 229 io.binmode
230 230 cat = io.read
231 231 end
232 232 return nil if $? && $?.exitstatus != 0
233 233 cat
234 234 end
235 235
236 236 def annotate(path, identifier=nil)
237 237 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
238 238 cmd = "#{self.class.sq_bin} blame #{target(path)}@#{identifier}"
239 239 cmd << credentials_string
240 240 blame = Annotate.new
241 241 shellout(cmd) do |io|
242 242 io.each_line do |line|
243 243 next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
244 244 rev = $1
245 245 blame.add_line($3.rstrip,
246 246 Revision.new(
247 247 :identifier => rev,
248 248 :revision => rev,
249 249 :author => $2.strip
250 250 ))
251 251 end
252 252 end
253 253 return nil if $? && $?.exitstatus != 0
254 254 blame
255 255 end
256 256
257 257 private
258 258
259 259 def credentials_string
260 260 str = ''
261 261 str << " --username #{shell_quote(@login)}" unless @login.blank?
262 262 str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
263 263 str << " --no-auth-cache --non-interactive"
264 264 str
265 265 end
266 266
267 267 # Helper that iterates over the child elements of a xml node
268 268 # MiniXml returns a hash when a single child is found
269 269 # or an array of hashes for multiple children
270 270 def each_xml_element(node, name)
271 271 if node && node[name]
272 272 if node[name].is_a?(Hash)
273 273 yield node[name]
274 274 else
275 275 node[name].each do |element|
276 276 yield element
277 277 end
278 278 end
279 279 end
280 280 end
281 281
282 282 def target(path = '')
283 283 base = path.match(/^\//) ? root_url : url
284 284 uri = "#{base}/#{path}"
285 285 uri = URI.escape(URI.escape(uri), '[]')
286 286 shell_quote(uri.gsub(/[?<>\*]/, ''))
287 287 end
288 288 end
289 289 end
290 290 end
291 291 end
General Comments 0
You need to be logged in to leave comments. Login now