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 cmd = "#{self.class.sq_bin} -R #{target('')} root"
87 return nil if $? && $?.exitstatus != 0
88 info = Info.new({:root_url => root_url.chomp,
89 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
97 @summary ||= hg 'rhsummary' do |io|
98 ActiveSupport::XmlMini.parse(io.read)['rhsummary']
103 def entries(path=nil, identifier=nil)
105 entries = Entries.new
106 cmd = "#{self.class.sq_bin} -R #{target('')} --cwd #{target('')} locate"
107 cmd << " -r #{hgrev(identifier, true)}"
108 cmd << " " + shell_quote("path:#{path}") unless path.empty?
109 shellout(cmd) do |io|
110 io.each_line do |line|
111 # HG uses antislashs as separator on Windows
112 line = line.gsub(/\\/, "/")
113 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
115 e = e.chomp.split(%r{[\/\\]})
116 entries << Entry.new({:name => e.first,
117 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
118 :kind => (e.size > 1 ? 'dir' : 'file'),
119 :lastrev => Revision.new
120 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
124 return nil if $? && $?.exitstatus != 0
128 # Fetch the revisions by using a template file that
129 # makes Mercurial produce a xml output.
130 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
131 revisions = Revisions.new
132 cmd = "#{self.class.sq_bin} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
133 if identifier_from && identifier_to
134 cmd << " -r #{hgrev(identifier_from, true)}:#{hgrev(identifier_to, true)}"
135 elsif identifier_from
136 cmd << " -r #{hgrev(identifier_from, true)}:"
138 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
139 cmd << " #{shell_quote path}" unless path.blank?
140 shellout(cmd) do |io|
142 # HG doesn't close the XML Document...
143 doc = REXML::Document.new(io.read << "</log>")
144 doc.elements.each("log/logentry") do |logentry|
146 copies = logentry.get_elements('paths/path-copied')
147 logentry.elements.each("paths/path") do |path|
148 # Detect if the added file is a copy
149 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
150 from_path = c.attributes['copyfrom-path']
151 from_rev = logentry.attributes['revision']
153 paths << {:action => path.attributes['action'],
154 :path => "/#{CGI.unescape(path.text)}",
155 :from_path => from_path ? "/#{CGI.unescape(from_path)}" : nil,
156 :from_revision => from_rev ? from_rev : nil
159 paths.sort! { |x,y| x[:path] <=> y[:path] }
161 revisions << Revision.new({:identifier => logentry.attributes['revision'],
162 :scmid => logentry.attributes['node'],
163 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
164 :time => Time.parse(logentry.elements['date'].text).localtime,
165 :message => logentry.elements['msg'].text,
173 return nil if $? && $?.exitstatus != 0
177 def diff(path, identifier_from, identifier_to=nil)
182 diff_args = "-r #{hgrev(identifier_to, true)} -r #{hgrev(identifier_from, true)}"
184 if self.class.client_version_above?([1, 2])
185 diff_args = "-c #{hgrev(identifier_from, true)}"
190 cmd = "#{self.class.sq_bin} -R #{target('')} --config diff.git=false diff --nodates #{diff_args}"
191 cmd << " -I #{target(path)}" unless path.empty?
192 shellout(cmd) do |io|
193 io.each_line do |line|
197 return nil if $? && $?.exitstatus != 0
201 def cat(path, identifier=nil)
202 hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io|
206 rescue HgCommandAborted
207 nil # means not found
210 def annotate(path, identifier=nil)
212 hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io|
213 io.each_line do |line|
214 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
215 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
217 blame.add_line($4.rstrip, r)
221 rescue HgCommandAborted
222 nil # means not found or cannot be annotated
225 class Revision < Redmine::Scm::Adapters::Revision
226 # Returns the readable identifier
227 def format_identifier
228 "#{revision}:#{scmid}"
232 # Runs 'hg' command with the given args
233 def hg(*args, &block)
234 repo_path = root_url || url
235 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
236 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
237 full_args << '--config' << 'diff.git=false'
239 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
240 if $? && $?.exitstatus != 0
241 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
247 # Returns correct revision identifier
248 def hgrev(identifier, sq=false)
249 rev = identifier.blank? ? 'tip' : identifier.to_s
250 rev = shell_quote(rev) if sq
257 root_url + '/' + without_leading_slash(path)