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