##// END OF EJS Templates
Change subversion adapter to not cache authentication and run non interactively...
Eric Davis -
r2714:1a9942ba9952
parent child
Show More
@@ -1,233 +1,234
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 'rexml/document'
19 require 'rexml/document'
20 require 'uri'
20 require 'uri'
21
21
22 module Redmine
22 module Redmine
23 module Scm
23 module Scm
24 module Adapters
24 module Adapters
25 class SubversionAdapter < AbstractAdapter
25 class SubversionAdapter < AbstractAdapter
26
26
27 # SVN executable name
27 # SVN executable name
28 SVN_BIN = "svn"
28 SVN_BIN = "svn"
29
29
30 class << self
30 class << self
31 def client_version
31 def client_version
32 @@client_version ||= (svn_binary_version || [])
32 @@client_version ||= (svn_binary_version || [])
33 end
33 end
34
34
35 def svn_binary_version
35 def svn_binary_version
36 cmd = "#{SVN_BIN} --version"
36 cmd = "#{SVN_BIN} --version"
37 version = nil
37 version = nil
38 shellout(cmd) do |io|
38 shellout(cmd) do |io|
39 # Read svn version in first returned line
39 # Read svn version in first returned line
40 if m = io.gets.to_s.match(%r{((\d+\.)+\d+)})
40 if m = io.gets.to_s.match(%r{((\d+\.)+\d+)})
41 version = m[0].scan(%r{\d+}).collect(&:to_i)
41 version = m[0].scan(%r{\d+}).collect(&:to_i)
42 end
42 end
43 end
43 end
44 return nil if $? && $?.exitstatus != 0
44 return nil if $? && $?.exitstatus != 0
45 version
45 version
46 end
46 end
47 end
47 end
48
48
49 # Get info about the svn repository
49 # Get info about the svn repository
50 def info
50 def info
51 cmd = "#{SVN_BIN} info --xml #{target('')}"
51 cmd = "#{SVN_BIN} info --xml #{target('')}"
52 cmd << credentials_string
52 cmd << credentials_string
53 info = nil
53 info = nil
54 shellout(cmd) do |io|
54 shellout(cmd) do |io|
55 begin
55 begin
56 doc = REXML::Document.new(io)
56 doc = REXML::Document.new(io)
57 #root_url = doc.elements["info/entry/repository/root"].text
57 #root_url = doc.elements["info/entry/repository/root"].text
58 info = Info.new({:root_url => doc.elements["info/entry/repository/root"].text,
58 info = Info.new({:root_url => doc.elements["info/entry/repository/root"].text,
59 :lastrev => Revision.new({
59 :lastrev => Revision.new({
60 :identifier => doc.elements["info/entry/commit"].attributes['revision'],
60 :identifier => doc.elements["info/entry/commit"].attributes['revision'],
61 :time => Time.parse(doc.elements["info/entry/commit/date"].text).localtime,
61 :time => Time.parse(doc.elements["info/entry/commit/date"].text).localtime,
62 :author => (doc.elements["info/entry/commit/author"] ? doc.elements["info/entry/commit/author"].text : "")
62 :author => (doc.elements["info/entry/commit/author"] ? doc.elements["info/entry/commit/author"].text : "")
63 })
63 })
64 })
64 })
65 rescue
65 rescue
66 end
66 end
67 end
67 end
68 return nil if $? && $?.exitstatus != 0
68 return nil if $? && $?.exitstatus != 0
69 info
69 info
70 rescue CommandFailed
70 rescue CommandFailed
71 return nil
71 return nil
72 end
72 end
73
73
74 # Returns an Entries collection
74 # Returns an Entries collection
75 # or nil if the given path doesn't exist in the repository
75 # or nil if the given path doesn't exist in the repository
76 def entries(path=nil, identifier=nil)
76 def entries(path=nil, identifier=nil)
77 path ||= ''
77 path ||= ''
78 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
78 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
79 entries = Entries.new
79 entries = Entries.new
80 cmd = "#{SVN_BIN} list --xml #{target(URI.escape(path))}@#{identifier}"
80 cmd = "#{SVN_BIN} list --xml #{target(URI.escape(path))}@#{identifier}"
81 cmd << credentials_string
81 cmd << credentials_string
82 shellout(cmd) do |io|
82 shellout(cmd) do |io|
83 output = io.read
83 output = io.read
84 begin
84 begin
85 doc = REXML::Document.new(output)
85 doc = REXML::Document.new(output)
86 doc.elements.each("lists/list/entry") do |entry|
86 doc.elements.each("lists/list/entry") do |entry|
87 commit = entry.elements['commit']
87 commit = entry.elements['commit']
88 commit_date = commit.elements['date']
88 commit_date = commit.elements['date']
89 # Skip directory if there is no commit date (usually that
89 # Skip directory if there is no commit date (usually that
90 # means that we don't have read access to it)
90 # means that we don't have read access to it)
91 next if entry.attributes['kind'] == 'dir' && commit_date.nil?
91 next if entry.attributes['kind'] == 'dir' && commit_date.nil?
92 name = entry.elements['name'].text
92 name = entry.elements['name'].text
93 entries << Entry.new({:name => URI.unescape(name),
93 entries << Entry.new({:name => URI.unescape(name),
94 :path => ((path.empty? ? "" : "#{path}/") + name),
94 :path => ((path.empty? ? "" : "#{path}/") + name),
95 :kind => entry.attributes['kind'],
95 :kind => entry.attributes['kind'],
96 :size => ((s = entry.elements['size']) ? s.text.to_i : nil),
96 :size => ((s = entry.elements['size']) ? s.text.to_i : nil),
97 :lastrev => Revision.new({
97 :lastrev => Revision.new({
98 :identifier => commit.attributes['revision'],
98 :identifier => commit.attributes['revision'],
99 :time => Time.parse(commit_date.text).localtime,
99 :time => Time.parse(commit_date.text).localtime,
100 :author => ((a = commit.elements['author']) ? a.text : nil)
100 :author => ((a = commit.elements['author']) ? a.text : nil)
101 })
101 })
102 })
102 })
103 end
103 end
104 rescue Exception => e
104 rescue Exception => e
105 logger.error("Error parsing svn output: #{e.message}")
105 logger.error("Error parsing svn output: #{e.message}")
106 logger.error("Output was:\n #{output}")
106 logger.error("Output was:\n #{output}")
107 end
107 end
108 end
108 end
109 return nil if $? && $?.exitstatus != 0
109 return nil if $? && $?.exitstatus != 0
110 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
110 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
111 entries.sort_by_name
111 entries.sort_by_name
112 end
112 end
113
113
114 def properties(path, identifier=nil)
114 def properties(path, identifier=nil)
115 # proplist xml output supported in svn 1.5.0 and higher
115 # proplist xml output supported in svn 1.5.0 and higher
116 return nil unless self.class.client_version_above?([1, 5, 0])
116 return nil unless self.class.client_version_above?([1, 5, 0])
117
117
118 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
118 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
119 cmd = "#{SVN_BIN} proplist --verbose --xml #{target(URI.escape(path))}@#{identifier}"
119 cmd = "#{SVN_BIN} proplist --verbose --xml #{target(URI.escape(path))}@#{identifier}"
120 cmd << credentials_string
120 cmd << credentials_string
121 properties = {}
121 properties = {}
122 shellout(cmd) do |io|
122 shellout(cmd) do |io|
123 output = io.read
123 output = io.read
124 begin
124 begin
125 doc = REXML::Document.new(output)
125 doc = REXML::Document.new(output)
126 doc.elements.each("properties/target/property") do |property|
126 doc.elements.each("properties/target/property") do |property|
127 properties[ property.attributes['name'] ] = property.text
127 properties[ property.attributes['name'] ] = property.text
128 end
128 end
129 rescue
129 rescue
130 end
130 end
131 end
131 end
132 return nil if $? && $?.exitstatus != 0
132 return nil if $? && $?.exitstatus != 0
133 properties
133 properties
134 end
134 end
135
135
136 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
136 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
137 path ||= ''
137 path ||= ''
138 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
138 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
139 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
139 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
140 revisions = Revisions.new
140 revisions = Revisions.new
141 cmd = "#{SVN_BIN} log --xml -r #{identifier_from}:#{identifier_to}"
141 cmd = "#{SVN_BIN} log --xml -r #{identifier_from}:#{identifier_to}"
142 cmd << credentials_string
142 cmd << credentials_string
143 cmd << " --verbose " if options[:with_paths]
143 cmd << " --verbose " if options[:with_paths]
144 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
144 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
145 cmd << ' ' + target(URI.escape(path))
145 cmd << ' ' + target(URI.escape(path))
146 shellout(cmd) do |io|
146 shellout(cmd) do |io|
147 begin
147 begin
148 doc = REXML::Document.new(io)
148 doc = REXML::Document.new(io)
149 doc.elements.each("log/logentry") do |logentry|
149 doc.elements.each("log/logentry") do |logentry|
150 paths = []
150 paths = []
151 logentry.elements.each("paths/path") do |path|
151 logentry.elements.each("paths/path") do |path|
152 paths << {:action => path.attributes['action'],
152 paths << {:action => path.attributes['action'],
153 :path => path.text,
153 :path => path.text,
154 :from_path => path.attributes['copyfrom-path'],
154 :from_path => path.attributes['copyfrom-path'],
155 :from_revision => path.attributes['copyfrom-rev']
155 :from_revision => path.attributes['copyfrom-rev']
156 }
156 }
157 end
157 end
158 paths.sort! { |x,y| x[:path] <=> y[:path] }
158 paths.sort! { |x,y| x[:path] <=> y[:path] }
159
159
160 revisions << Revision.new({:identifier => logentry.attributes['revision'],
160 revisions << Revision.new({:identifier => logentry.attributes['revision'],
161 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
161 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
162 :time => Time.parse(logentry.elements['date'].text).localtime,
162 :time => Time.parse(logentry.elements['date'].text).localtime,
163 :message => logentry.elements['msg'].text,
163 :message => logentry.elements['msg'].text,
164 :paths => paths
164 :paths => paths
165 })
165 })
166 end
166 end
167 rescue
167 rescue
168 end
168 end
169 end
169 end
170 return nil if $? && $?.exitstatus != 0
170 return nil if $? && $?.exitstatus != 0
171 revisions
171 revisions
172 end
172 end
173
173
174 def diff(path, identifier_from, identifier_to=nil, type="inline")
174 def diff(path, identifier_from, identifier_to=nil, type="inline")
175 path ||= ''
175 path ||= ''
176 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
176 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
177 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
177 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
178
178
179 cmd = "#{SVN_BIN} diff -r "
179 cmd = "#{SVN_BIN} diff -r "
180 cmd << "#{identifier_to}:"
180 cmd << "#{identifier_to}:"
181 cmd << "#{identifier_from}"
181 cmd << "#{identifier_from}"
182 cmd << " #{target(URI.escape(path))}@#{identifier_from}"
182 cmd << " #{target(URI.escape(path))}@#{identifier_from}"
183 cmd << credentials_string
183 cmd << credentials_string
184 diff = []
184 diff = []
185 shellout(cmd) do |io|
185 shellout(cmd) do |io|
186 io.each_line do |line|
186 io.each_line do |line|
187 diff << line
187 diff << line
188 end
188 end
189 end
189 end
190 return nil if $? && $?.exitstatus != 0
190 return nil if $? && $?.exitstatus != 0
191 diff
191 diff
192 end
192 end
193
193
194 def cat(path, identifier=nil)
194 def cat(path, identifier=nil)
195 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
195 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
196 cmd = "#{SVN_BIN} cat #{target(URI.escape(path))}@#{identifier}"
196 cmd = "#{SVN_BIN} cat #{target(URI.escape(path))}@#{identifier}"
197 cmd << credentials_string
197 cmd << credentials_string
198 cat = nil
198 cat = nil
199 shellout(cmd) do |io|
199 shellout(cmd) do |io|
200 io.binmode
200 io.binmode
201 cat = io.read
201 cat = io.read
202 end
202 end
203 return nil if $? && $?.exitstatus != 0
203 return nil if $? && $?.exitstatus != 0
204 cat
204 cat
205 end
205 end
206
206
207 def annotate(path, identifier=nil)
207 def annotate(path, identifier=nil)
208 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
208 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
209 cmd = "#{SVN_BIN} blame #{target(URI.escape(path))}@#{identifier}"
209 cmd = "#{SVN_BIN} blame #{target(URI.escape(path))}@#{identifier}"
210 cmd << credentials_string
210 cmd << credentials_string
211 blame = Annotate.new
211 blame = Annotate.new
212 shellout(cmd) do |io|
212 shellout(cmd) do |io|
213 io.each_line do |line|
213 io.each_line do |line|
214 next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
214 next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
215 blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip))
215 blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip))
216 end
216 end
217 end
217 end
218 return nil if $? && $?.exitstatus != 0
218 return nil if $? && $?.exitstatus != 0
219 blame
219 blame
220 end
220 end
221
221
222 private
222 private
223
223
224 def credentials_string
224 def credentials_string
225 str = ''
225 str = ''
226 str << " --username #{shell_quote(@login)}" unless @login.blank?
226 str << " --username #{shell_quote(@login)}" unless @login.blank?
227 str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
227 str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
228 str << " --no-auth-cache --non-interactive"
228 str
229 str
229 end
230 end
230 end
231 end
231 end
232 end
232 end
233 end
233 end
234 end
General Comments 0
You need to be logged in to leave comments. Login now