annotate lib/redmine/scm/adapters/mercurial_adapter.rb @ 1174:928c0d0f624e bug_365

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