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