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