##// END OF EJS Templates
Merged r6245 from trunk....
Toshi MARUYAMA -
r6133:94f5c737465e
parent child
Show More
@@ -1,327 +1,331
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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.blank? ? 'UTF-8' : path_encoding
86 @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
87 end
87 end
88
88
89 def path_encoding
89 def path_encoding
90 @path_encoding
90 @path_encoding
91 end
91 end
92
92
93 def info
93 def info
94 tip = summary['repository']['tip']
94 tip = summary['repository']['tip']
95 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
95 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
96 :lastrev => Revision.new(:revision => tip['revision'],
96 :lastrev => Revision.new(:revision => tip['revision'],
97 :scmid => tip['node']))
97 :scmid => tip['node']))
98 # rescue HgCommandAborted
99 rescue Exception => e
100 logger.error "hg: error during getting info: #{e.message}"
101 nil
98 end
102 end
99
103
100 def tags
104 def tags
101 as_ary(summary['repository']['tag']).map { |e| e['name'] }
105 as_ary(summary['repository']['tag']).map { |e| e['name'] }
102 end
106 end
103
107
104 # Returns map of {'tag' => 'nodeid', ...}
108 # Returns map of {'tag' => 'nodeid', ...}
105 def tagmap
109 def tagmap
106 alist = as_ary(summary['repository']['tag']).map do |e|
110 alist = as_ary(summary['repository']['tag']).map do |e|
107 e.values_at('name', 'node')
111 e.values_at('name', 'node')
108 end
112 end
109 Hash[*alist.flatten]
113 Hash[*alist.flatten]
110 end
114 end
111
115
112 def branches
116 def branches
113 as_ary(summary['repository']['branch']).map { |e| e['name'] }
117 as_ary(summary['repository']['branch']).map { |e| e['name'] }
114 end
118 end
115
119
116 # Returns map of {'branch' => 'nodeid', ...}
120 # Returns map of {'branch' => 'nodeid', ...}
117 def branchmap
121 def branchmap
118 alist = as_ary(summary['repository']['branch']).map do |e|
122 alist = as_ary(summary['repository']['branch']).map do |e|
119 e.values_at('name', 'node')
123 e.values_at('name', 'node')
120 end
124 end
121 Hash[*alist.flatten]
125 Hash[*alist.flatten]
122 end
126 end
123
127
124 def summary
128 def summary
125 return @summary if @summary
129 return @summary if @summary
126 hg 'rhsummary' do |io|
130 hg 'rhsummary' do |io|
127 output = io.read
131 output = io.read
128 if output.respond_to?(:force_encoding)
132 if output.respond_to?(:force_encoding)
129 output.force_encoding('UTF-8')
133 output.force_encoding('UTF-8')
130 end
134 end
131 begin
135 begin
132 @summary = ActiveSupport::XmlMini.parse(output)['rhsummary']
136 @summary = ActiveSupport::XmlMini.parse(output)['rhsummary']
133 rescue
137 rescue
134 end
138 end
135 end
139 end
136 end
140 end
137 private :summary
141 private :summary
138
142
139 def entries(path=nil, identifier=nil, options={})
143 def entries(path=nil, identifier=nil, options={})
140 p1 = scm_iconv(@path_encoding, 'UTF-8', path)
144 p1 = scm_iconv(@path_encoding, 'UTF-8', path)
141 manifest = hg('rhmanifest', '-r', CGI.escape(hgrev(identifier)),
145 manifest = hg('rhmanifest', '-r', CGI.escape(hgrev(identifier)),
142 CGI.escape(without_leading_slash(p1.to_s))) do |io|
146 CGI.escape(without_leading_slash(p1.to_s))) do |io|
143 output = io.read
147 output = io.read
144 if output.respond_to?(:force_encoding)
148 if output.respond_to?(:force_encoding)
145 output.force_encoding('UTF-8')
149 output.force_encoding('UTF-8')
146 end
150 end
147 begin
151 begin
148 ActiveSupport::XmlMini.parse(output)['rhmanifest']['repository']['manifest']
152 ActiveSupport::XmlMini.parse(output)['rhmanifest']['repository']['manifest']
149 rescue
153 rescue
150 end
154 end
151 end
155 end
152 path_prefix = path.blank? ? '' : with_trailling_slash(path)
156 path_prefix = path.blank? ? '' : with_trailling_slash(path)
153
157
154 entries = Entries.new
158 entries = Entries.new
155 as_ary(manifest['dir']).each do |e|
159 as_ary(manifest['dir']).each do |e|
156 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
160 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
157 p = "#{path_prefix}#{n}"
161 p = "#{path_prefix}#{n}"
158 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
162 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
159 end
163 end
160
164
161 as_ary(manifest['file']).each do |e|
165 as_ary(manifest['file']).each do |e|
162 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
166 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
163 p = "#{path_prefix}#{n}"
167 p = "#{path_prefix}#{n}"
164 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
168 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
165 :identifier => e['node'],
169 :identifier => e['node'],
166 :time => Time.at(e['time'].to_i))
170 :time => Time.at(e['time'].to_i))
167 entries << Entry.new(:name => n, :path => p, :kind => 'file',
171 entries << Entry.new(:name => n, :path => p, :kind => 'file',
168 :size => e['size'].to_i, :lastrev => lr)
172 :size => e['size'].to_i, :lastrev => lr)
169 end
173 end
170
174
171 entries
175 entries
172 rescue HgCommandAborted
176 rescue HgCommandAborted
173 nil # means not found
177 nil # means not found
174 end
178 end
175
179
176 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
180 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
177 revs = Revisions.new
181 revs = Revisions.new
178 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
182 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
179 revs
183 revs
180 end
184 end
181
185
182 # Iterates the revisions by using a template file that
186 # Iterates the revisions by using a template file that
183 # makes Mercurial produce a xml output.
187 # makes Mercurial produce a xml output.
184 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
188 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
185 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
189 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
186 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
190 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
187 hg_args << '--limit' << options[:limit] if options[:limit]
191 hg_args << '--limit' << options[:limit] if options[:limit]
188 hg_args << hgtarget(path) unless path.blank?
192 hg_args << hgtarget(path) unless path.blank?
189 log = hg(*hg_args) do |io|
193 log = hg(*hg_args) do |io|
190 output = io.read
194 output = io.read
191 if output.respond_to?(:force_encoding)
195 if output.respond_to?(:force_encoding)
192 output.force_encoding('UTF-8')
196 output.force_encoding('UTF-8')
193 end
197 end
194 begin
198 begin
195 # Mercurial < 1.5 does not support footer template for '</log>'
199 # Mercurial < 1.5 does not support footer template for '</log>'
196 ActiveSupport::XmlMini.parse("#{output}</log>")['log']
200 ActiveSupport::XmlMini.parse("#{output}</log>")['log']
197 rescue
201 rescue
198 end
202 end
199 end
203 end
200 as_ary(log['logentry']).each do |le|
204 as_ary(log['logentry']).each do |le|
201 cpalist = as_ary(le['paths']['path-copied']).map do |e|
205 cpalist = as_ary(le['paths']['path-copied']).map do |e|
202 [e['__content__'], e['copyfrom-path']].map do |s|
206 [e['__content__'], e['copyfrom-path']].map do |s|
203 scm_iconv('UTF-8', @path_encoding, CGI.unescape(s))
207 scm_iconv('UTF-8', @path_encoding, CGI.unescape(s))
204 end
208 end
205 end
209 end
206 cpmap = Hash[*cpalist.flatten]
210 cpmap = Hash[*cpalist.flatten]
207 paths = as_ary(le['paths']['path']).map do |e|
211 paths = as_ary(le['paths']['path']).map do |e|
208 p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
212 p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
209 {:action => e['action'],
213 {:action => e['action'],
210 :path => with_leading_slash(p),
214 :path => with_leading_slash(p),
211 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
215 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
212 :from_revision => (cpmap.member?(p) ? le['node'] : nil)}
216 :from_revision => (cpmap.member?(p) ? le['node'] : nil)}
213 end.sort { |a, b| a[:path] <=> b[:path] }
217 end.sort { |a, b| a[:path] <=> b[:path] }
214 yield Revision.new(:revision => le['revision'],
218 yield Revision.new(:revision => le['revision'],
215 :scmid => le['node'],
219 :scmid => le['node'],
216 :author => (le['author']['__content__'] rescue ''),
220 :author => (le['author']['__content__'] rescue ''),
217 :time => Time.parse(le['date']['__content__']),
221 :time => Time.parse(le['date']['__content__']),
218 :message => le['msg']['__content__'],
222 :message => le['msg']['__content__'],
219 :paths => paths)
223 :paths => paths)
220 end
224 end
221 self
225 self
222 end
226 end
223
227
224 # Returns list of nodes in the specified branch
228 # Returns list of nodes in the specified branch
225 def nodes_in_branch(branch, options={})
229 def nodes_in_branch(branch, options={})
226 hg_args = ['rhlog', '--template', '{node|short}\n', '--rhbranch', CGI.escape(branch)]
230 hg_args = ['rhlog', '--template', '{node|short}\n', '--rhbranch', CGI.escape(branch)]
227 hg_args << '--from' << CGI.escape(branch)
231 hg_args << '--from' << CGI.escape(branch)
228 hg_args << '--to' << '0'
232 hg_args << '--to' << '0'
229 hg_args << '--limit' << options[:limit] if options[:limit]
233 hg_args << '--limit' << options[:limit] if options[:limit]
230 hg(*hg_args) { |io| io.readlines.map { |e| e.chomp } }
234 hg(*hg_args) { |io| io.readlines.map { |e| e.chomp } }
231 end
235 end
232
236
233 def diff(path, identifier_from, identifier_to=nil)
237 def diff(path, identifier_from, identifier_to=nil)
234 hg_args = %w|rhdiff|
238 hg_args = %w|rhdiff|
235 if identifier_to
239 if identifier_to
236 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
240 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
237 else
241 else
238 hg_args << '-c' << hgrev(identifier_from)
242 hg_args << '-c' << hgrev(identifier_from)
239 end
243 end
240 unless path.blank?
244 unless path.blank?
241 p = scm_iconv(@path_encoding, 'UTF-8', path)
245 p = scm_iconv(@path_encoding, 'UTF-8', path)
242 hg_args << CGI.escape(hgtarget(p))
246 hg_args << CGI.escape(hgtarget(p))
243 end
247 end
244 diff = []
248 diff = []
245 hg *hg_args do |io|
249 hg *hg_args do |io|
246 io.each_line do |line|
250 io.each_line do |line|
247 diff << line
251 diff << line
248 end
252 end
249 end
253 end
250 diff
254 diff
251 rescue HgCommandAborted
255 rescue HgCommandAborted
252 nil # means not found
256 nil # means not found
253 end
257 end
254
258
255 def cat(path, identifier=nil)
259 def cat(path, identifier=nil)
256 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
260 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
257 hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
261 hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
258 io.binmode
262 io.binmode
259 io.read
263 io.read
260 end
264 end
261 rescue HgCommandAborted
265 rescue HgCommandAborted
262 nil # means not found
266 nil # means not found
263 end
267 end
264
268
265 def annotate(path, identifier=nil)
269 def annotate(path, identifier=nil)
266 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
270 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
267 blame = Annotate.new
271 blame = Annotate.new
268 hg 'rhannotate', '-ncu', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
272 hg 'rhannotate', '-ncu', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
269 io.each_line do |line|
273 io.each_line do |line|
270 line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding)
274 line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding)
271 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
275 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
272 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
276 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
273 :identifier => $3)
277 :identifier => $3)
274 blame.add_line($4.rstrip, r)
278 blame.add_line($4.rstrip, r)
275 end
279 end
276 end
280 end
277 blame
281 blame
278 rescue HgCommandAborted
282 rescue HgCommandAborted
279 # means not found or cannot be annotated
283 # means not found or cannot be annotated
280 Annotate.new
284 Annotate.new
281 end
285 end
282
286
283 class Revision < Redmine::Scm::Adapters::Revision
287 class Revision < Redmine::Scm::Adapters::Revision
284 # Returns the readable identifier
288 # Returns the readable identifier
285 def format_identifier
289 def format_identifier
286 "#{revision}:#{scmid}"
290 "#{revision}:#{scmid}"
287 end
291 end
288 end
292 end
289
293
290 # Runs 'hg' command with the given args
294 # Runs 'hg' command with the given args
291 def hg(*args, &block)
295 def hg(*args, &block)
292 repo_path = root_url || url
296 repo_path = root_url || url
293 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
297 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
294 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
298 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
295 full_args << '--config' << 'diff.git=false'
299 full_args << '--config' << 'diff.git=false'
296 full_args += args
300 full_args += args
297 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
301 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
298 if $? && $?.exitstatus != 0
302 if $? && $?.exitstatus != 0
299 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
303 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
300 end
304 end
301 ret
305 ret
302 end
306 end
303 private :hg
307 private :hg
304
308
305 # Returns correct revision identifier
309 # Returns correct revision identifier
306 def hgrev(identifier, sq=false)
310 def hgrev(identifier, sq=false)
307 rev = identifier.blank? ? 'tip' : identifier.to_s
311 rev = identifier.blank? ? 'tip' : identifier.to_s
308 rev = shell_quote(rev) if sq
312 rev = shell_quote(rev) if sq
309 rev
313 rev
310 end
314 end
311 private :hgrev
315 private :hgrev
312
316
313 def hgtarget(path)
317 def hgtarget(path)
314 path ||= ''
318 path ||= ''
315 root_url + '/' + without_leading_slash(path)
319 root_url + '/' + without_leading_slash(path)
316 end
320 end
317 private :hgtarget
321 private :hgtarget
318
322
319 def as_ary(o)
323 def as_ary(o)
320 return [] unless o
324 return [] unless o
321 o.is_a?(Array) ? o : Array[o]
325 o.is_a?(Array) ? o : Array[o]
322 end
326 end
323 private :as_ary
327 private :as_ary
324 end
328 end
325 end
329 end
326 end
330 end
327 end
331 end
General Comments 0
You need to be logged in to leave comments. Login now