##// END OF EJS Templates
scm: mercurial: override initialize() for path encoding (#2664)....
Toshi MARUYAMA -
r4799:1236e037f229
parent child
Show More
@@ -1,286 +1,291
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 def initialize(url, root_url=nil, login=nil, password=nil)
82 super
83 @path_encoding = 'UTF-8'
84 end
85
81 86 def info
82 87 tip = summary['repository']['tip']
83 88 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
84 89 :lastrev => Revision.new(:revision => tip['revision'],
85 90 :scmid => tip['node']))
86 91 end
87 92
88 93 def tags
89 94 as_ary(summary['repository']['tag']).map { |e| e['name'] }
90 95 end
91 96
92 97 # Returns map of {'tag' => 'nodeid', ...}
93 98 def tagmap
94 99 alist = as_ary(summary['repository']['tag']).map do |e|
95 100 e.values_at('name', 'node')
96 101 end
97 102 Hash[*alist.flatten]
98 103 end
99 104
100 105 def branches
101 106 as_ary(summary['repository']['branch']).map { |e| e['name'] }
102 107 end
103 108
104 109 # Returns map of {'branch' => 'nodeid', ...}
105 110 def branchmap
106 111 alist = as_ary(summary['repository']['branch']).map do |e|
107 112 e.values_at('name', 'node')
108 113 end
109 114 Hash[*alist.flatten]
110 115 end
111 116
112 117 def summary
113 118 return @summary if @summary
114 119 hg 'rhsummary' do |io|
115 120 begin
116 121 @summary = ActiveSupport::XmlMini.parse(io.read)['rhsummary']
117 122 rescue
118 123 end
119 124 end
120 125 end
121 126 private :summary
122 127
123 128 def entries(path=nil, identifier=nil)
124 129 manifest = hg('rhmanifest', '-r', hgrev(identifier),
125 130 CGI.escape(without_leading_slash(path.to_s))) do |io|
126 131 begin
127 132 ActiveSupport::XmlMini.parse(io.read)['rhmanifest']['repository']['manifest']
128 133 rescue
129 134 end
130 135 end
131 136 path_prefix = path.blank? ? '' : with_trailling_slash(path)
132 137
133 138 entries = Entries.new
134 139 as_ary(manifest['dir']).each do |e|
135 140 n = CGI.unescape(e['name'])
136 141 p = "#{path_prefix}#{n}"
137 142 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
138 143 end
139 144
140 145 as_ary(manifest['file']).each do |e|
141 146 n = CGI.unescape(e['name'])
142 147 p = "#{path_prefix}#{n}"
143 148 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
144 149 :identifier => e['node'],
145 150 :time => Time.at(e['time'].to_i))
146 151 entries << Entry.new(:name => n, :path => p, :kind => 'file',
147 152 :size => e['size'].to_i, :lastrev => lr)
148 153 end
149 154
150 155 entries
151 156 rescue HgCommandAborted
152 157 nil # means not found
153 158 end
154 159
155 160 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
156 161 revs = Revisions.new
157 162 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
158 163 revs
159 164 end
160 165
161 166 # Iterates the revisions by using a template file that
162 167 # makes Mercurial produce a xml output.
163 168 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
164 169 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
165 170 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
166 171 hg_args << '--limit' << options[:limit] if options[:limit]
167 172 hg_args << hgtarget(path) unless path.blank?
168 173 log = hg(*hg_args) do |io|
169 174 begin
170 175 # Mercurial < 1.5 does not support footer template for '</log>'
171 176 ActiveSupport::XmlMini.parse("#{io.read}</log>")['log']
172 177 rescue
173 178 end
174 179 end
175 180
176 181 as_ary(log['logentry']).each do |le|
177 182 cpalist = as_ary(le['paths']['path-copied']).map do |e|
178 183 [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) }
179 184 end
180 185 cpmap = Hash[*cpalist.flatten]
181 186
182 187 paths = as_ary(le['paths']['path']).map do |e|
183 188 p = CGI.unescape(e['__content__'])
184 189 {:action => e['action'], :path => with_leading_slash(p),
185 190 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
186 191 :from_revision => (cpmap.member?(p) ? le['revision'] : nil)}
187 192 end.sort { |a, b| a[:path] <=> b[:path] }
188 193
189 194 yield Revision.new(:revision => le['revision'],
190 195 :scmid => le['node'],
191 196 :author => (le['author']['__content__'] rescue ''),
192 197 :time => Time.parse(le['date']['__content__']).localtime,
193 198 :message => le['msg']['__content__'],
194 199 :paths => paths)
195 200 end
196 201 self
197 202 end
198 203
199 204 def diff(path, identifier_from, identifier_to=nil)
200 205 hg_args = %w|rhdiff|
201 206 if identifier_to
202 207 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
203 208 else
204 209 hg_args << '-c' << hgrev(identifier_from)
205 210 end
206 211 hg_args << CGI.escape(hgtarget(path)) unless path.blank?
207 212 diff = []
208 213 hg *hg_args do |io|
209 214 io.each_line do |line|
210 215 diff << line
211 216 end
212 217 end
213 218 diff
214 219 rescue HgCommandAborted
215 220 nil # means not found
216 221 end
217 222
218 223 def cat(path, identifier=nil)
219 224 hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io|
220 225 io.binmode
221 226 io.read
222 227 end
223 228 rescue HgCommandAborted
224 229 nil # means not found
225 230 end
226 231
227 232 def annotate(path, identifier=nil)
228 233 blame = Annotate.new
229 234 hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io|
230 235 io.each_line do |line|
231 236 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
232 237 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
233 238 :identifier => $3)
234 239 blame.add_line($4.rstrip, r)
235 240 end
236 241 end
237 242 blame
238 243 rescue HgCommandAborted
239 244 nil # means not found or cannot be annotated
240 245 end
241 246
242 247 class Revision < Redmine::Scm::Adapters::Revision
243 248 # Returns the readable identifier
244 249 def format_identifier
245 250 "#{revision}:#{scmid}"
246 251 end
247 252 end
248 253
249 254 # Runs 'hg' command with the given args
250 255 def hg(*args, &block)
251 256 repo_path = root_url || url
252 257 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
253 258 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
254 259 full_args << '--config' << 'diff.git=false'
255 260 full_args += args
256 261 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
257 262 if $? && $?.exitstatus != 0
258 263 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
259 264 end
260 265 ret
261 266 end
262 267 private :hg
263 268
264 269 # Returns correct revision identifier
265 270 def hgrev(identifier, sq=false)
266 271 rev = identifier.blank? ? 'tip' : identifier.to_s
267 272 rev = shell_quote(rev) if sq
268 273 rev
269 274 end
270 275 private :hgrev
271 276
272 277 def hgtarget(path)
273 278 path ||= ''
274 279 root_url + '/' + without_leading_slash(path)
275 280 end
276 281 private :hgtarget
277 282
278 283 def as_ary(o)
279 284 return [] unless o
280 285 o.is_a?(Array) ? o : Array[o]
281 286 end
282 287 private :as_ary
283 288 end
284 289 end
285 290 end
286 291 end
General Comments 0
You need to be logged in to leave comments. Login now