1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 require 'redmine/scm/adapters/abstract_adapter'
24 class MercurialAdapter < AbstractAdapter
26 # Mercurial executable name
27 HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
28 HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
29 HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
30 TEMPLATE_NAME = "hg-template"
31 TEMPLATE_EXTENSION = "tmpl"
33 # raised if hg command exited with error, e.g. unknown revision.
34 class HgCommandAborted < CommandFailed; end
42 @@sq_bin ||= shell_quote(HG_BIN)
46 @@client_version ||= (hgversion || [])
50 !client_version.empty?
54 # The hg version is expressed either as a
55 # release number (eg 0.9.5 or 1.0) or as a revision
56 # id composed of 12 hexa characters.
57 theversion = hgversion_from_command_line
58 if theversion.respond_to?(:force_encoding)
59 theversion.force_encoding('ASCII-8BIT')
61 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
62 m[2].scan(%r{\d+}).collect(&:to_i)
66 def hgversion_from_command_line
67 shellout("#{sq_bin} --version") { |io| io.read }.to_s
71 @@template_path ||= template_path_for(client_version)
74 def template_path_for(version)
75 if ((version <=> [0,9,5]) > 0) || version.empty?
80 "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
84 def initialize(url, root_url=nil, login=nil, password=nil)
86 @path_encoding = 'UTF-8'
90 tip = summary['repository']['tip']
91 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
92 :lastrev => Revision.new(:revision => tip['revision'],
93 :scmid => tip['node']))
97 as_ary(summary['repository']['tag']).map { |e| e['name'] }
100 # Returns map of {'tag' => 'nodeid', ...}
102 alist = as_ary(summary['repository']['tag']).map do |e|
103 e.values_at('name', 'node')
109 as_ary(summary['repository']['branch']).map { |e| e['name'] }
112 # Returns map of {'branch' => 'nodeid', ...}
114 alist = as_ary(summary['repository']['branch']).map do |e|
115 e.values_at('name', 'node')
121 return @summary if @summary
122 hg 'rhsummary' do |io|
124 @summary = ActiveSupport::XmlMini.parse(io.read)['rhsummary']
131 def entries(path=nil, identifier=nil)
132 p1 = scm_iconv(@path_encoding, 'UTF-8', path)
133 manifest = hg('rhmanifest', '-r', hgrev(identifier),
134 CGI.escape(without_leading_slash(p1.to_s))) do |io|
136 ActiveSupport::XmlMini.parse(io.read)['rhmanifest']['repository']['manifest']
140 path_prefix = path.blank? ? '' : with_trailling_slash(path)
142 entries = Entries.new
143 as_ary(manifest['dir']).each do |e|
144 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
145 p = "#{path_prefix}#{n}"
146 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
149 as_ary(manifest['file']).each do |e|
150 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
151 p = "#{path_prefix}#{n}"
152 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
153 :identifier => e['node'],
154 :time => Time.at(e['time'].to_i))
155 entries << Entry.new(:name => n, :path => p, :kind => 'file',
156 :size => e['size'].to_i, :lastrev => lr)
160 rescue HgCommandAborted
161 nil # means not found
164 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
166 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
170 # Iterates the revisions by using a template file that
171 # makes Mercurial produce a xml output.
172 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
173 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
174 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
175 hg_args << '--limit' << options[:limit] if options[:limit]
176 hg_args << hgtarget(path) unless path.blank?
177 log = hg(*hg_args) do |io|
179 # Mercurial < 1.5 does not support footer template for '</log>'
180 ActiveSupport::XmlMini.parse("#{io.read}</log>")['log']
185 as_ary(log['logentry']).each do |le|
186 cpalist = as_ary(le['paths']['path-copied']).map do |e|
187 [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) }
189 cpmap = Hash[*cpalist.flatten]
191 paths = as_ary(le['paths']['path']).map do |e|
192 p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
193 {:action => e['action'], :path => with_leading_slash(p),
194 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
195 :from_revision => (cpmap.member?(p) ? le['revision'] : nil)}
196 end.sort { |a, b| a[:path] <=> b[:path] }
198 yield Revision.new(:revision => le['revision'],
199 :scmid => le['node'],
200 :author => (le['author']['__content__'] rescue ''),
201 :time => Time.parse(le['date']['__content__']).localtime,
202 :message => le['msg']['__content__'],
208 def diff(path, identifier_from, identifier_to=nil)
211 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
213 hg_args << '-c' << hgrev(identifier_from)
216 p = scm_iconv(@path_encoding, 'UTF-8', path)
217 hg_args << CGI.escape(hgtarget(p))
221 io.each_line do |line|
226 rescue HgCommandAborted
227 nil # means not found
230 def cat(path, identifier=nil)
231 hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io|
235 rescue HgCommandAborted
236 nil # means not found
239 def annotate(path, identifier=nil)
241 hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io|
242 io.each_line do |line|
243 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
244 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
246 blame.add_line($4.rstrip, r)
250 rescue HgCommandAborted
251 nil # means not found or cannot be annotated
254 class Revision < Redmine::Scm::Adapters::Revision
255 # Returns the readable identifier
256 def format_identifier
257 "#{revision}:#{scmid}"
261 # Runs 'hg' command with the given args
262 def hg(*args, &block)
263 repo_path = root_url || url
264 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
265 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
266 full_args << '--config' << 'diff.git=false'
268 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
269 if $? && $?.exitstatus != 0
270 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
276 # Returns correct revision identifier
277 def hgrev(identifier, sq=false)
278 rev = identifier.blank? ? 'tip' : identifier.to_s
279 rev = shell_quote(rev) if sq
286 root_url + '/' + without_leading_slash(path)
292 o.is_a?(Array) ? o : Array[o]