Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 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@0: # 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@0: # 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@117: require 'cgi' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@0: module Adapters Chris@0: class MercurialAdapter < AbstractAdapter Chris@0: Chris@0: # Mercurial executable name Chris@0: HG_BIN = "hg" Chris@0: TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial" Chris@0: TEMPLATE_NAME = "hg-template" Chris@0: TEMPLATE_EXTENSION = "tmpl" Chris@0: Chris@0: class << self Chris@0: def client_version Chris@0: @@client_version ||= (hgversion || []) Chris@0: end Chris@0: Chris@0: 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@0: theversion = hgversion_from_command_line Chris@117: if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@117: m[2].scan(%r{\d+}).collect(&:to_i) Chris@0: end Chris@0: end Chris@0: Chris@0: def hgversion_from_command_line Chris@117: shellout("#{HG_BIN} --version") { |io| io.read }.to_s Chris@0: end Chris@0: Chris@0: def template_path Chris@0: @@template_path ||= template_path_for(client_version) Chris@0: end Chris@0: 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@0: "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}" Chris@0: end Chris@0: end Chris@0: Chris@0: def info Chris@0: cmd = "#{HG_BIN} -R #{target('')} root" Chris@0: root_url = nil Chris@0: shellout(cmd) do |io| Chris@0: root_url = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: info = Info.new({:root_url => root_url.chomp, Chris@0: :lastrev => revisions(nil,nil,nil,{:limit => 1}).last Chris@0: }) Chris@0: info Chris@0: rescue CommandFailed Chris@0: return nil Chris@0: end Chris@0: Chris@0: def entries(path=nil, identifier=nil) Chris@0: path ||= '' Chris@0: entries = Entries.new Chris@0: cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate" Chris@117: cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip") Chris@0: cmd << " " + shell_quote("path:#{path}") unless path.empty? Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: # HG uses antislashs as separator on Windows Chris@0: line = line.gsub(/\\/, "/") Chris@0: if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'') Chris@0: e ||= line Chris@0: e = e.chomp.split(%r{[\/\\]}) Chris@0: entries << Entry.new({:name => e.first, Chris@0: :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"), Chris@0: :kind => (e.size > 1 ? 'dir' : 'file'), Chris@0: :lastrev => Revision.new Chris@0: }) unless e.empty? || entries.detect{|entry| entry.name == e.first} Chris@0: end Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: entries.sort_by_name Chris@0: end Chris@0: Chris@0: # Fetch the revisions by using a template file that Chris@0: # makes Mercurial produce a xml output. Chris@0: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@0: revisions = Revisions.new Chris@0: cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}" Chris@0: if identifier_from && identifier_to Chris@0: cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}" Chris@0: elsif identifier_from Chris@0: cmd << " -r #{identifier_from.to_i}:" Chris@0: end Chris@0: cmd << " --limit #{options[:limit].to_i}" if options[:limit] Chris@117: cmd << " #{shell_quote path}" unless path.blank? Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: # HG doesn't close the XML Document... Chris@0: doc = REXML::Document.new(io.read << "") Chris@0: doc.elements.each("log/logentry") do |logentry| Chris@0: paths = [] Chris@0: copies = logentry.get_elements('paths/path-copied') Chris@0: logentry.elements.each("paths/path") do |path| Chris@0: # Detect if the added file is a copy Chris@0: if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text } Chris@0: from_path = c.attributes['copyfrom-path'] Chris@0: from_rev = logentry.attributes['revision'] Chris@0: end Chris@0: paths << {:action => path.attributes['action'], Chris@117: :path => "/#{CGI.unescape(path.text)}", Chris@117: :from_path => from_path ? "/#{CGI.unescape(from_path)}" : nil, Chris@0: :from_revision => from_rev ? from_rev : nil Chris@0: } Chris@0: end Chris@0: paths.sort! { |x,y| x[:path] <=> y[:path] } Chris@0: Chris@0: revisions << Revision.new({:identifier => logentry.attributes['revision'], Chris@0: :scmid => logentry.attributes['node'], Chris@0: :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""), Chris@0: :time => Time.parse(logentry.elements['date'].text).localtime, Chris@0: :message => logentry.elements['msg'].text, Chris@0: :paths => paths Chris@0: }) Chris@0: end Chris@0: rescue Chris@0: logger.debug($!) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: revisions Chris@0: end Chris@0: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path ||= '' Chris@0: if identifier_to Chris@0: identifier_to = identifier_to.to_i Chris@0: else Chris@0: identifier_to = identifier_from.to_i - 1 Chris@0: end Chris@117: if identifier_from Chris@117: identifier_from = identifier_from.to_i Chris@117: end Chris@0: cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates" Chris@0: cmd << " -I #{target(path)}" unless path.empty? Chris@0: diff = [] Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: diff Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: cmd = "#{HG_BIN} -R #{target('')} cat" Chris@117: cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip") Chris@0: cmd << " #{target(path)}" Chris@0: cat = nil Chris@0: shellout(cmd) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: cat Chris@0: end Chris@0: Chris@0: def annotate(path, identifier=nil) Chris@0: path ||= '' Chris@0: cmd = "#{HG_BIN} -R #{target('')}" Chris@0: cmd << " annotate -n -u" Chris@117: cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip") Chris@0: cmd << " -r #{identifier.to_i}" if identifier Chris@0: cmd << " #{target(path)}" Chris@0: blame = Annotate.new Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: next unless line =~ %r{^([^:]+)\s(\d+):(.*)$} Chris@0: blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip)) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: blame Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end