@@ -1,315 +1,320 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 BazaarAdapter < AbstractAdapter |
|
24 | 24 | |
|
25 | 25 | # Bazaar executable name |
|
26 | 26 | BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr" |
|
27 | 27 | |
|
28 | 28 | class << self |
|
29 | 29 | def client_command |
|
30 | 30 | @@bin ||= BZR_BIN |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def sq_bin |
|
34 | 34 | @@sq_bin ||= shell_quote_command |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | def client_version |
|
38 | 38 | @@client_version ||= (scm_command_version || []) |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def client_available |
|
42 | 42 | !client_version.empty? |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def scm_command_version |
|
46 | 46 | scm_version = scm_version_from_command_line.dup |
|
47 | 47 | if scm_version.respond_to?(:force_encoding) |
|
48 | 48 | scm_version.force_encoding('ASCII-8BIT') |
|
49 | 49 | end |
|
50 | 50 | if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) |
|
51 | 51 | m[2].scan(%r{\d+}).collect(&:to_i) |
|
52 | 52 | end |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def scm_version_from_command_line |
|
56 | 56 | shellout("#{sq_bin} --version") { |io| io.read }.to_s |
|
57 | 57 | end |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) | |
|
61 | @path_encoding = 'UTF-8' | |
|
62 | super | |
|
63 | end | |
|
64 | ||
|
60 | 65 | # Get info about the repository |
|
61 | 66 | def info |
|
62 | 67 | cmd_args = %w|revno| |
|
63 | 68 | cmd_args << bzr_target('') |
|
64 | 69 | info = nil |
|
65 | 70 | scm_cmd(*cmd_args) do |io| |
|
66 | 71 | if io.read =~ %r{^(\d+)\r?$} |
|
67 | 72 | info = Info.new({:root_url => url, |
|
68 | 73 | :lastrev => Revision.new({ |
|
69 | 74 | :identifier => $1 |
|
70 | 75 | }) |
|
71 | 76 | }) |
|
72 | 77 | end |
|
73 | 78 | end |
|
74 | 79 | info |
|
75 | 80 | rescue ScmCommandAborted |
|
76 | 81 | return nil |
|
77 | 82 | end |
|
78 | 83 | |
|
79 | 84 | # Returns an Entries collection |
|
80 | 85 | # or nil if the given path doesn't exist in the repository |
|
81 | 86 | def entries(path=nil, identifier=nil, options={}) |
|
82 | 87 | path ||= '' |
|
83 | 88 | entries = Entries.new |
|
84 | 89 | identifier = -1 unless identifier && identifier.to_i > 0 |
|
85 | 90 | cmd_args = %w|ls -v --show-ids| |
|
86 | 91 | cmd_args << "-r#{identifier.to_i}" |
|
87 | 92 | cmd_args << bzr_target(path) |
|
88 | 93 | scm_cmd(*cmd_args) do |io| |
|
89 | 94 | prefix = "#{url}/#{path}".gsub('\\', '/') |
|
90 | 95 | logger.debug "PREFIX: #{prefix}" |
|
91 | 96 | re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$} |
|
92 | 97 | io.each_line do |line| |
|
93 | 98 | next unless line =~ re |
|
94 | 99 | entries << Entry.new({:name => $3.strip, |
|
95 | 100 | :path => ((path.empty? ? "" : "#{path}/") + $3.strip), |
|
96 | 101 | :kind => ($4.blank? ? 'file' : 'dir'), |
|
97 | 102 | :size => nil, |
|
98 | 103 | :lastrev => Revision.new(:revision => $5.strip) |
|
99 | 104 | }) |
|
100 | 105 | end |
|
101 | 106 | end |
|
102 | 107 | if logger && logger.debug? |
|
103 | 108 | logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") |
|
104 | 109 | end |
|
105 | 110 | entries.sort_by_name |
|
106 | 111 | rescue ScmCommandAborted |
|
107 | 112 | return nil |
|
108 | 113 | end |
|
109 | 114 | |
|
110 | 115 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) |
|
111 | 116 | path ||= '' |
|
112 | 117 | identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1' |
|
113 | 118 | identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1 |
|
114 | 119 | revisions = Revisions.new |
|
115 | 120 | cmd_args = %w|log -v --show-ids| |
|
116 | 121 | cmd_args << "-r#{identifier_to}..#{identifier_from}" |
|
117 | 122 | cmd_args << bzr_target(path) |
|
118 | 123 | scm_cmd(*cmd_args) do |io| |
|
119 | 124 | revision = nil |
|
120 | 125 | parsing = nil |
|
121 | 126 | io.each_line do |line| |
|
122 | 127 | if line =~ /^----/ |
|
123 | 128 | revisions << revision if revision |
|
124 | 129 | revision = Revision.new(:paths => [], :message => '') |
|
125 | 130 | parsing = nil |
|
126 | 131 | else |
|
127 | 132 | next unless revision |
|
128 | 133 | if line =~ /^revno: (\d+)($|\s\[merge\]$)/ |
|
129 | 134 | revision.identifier = $1.to_i |
|
130 | 135 | elsif line =~ /^committer: (.+)$/ |
|
131 | 136 | revision.author = $1.strip |
|
132 | 137 | elsif line =~ /^revision-id:(.+)$/ |
|
133 | 138 | revision.scmid = $1.strip |
|
134 | 139 | elsif line =~ /^timestamp: (.+)$/ |
|
135 | 140 | revision.time = Time.parse($1).localtime |
|
136 | 141 | elsif line =~ /^ -----/ |
|
137 | 142 | # partial revisions |
|
138 | 143 | parsing = nil unless parsing == 'message' |
|
139 | 144 | elsif line =~ /^(message|added|modified|removed|renamed):/ |
|
140 | 145 | parsing = $1 |
|
141 | 146 | elsif line =~ /^ (.*)$/ |
|
142 | 147 | if parsing == 'message' |
|
143 | 148 | revision.message << "#{$1}\n" |
|
144 | 149 | else |
|
145 | 150 | if $1 =~ /^(.*)\s+(\S+)$/ |
|
146 | 151 | path = $1.strip |
|
147 | 152 | revid = $2 |
|
148 | 153 | case parsing |
|
149 | 154 | when 'added' |
|
150 | 155 | revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid} |
|
151 | 156 | when 'modified' |
|
152 | 157 | revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid} |
|
153 | 158 | when 'removed' |
|
154 | 159 | revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid} |
|
155 | 160 | when 'renamed' |
|
156 | 161 | new_path = path.split('=>').last |
|
157 | 162 | if new_path |
|
158 | 163 | revision.paths << {:action => 'M', :path => "/#{new_path.strip}", |
|
159 | 164 | :revision => revid} |
|
160 | 165 | end |
|
161 | 166 | end |
|
162 | 167 | end |
|
163 | 168 | end |
|
164 | 169 | else |
|
165 | 170 | parsing = nil |
|
166 | 171 | end |
|
167 | 172 | end |
|
168 | 173 | end |
|
169 | 174 | revisions << revision if revision |
|
170 | 175 | end |
|
171 | 176 | revisions |
|
172 | 177 | rescue ScmCommandAborted |
|
173 | 178 | return nil |
|
174 | 179 | end |
|
175 | 180 | |
|
176 | 181 | def diff(path, identifier_from, identifier_to=nil) |
|
177 | 182 | path ||= '' |
|
178 | 183 | if identifier_to |
|
179 | 184 | identifier_to = identifier_to.to_i |
|
180 | 185 | else |
|
181 | 186 | identifier_to = identifier_from.to_i - 1 |
|
182 | 187 | end |
|
183 | 188 | if identifier_from |
|
184 | 189 | identifier_from = identifier_from.to_i |
|
185 | 190 | end |
|
186 | 191 | diff = [] |
|
187 | 192 | cmd_args = %w|diff| |
|
188 | 193 | cmd_args << "-r#{identifier_to}..#{identifier_from}" |
|
189 | 194 | cmd_args << bzr_target(path) |
|
190 | 195 | scm_cmd_no_raise(*cmd_args) do |io| |
|
191 | 196 | io.each_line do |line| |
|
192 | 197 | diff << line |
|
193 | 198 | end |
|
194 | 199 | end |
|
195 | 200 | diff |
|
196 | 201 | end |
|
197 | 202 | |
|
198 | 203 | def cat(path, identifier=nil) |
|
199 | 204 | cat = nil |
|
200 | 205 | cmd_args = %w|cat| |
|
201 | 206 | cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0 |
|
202 | 207 | cmd_args << bzr_target(path) |
|
203 | 208 | scm_cmd(*cmd_args) do |io| |
|
204 | 209 | io.binmode |
|
205 | 210 | cat = io.read |
|
206 | 211 | end |
|
207 | 212 | cat |
|
208 | 213 | rescue ScmCommandAborted |
|
209 | 214 | return nil |
|
210 | 215 | end |
|
211 | 216 | |
|
212 | 217 | def annotate(path, identifier=nil) |
|
213 | 218 | blame = Annotate.new |
|
214 | 219 | cmd_args = %w|annotate -q --all| |
|
215 | 220 | cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0 |
|
216 | 221 | cmd_args << bzr_target(path) |
|
217 | 222 | scm_cmd(*cmd_args) do |io| |
|
218 | 223 | author = nil |
|
219 | 224 | identifier = nil |
|
220 | 225 | io.each_line do |line| |
|
221 | 226 | next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$} |
|
222 | 227 | rev = $1 |
|
223 | 228 | blame.add_line($3.rstrip, |
|
224 | 229 | Revision.new( |
|
225 | 230 | :identifier => rev, |
|
226 | 231 | :revision => rev, |
|
227 | 232 | :author => $2.strip |
|
228 | 233 | )) |
|
229 | 234 | end |
|
230 | 235 | end |
|
231 | 236 | blame |
|
232 | 237 | rescue ScmCommandAborted |
|
233 | 238 | return nil |
|
234 | 239 | end |
|
235 | 240 | |
|
236 | 241 | def self.branch_conf_path(path) |
|
237 | 242 | bcp = nil |
|
238 | 243 | m = path.match(%r{^(.*[/\\])\.bzr.*$}) |
|
239 | 244 | if m |
|
240 | 245 | bcp = m[1] |
|
241 | 246 | else |
|
242 | 247 | bcp = path |
|
243 | 248 | end |
|
244 | 249 | bcp.gsub!(%r{[\/\\]$}, "") |
|
245 | 250 | if bcp |
|
246 | 251 | bcp = File.join(bcp, ".bzr", "branch", "branch.conf") |
|
247 | 252 | end |
|
248 | 253 | bcp |
|
249 | 254 | end |
|
250 | 255 | |
|
251 | 256 | def append_revisions_only |
|
252 | 257 | return @aro if ! @aro.nil? |
|
253 | 258 | @aro = false |
|
254 | 259 | bcp = self.class.branch_conf_path(url) |
|
255 | 260 | if bcp && File.exist?(bcp) |
|
256 | 261 | begin |
|
257 | 262 | f = File::open(bcp, "r") |
|
258 | 263 | cnt = 0 |
|
259 | 264 | f.each_line do |line| |
|
260 | 265 | l = line.chomp.to_s |
|
261 | 266 | if l =~ /^\s*append_revisions_only\s*=\s*(\w+)\s*$/ |
|
262 | 267 | str_aro = $1 |
|
263 | 268 | if str_aro.upcase == "TRUE" |
|
264 | 269 | @aro = true |
|
265 | 270 | cnt += 1 |
|
266 | 271 | elsif str_aro.upcase == "FALSE" |
|
267 | 272 | @aro = false |
|
268 | 273 | cnt += 1 |
|
269 | 274 | end |
|
270 | 275 | if cnt > 1 |
|
271 | 276 | @aro = false |
|
272 | 277 | break |
|
273 | 278 | end |
|
274 | 279 | end |
|
275 | 280 | end |
|
276 | 281 | ensure |
|
277 | 282 | f.close |
|
278 | 283 | end |
|
279 | 284 | end |
|
280 | 285 | @aro |
|
281 | 286 | end |
|
282 | 287 | |
|
283 | 288 | def scm_cmd(*args, &block) |
|
284 | 289 | full_args = [] |
|
285 | 290 | full_args += args |
|
286 | 291 | ret = shellout( |
|
287 | 292 | self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '), |
|
288 | 293 | &block |
|
289 | 294 | ) |
|
290 | 295 | if $? && $?.exitstatus != 0 |
|
291 | 296 | raise ScmCommandAborted, "bzr exited with non-zero status: #{$?.exitstatus}" |
|
292 | 297 | end |
|
293 | 298 | ret |
|
294 | 299 | end |
|
295 | 300 | private :scm_cmd |
|
296 | 301 | |
|
297 | 302 | def scm_cmd_no_raise(*args, &block) |
|
298 | 303 | full_args = [] |
|
299 | 304 | full_args += args |
|
300 | 305 | ret = shellout( |
|
301 | 306 | self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '), |
|
302 | 307 | &block |
|
303 | 308 | ) |
|
304 | 309 | ret |
|
305 | 310 | end |
|
306 | 311 | private :scm_cmd_no_raise |
|
307 | 312 | |
|
308 | 313 | def bzr_target(path) |
|
309 | 314 | target(path, false) |
|
310 | 315 | end |
|
311 | 316 | private :bzr_target |
|
312 | 317 | end |
|
313 | 318 | end |
|
314 | 319 | end |
|
315 | 320 | end |
General Comments 0
You need to be logged in to leave comments.
Login now