##// END OF EJS Templates
scm: mercurial: fix Ruby 1.9 "hg diff" test fails (#7518)....
Toshi MARUYAMA -
r4739:7a3d385b8e53
parent child
Show More
@@ -1,249 +1,252
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'redmine/scm/adapters/abstract_adapter'
18 require 'redmine/scm/adapters/abstract_adapter'
19 require 'cgi'
19 require 'cgi'
20
20
21 module Redmine
21 module Redmine
22 module Scm
22 module Scm
23 module Adapters
23 module Adapters
24 class MercurialAdapter < AbstractAdapter
24 class MercurialAdapter < AbstractAdapter
25
25
26 # Mercurial executable name
26 # Mercurial executable name
27 HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
27 HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
28 HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
28 HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
29 HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
29 HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
30 TEMPLATE_NAME = "hg-template"
30 TEMPLATE_NAME = "hg-template"
31 TEMPLATE_EXTENSION = "tmpl"
31 TEMPLATE_EXTENSION = "tmpl"
32
32
33 # raised if hg command exited with error, e.g. unknown revision.
33 # raised if hg command exited with error, e.g. unknown revision.
34 class HgCommandAborted < CommandFailed; end
34 class HgCommandAborted < CommandFailed; end
35
35
36 class << self
36 class << self
37 def client_command
37 def client_command
38 @@bin ||= HG_BIN
38 @@bin ||= HG_BIN
39 end
39 end
40
40
41 def sq_bin
41 def sq_bin
42 @@sq_bin ||= shell_quote(HG_BIN)
42 @@sq_bin ||= shell_quote(HG_BIN)
43 end
43 end
44
44
45 def client_version
45 def client_version
46 @@client_version ||= (hgversion || [])
46 @@client_version ||= (hgversion || [])
47 end
47 end
48
48
49 def client_available
49 def client_available
50 !client_version.empty?
50 !client_version.empty?
51 end
51 end
52
52
53 def hgversion
53 def hgversion
54 # The hg version is expressed either as a
54 # The hg version is expressed either as a
55 # release number (eg 0.9.5 or 1.0) or as a revision
55 # release number (eg 0.9.5 or 1.0) or as a revision
56 # id composed of 12 hexa characters.
56 # id composed of 12 hexa characters.
57 theversion = hgversion_from_command_line
57 theversion = hgversion_from_command_line
58 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
58 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
59 m[2].scan(%r{\d+}).collect(&:to_i)
59 m[2].scan(%r{\d+}).collect(&:to_i)
60 end
60 end
61 end
61 end
62
62
63 def hgversion_from_command_line
63 def hgversion_from_command_line
64 shellout("#{sq_bin} --version") { |io| io.read }.to_s
64 shellout("#{sq_bin} --version") { |io| io.read }.to_s
65 end
65 end
66
66
67 def template_path
67 def template_path
68 @@template_path ||= template_path_for(client_version)
68 @@template_path ||= template_path_for(client_version)
69 end
69 end
70
70
71 def template_path_for(version)
71 def template_path_for(version)
72 if ((version <=> [0,9,5]) > 0) || version.empty?
72 if ((version <=> [0,9,5]) > 0) || version.empty?
73 ver = "1.0"
73 ver = "1.0"
74 else
74 else
75 ver = "0.9.5"
75 ver = "0.9.5"
76 end
76 end
77 "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
77 "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
78 end
78 end
79 end
79 end
80
80
81 def info
81 def info
82 tip = summary['repository']['tip']
82 tip = summary['repository']['tip']
83 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
83 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
84 :lastrev => Revision.new(:revision => tip['revision'],
84 :lastrev => Revision.new(:revision => tip['revision'],
85 :scmid => tip['node']))
85 :scmid => tip['node']))
86 end
86 end
87
87
88 def summary
88 def summary
89 @summary ||= hg 'rhsummary' do |io|
89 @summary ||= hg 'rhsummary' do |io|
90 ActiveSupport::XmlMini.parse(io.read)['rhsummary']
90 ActiveSupport::XmlMini.parse(io.read)['rhsummary']
91 end
91 end
92 end
92 end
93 private :summary
93 private :summary
94
94
95 def entries(path=nil, identifier=nil)
95 def entries(path=nil, identifier=nil)
96 manifest = hg('rhmanifest', '-r', hgrev(identifier),
96 manifest = hg('rhmanifest', '-r', hgrev(identifier),
97 CGI.escape(without_leading_slash(path.to_s))) do |io|
97 CGI.escape(without_leading_slash(path.to_s))) do |io|
98 ActiveSupport::XmlMini.parse(io.read)['rhmanifest']['repository']['manifest']
98 ActiveSupport::XmlMini.parse(io.read)['rhmanifest']['repository']['manifest']
99 end
99 end
100 path_prefix = path.blank? ? '' : with_trailling_slash(path)
100 path_prefix = path.blank? ? '' : with_trailling_slash(path)
101
101
102 entries = Entries.new
102 entries = Entries.new
103 as_ary(manifest['dir']).each do |e|
103 as_ary(manifest['dir']).each do |e|
104 n = CGI.unescape(e['name'])
104 n = CGI.unescape(e['name'])
105 p = "#{path_prefix}#{n}"
105 p = "#{path_prefix}#{n}"
106 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
106 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
107 end
107 end
108
108
109 as_ary(manifest['file']).each do |e|
109 as_ary(manifest['file']).each do |e|
110 n = CGI.unescape(e['name'])
110 n = CGI.unescape(e['name'])
111 p = "#{path_prefix}#{n}"
111 p = "#{path_prefix}#{n}"
112 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
112 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
113 :identifier => e['node'],
113 :identifier => e['node'],
114 :time => Time.at(e['time'].to_i))
114 :time => Time.at(e['time'].to_i))
115 entries << Entry.new(:name => n, :path => p, :kind => 'file',
115 entries << Entry.new(:name => n, :path => p, :kind => 'file',
116 :size => e['size'].to_i, :lastrev => lr)
116 :size => e['size'].to_i, :lastrev => lr)
117 end
117 end
118
118
119 entries
119 entries
120 rescue HgCommandAborted
120 rescue HgCommandAborted
121 nil # means not found
121 nil # means not found
122 end
122 end
123
123
124 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
124 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
125 revs = Revisions.new
125 revs = Revisions.new
126 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
126 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
127 revs
127 revs
128 end
128 end
129
129
130 # Iterates the revisions by using a template file that
130 # Iterates the revisions by using a template file that
131 # makes Mercurial produce a xml output.
131 # makes Mercurial produce a xml output.
132 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
132 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
133 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
133 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
134 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
134 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
135 hg_args << '--limit' << options[:limit] if options[:limit]
135 hg_args << '--limit' << options[:limit] if options[:limit]
136 hg_args << hgtarget(path) unless path.blank?
136 hg_args << hgtarget(path) unless path.blank?
137 log = hg(*hg_args) do |io|
137 log = hg(*hg_args) do |io|
138 # Mercurial < 1.5 does not support footer template for '</log>'
138 # Mercurial < 1.5 does not support footer template for '</log>'
139 ActiveSupport::XmlMini.parse("#{io.read}</log>")['log']
139 ActiveSupport::XmlMini.parse("#{io.read}</log>")['log']
140 end
140 end
141
141
142 as_ary(log['logentry']).each do |le|
142 as_ary(log['logentry']).each do |le|
143 cpalist = as_ary(le['paths']['path-copied']).map do |e|
143 cpalist = as_ary(le['paths']['path-copied']).map do |e|
144 [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) }
144 [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) }
145 end
145 end
146 cpmap = Hash[*cpalist.flatten]
146 cpmap = Hash[*cpalist.flatten]
147
147
148 paths = as_ary(le['paths']['path']).map do |e|
148 paths = as_ary(le['paths']['path']).map do |e|
149 p = CGI.unescape(e['__content__'])
149 p = CGI.unescape(e['__content__'])
150 {:action => e['action'], :path => with_leading_slash(p),
150 {:action => e['action'], :path => with_leading_slash(p),
151 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
151 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
152 :from_revision => (cpmap.member?(p) ? le['revision'] : nil)}
152 :from_revision => (cpmap.member?(p) ? le['revision'] : nil)}
153 end.sort { |a, b| a[:path] <=> b[:path] }
153 end.sort { |a, b| a[:path] <=> b[:path] }
154
154
155 yield Revision.new(:revision => le['revision'],
155 yield Revision.new(:revision => le['revision'],
156 :scmid => le['node'],
156 :scmid => le['node'],
157 :author => (le['author']['__content__'] rescue ''),
157 :author => (le['author']['__content__'] rescue ''),
158 :time => Time.parse(le['date']['__content__']).localtime,
158 :time => Time.parse(le['date']['__content__']).localtime,
159 :message => le['msg']['__content__'],
159 :message => le['msg']['__content__'],
160 :paths => paths)
160 :paths => paths)
161 end
161 end
162 self
162 self
163 end
163 end
164
164
165 def diff(path, identifier_from, identifier_to=nil)
165 def diff(path, identifier_from, identifier_to=nil)
166 hg_args = %w|rhdiff|
166 hg_args = %w|rhdiff|
167 if identifier_to
167 if identifier_to
168 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
168 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
169 else
169 else
170 hg_args << '-c' << hgrev(identifier_from)
170 hg_args << '-c' << hgrev(identifier_from)
171 end
171 end
172 hg_args << CGI.escape(hgtarget(path)) unless path.blank?
172 hg_args << CGI.escape(hgtarget(path)) unless path.blank?
173
173 diff = []
174 hg *hg_args do |io|
174 hg *hg_args do |io|
175 io.collect
175 io.each_line do |line|
176 diff << line
177 end
176 end
178 end
179 diff
177 rescue HgCommandAborted
180 rescue HgCommandAborted
178 nil # means not found
181 nil # means not found
179 end
182 end
180
183
181 def cat(path, identifier=nil)
184 def cat(path, identifier=nil)
182 hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io|
185 hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io|
183 io.binmode
186 io.binmode
184 io.read
187 io.read
185 end
188 end
186 rescue HgCommandAborted
189 rescue HgCommandAborted
187 nil # means not found
190 nil # means not found
188 end
191 end
189
192
190 def annotate(path, identifier=nil)
193 def annotate(path, identifier=nil)
191 blame = Annotate.new
194 blame = Annotate.new
192 hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io|
195 hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io|
193 io.each_line do |line|
196 io.each_line do |line|
194 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
197 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
195 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
198 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
196 :identifier => $3)
199 :identifier => $3)
197 blame.add_line($4.rstrip, r)
200 blame.add_line($4.rstrip, r)
198 end
201 end
199 end
202 end
200 blame
203 blame
201 rescue HgCommandAborted
204 rescue HgCommandAborted
202 nil # means not found or cannot be annotated
205 nil # means not found or cannot be annotated
203 end
206 end
204
207
205 class Revision < Redmine::Scm::Adapters::Revision
208 class Revision < Redmine::Scm::Adapters::Revision
206 # Returns the readable identifier
209 # Returns the readable identifier
207 def format_identifier
210 def format_identifier
208 "#{revision}:#{scmid}"
211 "#{revision}:#{scmid}"
209 end
212 end
210 end
213 end
211
214
212 # Runs 'hg' command with the given args
215 # Runs 'hg' command with the given args
213 def hg(*args, &block)
216 def hg(*args, &block)
214 repo_path = root_url || url
217 repo_path = root_url || url
215 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
218 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
216 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
219 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
217 full_args << '--config' << 'diff.git=false'
220 full_args << '--config' << 'diff.git=false'
218 full_args += args
221 full_args += args
219 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
222 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
220 if $? && $?.exitstatus != 0
223 if $? && $?.exitstatus != 0
221 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
224 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
222 end
225 end
223 ret
226 ret
224 end
227 end
225 private :hg
228 private :hg
226
229
227 # Returns correct revision identifier
230 # Returns correct revision identifier
228 def hgrev(identifier, sq=false)
231 def hgrev(identifier, sq=false)
229 rev = identifier.blank? ? 'tip' : identifier.to_s
232 rev = identifier.blank? ? 'tip' : identifier.to_s
230 rev = shell_quote(rev) if sq
233 rev = shell_quote(rev) if sq
231 rev
234 rev
232 end
235 end
233 private :hgrev
236 private :hgrev
234
237
235 def hgtarget(path)
238 def hgtarget(path)
236 path ||= ''
239 path ||= ''
237 root_url + '/' + without_leading_slash(path)
240 root_url + '/' + without_leading_slash(path)
238 end
241 end
239 private :hgtarget
242 private :hgtarget
240
243
241 def as_ary(o)
244 def as_ary(o)
242 return [] unless o
245 return [] unless o
243 o.is_a?(Array) ? o : Array[o]
246 o.is_a?(Array) ? o : Array[o]
244 end
247 end
245 private :as_ary
248 private :as_ary
246 end
249 end
247 end
250 end
248 end
251 end
249 end
252 end
General Comments 0
You need to be logged in to leave comments. Login now