Adds MercurialAdapter.client_version and prevent @hg --version@ to be called on each request.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1628 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-07-05 09:39:00 +00:00
parent 12fbd06c02
commit 29fb8db936
2 changed files with 62 additions and 54 deletions

View File

@ -28,6 +28,39 @@ module Redmine
TEMPLATE_NAME = "hg-template" TEMPLATE_NAME = "hg-template"
TEMPLATE_EXTENSION = "tmpl" TEMPLATE_EXTENSION = "tmpl"
class << self
def client_version
@@client_version ||= (hgversion || 'Unknown version')
end
def hgversion
# The hg version is expressed either as a
# release number (eg 0.9.5 or 1.0) or as a revision
# id composed of 12 hexa characters.
theversion = hgversion_from_command_line
if theversion.match(/^\d+(\.\d+)+/)
theversion.split(".").collect(&:to_i)
end
end
def hgversion_from_command_line
%x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
end
def template_path
@@template_path ||= template_path_for(client_version)
end
def template_path_for(version)
if version.is_a?(String) or ((version <=> [0,9,5]) > 0)
ver = "1.0"
else
ver = "0.9.5"
end
"#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
end
end
def info def info
cmd = "#{HG_BIN} -R #{target('')} root" cmd = "#{HG_BIN} -R #{target('')} root"
root_url = nil root_url = nil
@ -72,7 +105,7 @@ module Redmine
# makes Mercurial produce a xml output. # makes Mercurial produce a xml output.
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
revisions = Revisions.new revisions = Revisions.new
cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.template_path}" cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.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
@ -166,35 +199,6 @@ module Redmine
return nil if $? && $?.exitstatus != 0 return nil if $? && $?.exitstatus != 0
blame blame
end end
# The hg version version is expressed either as a
# release number (eg 0.9.5 or 1.0) or as a revision
# id composed of 12 hexa characters.
def hgversion
theversion = hgversion_from_command_line
if theversion.match(/^\d+(\.\d+)+/)
theversion.split(".").collect(&:to_i)
# elsif match = theversion.match(/[[:xdigit:]]{12}/)
# match[0]
else
"Unknown version"
end
end
def template_path
@template ||= begin
if hgversion.is_a?(String) or ((hgversion <=> [0,9,5]) > 0)
ver = "1.0"
else
ver = "0.9.5"
end
"#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
end
end
def hgversion_from_command_line
@hgversion ||= %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
end
end end
end end
end end

View File

@ -10,37 +10,41 @@ begin
REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository' REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
def test_hgversion
to_test = { "0.9.5" => [0,9,5],
"1.0" => [1,0],
"1e4ddc9ac9f7+20080325" => nil,
"1.0.1+20080525" => [1,0,1],
"1916e629a29d" => nil}
def test_version_template_0_9_5 to_test.each do |s, v|
# 0.9.5 test_hgversion_for(s, v)
test_version_template_for("0.9.5", [0,9,5], "0.9.5") end
end end
def test_version_template_1_0 def test_template_path
# 1.0 to_test = { [0,9,5] => "0.9.5",
test_version_template_for("1.0", [1,0], "1.0") [1,0] => "1.0",
end "Unknown version" => "1.0",
[1,0,1] => "1.0"}
def test_version_template_1_0_win to_test.each do |v, template|
test_version_template_for("1e4ddc9ac9f7+20080325", "Unknown version", "1.0") test_template_path_for(v, template)
end 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 end
private private
def test_version_template_for(hgversion, version, templateversion) def test_hgversion_for(hgversion, version)
Redmine::Scm::Adapters::MercurialAdapter.any_instance.stubs(:hgversion_from_command_line).returns(hgversion) Redmine::Scm::Adapters::MercurialAdapter.expects(:hgversion_from_command_line).returns(hgversion)
adapter = Redmine::Scm::Adapters::MercurialAdapter.new(REPOSITORY_PATH) adapter = Redmine::Scm::Adapters::MercurialAdapter
assert_equal version, adapter.hgversion assert_equal version, adapter.hgversion
assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{templateversion}.#{TEMPLATE_EXTENSION}", adapter.template_path end
assert File.exist?(adapter.template_path)
def test_template_path_for(version, template)
adapter = Redmine::Scm::Adapters::MercurialAdapter
assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}", adapter.template_path_for(version)
assert File.exist?(adapter.template_path_for(version))
end end
end end