##// END OF EJS Templates
Fixed: r4417 breaks MercurialAdapter with ruby 1.8.6 (#5117)....
Jean-Philippe Lang -
r4308:cfc3ee4f5a54
parent child
Show More
@@ -1,205 +1,205
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/abstract_adapter'
19 19
20 20 module Redmine
21 21 module Scm
22 22 module Adapters
23 23 class MercurialAdapter < AbstractAdapter
24 24
25 25 # Mercurial executable name
26 26 HG_BIN = "hg"
27 27 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
28 28 TEMPLATE_NAME = "hg-template"
29 29 TEMPLATE_EXTENSION = "tmpl"
30 30
31 31 class << self
32 32 def client_version
33 33 @@client_version ||= (hgversion || [])
34 34 end
35 35
36 36 def hgversion
37 37 # The hg version is expressed either as a
38 38 # release number (eg 0.9.5 or 1.0) or as a revision
39 39 # id composed of 12 hexa characters.
40 40 theversion = hgversion_from_command_line
41 if m = theversion.match(/\b\d+(\.\d+)+\b/)
42 m[0].split(".").collect(&:to_i)
41 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
42 m[2].scan(%r{\d+}).collect(&:to_i)
43 43 end
44 44 end
45 45
46 46 def hgversion_from_command_line
47 %x{#{HG_BIN} --version}.lines.first.to_s
47 shellout("#{HG_BIN} --version") { |io| io.read }.to_s
48 48 end
49 49
50 50 def template_path
51 51 @@template_path ||= template_path_for(client_version)
52 52 end
53 53
54 54 def template_path_for(version)
55 55 if ((version <=> [0,9,5]) > 0) || version.empty?
56 56 ver = "1.0"
57 57 else
58 58 ver = "0.9.5"
59 59 end
60 60 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
61 61 end
62 62 end
63 63
64 64 def info
65 65 cmd = "#{HG_BIN} -R #{target('')} root"
66 66 root_url = nil
67 67 shellout(cmd) do |io|
68 68 root_url = io.read
69 69 end
70 70 return nil if $? && $?.exitstatus != 0
71 71 info = Info.new({:root_url => root_url.chomp,
72 72 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
73 73 })
74 74 info
75 75 rescue CommandFailed
76 76 return nil
77 77 end
78 78
79 79 def entries(path=nil, identifier=nil)
80 80 path ||= ''
81 81 entries = Entries.new
82 82 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
83 83 cmd << " -r " + (identifier ? identifier.to_s : "tip")
84 84 cmd << " " + shell_quote("path:#{path}") unless path.empty?
85 85 shellout(cmd) do |io|
86 86 io.each_line do |line|
87 87 # HG uses antislashs as separator on Windows
88 88 line = line.gsub(/\\/, "/")
89 89 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
90 90 e ||= line
91 91 e = e.chomp.split(%r{[\/\\]})
92 92 entries << Entry.new({:name => e.first,
93 93 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
94 94 :kind => (e.size > 1 ? 'dir' : 'file'),
95 95 :lastrev => Revision.new
96 96 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
97 97 end
98 98 end
99 99 end
100 100 return nil if $? && $?.exitstatus != 0
101 101 entries.sort_by_name
102 102 end
103 103
104 104 # Fetch the revisions by using a template file that
105 105 # makes Mercurial produce a xml output.
106 106 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
107 107 revisions = Revisions.new
108 108 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
109 109 if identifier_from && identifier_to
110 110 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
111 111 elsif identifier_from
112 112 cmd << " -r #{identifier_from.to_i}:"
113 113 end
114 114 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
115 115 cmd << " #{path}" if path
116 116 shellout(cmd) do |io|
117 117 begin
118 118 # HG doesn't close the XML Document...
119 119 doc = REXML::Document.new(io.read << "</log>")
120 120 doc.elements.each("log/logentry") do |logentry|
121 121 paths = []
122 122 copies = logentry.get_elements('paths/path-copied')
123 123 logentry.elements.each("paths/path") do |path|
124 124 # Detect if the added file is a copy
125 125 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
126 126 from_path = c.attributes['copyfrom-path']
127 127 from_rev = logentry.attributes['revision']
128 128 end
129 129 paths << {:action => path.attributes['action'],
130 130 :path => "/#{path.text}",
131 131 :from_path => from_path ? "/#{from_path}" : nil,
132 132 :from_revision => from_rev ? from_rev : nil
133 133 }
134 134 end
135 135 paths.sort! { |x,y| x[:path] <=> y[:path] }
136 136
137 137 revisions << Revision.new({:identifier => logentry.attributes['revision'],
138 138 :scmid => logentry.attributes['node'],
139 139 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
140 140 :time => Time.parse(logentry.elements['date'].text).localtime,
141 141 :message => logentry.elements['msg'].text,
142 142 :paths => paths
143 143 })
144 144 end
145 145 rescue
146 146 logger.debug($!)
147 147 end
148 148 end
149 149 return nil if $? && $?.exitstatus != 0
150 150 revisions
151 151 end
152 152
153 153 def diff(path, identifier_from, identifier_to=nil)
154 154 path ||= ''
155 155 if identifier_to
156 156 identifier_to = identifier_to.to_i
157 157 else
158 158 identifier_to = identifier_from.to_i - 1
159 159 end
160 160 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
161 161 cmd << " -I #{target(path)}" unless path.empty?
162 162 diff = []
163 163 shellout(cmd) do |io|
164 164 io.each_line do |line|
165 165 diff << line
166 166 end
167 167 end
168 168 return nil if $? && $?.exitstatus != 0
169 169 diff
170 170 end
171 171
172 172 def cat(path, identifier=nil)
173 173 cmd = "#{HG_BIN} -R #{target('')} cat"
174 174 cmd << " -r " + (identifier ? identifier.to_s : "tip")
175 175 cmd << " #{target(path)}"
176 176 cat = nil
177 177 shellout(cmd) do |io|
178 178 io.binmode
179 179 cat = io.read
180 180 end
181 181 return nil if $? && $?.exitstatus != 0
182 182 cat
183 183 end
184 184
185 185 def annotate(path, identifier=nil)
186 186 path ||= ''
187 187 cmd = "#{HG_BIN} -R #{target('')}"
188 188 cmd << " annotate -n -u"
189 189 cmd << " -r " + (identifier ? identifier.to_s : "tip")
190 190 cmd << " -r #{identifier.to_i}" if identifier
191 191 cmd << " #{target(path)}"
192 192 blame = Annotate.new
193 193 shellout(cmd) do |io|
194 194 io.each_line do |line|
195 195 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
196 196 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
197 197 end
198 198 end
199 199 return nil if $? && $?.exitstatus != 0
200 200 blame
201 201 end
202 202 end
203 203 end
204 204 end
205 205 end
@@ -1,54 +1,60
1 1 require File.dirname(__FILE__) + '/../../../../../test_helper'
2 2 begin
3 3 require 'mocha'
4 4
5 5 class MercurialAdapterTest < ActiveSupport::TestCase
6 6
7 7 TEMPLATES_DIR = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATES_DIR
8 8 TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME
9 9 TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
10 10
11 11 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
12 12
13 13 def test_hgversion
14 14 to_test = { "Mercurial Distributed SCM (version 0.9.5)\n" => [0,9,5],
15 15 "Mercurial Distributed SCM (1.0)\n" => [1,0],
16 16 "Mercurial Distributed SCM (1e4ddc9ac9f7+20080325)\n" => nil,
17 17 "Mercurial Distributed SCM (1.0.1+20080525)\n" => [1,0,1],
18 18 "Mercurial Distributed SCM (1916e629a29d)\n" => nil,
19 "Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5]}
19 "Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5],
20 "(1.6)\n(1.7)\n(1.8)" => [1,6],
21 "(1.7.1)\r\n(1.8.1)\r\n(1.9.1)" => [1,7,1]}
20 22
21 23 to_test.each do |s, v|
22 24 test_hgversion_for(s, v)
23 25 end
24 26 end
25 27
26 28 def test_template_path
27 29 to_test = { [0,9,5] => "0.9.5",
28 30 [1,0] => "1.0",
29 31 [] => "1.0",
30 [1,0,1] => "1.0"}
31
32 [1,0,1] => "1.0",
33 [1,7] => "1.0",
34 [1,7,1] => "1.0"}
32 35 to_test.each do |v, template|
33 36 test_template_path_for(v, template)
34 37 end
35 38 end
36 39
37 40 private
38 41
39 42 def test_hgversion_for(hgversion, version)
40 43 Redmine::Scm::Adapters::MercurialAdapter.expects(:hgversion_from_command_line).returns(hgversion)
41 44 adapter = Redmine::Scm::Adapters::MercurialAdapter
42 45 assert_equal version, adapter.hgversion
43 46 end
44 47
45 48 def test_template_path_for(version, template)
46 49 adapter = Redmine::Scm::Adapters::MercurialAdapter
47 50 assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}", adapter.template_path_for(version)
48 51 assert File.exist?(adapter.template_path_for(version))
49 52 end
50 53 end
51 54
52 55 rescue LoadError
56 class MercurialMochaFake < ActiveSupport::TestCase
53 57 def test_fake; assert(false, "Requires mocha to run those tests") end
54 58 end
59 end
60
General Comments 0
You need to be logged in to leave comments. Login now