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