annotate .svn/pristine/2c/2c3a4de5492d19130df8d9d9f0bc6d32734619b7.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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