@@ -1,287 +1,296 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 'cgi' |
|
19 | 19 | |
|
20 | 20 | module Redmine |
|
21 | 21 | module Scm |
|
22 | 22 | module Adapters |
|
23 | 23 | class CommandFailed < StandardError #:nodoc: |
|
24 | 24 | end |
|
25 | 25 | |
|
26 | 26 | class AbstractAdapter #:nodoc: |
|
27 | 27 | class << self |
|
28 | 28 | # Returns the version of the scm client |
|
29 | # Eg: [1, 5, 0] | |
|
29 | # Eg: [1, 5, 0] or [] if unknown | |
|
30 | 30 | def client_version |
|
31 | 'Unknown version' | |
|
31 | [] | |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | # Returns the version string of the scm client |
|
35 | # Eg: '1.5.0' | |
|
35 | # Eg: '1.5.0' or 'Unknown version' if unknown | |
|
36 | 36 | def client_version_string |
|
37 | client_version.is_a?(Array) ? client_version.join('.') : client_version.to_s | |
|
37 | v = client_version || 'Unknown version' | |
|
38 | v.is_a?(Array) ? v.join('.') : v.to_s | |
|
39 | end | |
|
40 | ||
|
41 | # Returns true if the current client version is above | |
|
42 | # or equals the given one | |
|
43 | # If option is :unknown is set to true, it will return | |
|
44 | # true if the client version is unknown | |
|
45 | def client_version_above?(v, options={}) | |
|
46 | ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown]) | |
|
38 | 47 | end |
|
39 | 48 | end |
|
40 | 49 | |
|
41 | 50 | def initialize(url, root_url=nil, login=nil, password=nil) |
|
42 | 51 | @url = url |
|
43 | 52 | @login = login if login && !login.empty? |
|
44 | 53 | @password = (password || "") if @login |
|
45 | 54 | @root_url = root_url.blank? ? retrieve_root_url : root_url |
|
46 | 55 | end |
|
47 | 56 | |
|
48 | 57 | def adapter_name |
|
49 | 58 | 'Abstract' |
|
50 | 59 | end |
|
51 | 60 | |
|
52 | 61 | def supports_cat? |
|
53 | 62 | true |
|
54 | 63 | end |
|
55 | 64 | |
|
56 | 65 | def supports_annotate? |
|
57 | 66 | respond_to?('annotate') |
|
58 | 67 | end |
|
59 | 68 | |
|
60 | 69 | def root_url |
|
61 | 70 | @root_url |
|
62 | 71 | end |
|
63 | 72 | |
|
64 | 73 | def url |
|
65 | 74 | @url |
|
66 | 75 | end |
|
67 | 76 | |
|
68 | 77 | # get info about the svn repository |
|
69 | 78 | def info |
|
70 | 79 | return nil |
|
71 | 80 | end |
|
72 | 81 | |
|
73 | 82 | # Returns the entry identified by path and revision identifier |
|
74 | 83 | # or nil if entry doesn't exist in the repository |
|
75 | 84 | def entry(path=nil, identifier=nil) |
|
76 | 85 | parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?} |
|
77 | 86 | search_path = parts[0..-2].join('/') |
|
78 | 87 | search_name = parts[-1] |
|
79 | 88 | if search_path.blank? && search_name.blank? |
|
80 | 89 | # Root entry |
|
81 | 90 | Entry.new(:path => '', :kind => 'dir') |
|
82 | 91 | else |
|
83 | 92 | # Search for the entry in the parent directory |
|
84 | 93 | es = entries(search_path, identifier) |
|
85 | 94 | es ? es.detect {|e| e.name == search_name} : nil |
|
86 | 95 | end |
|
87 | 96 | end |
|
88 | 97 | |
|
89 | 98 | # Returns an Entries collection |
|
90 | 99 | # or nil if the given path doesn't exist in the repository |
|
91 | 100 | def entries(path=nil, identifier=nil) |
|
92 | 101 | return nil |
|
93 | 102 | end |
|
94 | 103 | |
|
95 | 104 | def properties(path, identifier=nil) |
|
96 | 105 | return nil |
|
97 | 106 | end |
|
98 | 107 | |
|
99 | 108 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) |
|
100 | 109 | return nil |
|
101 | 110 | end |
|
102 | 111 | |
|
103 | 112 | def diff(path, identifier_from, identifier_to=nil) |
|
104 | 113 | return nil |
|
105 | 114 | end |
|
106 | 115 | |
|
107 | 116 | def cat(path, identifier=nil) |
|
108 | 117 | return nil |
|
109 | 118 | end |
|
110 | 119 | |
|
111 | 120 | def with_leading_slash(path) |
|
112 | 121 | path ||= '' |
|
113 | 122 | (path[0,1]!="/") ? "/#{path}" : path |
|
114 | 123 | end |
|
115 | 124 | |
|
116 | 125 | def with_trailling_slash(path) |
|
117 | 126 | path ||= '' |
|
118 | 127 | (path[-1,1] == "/") ? path : "#{path}/" |
|
119 | 128 | end |
|
120 | 129 | |
|
121 | 130 | def without_leading_slash(path) |
|
122 | 131 | path ||= '' |
|
123 | 132 | path.gsub(%r{^/+}, '') |
|
124 | 133 | end |
|
125 | 134 | |
|
126 | 135 | def without_trailling_slash(path) |
|
127 | 136 | path ||= '' |
|
128 | 137 | (path[-1,1] == "/") ? path[0..-2] : path |
|
129 | 138 | end |
|
130 | 139 | |
|
131 | 140 | def shell_quote(str) |
|
132 | 141 | if RUBY_PLATFORM =~ /mswin/ |
|
133 | 142 | '"' + str.gsub(/"/, '\\"') + '"' |
|
134 | 143 | else |
|
135 | 144 | "'" + str.gsub(/'/, "'\"'\"'") + "'" |
|
136 | 145 | end |
|
137 | 146 | end |
|
138 | 147 | |
|
139 | 148 | private |
|
140 | 149 | def retrieve_root_url |
|
141 | 150 | info = self.info |
|
142 | 151 | info ? info.root_url : nil |
|
143 | 152 | end |
|
144 | 153 | |
|
145 | 154 | def target(path) |
|
146 | 155 | path ||= '' |
|
147 | 156 | base = path.match(/^\//) ? root_url : url |
|
148 | 157 | shell_quote("#{base}/#{path}".gsub(/[?<>\*]/, '')) |
|
149 | 158 | end |
|
150 | 159 | |
|
151 | 160 | def logger |
|
152 | 161 | self.class.logger |
|
153 | 162 | end |
|
154 | 163 | |
|
155 | 164 | def shellout(cmd, &block) |
|
156 | 165 | self.class.shellout(cmd, &block) |
|
157 | 166 | end |
|
158 | 167 | |
|
159 | 168 | def self.logger |
|
160 | 169 | RAILS_DEFAULT_LOGGER |
|
161 | 170 | end |
|
162 | 171 | |
|
163 | 172 | def self.shellout(cmd, &block) |
|
164 | 173 | logger.debug "Shelling out: #{cmd}" if logger && logger.debug? |
|
165 | 174 | begin |
|
166 | 175 | IO.popen(cmd, "r+") do |io| |
|
167 | 176 | io.close_write |
|
168 | 177 | block.call(io) if block_given? |
|
169 | 178 | end |
|
170 | 179 | rescue Errno::ENOENT => e |
|
171 | 180 | msg = strip_credential(e.message) |
|
172 | 181 | # The command failed, log it and re-raise |
|
173 | 182 | logger.error("SCM command failed: #{strip_credential(cmd)}\n with: #{msg}") |
|
174 | 183 | raise CommandFailed.new(msg) |
|
175 | 184 | end |
|
176 | 185 | end |
|
177 | 186 | |
|
178 | 187 | # Hides username/password in a given command |
|
179 | 188 | def self.hide_credential(cmd) |
|
180 | 189 | q = (RUBY_PLATFORM =~ /mswin/ ? '"' : "'") |
|
181 | 190 | cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx') |
|
182 | 191 | end |
|
183 | 192 | |
|
184 | 193 | def strip_credential(cmd) |
|
185 | 194 | self.class.hide_credential(cmd) |
|
186 | 195 | end |
|
187 | 196 | end |
|
188 | 197 | |
|
189 | 198 | class Entries < Array |
|
190 | 199 | def sort_by_name |
|
191 | 200 | sort {|x,y| |
|
192 | 201 | if x.kind == y.kind |
|
193 | 202 | x.name <=> y.name |
|
194 | 203 | else |
|
195 | 204 | x.kind <=> y.kind |
|
196 | 205 | end |
|
197 | 206 | } |
|
198 | 207 | end |
|
199 | 208 | |
|
200 | 209 | def revisions |
|
201 | 210 | revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact) |
|
202 | 211 | end |
|
203 | 212 | end |
|
204 | 213 | |
|
205 | 214 | class Info |
|
206 | 215 | attr_accessor :root_url, :lastrev |
|
207 | 216 | def initialize(attributes={}) |
|
208 | 217 | self.root_url = attributes[:root_url] if attributes[:root_url] |
|
209 | 218 | self.lastrev = attributes[:lastrev] |
|
210 | 219 | end |
|
211 | 220 | end |
|
212 | 221 | |
|
213 | 222 | class Entry |
|
214 | 223 | attr_accessor :name, :path, :kind, :size, :lastrev |
|
215 | 224 | def initialize(attributes={}) |
|
216 | 225 | self.name = attributes[:name] if attributes[:name] |
|
217 | 226 | self.path = attributes[:path] if attributes[:path] |
|
218 | 227 | self.kind = attributes[:kind] if attributes[:kind] |
|
219 | 228 | self.size = attributes[:size].to_i if attributes[:size] |
|
220 | 229 | self.lastrev = attributes[:lastrev] |
|
221 | 230 | end |
|
222 | 231 | |
|
223 | 232 | def is_file? |
|
224 | 233 | 'file' == self.kind |
|
225 | 234 | end |
|
226 | 235 | |
|
227 | 236 | def is_dir? |
|
228 | 237 | 'dir' == self.kind |
|
229 | 238 | end |
|
230 | 239 | |
|
231 | 240 | def is_text? |
|
232 | 241 | Redmine::MimeType.is_type?('text', name) |
|
233 | 242 | end |
|
234 | 243 | end |
|
235 | 244 | |
|
236 | 245 | class Revisions < Array |
|
237 | 246 | def latest |
|
238 | 247 | sort {|x,y| |
|
239 | 248 | unless x.time.nil? or y.time.nil? |
|
240 | 249 | x.time <=> y.time |
|
241 | 250 | else |
|
242 | 251 | 0 |
|
243 | 252 | end |
|
244 | 253 | }.last |
|
245 | 254 | end |
|
246 | 255 | end |
|
247 | 256 | |
|
248 | 257 | class Revision |
|
249 | 258 | attr_accessor :identifier, :scmid, :name, :author, :time, :message, :paths, :revision, :branch |
|
250 | 259 | def initialize(attributes={}) |
|
251 | 260 | self.identifier = attributes[:identifier] |
|
252 | 261 | self.scmid = attributes[:scmid] |
|
253 | 262 | self.name = attributes[:name] || self.identifier |
|
254 | 263 | self.author = attributes[:author] |
|
255 | 264 | self.time = attributes[:time] |
|
256 | 265 | self.message = attributes[:message] || "" |
|
257 | 266 | self.paths = attributes[:paths] |
|
258 | 267 | self.revision = attributes[:revision] |
|
259 | 268 | self.branch = attributes[:branch] |
|
260 | 269 | end |
|
261 | 270 | |
|
262 | 271 | end |
|
263 | 272 | |
|
264 | 273 | class Annotate |
|
265 | 274 | attr_reader :lines, :revisions |
|
266 | 275 | |
|
267 | 276 | def initialize |
|
268 | 277 | @lines = [] |
|
269 | 278 | @revisions = [] |
|
270 | 279 | end |
|
271 | 280 | |
|
272 | 281 | def add_line(line, revision) |
|
273 | 282 | @lines << line |
|
274 | 283 | @revisions << revision |
|
275 | 284 | end |
|
276 | 285 | |
|
277 | 286 | def content |
|
278 | 287 | content = lines.join("\n") |
|
279 | 288 | end |
|
280 | 289 | |
|
281 | 290 | def empty? |
|
282 | 291 | lines.empty? |
|
283 | 292 | end |
|
284 | 293 | end |
|
285 | 294 | end |
|
286 | 295 | end |
|
287 | 296 | end |
@@ -1,205 +1,205 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | |
|
20 | 20 | module Redmine |
|
21 | 21 | module Scm |
|
22 | 22 | module Adapters |
|
23 | 23 | class MercurialAdapter < AbstractAdapter |
|
24 | 24 | |
|
25 | 25 | # Mercurial executable name |
|
26 | 26 | HG_BIN = "hg" |
|
27 | 27 | TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial" |
|
28 | 28 | TEMPLATE_NAME = "hg-template" |
|
29 | 29 | TEMPLATE_EXTENSION = "tmpl" |
|
30 | 30 | |
|
31 | 31 | class << self |
|
32 | 32 | def client_version |
|
33 |
@@client_version ||= (hgversion || |
|
|
33 | @@client_version ||= (hgversion || []) | |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | def hgversion |
|
37 | 37 | # The hg version is expressed either as a |
|
38 | 38 | # release number (eg 0.9.5 or 1.0) or as a revision |
|
39 | 39 | # id composed of 12 hexa characters. |
|
40 | 40 | theversion = hgversion_from_command_line |
|
41 | 41 | if theversion.match(/^\d+(\.\d+)+/) |
|
42 | 42 | theversion.split(".").collect(&:to_i) |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def hgversion_from_command_line |
|
47 | 47 | %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1] |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def template_path |
|
51 | 51 | @@template_path ||= template_path_for(client_version) |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def template_path_for(version) |
|
55 |
if |
|
|
55 | if ((version <=> [0,9,5]) > 0) || version.empty? | |
|
56 | 56 | ver = "1.0" |
|
57 | 57 | else |
|
58 | 58 | ver = "0.9.5" |
|
59 | 59 | end |
|
60 | 60 | "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}" |
|
61 | 61 | end |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def info |
|
65 | 65 | cmd = "#{HG_BIN} -R #{target('')} root" |
|
66 | 66 | root_url = nil |
|
67 | 67 | shellout(cmd) do |io| |
|
68 | 68 | root_url = io.gets |
|
69 | 69 | end |
|
70 | 70 | return nil if $? && $?.exitstatus != 0 |
|
71 | 71 | info = Info.new({:root_url => root_url.chomp, |
|
72 | 72 | :lastrev => revisions(nil,nil,nil,{:limit => 1}).last |
|
73 | 73 | }) |
|
74 | 74 | info |
|
75 | 75 | rescue CommandFailed |
|
76 | 76 | return nil |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def entries(path=nil, identifier=nil) |
|
80 | 80 | path ||= '' |
|
81 | 81 | entries = Entries.new |
|
82 | 82 | cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate" |
|
83 | 83 | cmd << " -r " + (identifier ? identifier.to_s : "tip") |
|
84 | 84 | cmd << " " + shell_quote("path:#{path}") unless path.empty? |
|
85 | 85 | shellout(cmd) do |io| |
|
86 | 86 | io.each_line do |line| |
|
87 | 87 | # HG uses antislashs as separator on Windows |
|
88 | 88 | line = line.gsub(/\\/, "/") |
|
89 | 89 | if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'') |
|
90 | 90 | e ||= line |
|
91 | 91 | e = e.chomp.split(%r{[\/\\]}) |
|
92 | 92 | entries << Entry.new({:name => e.first, |
|
93 | 93 | :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"), |
|
94 | 94 | :kind => (e.size > 1 ? 'dir' : 'file'), |
|
95 | 95 | :lastrev => Revision.new |
|
96 | 96 | }) unless entries.detect{|entry| entry.name == e.first} |
|
97 | 97 | end |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | return nil if $? && $?.exitstatus != 0 |
|
101 | 101 | entries.sort_by_name |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 104 | # Fetch the revisions by using a template file that |
|
105 | 105 | # makes Mercurial produce a xml output. |
|
106 | 106 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) |
|
107 | 107 | revisions = Revisions.new |
|
108 | 108 | cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}" |
|
109 | 109 | if identifier_from && identifier_to |
|
110 | 110 | cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}" |
|
111 | 111 | elsif identifier_from |
|
112 | 112 | cmd << " -r #{identifier_from.to_i}:" |
|
113 | 113 | end |
|
114 | 114 | cmd << " --limit #{options[:limit].to_i}" if options[:limit] |
|
115 | 115 | cmd << " #{path}" if path |
|
116 | 116 | shellout(cmd) do |io| |
|
117 | 117 | begin |
|
118 | 118 | # HG doesn't close the XML Document... |
|
119 | 119 | doc = REXML::Document.new(io.read << "</log>") |
|
120 | 120 | doc.elements.each("log/logentry") do |logentry| |
|
121 | 121 | paths = [] |
|
122 | 122 | copies = logentry.get_elements('paths/path-copied') |
|
123 | 123 | logentry.elements.each("paths/path") do |path| |
|
124 | 124 | # Detect if the added file is a copy |
|
125 | 125 | if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text } |
|
126 | 126 | from_path = c.attributes['copyfrom-path'] |
|
127 | 127 | from_rev = logentry.attributes['revision'] |
|
128 | 128 | end |
|
129 | 129 | paths << {:action => path.attributes['action'], |
|
130 | 130 | :path => "/#{path.text}", |
|
131 | 131 | :from_path => from_path ? "/#{from_path}" : nil, |
|
132 | 132 | :from_revision => from_rev ? from_rev : nil |
|
133 | 133 | } |
|
134 | 134 | end |
|
135 | 135 | paths.sort! { |x,y| x[:path] <=> y[:path] } |
|
136 | 136 | |
|
137 | 137 | revisions << Revision.new({:identifier => logentry.attributes['revision'], |
|
138 | 138 | :scmid => logentry.attributes['node'], |
|
139 | 139 | :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""), |
|
140 | 140 | :time => Time.parse(logentry.elements['date'].text).localtime, |
|
141 | 141 | :message => logentry.elements['msg'].text, |
|
142 | 142 | :paths => paths |
|
143 | 143 | }) |
|
144 | 144 | end |
|
145 | 145 | rescue |
|
146 | 146 | logger.debug($!) |
|
147 | 147 | end |
|
148 | 148 | end |
|
149 | 149 | return nil if $? && $?.exitstatus != 0 |
|
150 | 150 | revisions |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | def diff(path, identifier_from, identifier_to=nil) |
|
154 | 154 | path ||= '' |
|
155 | 155 | if identifier_to |
|
156 | 156 | identifier_to = identifier_to.to_i |
|
157 | 157 | else |
|
158 | 158 | identifier_to = identifier_from.to_i - 1 |
|
159 | 159 | end |
|
160 | 160 | cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates" |
|
161 | 161 | cmd << " -I #{target(path)}" unless path.empty? |
|
162 | 162 | diff = [] |
|
163 | 163 | shellout(cmd) do |io| |
|
164 | 164 | io.each_line do |line| |
|
165 | 165 | diff << line |
|
166 | 166 | end |
|
167 | 167 | end |
|
168 | 168 | return nil if $? && $?.exitstatus != 0 |
|
169 | 169 | diff |
|
170 | 170 | end |
|
171 | 171 | |
|
172 | 172 | def cat(path, identifier=nil) |
|
173 | 173 | cmd = "#{HG_BIN} -R #{target('')} cat" |
|
174 | 174 | cmd << " -r " + (identifier ? identifier.to_s : "tip") |
|
175 | 175 | cmd << " #{target(path)}" |
|
176 | 176 | cat = nil |
|
177 | 177 | shellout(cmd) do |io| |
|
178 | 178 | io.binmode |
|
179 | 179 | cat = io.read |
|
180 | 180 | end |
|
181 | 181 | return nil if $? && $?.exitstatus != 0 |
|
182 | 182 | cat |
|
183 | 183 | end |
|
184 | 184 | |
|
185 | 185 | def annotate(path, identifier=nil) |
|
186 | 186 | path ||= '' |
|
187 | 187 | cmd = "#{HG_BIN} -R #{target('')}" |
|
188 | 188 | cmd << " annotate -n -u" |
|
189 | 189 | cmd << " -r " + (identifier ? identifier.to_s : "tip") |
|
190 | 190 | cmd << " -r #{identifier.to_i}" if identifier |
|
191 | 191 | cmd << " #{target(path)}" |
|
192 | 192 | blame = Annotate.new |
|
193 | 193 | shellout(cmd) do |io| |
|
194 | 194 | io.each_line do |line| |
|
195 | 195 | next unless line =~ %r{^([^:]+)\s(\d+):(.*)$} |
|
196 | 196 | blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip)) |
|
197 | 197 | end |
|
198 | 198 | end |
|
199 | 199 | return nil if $? && $?.exitstatus != 0 |
|
200 | 200 | blame |
|
201 | 201 | end |
|
202 | 202 | end |
|
203 | 203 | end |
|
204 | 204 | end |
|
205 | 205 | end |
@@ -1,228 +1,228 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 'rexml/document' |
|
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 = "svn" |
|
28 | 28 | |
|
29 | 29 | class << self |
|
30 | 30 | def client_version |
|
31 |
@@client_version ||= (svn_binary_version || |
|
|
31 | @@client_version ||= (svn_binary_version || []) | |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def svn_binary_version |
|
35 | 35 | cmd = "#{SVN_BIN} --version" |
|
36 | 36 | version = nil |
|
37 | 37 | shellout(cmd) do |io| |
|
38 | 38 | # Read svn version in first returned line |
|
39 | 39 | if m = io.gets.match(%r{((\d+\.)+\d+)}) |
|
40 | 40 | version = m[0].scan(%r{\d+}).collect(&:to_i) |
|
41 | 41 | end |
|
42 | 42 | end |
|
43 | 43 | return nil if $? && $?.exitstatus != 0 |
|
44 | 44 | version |
|
45 | 45 | end |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | # Get info about the svn repository |
|
49 | 49 | def info |
|
50 | 50 | cmd = "#{SVN_BIN} info --xml #{target('')}" |
|
51 | 51 | cmd << credentials_string |
|
52 | 52 | info = nil |
|
53 | 53 | shellout(cmd) do |io| |
|
54 | 54 | begin |
|
55 | 55 | doc = REXML::Document.new(io) |
|
56 | 56 | #root_url = doc.elements["info/entry/repository/root"].text |
|
57 | 57 | info = Info.new({:root_url => doc.elements["info/entry/repository/root"].text, |
|
58 | 58 | :lastrev => Revision.new({ |
|
59 | 59 | :identifier => doc.elements["info/entry/commit"].attributes['revision'], |
|
60 | 60 | :time => Time.parse(doc.elements["info/entry/commit/date"].text).localtime, |
|
61 | 61 | :author => (doc.elements["info/entry/commit/author"] ? doc.elements["info/entry/commit/author"].text : "") |
|
62 | 62 | }) |
|
63 | 63 | }) |
|
64 | 64 | rescue |
|
65 | 65 | end |
|
66 | 66 | end |
|
67 | 67 | return nil if $? && $?.exitstatus != 0 |
|
68 | 68 | info |
|
69 | 69 | rescue CommandFailed |
|
70 | 70 | return nil |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | # Returns an Entries collection |
|
74 | 74 | # or nil if the given path doesn't exist in the repository |
|
75 | 75 | def entries(path=nil, identifier=nil) |
|
76 | 76 | path ||= '' |
|
77 | 77 | identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" |
|
78 | 78 | entries = Entries.new |
|
79 | 79 | cmd = "#{SVN_BIN} list --xml #{target(path)}@#{identifier}" |
|
80 | 80 | cmd << credentials_string |
|
81 | 81 | shellout(cmd) do |io| |
|
82 | 82 | output = io.read |
|
83 | 83 | begin |
|
84 | 84 | doc = REXML::Document.new(output) |
|
85 | 85 | doc.elements.each("lists/list/entry") do |entry| |
|
86 | 86 | # Skip directory if there is no commit date (usually that |
|
87 | 87 | # means that we don't have read access to it) |
|
88 | 88 | next if entry.attributes['kind'] == 'dir' && entry.elements['commit'].elements['date'].nil? |
|
89 | 89 | entries << Entry.new({:name => entry.elements['name'].text, |
|
90 | 90 | :path => ((path.empty? ? "" : "#{path}/") + entry.elements['name'].text), |
|
91 | 91 | :kind => entry.attributes['kind'], |
|
92 | 92 | :size => (entry.elements['size'] and entry.elements['size'].text).to_i, |
|
93 | 93 | :lastrev => Revision.new({ |
|
94 | 94 | :identifier => entry.elements['commit'].attributes['revision'], |
|
95 | 95 | :time => Time.parse(entry.elements['commit'].elements['date'].text).localtime, |
|
96 | 96 | :author => (entry.elements['commit'].elements['author'] ? entry.elements['commit'].elements['author'].text : "") |
|
97 | 97 | }) |
|
98 | 98 | }) |
|
99 | 99 | end |
|
100 | 100 | rescue Exception => e |
|
101 | 101 | logger.error("Error parsing svn output: #{e.message}") |
|
102 | 102 | logger.error("Output was:\n #{output}") |
|
103 | 103 | end |
|
104 | 104 | end |
|
105 | 105 | return nil if $? && $?.exitstatus != 0 |
|
106 | 106 | logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug? |
|
107 | 107 | entries.sort_by_name |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def properties(path, identifier=nil) |
|
111 | 111 | # proplist xml output supported in svn 1.5.0 and higher |
|
112 |
return nil |
|
|
112 | return nil unless self.class.client_version_above?([1, 5, 0]) | |
|
113 | 113 | |
|
114 | 114 | identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" |
|
115 | 115 | cmd = "#{SVN_BIN} proplist --verbose --xml #{target(path)}@#{identifier}" |
|
116 | 116 | cmd << credentials_string |
|
117 | 117 | properties = {} |
|
118 | 118 | shellout(cmd) do |io| |
|
119 | 119 | output = io.read |
|
120 | 120 | begin |
|
121 | 121 | doc = REXML::Document.new(output) |
|
122 | 122 | doc.elements.each("properties/target/property") do |property| |
|
123 | 123 | properties[ property.attributes['name'] ] = property.text |
|
124 | 124 | end |
|
125 | 125 | rescue |
|
126 | 126 | end |
|
127 | 127 | end |
|
128 | 128 | return nil if $? && $?.exitstatus != 0 |
|
129 | 129 | properties |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) |
|
133 | 133 | path ||= '' |
|
134 | 134 | identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD" |
|
135 | 135 | identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1 |
|
136 | 136 | revisions = Revisions.new |
|
137 | 137 | cmd = "#{SVN_BIN} log --xml -r #{identifier_from}:#{identifier_to}" |
|
138 | 138 | cmd << credentials_string |
|
139 | 139 | cmd << " --verbose " if options[:with_paths] |
|
140 | 140 | cmd << ' ' + target(path) |
|
141 | 141 | shellout(cmd) do |io| |
|
142 | 142 | begin |
|
143 | 143 | doc = REXML::Document.new(io) |
|
144 | 144 | doc.elements.each("log/logentry") do |logentry| |
|
145 | 145 | paths = [] |
|
146 | 146 | logentry.elements.each("paths/path") do |path| |
|
147 | 147 | paths << {:action => path.attributes['action'], |
|
148 | 148 | :path => path.text, |
|
149 | 149 | :from_path => path.attributes['copyfrom-path'], |
|
150 | 150 | :from_revision => path.attributes['copyfrom-rev'] |
|
151 | 151 | } |
|
152 | 152 | end |
|
153 | 153 | paths.sort! { |x,y| x[:path] <=> y[:path] } |
|
154 | 154 | |
|
155 | 155 | revisions << Revision.new({:identifier => logentry.attributes['revision'], |
|
156 | 156 | :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""), |
|
157 | 157 | :time => Time.parse(logentry.elements['date'].text).localtime, |
|
158 | 158 | :message => logentry.elements['msg'].text, |
|
159 | 159 | :paths => paths |
|
160 | 160 | }) |
|
161 | 161 | end |
|
162 | 162 | rescue |
|
163 | 163 | end |
|
164 | 164 | end |
|
165 | 165 | return nil if $? && $?.exitstatus != 0 |
|
166 | 166 | revisions |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | def diff(path, identifier_from, identifier_to=nil, type="inline") |
|
170 | 170 | path ||= '' |
|
171 | 171 | identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : '' |
|
172 | 172 | identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1) |
|
173 | 173 | |
|
174 | 174 | cmd = "#{SVN_BIN} diff -r " |
|
175 | 175 | cmd << "#{identifier_to}:" |
|
176 | 176 | cmd << "#{identifier_from}" |
|
177 | 177 | cmd << " #{target(path)}@#{identifier_from}" |
|
178 | 178 | cmd << credentials_string |
|
179 | 179 | diff = [] |
|
180 | 180 | shellout(cmd) do |io| |
|
181 | 181 | io.each_line do |line| |
|
182 | 182 | diff << line |
|
183 | 183 | end |
|
184 | 184 | end |
|
185 | 185 | return nil if $? && $?.exitstatus != 0 |
|
186 | 186 | diff |
|
187 | 187 | end |
|
188 | 188 | |
|
189 | 189 | def cat(path, identifier=nil) |
|
190 | 190 | identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" |
|
191 | 191 | cmd = "#{SVN_BIN} cat #{target(path)}@#{identifier}" |
|
192 | 192 | cmd << credentials_string |
|
193 | 193 | cat = nil |
|
194 | 194 | shellout(cmd) do |io| |
|
195 | 195 | io.binmode |
|
196 | 196 | cat = io.read |
|
197 | 197 | end |
|
198 | 198 | return nil if $? && $?.exitstatus != 0 |
|
199 | 199 | cat |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | def annotate(path, identifier=nil) |
|
203 | 203 | identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" |
|
204 | 204 | cmd = "#{SVN_BIN} blame #{target(path)}@#{identifier}" |
|
205 | 205 | cmd << credentials_string |
|
206 | 206 | blame = Annotate.new |
|
207 | 207 | shellout(cmd) do |io| |
|
208 | 208 | io.each_line do |line| |
|
209 | 209 | next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$} |
|
210 | 210 | blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip)) |
|
211 | 211 | end |
|
212 | 212 | end |
|
213 | 213 | return nil if $? && $?.exitstatus != 0 |
|
214 | 214 | blame |
|
215 | 215 | end |
|
216 | 216 | |
|
217 | 217 | private |
|
218 | 218 | |
|
219 | 219 | def credentials_string |
|
220 | 220 | str = '' |
|
221 | 221 | str << " --username #{shell_quote(@login)}" unless @login.blank? |
|
222 | 222 | str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank? |
|
223 | 223 | str |
|
224 | 224 | end |
|
225 | 225 | end |
|
226 | 226 | end |
|
227 | 227 | end |
|
228 | 228 | end |
@@ -1,53 +1,53 | |||
|
1 | 1 | require File.dirname(__FILE__) + '/../test_helper' |
|
2 | 2 | begin |
|
3 | 3 | require 'mocha' |
|
4 | 4 | |
|
5 | 5 | class MercurialAdapterTest < Test::Unit::TestCase |
|
6 | 6 | |
|
7 | 7 | TEMPLATES_DIR = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATES_DIR |
|
8 | 8 | TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME |
|
9 | 9 | TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION |
|
10 | 10 | |
|
11 | 11 | REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository' |
|
12 | 12 | |
|
13 | 13 | def test_hgversion |
|
14 | 14 | to_test = { "0.9.5" => [0,9,5], |
|
15 | 15 | "1.0" => [1,0], |
|
16 | 16 | "1e4ddc9ac9f7+20080325" => nil, |
|
17 | 17 | "1.0.1+20080525" => [1,0,1], |
|
18 | 18 | "1916e629a29d" => nil} |
|
19 | 19 | |
|
20 | 20 | to_test.each do |s, v| |
|
21 | 21 | test_hgversion_for(s, v) |
|
22 | 22 | end |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def test_template_path |
|
26 | 26 | to_test = { [0,9,5] => "0.9.5", |
|
27 | 27 | [1,0] => "1.0", |
|
28 |
|
|
|
28 | [] => "1.0", | |
|
29 | 29 | [1,0,1] => "1.0"} |
|
30 | 30 | |
|
31 | 31 | to_test.each do |v, template| |
|
32 | 32 | test_template_path_for(v, template) |
|
33 | 33 | end |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | private |
|
37 | 37 | |
|
38 | 38 | def test_hgversion_for(hgversion, version) |
|
39 | 39 | Redmine::Scm::Adapters::MercurialAdapter.expects(:hgversion_from_command_line).returns(hgversion) |
|
40 | 40 | adapter = Redmine::Scm::Adapters::MercurialAdapter |
|
41 | 41 | assert_equal version, adapter.hgversion |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_template_path_for(version, template) |
|
45 | 45 | adapter = Redmine::Scm::Adapters::MercurialAdapter |
|
46 | 46 | assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}", adapter.template_path_for(version) |
|
47 | 47 | assert File.exist?(adapter.template_path_for(version)) |
|
48 | 48 | end |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | rescue LoadError |
|
52 | 52 | def test_fake; assert(false, "Requires mocha to run those tests") end |
|
53 | 53 | end |
General Comments 0
You need to be logged in to leave comments.
Login now