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 m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
59 m[2].scan(%r{\d+}).collect(&:to_i)
63 def hgversion_from_command_line
64 shellout("#{sq_bin} --version") { |io| io.read }.to_s
68 @@template_path ||= template_path_for(client_version)
71 def template_path_for(version)
72 if ((version <=> [0,9,5]) > 0) || version.empty?
77 "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
82 tip = summary['repository']['tip']
83 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
84 :lastrev => Revision.new(:revision => tip['revision'],
85 :scmid => tip['node']))
89 @summary ||= hg 'rhsummary' do |io|
90 ActiveSupport::XmlMini.parse(io.read)['rhsummary']
95 def entries(path=nil, identifier=nil)
96 manifest = hg('rhmanifest', '-r', hgrev(identifier),
97 CGI.escape(without_leading_slash(path.to_s))) do |io|
98 ActiveSupport::XmlMini.parse(io.read)['rhmanifest']['repository']['manifest']
100 path_prefix = path.blank? ? '' : with_trailling_slash(path)
102 entries = Entries.new
103 as_ary(manifest['dir']).each do |e|
104 n = CGI.unescape(e['name'])
105 p = "#{path_prefix}#{n}"
106 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
109 as_ary(manifest['file']).each do |e|
110 n = CGI.unescape(e['name'])
111 p = "#{path_prefix}#{n}"
112 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
113 :identifier => e['node'],
114 :time => Time.at(e['time'].to_i))
115 entries << Entry.new(:name => n, :path => p, :kind => 'file',
116 :size => e['size'].to_i, :lastrev => lr)
120 rescue HgCommandAborted
121 nil # means not found
124 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
126 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
130 # Iterates the revisions by using a template file that
131 # makes Mercurial produce a xml output.
132 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
133 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
134 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
135 hg_args << '--limit' << options[:limit] if options[:limit]
136 hg_args << hgtarget(path) unless path.blank?
137 log = hg(*hg_args) do |io|
138 # Mercurial < 1.5 does not support footer template for '</log>'
139 ActiveSupport::XmlMini.parse("#{io.read}</log>")['log']
142 as_ary(log['logentry']).each do |le|
143 cpalist = as_ary(le['paths']['path-copied']).map do |e|
144 [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) }
146 cpmap = Hash[*cpalist.flatten]
148 paths = as_ary(le['paths']['path']).map do |e|
149 p = CGI.unescape(e['__content__'])
150 {:action => e['action'], :path => with_leading_slash(p),
151 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
152 :from_revision => (cpmap.member?(p) ? le['revision'] : nil)}
153 end.sort { |a, b| a[:path] <=> b[:path] }
155 yield Revision.new(:revision => le['revision'],
156 :scmid => le['node'],
157 :author => (le['author']['__content__'] rescue ''),
158 :time => Time.parse(le['date']['__content__']).localtime,
159 :message => le['msg']['__content__'],
165 def diff(path, identifier_from, identifier_to=nil)
168 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
170 hg_args << '-c' << hgrev(identifier_from)
172 hg_args << CGI.escape(hgtarget(path)) unless path.blank?
175 io.each_line do |line|
180 rescue HgCommandAborted
181 nil # means not found
184 def cat(path, identifier=nil)
185 hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io|
189 rescue HgCommandAborted
190 nil # means not found
193 def annotate(path, identifier=nil)
195 hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io|
196 io.each_line do |line|
197 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
198 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
200 blame.add_line($4.rstrip, r)
204 rescue HgCommandAborted
205 nil # means not found or cannot be annotated
208 class Revision < Redmine::Scm::Adapters::Revision
209 # Returns the readable identifier
210 def format_identifier
211 "#{revision}:#{scmid}"
215 # Runs 'hg' command with the given args
216 def hg(*args, &block)
217 repo_path = root_url || url
218 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
219 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
220 full_args << '--config' << 'diff.git=false'
222 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
223 if $? && $?.exitstatus != 0
224 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
230 # Returns correct revision identifier
231 def hgrev(identifier, sq=false)
232 rev = identifier.blank? ? 'tip' : identifier.to_s
233 rev = shell_quote(rev) if sq
240 root_url + '/' + without_leading_slash(path)
246 o.is_a?(Array) ? o : Array[o]