annotate lib/redmine/scm/adapters/.svn/text-base/mercurial_adapter.rb.svn-base @ 853:969bb872d4bf feature_142

Close obsolete branch feature_142
author Chris Cannam
date Thu, 14 Jul 2011 14:26:44 +0100
parents 753f1380d6bc
children 851510f1b535
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@245 98 end
Chris@245 99
Chris@245 100 def tags
Chris@245 101 as_ary(summary['repository']['tag']).map { |e| e['name'] }
Chris@245 102 end
Chris@245 103
Chris@245 104 # Returns map of {'tag' => 'nodeid', ...}
Chris@245 105 def tagmap
Chris@245 106 alist = as_ary(summary['repository']['tag']).map do |e|
Chris@245 107 e.values_at('name', 'node')
Chris@245 108 end
Chris@245 109 Hash[*alist.flatten]
Chris@245 110 end
Chris@245 111
Chris@245 112 def branches
Chris@245 113 as_ary(summary['repository']['branch']).map { |e| e['name'] }
Chris@245 114 end
Chris@245 115
Chris@245 116 # Returns map of {'branch' => 'nodeid', ...}
Chris@245 117 def branchmap
Chris@245 118 alist = as_ary(summary['repository']['branch']).map do |e|
Chris@245 119 e.values_at('name', 'node')
Chris@245 120 end
Chris@245 121 Hash[*alist.flatten]
Chris@245 122 end
Chris@245 123
Chris@245 124 def summary
Chris@441 125 return @summary if @summary
Chris@245 126 hg 'rhsummary' do |io|
Chris@245 127 output = io.read
Chris@245 128 if output.respond_to?(:force_encoding)
Chris@245 129 output.force_encoding('UTF-8')
Chris@245 130 end
Chris@245 131 begin
Chris@245 132 @summary = ActiveSupport::XmlMini.parse(output)['rhsummary']
Chris@245 133 rescue
Chris@0 134 end
Chris@0 135 end
Chris@245 136 end
Chris@245 137 private :summary
Chris@245 138
Chris@441 139 def entries(path=nil, identifier=nil, options={})
Chris@245 140 p1 = scm_iconv(@path_encoding, 'UTF-8', path)
Chris@245 141 manifest = hg('rhmanifest', '-r', CGI.escape(hgrev(identifier)),
Chris@245 142 CGI.escape(without_leading_slash(p1.to_s))) do |io|
Chris@245 143 output = io.read
Chris@245 144 if output.respond_to?(:force_encoding)
Chris@245 145 output.force_encoding('UTF-8')
Chris@245 146 end
Chris@245 147 begin
Chris@245 148 ActiveSupport::XmlMini.parse(output)['rhmanifest']['repository']['manifest']
Chris@245 149 rescue
Chris@245 150 end
Chris@245 151 end
Chris@245 152 path_prefix = path.blank? ? '' : with_trailling_slash(path)
Chris@245 153
Chris@245 154 entries = Entries.new
Chris@245 155 as_ary(manifest['dir']).each do |e|
Chris@245 156 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
Chris@245 157 p = "#{path_prefix}#{n}"
Chris@245 158 entries << Entry.new(:name => n, :path => p, :kind => 'dir')
Chris@245 159 end
Chris@245 160
Chris@245 161 as_ary(manifest['file']).each do |e|
Chris@245 162 n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
Chris@245 163 p = "#{path_prefix}#{n}"
Chris@245 164 lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
Chris@245 165 :identifier => e['node'],
Chris@245 166 :time => Time.at(e['time'].to_i))
Chris@245 167 entries << Entry.new(:name => n, :path => p, :kind => 'file',
Chris@245 168 :size => e['size'].to_i, :lastrev => lr)
Chris@245 169 end
Chris@245 170
Chris@245 171 entries
Chris@245 172 rescue HgCommandAborted
Chris@245 173 nil # means not found
Chris@0 174 end
Chris@119 175
Chris@245 176 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@245 177 revs = Revisions.new
Chris@245 178 each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
Chris@245 179 revs
Chris@245 180 end
Chris@245 181
Chris@245 182 # Iterates the revisions by using a template file that
Chris@0 183 # makes Mercurial produce a xml output.
Chris@245 184 def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@245 185 hg_args = ['log', '--debug', '-C', '--style', self.class.template_path]
Chris@245 186 hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
Chris@245 187 hg_args << '--limit' << options[:limit] if options[:limit]
Chris@245 188 hg_args << hgtarget(path) unless path.blank?
Chris@245 189 log = hg(*hg_args) do |io|
Chris@245 190 output = io.read
Chris@245 191 if output.respond_to?(:force_encoding)
Chris@245 192 output.force_encoding('UTF-8')
Chris@245 193 end
Chris@0 194 begin
Chris@245 195 # Mercurial < 1.5 does not support footer template for '</log>'
Chris@245 196 ActiveSupport::XmlMini.parse("#{output}</log>")['log']
Chris@0 197 rescue
Chris@0 198 end
Chris@0 199 end
Chris@245 200 as_ary(log['logentry']).each do |le|
Chris@245 201 cpalist = as_ary(le['paths']['path-copied']).map do |e|
Chris@441 202 [e['__content__'], e['copyfrom-path']].map do |s|
Chris@441 203 scm_iconv('UTF-8', @path_encoding, CGI.unescape(s))
Chris@441 204 end
Chris@245 205 end
Chris@245 206 cpmap = Hash[*cpalist.flatten]
Chris@245 207 paths = as_ary(le['paths']['path']).map do |e|
Chris@245 208 p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
Chris@441 209 {:action => e['action'],
Chris@441 210 :path => with_leading_slash(p),
Chris@441 211 :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
Chris@441 212 :from_revision => (cpmap.member?(p) ? le['node'] : nil)}
Chris@245 213 end.sort { |a, b| a[:path] <=> b[:path] }
Chris@245 214 yield Revision.new(:revision => le['revision'],
Chris@441 215 :scmid => le['node'],
Chris@441 216 :author => (le['author']['__content__'] rescue ''),
Chris@441 217 :time => Time.parse(le['date']['__content__']),
Chris@441 218 :message => le['msg']['__content__'],
Chris@441 219 :paths => paths)
Chris@245 220 end
Chris@245 221 self
Chris@0 222 end
Chris@119 223
Chris@441 224 # Returns list of nodes in the specified branch
Chris@441 225 def nodes_in_branch(branch, options={})
Chris@441 226 hg_args = ['rhlog', '--template', '{node|short}\n', '--rhbranch', CGI.escape(branch)]
Chris@441 227 hg_args << '--from' << CGI.escape(branch)
Chris@441 228 hg_args << '--to' << '0'
Chris@441 229 hg_args << '--limit' << options[:limit] if options[:limit]
Chris@441 230 hg(*hg_args) { |io| io.readlines.map { |e| e.chomp } }
Chris@441 231 end
Chris@441 232
Chris@0 233 def diff(path, identifier_from, identifier_to=nil)
Chris@245 234 hg_args = %w|rhdiff|
Chris@245 235 if identifier_to
Chris@245 236 hg_args << '-r' << hgrev(identifier_to) << '-r' << hgrev(identifier_from)
Chris@245 237 else
Chris@245 238 hg_args << '-c' << hgrev(identifier_from)
Chris@245 239 end
Chris@245 240 unless path.blank?
Chris@245 241 p = scm_iconv(@path_encoding, 'UTF-8', path)
Chris@245 242 hg_args << CGI.escape(hgtarget(p))
Chris@245 243 end
Chris@119 244 diff = []
Chris@245 245 hg *hg_args do |io|
Chris@0 246 io.each_line do |line|
Chris@0 247 diff << line
Chris@0 248 end
Chris@0 249 end
Chris@0 250 diff
Chris@245 251 rescue HgCommandAborted
Chris@245 252 nil # means not found
Chris@0 253 end
Chris@119 254
Chris@0 255 def cat(path, identifier=nil)
Chris@245 256 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
Chris@441 257 hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
Chris@0 258 io.binmode
Chris@245 259 io.read
Chris@0 260 end
Chris@245 261 rescue HgCommandAborted
Chris@245 262 nil # means not found
Chris@0 263 end
Chris@119 264
Chris@0 265 def annotate(path, identifier=nil)
Chris@245 266 p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
Chris@0 267 blame = Annotate.new
Chris@441 268 hg 'rhannotate', '-ncu', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
Chris@0 269 io.each_line do |line|
Chris@245 270 line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding)
Chris@119 271 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
Chris@119 272 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
Chris@119 273 :identifier => $3)
Chris@119 274 blame.add_line($4.rstrip, r)
Chris@0 275 end
Chris@0 276 end
Chris@0 277 blame
Chris@245 278 rescue HgCommandAborted
Chris@245 279 nil # means not found or cannot be annotated
Chris@0 280 end
Chris@119 281
Chris@119 282 class Revision < Redmine::Scm::Adapters::Revision
Chris@119 283 # Returns the readable identifier
Chris@119 284 def format_identifier
Chris@119 285 "#{revision}:#{scmid}"
Chris@119 286 end
Chris@119 287 end
Chris@119 288
Chris@245 289 # Runs 'hg' command with the given args
Chris@245 290 def hg(*args, &block)
Chris@245 291 repo_path = root_url || url
Chris@245 292 full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8']
Chris@245 293 full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
Chris@245 294 full_args << '--config' << 'diff.git=false'
Chris@245 295 full_args += args
Chris@245 296 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
Chris@245 297 if $? && $?.exitstatus != 0
Chris@245 298 raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
Chris@245 299 end
Chris@245 300 ret
Chris@245 301 end
Chris@245 302 private :hg
Chris@245 303
Chris@119 304 # Returns correct revision identifier
Chris@245 305 def hgrev(identifier, sq=false)
Chris@245 306 rev = identifier.blank? ? 'tip' : identifier.to_s
Chris@245 307 rev = shell_quote(rev) if sq
Chris@245 308 rev
Chris@119 309 end
Chris@119 310 private :hgrev
Chris@245 311
Chris@245 312 def hgtarget(path)
Chris@245 313 path ||= ''
Chris@245 314 root_url + '/' + without_leading_slash(path)
Chris@245 315 end
Chris@245 316 private :hgtarget
Chris@245 317
Chris@245 318 def as_ary(o)
Chris@245 319 return [] unless o
Chris@245 320 o.is_a?(Array) ? o : Array[o]
Chris@245 321 end
Chris@245 322 private :as_ary
Chris@0 323 end
Chris@0 324 end
Chris@0 325 end
Chris@0 326 end