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