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