Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require 'redmine/scm/adapters/abstract_adapter' Chris@1296: require 'cgi' Chris@1296: Chris@1296: module Redmine Chris@1296: module Scm Chris@1296: module Adapters Chris@1296: class MercurialAdapter < AbstractAdapter Chris@1296: Chris@1296: # Mercurial executable name Chris@1296: HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg" Chris@1296: HELPERS_DIR = File.dirname(__FILE__) + "/mercurial" Chris@1296: HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py" Chris@1296: TEMPLATE_NAME = "hg-template" Chris@1296: TEMPLATE_EXTENSION = "tmpl" Chris@1296: Chris@1296: # raised if hg command exited with error, e.g. unknown revision. Chris@1296: class HgCommandAborted < CommandFailed; end Chris@1296: Chris@1296: class << self Chris@1296: def client_command Chris@1296: @@bin ||= HG_BIN Chris@1296: end Chris@1296: Chris@1296: def sq_bin Chris@1296: @@sq_bin ||= shell_quote_command Chris@1296: end Chris@1296: Chris@1296: def client_version Chris@1296: @@client_version ||= (hgversion || []) Chris@1296: end Chris@1296: Chris@1296: def client_available Chris@1296: client_version_above?([1, 2]) Chris@1296: end Chris@1296: Chris@1296: def hgversion Chris@1296: # The hg version is expressed either as a Chris@1296: # release number (eg 0.9.5 or 1.0) or as a revision Chris@1296: # id composed of 12 hexa characters. Chris@1296: theversion = hgversion_from_command_line.dup Chris@1296: if theversion.respond_to?(:force_encoding) Chris@1296: theversion.force_encoding('ASCII-8BIT') Chris@1296: end Chris@1296: if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@1296: m[2].scan(%r{\d+}).collect(&:to_i) Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def hgversion_from_command_line Chris@1296: shellout("#{sq_bin} --version") { |io| io.read }.to_s Chris@1296: end Chris@1296: Chris@1296: def template_path Chris@1296: @@template_path ||= template_path_for(client_version) Chris@1296: end Chris@1296: Chris@1296: def template_path_for(version) Chris@1296: "#{HELPERS_DIR}/#{TEMPLATE_NAME}-1.0.#{TEMPLATE_EXTENSION}" Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) Chris@1296: super Chris@1296: @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding Chris@1296: end Chris@1296: Chris@1296: def path_encoding Chris@1296: @path_encoding Chris@1296: end Chris@1296: Chris@1296: def info Chris@1296: tip = summary['repository']['tip'] Chris@1296: Info.new(:root_url => CGI.unescape(summary['repository']['root']), Chris@1296: :lastrev => Revision.new(:revision => tip['revision'], Chris@1296: :scmid => tip['node'])) Chris@1296: # rescue HgCommandAborted Chris@1296: rescue Exception => e Chris@1296: logger.error "hg: error during getting info: #{e.message}" Chris@1296: nil Chris@1296: end Chris@1296: Chris@1296: def tags Chris@1296: as_ary(summary['repository']['tag']).map { |e| e['name'] } Chris@1296: end Chris@1296: Chris@1296: # Returns map of {'tag' => 'nodeid', ...} Chris@1296: def tagmap Chris@1296: alist = as_ary(summary['repository']['tag']).map do |e| Chris@1296: e.values_at('name', 'node') Chris@1296: end Chris@1296: Hash[*alist.flatten] Chris@1296: end Chris@1296: Chris@1296: def branches Chris@1296: brs = [] Chris@1296: as_ary(summary['repository']['branch']).each do |e| Chris@1296: br = Branch.new(e['name']) Chris@1296: br.revision = e['revision'] Chris@1296: br.scmid = e['node'] Chris@1296: brs << br Chris@1296: end Chris@1296: brs Chris@1296: end Chris@1296: Chris@1296: # Returns map of {'branch' => 'nodeid', ...} Chris@1296: def branchmap Chris@1296: alist = as_ary(summary['repository']['branch']).map do |e| Chris@1296: e.values_at('name', 'node') Chris@1296: end Chris@1296: Hash[*alist.flatten] Chris@1296: end Chris@1296: Chris@1296: def summary Chris@1296: return @summary if @summary Chris@1296: hg 'rhsummary' do |io| Chris@1296: output = io.read Chris@1296: if output.respond_to?(:force_encoding) Chris@1296: output.force_encoding('UTF-8') Chris@1296: end Chris@1296: begin Chris@1296: @summary = parse_xml(output)['rhsummary'] Chris@1296: rescue Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: private :summary Chris@1296: Chris@1296: def entries(path=nil, identifier=nil, options={}) Chris@1296: p1 = scm_iconv(@path_encoding, 'UTF-8', path) Chris@1296: manifest = hg('rhmanifest', '-r', CGI.escape(hgrev(identifier)), Chris@1296: CGI.escape(without_leading_slash(p1.to_s))) do |io| Chris@1296: output = io.read Chris@1296: if output.respond_to?(:force_encoding) Chris@1296: output.force_encoding('UTF-8') Chris@1296: end Chris@1296: begin Chris@1296: parse_xml(output)['rhmanifest']['repository']['manifest'] Chris@1296: rescue Chris@1296: end Chris@1296: end Chris@1296: path_prefix = path.blank? ? '' : with_trailling_slash(path) Chris@1296: Chris@1296: entries = Entries.new Chris@1296: as_ary(manifest['dir']).each do |e| Chris@1296: n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name'])) Chris@1296: p = "#{path_prefix}#{n}" Chris@1296: entries << Entry.new(:name => n, :path => p, :kind => 'dir') Chris@1296: end Chris@1296: Chris@1296: as_ary(manifest['file']).each do |e| Chris@1296: n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name'])) Chris@1296: p = "#{path_prefix}#{n}" Chris@1296: lr = Revision.new(:revision => e['revision'], :scmid => e['node'], Chris@1296: :identifier => e['node'], Chris@1296: :time => Time.at(e['time'].to_i)) Chris@1296: entries << Entry.new(:name => n, :path => p, :kind => 'file', Chris@1296: :size => e['size'].to_i, :lastrev => lr) Chris@1296: end Chris@1296: Chris@1296: entries Chris@1296: rescue HgCommandAborted Chris@1296: nil # means not found Chris@1296: end Chris@1296: Chris@1296: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@1296: revs = Revisions.new Chris@1296: each_revision(path, identifier_from, identifier_to, options) { |e| revs << e } Chris@1296: revs Chris@1296: end Chris@1296: Chris@1296: # Iterates the revisions by using a template file that Chris@1296: # makes Mercurial produce a xml output. Chris@1296: def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@1296: hg_args = ['log', '--debug', '-C', '--style', self.class.template_path] Chris@1296: hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}" Chris@1296: hg_args << '--limit' << options[:limit] if options[:limit] Chris@1296: hg_args << hgtarget(path) unless path.blank? Chris@1296: log = hg(*hg_args) do |io| Chris@1296: output = io.read Chris@1296: if output.respond_to?(:force_encoding) Chris@1296: output.force_encoding('UTF-8') Chris@1296: end Chris@1296: begin Chris@1296: # Mercurial < 1.5 does not support footer template for '' Chris@1296: parse_xml("#{output}")['log'] Chris@1296: rescue Chris@1296: end Chris@1296: end Chris@1296: as_ary(log['logentry']).each do |le| Chris@1296: cpalist = as_ary(le['paths']['path-copied']).map do |e| Chris@1296: [e['__content__'], e['copyfrom-path']].map do |s| Chris@1296: scm_iconv('UTF-8', @path_encoding, CGI.unescape(s)) Chris@1296: end Chris@1296: end Chris@1296: cpmap = Hash[*cpalist.flatten] Chris@1296: paths = as_ary(le['paths']['path']).map do |e| Chris@1296: p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) ) Chris@1296: {:action => e['action'], Chris@1296: :path => with_leading_slash(p), Chris@1296: :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil), Chris@1296: :from_revision => (cpmap.member?(p) ? le['node'] : nil)} Chris@1296: end.sort { |a, b| a[:path] <=> b[:path] } Chris@1296: parents_ary = [] Chris@1296: as_ary(le['parents']['parent']).map do |par| Chris@1296: parents_ary << par['__content__'] if par['__content__'] != "000000000000" Chris@1296: end Chris@1296: yield Revision.new(:revision => le['revision'], Chris@1296: :scmid => le['node'], Chris@1296: :author => (le['author']['__content__'] rescue ''), Chris@1296: :time => Time.parse(le['date']['__content__']), Chris@1296: :message => le['msg']['__content__'], Chris@1296: :paths => paths, Chris@1296: :parents => parents_ary) Chris@1296: end Chris@1296: self Chris@1296: end Chris@1296: Chris@1296: # Returns list of nodes in the specified branch Chris@1296: def nodes_in_branch(branch, options={}) Chris@1296: hg_args = ['rhlog', '--template', '{node|short}\n', '--rhbranch', CGI.escape(branch)] Chris@1296: hg_args << '--from' << CGI.escape(branch) Chris@1296: hg_args << '--to' << '0' Chris@1296: hg_args << '--limit' << options[:limit] if options[:limit] Chris@1296: hg(*hg_args) { |io| io.readlines.map { |e| e.chomp } } Chris@1296: end Chris@1296: Chris@1296: def diff(path, identifier_from, identifier_to=nil) Chris@1296: hg_args = %w|rhdiff| Chris@1296: if identifier_to Chris@1296: hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from) Chris@1296: else Chris@1296: hg_args << '-c' << hgrev(identifier_from) Chris@1296: end Chris@1296: unless path.blank? Chris@1296: p = scm_iconv(@path_encoding, 'UTF-8', path) Chris@1296: hg_args << CGI.escape(hgtarget(p)) Chris@1296: end Chris@1296: diff = [] Chris@1296: hg *hg_args do |io| Chris@1296: io.each_line do |line| Chris@1296: diff << line Chris@1296: end Chris@1296: end Chris@1296: diff Chris@1296: rescue HgCommandAborted Chris@1296: nil # means not found Chris@1296: end Chris@1296: Chris@1296: def cat(path, identifier=nil) Chris@1296: p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path)) Chris@1296: hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io| Chris@1296: io.binmode Chris@1296: io.read Chris@1296: end Chris@1296: rescue HgCommandAborted Chris@1296: nil # means not found Chris@1296: end Chris@1296: Chris@1296: def annotate(path, identifier=nil) Chris@1296: p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path)) Chris@1296: blame = Annotate.new Chris@1296: hg 'rhannotate', '-ncu', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io| Chris@1296: io.each_line do |line| Chris@1296: line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding) Chris@1296: next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$} Chris@1296: r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3, Chris@1296: :identifier => $3) Chris@1296: blame.add_line($4.rstrip, r) Chris@1296: end Chris@1296: end Chris@1296: blame Chris@1296: rescue HgCommandAborted Chris@1296: # means not found or cannot be annotated Chris@1296: Annotate.new Chris@1296: end Chris@1296: Chris@1296: class Revision < Redmine::Scm::Adapters::Revision Chris@1296: # Returns the readable identifier Chris@1296: def format_identifier Chris@1296: "#{revision}:#{scmid}" Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Runs 'hg' command with the given args Chris@1296: def hg(*args, &block) Chris@1296: repo_path = root_url || url Chris@1296: full_args = ['-R', repo_path, '--encoding', 'utf-8'] Chris@1296: full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}" Chris@1296: full_args << '--config' << 'diff.git=false' Chris@1296: full_args += args Chris@1296: ret = shellout( Chris@1296: self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '), Chris@1296: &block Chris@1296: ) Chris@1296: if $? && $?.exitstatus != 0 Chris@1296: raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}" Chris@1296: end Chris@1296: ret Chris@1296: end Chris@1296: private :hg Chris@1296: Chris@1296: # Returns correct revision identifier Chris@1296: def hgrev(identifier, sq=false) Chris@1296: rev = identifier.blank? ? 'tip' : identifier.to_s Chris@1296: rev = shell_quote(rev) if sq Chris@1296: rev Chris@1296: end Chris@1296: private :hgrev Chris@1296: Chris@1296: def hgtarget(path) Chris@1296: path ||= '' Chris@1296: root_url + '/' + without_leading_slash(path) Chris@1296: end Chris@1296: private :hgtarget Chris@1296: Chris@1296: def as_ary(o) Chris@1296: return [] unless o Chris@1296: o.is_a?(Array) ? o : Array[o] Chris@1296: end Chris@1296: private :as_ary Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end