annotate .svn/pristine/6f/6f938056d9ea2cfe9c4f2e93944e625cd1a36508.svn-base @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 require 'redmine/scm/adapters/abstract_adapter'
Chris@909 19 require 'cgi'
Chris@909 20
Chris@909 21 module Redmine
Chris@909 22 module Scm
Chris@909 23 module Adapters
Chris@909 24 class MercurialAdapter < AbstractAdapter
Chris@909 25
Chris@909 26 # Mercurial executable name
Chris@909 27 HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
Chris@909 28 HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
Chris@909 29 HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
Chris@909 30 TEMPLATE_NAME = "hg-template"
Chris@909 31 TEMPLATE_EXTENSION = "tmpl"
Chris@909 32
Chris@909 33 # raised if hg command exited with error, e.g. unknown revision.
Chris@909 34 class HgCommandAborted < CommandFailed; end
Chris@909 35
Chris@909 36 class << self
Chris@909 37 def client_command
Chris@909 38 @@bin ||= HG_BIN
Chris@909 39 end
Chris@909 40
Chris@909 41 def sq_bin
Chris@909 42 @@sq_bin ||= shell_quote_command
Chris@909 43 end
Chris@909 44
Chris@909 45 def client_version
Chris@909 46 @@client_version ||= (hgversion || [])
Chris@909 47 end
Chris@909 48
Chris@909 49 def client_available
Chris@909 50 client_version_above?([1, 2])
Chris@909 51 end
Chris@909 52
Chris@909 53 def hgversion
Chris@909 54 # The hg version is expressed either as a
Chris@909 55 # release number (eg 0.9.5 or 1.0) or as a revision
Chris@909 56 # id composed of 12 hexa characters.
Chris@909 57 theversion = hgversion_from_command_line.dup
Chris@909 58 if theversion.respond_to?(:force_encoding)
Chris@909 59 theversion.force_encoding('ASCII-8BIT')
Chris@909 60 end
Chris@909 61 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
Chris@909 62 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@909 63 end
Chris@909 64 end
Chris@909 65
Chris@909 66 def hgversion_from_command_line
Chris@909 67 shellout("#{sq_bin} --version") { |io| io.read }.to_s
Chris@909 68 end
Chris@909 69
Chris@909 70 def template_path
Chris@909 71 @@template_path ||= template_path_for(client_version)
Chris@909 72 end
Chris@909 73
Chris@909 74 def template_path_for(version)
Chris@909 75 "#{HELPERS_DIR}/#{TEMPLATE_NAME}-1.0.#{TEMPLATE_EXTENSION}"
Chris@909 76 end
Chris@909 77 end
Chris@909 78
Chris@909 79 def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
Chris@909 80 super
Chris@909 81 @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
Chris@909 82 end
Chris@909 83
Chris@909 84 def path_encoding
Chris@909 85 @path_encoding
Chris@909 86 end
Chris@909 87
Chris@909 88 def info
Chris@909 89 tip = summary['repository']['tip']
Chris@909 90 Info.new(:root_url => CGI.unescape(summary['repository']['root']),
Chris@909 91 :lastrev => Revision.new(:revision => tip['revision'],
Chris@909 92 :scmid => tip['node']))
Chris@909 93 # rescue HgCommandAborted
Chris@909 94 rescue Exception => e
Chris@909 95 logger.error "hg: error during getting info: #{e.message}"
Chris@909 96 nil
Chris@909 97 end
Chris@909 98
Chris@909 99 def tags
Chris@909 100 as_ary(summary['repository']['tag']).map { |e| e['name'] }
Chris@909 101 end
Chris@909 102
Chris@909 103 # Returns map of {'tag' => 'nodeid', ...}
Chris@909 104 def tagmap
Chris@909 105 alist = as_ary(summary['repository']['tag']).map do |e|
Chris@909 106 e.values_at('name', 'node')
Chris@909 107 end
Chris@909 108 Hash[*alist.flatten]
Chris@909 109 end
Chris@909 110
Chris@909 111 def branches
Chris@909 112 brs = []
Chris@909 113 as_ary(summary['repository']['branch']).each do |e|
Chris@909 114 br = Branch.new(e['name'])
Chris@909 115 br.revision = e['revision']
Chris@909 116 br.scmid = e['node']
Chris@909 117 brs << br
Chris@909 118 end
Chris@909 119 brs
Chris@909 120 end
Chris@909 121
Chris@909 122 # Returns map of {'branch' => 'nodeid', ...}
Chris@909 123 def branchmap
Chris@909 124 alist = as_ary(summary['repository']['branch']).map do |e|
Chris@909 125 e.values_at('name', 'node')
Chris@909 126 end
Chris@909 127 Hash[*alist.flatten]
Chris@909 128 end
Chris@909 129
Chris@909 130 def summary
Chris@909 131 return @summary if @summary
Chris@909 132 hg 'rhsummary' do |io|
Chris@909 133 output = io.read
Chris@909 134 if output.respond_to?(:force_encoding)
Chris@909 135 output.force_encoding('UTF-8')
Chris@909 136 end
Chris@909 137 begin
Chris@909 138 @summary = ActiveSupport::XmlMini.parse(output)['rhsummary']
Chris@909 139 rescue
Chris@909 140 end
Chris@909 141 end
Chris@909 142 end
Chris@909 143 private :summary
Chris@909 144
Chris@909 145 def entries(path=nil, identifier=nil, options={})
Chris@909 146 p1 = scm_iconv(@path_encoding, 'UTF-8', path)
Chris@909 147 manifest = hg('rhmanifest', '-r', CGI.escape(hgrev(identifier)),
Chris@909 148 CGI.escape(without_leading_slash(p1.to_s))) do |io|
Chris@909 149 output = io.read
Chris@909 150 if output.respond_to?(:force_encoding)
Chris@909 151 output.force_encoding('UTF-8')
Chris@909 152 end
Chris@909 153 begin
Chris@909 154 ActiveSupport::XmlMini.parse(output)['rhmanifest']['repository']['manifest']
Chris@909 155 rescue
Chris@909 156 end
Chris@909 157 end
Chris@909 158 path_prefix = path.blank? ? '' : with_trailling_slash(path)
Chris@909 159
Chris@909 160 entries = Entries.new
Chris@909 161 as_ary(manifest['dir']).each do |e|
Chris@909 162 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
Chris@909 163 p = "#{path_prefix}#{n}"
Chris@909 164 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
Chris@909 165 end
Chris@909 166
Chris@909 167 as_ary(manifest['file']).each do |e|
Chris@909 168 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
Chris@909 169 p = "#{path_prefix}#{n}"
Chris@909 170 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
Chris@909 171 :identifier => e['node'],
Chris@909 172 :time => Time.at(e['time'].to_i))
Chris@909 173 entries << Entry.new(:name => n, :path => p, :kind => 'file',
Chris@909 174 :size => e['size'].to_i, :lastrev => lr)
Chris@909 175 end
Chris@909 176
Chris@909 177 entries
Chris@909 178 rescue HgCommandAborted
Chris@909 179 nil # means not found
Chris@909 180 end
Chris@909 181
Chris@909 182 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@909 183 revs = Revisions.new
Chris@909 184 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
Chris@909 185 revs
Chris@909 186 end
Chris@909 187
Chris@909 188 # Iterates the revisions by using a template file that
Chris@909 189 # makes Mercurial produce a xml output.
Chris@909 190 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@909 191 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
Chris@909 192 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
Chris@909 193 hg_args << '--limit' << options[:limit] if options[:limit]
Chris@909 194 hg_args << hgtarget(path) unless path.blank?
Chris@909 195 log = hg(*hg_args) do |io|
Chris@909 196 output = io.read
Chris@909 197 if output.respond_to?(:force_encoding)
Chris@909 198 output.force_encoding('UTF-8')
Chris@909 199 end
Chris@909 200 begin
Chris@909 201 # Mercurial < 1.5 does not support footer template for '</log>'
Chris@909 202 ActiveSupport::XmlMini.parse("#{output}</log>")['log']
Chris@909 203 rescue
Chris@909 204 end
Chris@909 205 end
Chris@909 206 as_ary(log['logentry']).each do |le|
Chris@909 207 cpalist = as_ary(le['paths']['path-copied']).map do |e|
Chris@909 208 [e['__content__'], e['copyfrom-path']].map do |s|
Chris@909 209 scm_iconv('UTF-8', @path_encoding, CGI.unescape(s))
Chris@909 210 end
Chris@909 211 end
Chris@909 212 cpmap = Hash[*cpalist.flatten]
Chris@909 213 paths = as_ary(le['paths']['path']).map do |e|
Chris@909 214 p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
Chris@909 215 {:action => e['action'],
Chris@909 216 :path => with_leading_slash(p),
Chris@909 217 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
Chris@909 218 :from_revision => (cpmap.member?(p) ? le['node'] : nil)}
Chris@909 219 end.sort { |a, b| a[:path] <=> b[:path] }
Chris@909 220 parents_ary = []
Chris@909 221 as_ary(le['parents']['parent']).map do |par|
Chris@909 222 parents_ary << par['__content__'] if par['__content__'] != "000000000000"
Chris@909 223 end
Chris@909 224 yield Revision.new(:revision => le['revision'],
Chris@909 225 :scmid => le['node'],
Chris@909 226 :author => (le['author']['__content__'] rescue ''),
Chris@909 227 :time => Time.parse(le['date']['__content__']),
Chris@909 228 :message => le['msg']['__content__'],
Chris@909 229 :paths => paths,
Chris@909 230 :parents => parents_ary)
Chris@909 231 end
Chris@909 232 self
Chris@909 233 end
Chris@909 234
Chris@909 235 # Returns list of nodes in the specified branch
Chris@909 236 def nodes_in_branch(branch, options={})
Chris@909 237 hg_args = ['rhlog', '--template', '{node|short}\n', '--rhbranch', CGI.escape(branch)]
Chris@909 238 hg_args << '--from' << CGI.escape(branch)
Chris@909 239 hg_args << '--to' << '0'
Chris@909 240 hg_args << '--limit' << options[:limit] if options[:limit]
Chris@909 241 hg(*hg_args) { |io| io.readlines.map { |e| e.chomp } }
Chris@909 242 end
Chris@909 243
Chris@909 244 def diff(path, identifier_from, identifier_to=nil)
Chris@909 245 hg_args = %w|rhdiff|
Chris@909 246 if identifier_to
Chris@909 247 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
Chris@909 248 else
Chris@909 249 hg_args << '-c' << hgrev(identifier_from)
Chris@909 250 end
Chris@909 251 unless path.blank?
Chris@909 252 p = scm_iconv(@path_encoding, 'UTF-8', path)
Chris@909 253 hg_args << CGI.escape(hgtarget(p))
Chris@909 254 end
Chris@909 255 diff = []
Chris@909 256 hg *hg_args do |io|
Chris@909 257 io.each_line do |line|
Chris@909 258 diff << line
Chris@909 259 end
Chris@909 260 end
Chris@909 261 diff
Chris@909 262 rescue HgCommandAborted
Chris@909 263 nil # means not found
Chris@909 264 end
Chris@909 265
Chris@909 266 def cat(path, identifier=nil)
Chris@909 267 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
Chris@909 268 hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
Chris@909 269 io.binmode
Chris@909 270 io.read
Chris@909 271 end
Chris@909 272 rescue HgCommandAborted
Chris@909 273 nil # means not found
Chris@909 274 end
Chris@909 275
Chris@909 276 def annotate(path, identifier=nil)
Chris@909 277 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
Chris@909 278 blame = Annotate.new
Chris@909 279 hg 'rhannotate', '-ncu', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
Chris@909 280 io.each_line do |line|
Chris@909 281 line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding)
Chris@909 282 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
Chris@909 283 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
Chris@909 284 :identifier => $3)
Chris@909 285 blame.add_line($4.rstrip, r)
Chris@909 286 end
Chris@909 287 end
Chris@909 288 blame
Chris@909 289 rescue HgCommandAborted
Chris@909 290 # means not found or cannot be annotated
Chris@909 291 Annotate.new
Chris@909 292 end
Chris@909 293
Chris@909 294 class Revision < Redmine::Scm::Adapters::Revision
Chris@909 295 # Returns the readable identifier
Chris@909 296 def format_identifier
Chris@909 297 "#{revision}:#{scmid}"
Chris@909 298 end
Chris@909 299 end
Chris@909 300
Chris@909 301 # Runs 'hg' command with the given args
Chris@909 302 def hg(*args, &block)
Chris@909 303 repo_path = root_url || url
Chris@909 304 full_args = ['-R', repo_path, '--encoding', 'utf-8']
Chris@909 305 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
Chris@909 306 full_args << '--config' << 'diff.git=false'
Chris@909 307 full_args += args
Chris@909 308 ret = shellout(
Chris@909 309 self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
Chris@909 310 &block
Chris@909 311 )
Chris@909 312 if $? && $?.exitstatus != 0
Chris@909 313 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
Chris@909 314 end
Chris@909 315 ret
Chris@909 316 end
Chris@909 317 private :hg
Chris@909 318
Chris@909 319 # Returns correct revision identifier
Chris@909 320 def hgrev(identifier, sq=false)
Chris@909 321 rev = identifier.blank? ? 'tip' : identifier.to_s
Chris@909 322 rev = shell_quote(rev) if sq
Chris@909 323 rev
Chris@909 324 end
Chris@909 325 private :hgrev
Chris@909 326
Chris@909 327 def hgtarget(path)
Chris@909 328 path ||= ''
Chris@909 329 root_url + '/' + without_leading_slash(path)
Chris@909 330 end
Chris@909 331 private :hgtarget
Chris@909 332
Chris@909 333 def as_ary(o)
Chris@909 334 return [] unless o
Chris@909 335 o.is_a?(Array) ? o : Array[o]
Chris@909 336 end
Chris@909 337 private :as_ary
Chris@909 338 end
Chris@909 339 end
Chris@909 340 end
Chris@909 341 end