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