annotate lib/redmine/scm/adapters/mercurial_adapter.rb @ 251:c2ffbc10233e cannam

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