##// END OF EJS Templates
scm: mercurial: convert copied file path encoding (#2664)....
Toshi MARUYAMA -
r4875:32e69dcaca6c
parent child
Show More
@@ -1,313 +1,315
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 'cgi'
20 20
21 21 module Redmine
22 22 module Scm
23 23 module Adapters
24 24 class MercurialAdapter < AbstractAdapter
25 25
26 26 # Mercurial executable name
27 27 HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
28 28 HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
29 29 HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
30 30 TEMPLATE_NAME = "hg-template"
31 31 TEMPLATE_EXTENSION = "tmpl"
32 32
33 33 # raised if hg command exited with error, e.g. unknown revision.
34 34 class HgCommandAborted < CommandFailed; end
35 35
36 36 class << self
37 37 def client_command
38 38 @@bin ||= HG_BIN
39 39 end
40 40
41 41 def sq_bin
42 42 @@sq_bin ||= shell_quote(HG_BIN)
43 43 end
44 44
45 45 def client_version
46 46 @@client_version ||= (hgversion || [])
47 47 end
48 48
49 49 def client_available
50 50 !client_version.empty?
51 51 end
52 52
53 53 def hgversion
54 54 # The hg version is expressed either as a
55 55 # release number (eg 0.9.5 or 1.0) or as a revision
56 56 # id composed of 12 hexa characters.
57 57 theversion = hgversion_from_command_line.dup
58 58 if theversion.respond_to?(:force_encoding)
59 59 theversion.force_encoding('ASCII-8BIT')
60 60 end
61 61 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
62 62 m[2].scan(%r{\d+}).collect(&:to_i)
63 63 end
64 64 end
65 65
66 66 def hgversion_from_command_line
67 67 shellout("#{sq_bin} --version") { |io| io.read }.to_s
68 68 end
69 69
70 70 def template_path
71 71 @@template_path ||= template_path_for(client_version)
72 72 end
73 73
74 74 def template_path_for(version)
75 75 if ((version <=> [0,9,5]) > 0) || version.empty?
76 76 ver = "1.0"
77 77 else
78 78 ver = "0.9.5"
79 79 end
80 80 "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
81 81 end
82 82 end
83 83
84 84 def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
85 85 super
86 86 @path_encoding = path_encoding || 'UTF-8'
87 87 end
88 88
89 89 def info
90 90 tip = summary['repository']['tip']
91 91 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
92 92 :lastrev => Revision.new(:revision => tip['revision'],
93 93 :scmid => tip['node']))
94 94 end
95 95
96 96 def tags
97 97 as_ary(summary['repository']['tag']).map { |e| e['name'] }
98 98 end
99 99
100 100 # Returns map of {'tag' => 'nodeid', ...}
101 101 def tagmap
102 102 alist = as_ary(summary['repository']['tag']).map do |e|
103 103 e.values_at('name', 'node')
104 104 end
105 105 Hash[*alist.flatten]
106 106 end
107 107
108 108 def branches
109 109 as_ary(summary['repository']['branch']).map { |e| e['name'] }
110 110 end
111 111
112 112 # Returns map of {'branch' => 'nodeid', ...}
113 113 def branchmap
114 114 alist = as_ary(summary['repository']['branch']).map do |e|
115 115 e.values_at('name', 'node')
116 116 end
117 117 Hash[*alist.flatten]
118 118 end
119 119
120 120 def summary
121 121 return @summary if @summary
122 122 hg 'rhsummary' do |io|
123 123 output = io.read
124 124 if output.respond_to?(:force_encoding)
125 125 output.force_encoding('UTF-8')
126 126 end
127 127 begin
128 128 @summary = ActiveSupport::XmlMini.parse(output)['rhsummary']
129 129 rescue
130 130 end
131 131 end
132 132 end
133 133 private :summary
134 134
135 135 def entries(path=nil, identifier=nil)
136 136 p1 = scm_iconv(@path_encoding, 'UTF-8', path)
137 137 manifest = hg('rhmanifest', '-r', CGI.escape(hgrev(identifier)),
138 138 CGI.escape(without_leading_slash(p1.to_s))) do |io|
139 139 output = io.read
140 140 if output.respond_to?(:force_encoding)
141 141 output.force_encoding('UTF-8')
142 142 end
143 143 begin
144 144 ActiveSupport::XmlMini.parse(output)['rhmanifest']['repository']['manifest']
145 145 rescue
146 146 end
147 147 end
148 148 path_prefix = path.blank? ? '' : with_trailling_slash(path)
149 149
150 150 entries = Entries.new
151 151 as_ary(manifest['dir']).each do |e|
152 152 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
153 153 p = "#{path_prefix}#{n}"
154 154 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
155 155 end
156 156
157 157 as_ary(manifest['file']).each do |e|
158 158 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
159 159 p = "#{path_prefix}#{n}"
160 160 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
161 161 :identifier => e['node'],
162 162 :time => Time.at(e['time'].to_i))
163 163 entries << Entry.new(:name => n, :path => p, :kind => 'file',
164 164 :size => e['size'].to_i, :lastrev => lr)
165 165 end
166 166
167 167 entries
168 168 rescue HgCommandAborted
169 169 nil # means not found
170 170 end
171 171
172 172 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
173 173 revs = Revisions.new
174 174 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
175 175 revs
176 176 end
177 177
178 178 # Iterates the revisions by using a template file that
179 179 # makes Mercurial produce a xml output.
180 180 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
181 181 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
182 182 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
183 183 hg_args << '--limit' << options[:limit] if options[:limit]
184 184 hg_args << hgtarget(path) unless path.blank?
185 185 log = hg(*hg_args) do |io|
186 186 output = io.read
187 187 if output.respond_to?(:force_encoding)
188 188 output.force_encoding('UTF-8')
189 189 end
190 190 begin
191 191 # Mercurial < 1.5 does not support footer template for '</log>'
192 192 ActiveSupport::XmlMini.parse("#{output}</log>")['log']
193 193 rescue
194 194 end
195 195 end
196 196
197 197 as_ary(log['logentry']).each do |le|
198 198 cpalist = as_ary(le['paths']['path-copied']).map do |e|
199 [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) }
199 [e['__content__'], e['copyfrom-path']].map do |s|
200 scm_iconv('UTF-8', @path_encoding, CGI.unescape(s))
201 end
200 202 end
201 203 cpmap = Hash[*cpalist.flatten]
202 204
203 205 paths = as_ary(le['paths']['path']).map do |e|
204 206 p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
205 207 {:action => e['action'], :path => with_leading_slash(p),
206 208 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
207 209 :from_revision => (cpmap.member?(p) ? le['revision'] : nil)}
208 210 end.sort { |a, b| a[:path] <=> b[:path] }
209 211
210 212 yield Revision.new(:revision => le['revision'],
211 213 :scmid => le['node'],
212 214 :author => (le['author']['__content__'] rescue ''),
213 215 :time => Time.parse(le['date']['__content__']),
214 216 :message => le['msg']['__content__'],
215 217 :paths => paths)
216 218 end
217 219 self
218 220 end
219 221
220 222 def diff(path, identifier_from, identifier_to=nil)
221 223 hg_args = %w|rhdiff|
222 224 if identifier_to
223 225 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
224 226 else
225 227 hg_args << '-c' << hgrev(identifier_from)
226 228 end
227 229 unless path.blank?
228 230 p = scm_iconv(@path_encoding, 'UTF-8', path)
229 231 hg_args << CGI.escape(hgtarget(p))
230 232 end
231 233 diff = []
232 234 hg *hg_args do |io|
233 235 io.each_line do |line|
234 236 diff << line
235 237 end
236 238 end
237 239 diff
238 240 rescue HgCommandAborted
239 241 nil # means not found
240 242 end
241 243
242 244 def cat(path, identifier=nil)
243 245 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
244 246 hg 'rhcat', '-r', hgrev(identifier), hgtarget(p) do |io|
245 247 io.binmode
246 248 io.read
247 249 end
248 250 rescue HgCommandAborted
249 251 nil # means not found
250 252 end
251 253
252 254 def annotate(path, identifier=nil)
253 255 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
254 256 blame = Annotate.new
255 257 hg 'rhannotate', '-ncu', '-r', hgrev(identifier), hgtarget(p) do |io|
256 258 io.each_line do |line|
257 259 line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding)
258 260 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
259 261 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
260 262 :identifier => $3)
261 263 blame.add_line($4.rstrip, r)
262 264 end
263 265 end
264 266 blame
265 267 rescue HgCommandAborted
266 268 nil # means not found or cannot be annotated
267 269 end
268 270
269 271 class Revision < Redmine::Scm::Adapters::Revision
270 272 # Returns the readable identifier
271 273 def format_identifier
272 274 "#{revision}:#{scmid}"
273 275 end
274 276 end
275 277
276 278 # Runs 'hg' command with the given args
277 279 def hg(*args, &block)
278 280 repo_path = root_url || url
279 281 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
280 282 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
281 283 full_args << '--config' << 'diff.git=false'
282 284 full_args += args
283 285 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
284 286 if $? && $?.exitstatus != 0
285 287 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
286 288 end
287 289 ret
288 290 end
289 291 private :hg
290 292
291 293 # Returns correct revision identifier
292 294 def hgrev(identifier, sq=false)
293 295 rev = identifier.blank? ? 'tip' : identifier.to_s
294 296 rev = shell_quote(rev) if sq
295 297 rev
296 298 end
297 299 private :hgrev
298 300
299 301 def hgtarget(path)
300 302 path ||= ''
301 303 root_url + '/' + without_leading_slash(path)
302 304 end
303 305 private :hgtarget
304 306
305 307 def as_ary(o)
306 308 return [] unless o
307 309 o.is_a?(Array) ? o : Array[o]
308 310 end
309 311 private :as_ary
310 312 end
311 313 end
312 314 end
313 315 end
General Comments 0
You need to be logged in to leave comments. Login now