##// END OF EJS Templates
Adds MercurialAdapter.client_version and prevent @hg --version@ to be called on each request....
Jean-Philippe Lang -
r1614:29fb8db9362c
parent child
Show More
@@ -28,6 +28,39 module Redmine
28 TEMPLATE_NAME = "hg-template"
28 TEMPLATE_NAME = "hg-template"
29 TEMPLATE_EXTENSION = "tmpl"
29 TEMPLATE_EXTENSION = "tmpl"
30
30
31 class << self
32 def client_version
33 @@client_version ||= (hgversion || 'Unknown version')
34 end
35
36 def hgversion
37 # The hg version is expressed either as a
38 # release number (eg 0.9.5 or 1.0) or as a revision
39 # id composed of 12 hexa characters.
40 theversion = hgversion_from_command_line
41 if theversion.match(/^\d+(\.\d+)+/)
42 theversion.split(".").collect(&:to_i)
43 end
44 end
45
46 def hgversion_from_command_line
47 %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
48 end
49
50 def template_path
51 @@template_path ||= template_path_for(client_version)
52 end
53
54 def template_path_for(version)
55 if version.is_a?(String) or ((version <=> [0,9,5]) > 0)
56 ver = "1.0"
57 else
58 ver = "0.9.5"
59 end
60 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
61 end
62 end
63
31 def info
64 def info
32 cmd = "#{HG_BIN} -R #{target('')} root"
65 cmd = "#{HG_BIN} -R #{target('')} root"
33 root_url = nil
66 root_url = nil
@@ -72,7 +105,7 module Redmine
72 # makes Mercurial produce a xml output.
105 # makes Mercurial produce a xml output.
73 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
106 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
74 revisions = Revisions.new
107 revisions = Revisions.new
75 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.template_path}"
108 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}"
76 if identifier_from && identifier_to
109 if identifier_from && identifier_to
77 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
110 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
78 elsif identifier_from
111 elsif identifier_from
@@ -166,35 +199,6 module Redmine
166 return nil if $? && $?.exitstatus != 0
199 return nil if $? && $?.exitstatus != 0
167 blame
200 blame
168 end
201 end
169
170 # The hg version version is expressed either as a
171 # release number (eg 0.9.5 or 1.0) or as a revision
172 # id composed of 12 hexa characters.
173 def hgversion
174 theversion = hgversion_from_command_line
175 if theversion.match(/^\d+(\.\d+)+/)
176 theversion.split(".").collect(&:to_i)
177 # elsif match = theversion.match(/[[:xdigit:]]{12}/)
178 # match[0]
179 else
180 "Unknown version"
181 end
182 end
183
184 def template_path
185 @template ||= begin
186 if hgversion.is_a?(String) or ((hgversion <=> [0,9,5]) > 0)
187 ver = "1.0"
188 else
189 ver = "0.9.5"
190 end
191 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
192 end
193 end
194
195 def hgversion_from_command_line
196 @hgversion ||= %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
197 end
198 end
202 end
199 end
203 end
200 end
204 end
@@ -10,37 +10,41 begin
10
10
11 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
11 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
12
12
13
13 def test_hgversion
14 def test_version_template_0_9_5
14 to_test = { "0.9.5" => [0,9,5],
15 # 0.9.5
15 "1.0" => [1,0],
16 test_version_template_for("0.9.5", [0,9,5], "0.9.5")
16 "1e4ddc9ac9f7+20080325" => nil,
17 end
17 "1.0.1+20080525" => [1,0,1],
18
18 "1916e629a29d" => nil}
19 def test_version_template_1_0
19
20 # 1.0
20 to_test.each do |s, v|
21 test_version_template_for("1.0", [1,0], "1.0")
21 test_hgversion_for(s, v)
22 end
22 end
23
24 def test_version_template_1_0_win
25 test_version_template_for("1e4ddc9ac9f7+20080325", "Unknown version", "1.0")
26 end
23 end
27
24
28 def test_version_template_1_0_1_win
25 def test_template_path
29 test_version_template_for("1.0.1+20080525", [1,0,1], "1.0")
26 to_test = { [0,9,5] => "0.9.5",
30 end
27 [1,0] => "1.0",
31
28 "Unknown version" => "1.0",
32 def test_version_template_changeset_id
29 [1,0,1] => "1.0"}
33 test_version_template_for("1916e629a29d", "Unknown version", "1.0")
30
31 to_test.each do |v, template|
32 test_template_path_for(v, template)
33 end
34 end
34 end
35
35
36 private
36 private
37
37
38 def test_version_template_for(hgversion, version, templateversion)
38 def test_hgversion_for(hgversion, version)
39 Redmine::Scm::Adapters::MercurialAdapter.any_instance.stubs(:hgversion_from_command_line).returns(hgversion)
39 Redmine::Scm::Adapters::MercurialAdapter.expects(:hgversion_from_command_line).returns(hgversion)
40 adapter = Redmine::Scm::Adapters::MercurialAdapter.new(REPOSITORY_PATH)
40 adapter = Redmine::Scm::Adapters::MercurialAdapter
41 assert_equal version, adapter.hgversion
41 assert_equal version, adapter.hgversion
42 assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{templateversion}.#{TEMPLATE_EXTENSION}", adapter.template_path
42 end
43 assert File.exist?(adapter.template_path)
43
44 def test_template_path_for(version, template)
45 adapter = Redmine::Scm::Adapters::MercurialAdapter
46 assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}", adapter.template_path_for(version)
47 assert File.exist?(adapter.template_path_for(version))
44 end
48 end
45 end
49 end
46
50
General Comments 0
You need to be logged in to leave comments. Login now