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