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