##// END OF EJS Templates
scm: mercurial: change diff option from --git to --config diff.git=false (#7253)....
Toshi MARUYAMA -
r4568:80b329d031dc
parent child
Show More
@@ -1,225 +1,225
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 require 'cgi'
20 20
21 21 module Redmine
22 22 module Scm
23 23 module Adapters
24 24 class MercurialAdapter < AbstractAdapter
25 25
26 26 # Mercurial executable name
27 27 HG_BIN = "hg"
28 28 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
29 29 TEMPLATE_NAME = "hg-template"
30 30 TEMPLATE_EXTENSION = "tmpl"
31 31
32 32 class << self
33 33 def client_version
34 34 @@client_version ||= (hgversion || [])
35 35 end
36 36
37 37 def hgversion
38 38 # The hg version is expressed either as a
39 39 # release number (eg 0.9.5 or 1.0) or as a revision
40 40 # id composed of 12 hexa characters.
41 41 theversion = hgversion_from_command_line
42 42 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
43 43 m[2].scan(%r{\d+}).collect(&:to_i)
44 44 end
45 45 end
46 46
47 47 def hgversion_from_command_line
48 48 shellout("#{HG_BIN} --version") { |io| io.read }.to_s
49 49 end
50 50
51 51 def template_path
52 52 @@template_path ||= template_path_for(client_version)
53 53 end
54 54
55 55 def template_path_for(version)
56 56 if ((version <=> [0,9,5]) > 0) || version.empty?
57 57 ver = "1.0"
58 58 else
59 59 ver = "0.9.5"
60 60 end
61 61 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
62 62 end
63 63 end
64 64
65 65 def info
66 66 cmd = "#{HG_BIN} -R #{target('')} root"
67 67 root_url = nil
68 68 shellout(cmd) do |io|
69 69 root_url = io.read
70 70 end
71 71 return nil if $? && $?.exitstatus != 0
72 72 info = Info.new({:root_url => root_url.chomp,
73 73 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
74 74 })
75 75 info
76 76 rescue CommandFailed
77 77 return nil
78 78 end
79 79
80 80 def entries(path=nil, identifier=nil)
81 81 path ||= ''
82 82 entries = Entries.new
83 83 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
84 84 cmd << " -r #{hgrev(identifier)}"
85 85 cmd << " " + shell_quote("path:#{path}") unless path.empty?
86 86 shellout(cmd) do |io|
87 87 io.each_line do |line|
88 88 # HG uses antislashs as separator on Windows
89 89 line = line.gsub(/\\/, "/")
90 90 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
91 91 e ||= line
92 92 e = e.chomp.split(%r{[\/\\]})
93 93 entries << Entry.new({:name => e.first,
94 94 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
95 95 :kind => (e.size > 1 ? 'dir' : 'file'),
96 96 :lastrev => Revision.new
97 97 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
98 98 end
99 99 end
100 100 end
101 101 return nil if $? && $?.exitstatus != 0
102 102 entries.sort_by_name
103 103 end
104 104
105 105 # Fetch the revisions by using a template file that
106 106 # makes Mercurial produce a xml output.
107 107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
108 108 revisions = Revisions.new
109 109 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
110 110 if identifier_from && identifier_to
111 111 cmd << " -r #{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
112 112 elsif identifier_from
113 113 cmd << " -r #{hgrev(identifier_from)}:"
114 114 end
115 115 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
116 116 cmd << " #{shell_quote path}" unless path.blank?
117 117 shellout(cmd) do |io|
118 118 begin
119 119 # HG doesn't close the XML Document...
120 120 doc = REXML::Document.new(io.read << "</log>")
121 121 doc.elements.each("log/logentry") do |logentry|
122 122 paths = []
123 123 copies = logentry.get_elements('paths/path-copied')
124 124 logentry.elements.each("paths/path") do |path|
125 125 # Detect if the added file is a copy
126 126 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
127 127 from_path = c.attributes['copyfrom-path']
128 128 from_rev = logentry.attributes['revision']
129 129 end
130 130 paths << {:action => path.attributes['action'],
131 131 :path => "/#{CGI.unescape(path.text)}",
132 132 :from_path => from_path ? "/#{CGI.unescape(from_path)}" : nil,
133 133 :from_revision => from_rev ? from_rev : nil
134 134 }
135 135 end
136 136 paths.sort! { |x,y| x[:path] <=> y[:path] }
137 137
138 138 revisions << Revision.new({:identifier => logentry.attributes['revision'],
139 139 :scmid => logentry.attributes['node'],
140 140 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
141 141 :time => Time.parse(logentry.elements['date'].text).localtime,
142 142 :message => logentry.elements['msg'].text,
143 143 :paths => paths
144 144 })
145 145 end
146 146 rescue
147 147 logger.debug($!)
148 148 end
149 149 end
150 150 return nil if $? && $?.exitstatus != 0
151 151 revisions
152 152 end
153 153
154 154 def diff(path, identifier_from, identifier_to=nil)
155 155 path ||= ''
156 156 diff_args = ''
157 157 diff = []
158 158 if identifier_to
159 159 diff_args = "-r #{hgrev(identifier_to)} -r #{hgrev(identifier_from)}"
160 160 else
161 161 if self.class.client_version_above?([1, 2])
162 162 diff_args = "-c #{hgrev(identifier_from)}"
163 163 else
164 164 return []
165 165 end
166 166 end
167 cmd = "#{HG_BIN} -R #{target('')} diff --nodates --git #{diff_args}"
167 cmd = "#{HG_BIN} -R #{target('')} --config diff.git=false diff --nodates #{diff_args}"
168 168 cmd << " -I #{target(path)}" unless path.empty?
169 169 shellout(cmd) do |io|
170 170 io.each_line do |line|
171 171 diff << line
172 172 end
173 173 end
174 174 return nil if $? && $?.exitstatus != 0
175 175 diff
176 176 end
177 177
178 178 def cat(path, identifier=nil)
179 179 cmd = "#{HG_BIN} -R #{target('')} cat"
180 180 cmd << " -r #{hgrev(identifier)}"
181 181 cmd << " #{target(path)}"
182 182 cat = nil
183 183 shellout(cmd) do |io|
184 184 io.binmode
185 185 cat = io.read
186 186 end
187 187 return nil if $? && $?.exitstatus != 0
188 188 cat
189 189 end
190 190
191 191 def annotate(path, identifier=nil)
192 192 path ||= ''
193 193 cmd = "#{HG_BIN} -R #{target('')}"
194 194 cmd << " annotate -ncu"
195 195 cmd << " -r #{hgrev(identifier)}"
196 196 cmd << " #{target(path)}"
197 197 blame = Annotate.new
198 198 shellout(cmd) do |io|
199 199 io.each_line do |line|
200 200 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
201 201 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
202 202 :identifier => $3)
203 203 blame.add_line($4.rstrip, r)
204 204 end
205 205 end
206 206 return nil if $? && $?.exitstatus != 0
207 207 blame
208 208 end
209 209
210 210 class Revision < Redmine::Scm::Adapters::Revision
211 211 # Returns the readable identifier
212 212 def format_identifier
213 213 "#{revision}:#{scmid}"
214 214 end
215 215 end
216 216
217 217 # Returns correct revision identifier
218 218 def hgrev(identifier)
219 219 shell_quote(identifier.blank? ? 'tip' : identifier.to_s)
220 220 end
221 221 private :hgrev
222 222 end
223 223 end
224 224 end
225 225 end
@@ -1,130 +1,130
1 1 require File.expand_path('../../../../../../test_helper', __FILE__)
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 if File.directory?(REPOSITORY_PATH)
14 14 def setup
15 15 @adapter = Redmine::Scm::Adapters::MercurialAdapter.new(REPOSITORY_PATH)
16 16 end
17 17
18 18 def test_hgversion
19 19 to_test = { "Mercurial Distributed SCM (version 0.9.5)\n" => [0,9,5],
20 20 "Mercurial Distributed SCM (1.0)\n" => [1,0],
21 21 "Mercurial Distributed SCM (1e4ddc9ac9f7+20080325)\n" => nil,
22 22 "Mercurial Distributed SCM (1.0.1+20080525)\n" => [1,0,1],
23 23 "Mercurial Distributed SCM (1916e629a29d)\n" => nil,
24 24 "Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5],
25 25 "(1.6)\n(1.7)\n(1.8)" => [1,6],
26 26 "(1.7.1)\r\n(1.8.1)\r\n(1.9.1)" => [1,7,1]}
27 27
28 28 to_test.each do |s, v|
29 29 test_hgversion_for(s, v)
30 30 end
31 31 end
32 32
33 33 def test_template_path
34 34 to_test = { [0,9,5] => "0.9.5",
35 35 [1,0] => "1.0",
36 36 [] => "1.0",
37 37 [1,0,1] => "1.0",
38 38 [1,7] => "1.0",
39 39 [1,7,1] => "1.0" }
40 40 to_test.each do |v, template|
41 41 test_template_path_for(v, template)
42 42 end
43 43 end
44 44
45 45 def test_diff
46 46 if @adapter.class.client_version_above?([1, 2])
47 47 assert_nil @adapter.diff(nil, '100000')
48 48 end
49 49 assert_nil @adapter.diff(nil, '100000', '200000')
50 50 [2, '400bb8672109', '400', 400].each do |r1|
51 51 diff1 = @adapter.diff(nil, r1)
52 52 if @adapter.class.client_version_above?([1, 2])
53 53 assert_equal 28, diff1.size
54 54 buf = diff1[24].gsub(/\r\n|\r|\n/, "")
55 55 assert_equal "+ return true unless klass.respond_to?('watched_by')", buf
56 56 else
57 57 assert_equal 0, diff1.size
58 58 end
59 59 [4, 'def6d2f1254a'].each do |r2|
60 60 diff2 = @adapter.diff(nil,r1,r2)
61 assert_equal 50, diff2.size
62 buf = diff2[42].gsub(/\r\n|\r|\n/, "")
61 assert_equal 49, diff2.size
62 buf = diff2[41].gsub(/\r\n|\r|\n/, "")
63 63 assert_equal "+class WelcomeController < ApplicationController", buf
64 64 diff3 = @adapter.diff('sources/watchers_controller.rb', r1, r2)
65 65 assert_equal 20, diff3.size
66 66 buf = diff3[12].gsub(/\r\n|\r|\n/, "")
67 67 assert_equal "+ @watched.remove_watcher(user)", buf
68 68 end
69 69 end
70 70 end
71 71
72 72 def test_cat
73 73 [2, '400bb8672109', '400', 400].each do |r|
74 74 buf = @adapter.cat('sources/welcome_controller.rb', r)
75 75 assert buf
76 76 lines = buf.split("\r\n")
77 77 assert_equal 25, lines.length
78 78 assert_equal 'class WelcomeController < ApplicationController', lines[17]
79 79 end
80 80 assert_nil @adapter.cat('sources/welcome_controller.rb')
81 81 end
82 82
83 83 def test_annotate
84 84 assert_equal [], @adapter.annotate("sources/welcome_controller.rb").lines
85 85 [2, '400bb8672109', '400', 400].each do |r|
86 86 ann = @adapter.annotate('sources/welcome_controller.rb', r)
87 87 assert ann
88 88 assert_equal '1', ann.revisions[17].revision
89 89 assert_equal '9d5b5b004199', ann.revisions[17].identifier
90 90 assert_equal 'jsmith', ann.revisions[0].author
91 91 assert_equal 25, ann.lines.length
92 92 assert_equal 'class WelcomeController < ApplicationController', ann.lines[17]
93 93 end
94 94 end
95 95
96 96 def test_access_by_nodeid
97 97 path = 'sources/welcome_controller.rb'
98 98 assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400bb8672109')
99 99 end
100 100
101 101 def test_access_by_fuzzy_nodeid
102 102 path = 'sources/welcome_controller.rb'
103 103 # falls back to nodeid
104 104 assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400')
105 105 end
106 106
107 107 private
108 108
109 109 def test_hgversion_for(hgversion, version)
110 110 @adapter.class.expects(:hgversion_from_command_line).returns(hgversion)
111 111 assert_equal version, @adapter.class.hgversion
112 112 end
113 113
114 114 def test_template_path_for(version, template)
115 115 assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
116 116 @adapter.class.template_path_for(version)
117 117 assert File.exist?(@adapter.class.template_path_for(version))
118 118 end
119 119 else
120 120 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
121 121 def test_fake; assert true end
122 122 end
123 123 end
124 124
125 125 rescue LoadError
126 126 class MercurialMochaFake < ActiveSupport::TestCase
127 127 def test_fake; assert(false, "Requires mocha to run those tests") end
128 128 end
129 129 end
130 130
General Comments 0
You need to be logged in to leave comments. Login now