Mercurial adapter improvements (patch #1199 by Pierre Paysant-Le Roux).
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1499 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
a6da479a63
commit
aa9d04a4a7
|
@ -94,6 +94,11 @@ module Redmine
|
||||||
path ||= ''
|
path ||= ''
|
||||||
(path[0,1]!="/") ? "/#{path}" : path
|
(path[0,1]!="/") ? "/#{path}" : path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_trailling_slash(path)
|
||||||
|
path ||= ''
|
||||||
|
(path[-1,1] == "/") ? path : "#{path}/"
|
||||||
|
end
|
||||||
|
|
||||||
def shell_quote(str)
|
def shell_quote(str)
|
||||||
if RUBY_PLATFORM =~ /mswin/
|
if RUBY_PLATFORM =~ /mswin/
|
||||||
|
@ -102,7 +107,7 @@ module Redmine
|
||||||
"'" + str.gsub(/'/, "'\"'\"'") + "'"
|
"'" + str.gsub(/'/, "'\"'\"'") + "'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def retrieve_root_url
|
def retrieve_root_url
|
||||||
info = self.info
|
info = self.info
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
changeset = 'This template must be used with --debug option\n'
|
||||||
|
changeset_quiet = 'This template must be used with --debug option\n'
|
||||||
|
changeset_verbose = 'This template must be used with --debug option\n'
|
||||||
|
changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodate}</date>\n<paths>\n{files}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n{tags}</logentry>\n\n'
|
||||||
|
|
||||||
|
file = '<path action="M">{file|escape}</path>\n'
|
||||||
|
file_add = '<path action="A">{file_add|escape}</path>\n'
|
||||||
|
file_del = '<path action="D">{file_del|escape}</path>\n'
|
||||||
|
file_copy = '<path-copied copyfrom-path="{source|escape}">{name|urlescape}</path-copied>\n'
|
||||||
|
tag = '<tag>{tag|escape}</tag>\n'
|
||||||
|
header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
|
||||||
|
# footer="</log>"
|
|
@ -0,0 +1,12 @@
|
||||||
|
changeset = 'This template must be used with --debug option\n'
|
||||||
|
changeset_quiet = 'This template must be used with --debug option\n'
|
||||||
|
changeset_verbose = 'This template must be used with --debug option\n'
|
||||||
|
changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodate}</date>\n<paths>\n{file_mods}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n{tags}</logentry>\n\n'
|
||||||
|
|
||||||
|
file_mod = '<path action="M">{file_mod|escape}</path>\n'
|
||||||
|
file_add = '<path action="A">{file_add|escape}</path>\n'
|
||||||
|
file_del = '<path action="D">{file_del|escape}</path>\n'
|
||||||
|
file_copy = '<path-copied copyfrom-path="{source|escape}">{name|urlescape}</path-copied>\n'
|
||||||
|
tag = '<tag>{tag|escape}</tag>\n'
|
||||||
|
header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
|
||||||
|
# footer="</log>"
|
|
@ -21,9 +21,12 @@ module Redmine
|
||||||
module Scm
|
module Scm
|
||||||
module Adapters
|
module Adapters
|
||||||
class MercurialAdapter < AbstractAdapter
|
class MercurialAdapter < AbstractAdapter
|
||||||
|
|
||||||
# Mercurial executable name
|
# Mercurial executable name
|
||||||
HG_BIN = "hg"
|
HG_BIN = "hg"
|
||||||
|
TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
|
||||||
|
TEMPLATE_NAME = "hg-template"
|
||||||
|
TEMPLATE_EXTENSION = "tmpl"
|
||||||
|
|
||||||
def info
|
def info
|
||||||
cmd = "#{HG_BIN} -R #{target('')} root"
|
cmd = "#{HG_BIN} -R #{target('')} root"
|
||||||
|
@ -33,8 +36,8 @@ module Redmine
|
||||||
end
|
end
|
||||||
return nil if $? && $?.exitstatus != 0
|
return nil if $? && $?.exitstatus != 0
|
||||||
info = Info.new({:root_url => root_url.chomp,
|
info = Info.new({:root_url => root_url.chomp,
|
||||||
:lastrev => revisions(nil,nil,nil,{:limit => 1}).last
|
:lastrev => revisions(nil,nil,nil,{:limit => 1}).last
|
||||||
})
|
})
|
||||||
info
|
info
|
||||||
rescue CommandFailed
|
rescue CommandFailed
|
||||||
return nil
|
return nil
|
||||||
|
@ -43,62 +46,72 @@ module Redmine
|
||||||
def entries(path=nil, identifier=nil)
|
def entries(path=nil, identifier=nil)
|
||||||
path ||= ''
|
path ||= ''
|
||||||
entries = Entries.new
|
entries = Entries.new
|
||||||
cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate"
|
cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
|
||||||
cmd << " -r #{identifier.to_i}" if identifier
|
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
||||||
cmd << " " + shell_quote('glob:**')
|
cmd << " " + shell_quote("path:#{path}") unless path.empty?
|
||||||
shellout(cmd) do |io|
|
shellout(cmd) do |io|
|
||||||
io.each_line do |line|
|
io.each_line do |line|
|
||||||
e = line.chomp.split(%r{[\/\\]})
|
# HG uses antislashs as separator on Windows
|
||||||
entries << Entry.new({:name => e.first,
|
line = line.gsub(/\\/, "/")
|
||||||
:path => (path.empty? ? e.first : "#{path}/#{e.first}"),
|
if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
|
||||||
:kind => (e.size > 1 ? 'dir' : 'file'),
|
e ||= line
|
||||||
:lastrev => Revision.new
|
e = e.chomp.split(%r{[\/\\]})
|
||||||
}) unless entries.detect{|entry| entry.name == e.first}
|
entries << Entry.new({:name => e.first,
|
||||||
|
:path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
|
||||||
|
:kind => (e.size > 1 ? 'dir' : 'file'),
|
||||||
|
:lastrev => Revision.new
|
||||||
|
}) unless entries.detect{|entry| entry.name == e.first}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return nil if $? && $?.exitstatus != 0
|
return nil if $? && $?.exitstatus != 0
|
||||||
entries.sort_by_name
|
entries.sort_by_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
# Fetch the revisions by using a template file that
|
||||||
|
# makes Mercurial produce a xml output.
|
||||||
|
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
||||||
revisions = Revisions.new
|
revisions = Revisions.new
|
||||||
cmd = "#{HG_BIN} -v --encoding utf8 -R #{target('')} log"
|
cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.template_path}"
|
||||||
if identifier_from && identifier_to
|
if identifier_from && identifier_to
|
||||||
cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
||||||
elsif identifier_from
|
elsif identifier_from
|
||||||
cmd << " -r #{identifier_from.to_i}:"
|
cmd << " -r #{identifier_from.to_i}:"
|
||||||
end
|
end
|
||||||
cmd << " --limit #{options[:limit].to_i}" if options[:limit]
|
cmd << " --limit #{options[:limit].to_i}" if options[:limit]
|
||||||
|
cmd << " #{path}" if path
|
||||||
shellout(cmd) do |io|
|
shellout(cmd) do |io|
|
||||||
changeset = {}
|
begin
|
||||||
parsing_descr = false
|
# HG doesn't close the XML Document...
|
||||||
line_feeds = 0
|
doc = REXML::Document.new(io.read << "</log>")
|
||||||
|
doc.elements.each("log/logentry") do |logentry|
|
||||||
io.each_line do |line|
|
paths = []
|
||||||
if line =~ /^(\w+):\s*(.*)$/
|
copies = logentry.get_elements('paths/path-copied')
|
||||||
key = $1
|
logentry.elements.each("paths/path") do |path|
|
||||||
value = $2
|
# Detect if the added file is a copy
|
||||||
if parsing_descr && line_feeds > 1
|
if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
|
||||||
parsing_descr = false
|
from_path = c.attributes['copyfrom-path']
|
||||||
revisions << build_revision_from_changeset(changeset)
|
from_rev = logentry.attributes['revision']
|
||||||
changeset = {}
|
|
||||||
end
|
|
||||||
if !parsing_descr
|
|
||||||
changeset.store key.to_sym, value
|
|
||||||
if $1 == "description"
|
|
||||||
parsing_descr = true
|
|
||||||
line_feeds = 0
|
|
||||||
next
|
|
||||||
end
|
end
|
||||||
|
paths << {:action => path.attributes['action'],
|
||||||
|
:path => "/#{path.text}",
|
||||||
|
:from_path => from_path ? "/#{from_path}" : nil,
|
||||||
|
:from_revision => from_rev ? from_rev : nil
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
paths.sort! { |x,y| x[:path] <=> y[:path] }
|
||||||
|
|
||||||
|
revisions << Revision.new({:identifier => logentry.attributes['revision'],
|
||||||
|
:scmid => logentry.attributes['node'],
|
||||||
|
:author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
|
||||||
|
:time => Time.parse(logentry.elements['date'].text).localtime,
|
||||||
|
:message => logentry.elements['msg'].text,
|
||||||
|
:paths => paths
|
||||||
|
})
|
||||||
end
|
end
|
||||||
if parsing_descr
|
rescue
|
||||||
changeset[:description] << line
|
logger.debug($!)
|
||||||
line_feeds += 1 if line.chomp.empty?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
# Add the last changeset if there is one left
|
|
||||||
revisions << build_revision_from_changeset(changeset) if changeset[:date]
|
|
||||||
end
|
end
|
||||||
return nil if $? && $?.exitstatus != 0
|
return nil if $? && $?.exitstatus != 0
|
||||||
revisions
|
revisions
|
||||||
|
@ -125,7 +138,7 @@ module Redmine
|
||||||
|
|
||||||
def cat(path, identifier=nil)
|
def cat(path, identifier=nil)
|
||||||
cmd = "#{HG_BIN} -R #{target('')} cat"
|
cmd = "#{HG_BIN} -R #{target('')} cat"
|
||||||
cmd << " -r #{identifier.to_i}" if identifier
|
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
||||||
cmd << " #{target(path)}"
|
cmd << " #{target(path)}"
|
||||||
cat = nil
|
cat = nil
|
||||||
shellout(cmd) do |io|
|
shellout(cmd) do |io|
|
||||||
|
@ -140,6 +153,7 @@ module Redmine
|
||||||
path ||= ''
|
path ||= ''
|
||||||
cmd = "#{HG_BIN} -R #{target('')}"
|
cmd = "#{HG_BIN} -R #{target('')}"
|
||||||
cmd << " annotate -n -u"
|
cmd << " annotate -n -u"
|
||||||
|
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
||||||
cmd << " -r #{identifier.to_i}" if identifier
|
cmd << " -r #{identifier.to_i}" if identifier
|
||||||
cmd << " #{target(path)}"
|
cmd << " #{target(path)}"
|
||||||
blame = Annotate.new
|
blame = Annotate.new
|
||||||
|
@ -153,45 +167,35 @@ module Redmine
|
||||||
blame
|
blame
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
# The hg version version is expressed either as a
|
||||||
|
# release number (eg 0.9.5 or 1.0) or as a revision
|
||||||
# Builds a revision objet from the changeset returned by hg command
|
# id composed of 12 hexa characters.
|
||||||
def build_revision_from_changeset(changeset)
|
def hgversion
|
||||||
rev_id = changeset[:changeset].to_s.split(':').first.to_i
|
theversion = hgversion_from_command_line
|
||||||
|
if theversion.match(/^\d+(\.\d+)+/)
|
||||||
# Changes
|
theversion.split(".").collect(&:to_i)
|
||||||
paths = (rev_id == 0) ?
|
# elsif match = theversion.match(/[[:xdigit:]]{12}/)
|
||||||
# Can't get changes for revision 0 with hg status
|
# match[0]
|
||||||
changeset[:files].to_s.split.collect{|path| {:action => 'A', :path => "/#{path}"}} :
|
else
|
||||||
status(rev_id)
|
"Unknown version"
|
||||||
|
end
|
||||||
Revision.new({:identifier => rev_id,
|
|
||||||
:scmid => changeset[:changeset].to_s.split(':').last,
|
|
||||||
:author => changeset[:user],
|
|
||||||
:time => Time.parse(changeset[:date]),
|
|
||||||
:message => changeset[:description],
|
|
||||||
:paths => paths
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the file changes for a given revision
|
def template_path
|
||||||
def status(rev_id)
|
@template ||= begin
|
||||||
cmd = "#{HG_BIN} -R #{target('')} status --rev #{rev_id.to_i - 1}:#{rev_id.to_i}"
|
if hgversion.is_a?(String) or ((hgversion <=> [0,9,5]) > 0)
|
||||||
result = []
|
ver = "1.0"
|
||||||
shellout(cmd) do |io|
|
else
|
||||||
io.each_line do |line|
|
ver = "0.9.5"
|
||||||
action, file = line.chomp.split
|
end
|
||||||
next unless action && file
|
"#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
|
||||||
file.gsub!("\\", "/")
|
end
|
||||||
case action
|
end
|
||||||
when 'R'
|
|
||||||
result << { :action => 'D' , :path => "/#{file}" }
|
private
|
||||||
else
|
|
||||||
result << { :action => action, :path => "/#{file}" }
|
def hgversion_from_command_line
|
||||||
end
|
@hgversion ||= %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
|
||||||
end
|
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
require File.dirname(__FILE__) + '/../test_helper'
|
||||||
|
begin
|
||||||
|
require 'mocha'
|
||||||
|
|
||||||
|
class MercurialAdapterTest < Test::Unit::TestCase
|
||||||
|
|
||||||
|
TEMPLATES_DIR = "#{RAILS_ROOT}/extra/mercurial"
|
||||||
|
TEMPLATE_NAME = "hg-template"
|
||||||
|
TEMPLATE_EXTENSION = "tmpl"
|
||||||
|
|
||||||
|
REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
|
||||||
|
|
||||||
|
|
||||||
|
def test_version_template_0_9_5
|
||||||
|
# 0.9.5
|
||||||
|
test_version_template_for("0.9.5", [0,9,5], "0.9.5")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_version_template_1_0
|
||||||
|
# 1.0
|
||||||
|
test_version_template_for("1.0", [1,0], "1.0")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_version_template_1_0_win
|
||||||
|
test_version_template_for("1e4ddc9ac9f7+20080325", "Unknown version", "1.0")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_version_template_1_0_1_win
|
||||||
|
test_version_template_for("1.0.1+20080525", [1,0,1], "1.0")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_version_template_changeset_id
|
||||||
|
test_version_template_for("1916e629a29d", "Unknown version", "1.0")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def test_version_template_for(hgversion, version, templateversion)
|
||||||
|
Redmine::Scm::Adapters::MercurialAdapter.any_instance.stubs(:hgversion_from_command_line).returns(hgversion)
|
||||||
|
adapter = Redmine::Scm::Adapters::MercurialAdapter.new(REPOSITORY_PATH)
|
||||||
|
assert_equal version, adapter.hgversion
|
||||||
|
assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{templateversion}.#{TEMPLATE_EXTENSION}", adapter.template_path
|
||||||
|
assert File.exist?(adapter.template_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
rescue LoadError
|
||||||
|
def test_fake; assert(false, "Requires mocha to run those tests") end
|
||||||
|
end
|
|
@ -48,6 +48,26 @@ class RepositoryMercurialTest < Test::Unit::TestCase
|
||||||
@repository.fetch_changesets
|
@repository.fetch_changesets
|
||||||
assert_equal 6, @repository.changesets.count
|
assert_equal 6, @repository.changesets.count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_entries
|
||||||
|
assert_equal 2, @repository.entries("sources", 2).size
|
||||||
|
assert_equal 1, @repository.entries("sources", 3).size
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_locate_on_outdated_repository
|
||||||
|
# Change the working dir state
|
||||||
|
%x{hg -R #{REPOSITORY_PATH} up -r 0}
|
||||||
|
assert_equal 1, @repository.entries("images", 0).size
|
||||||
|
assert_equal 2, @repository.entries("images").size
|
||||||
|
assert_equal 2, @repository.entries("images", 2).size
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def test_cat
|
||||||
|
assert @repository.scm.cat("sources/welcome_controller.rb", 2)
|
||||||
|
assert_nil @repository.scm.cat("sources/welcome_controller.rb")
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
|
puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
|
||||||
def test_fake; assert true end
|
def test_fake; assert true end
|
||||||
|
|
Loading…
Reference in New Issue