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