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