annotate .svn/pristine/3b/3b31c4786bae9b8126d2ce8808d4fe7afdaa1e91.svn-base @ 1613:90bed4e10cc8 deploy

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