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