annotate lib/redmine/scm/adapters/.svn/text-base/mercurial_adapter.rb.svn-base @ 870:b8475c3e5f30 feature_126

Close obsolete branch feature_126
author Chris Cannam
date Sat, 02 Apr 2011 16:11:30 +0100
parents 513646585e45
children af80e5618e9b 8661b858af72
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@0 19
Chris@0 20 module Redmine
Chris@0 21 module Scm
Chris@0 22 module Adapters
Chris@0 23 class MercurialAdapter < AbstractAdapter
Chris@0 24
Chris@0 25 # Mercurial executable name
Chris@0 26 HG_BIN = "hg"
Chris@0 27 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
Chris@0 28 TEMPLATE_NAME = "hg-template"
Chris@0 29 TEMPLATE_EXTENSION = "tmpl"
Chris@0 30
Chris@0 31 class << self
Chris@0 32 def client_version
Chris@0 33 @@client_version ||= (hgversion || [])
Chris@0 34 end
Chris@0 35
Chris@0 36 def hgversion
Chris@0 37 # The hg version is expressed either as a
Chris@0 38 # release number (eg 0.9.5 or 1.0) or as a revision
Chris@0 39 # id composed of 12 hexa characters.
Chris@0 40 theversion = hgversion_from_command_line
Chris@0 41 if theversion.match(/^\d+(\.\d+)+/)
Chris@0 42 theversion.split(".").collect(&:to_i)
Chris@0 43 end
Chris@0 44 end
Chris@0 45
Chris@0 46 def hgversion_from_command_line
Chris@0 47 %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
Chris@0 48 end
Chris@0 49
Chris@0 50 def template_path
Chris@0 51 @@template_path ||= template_path_for(client_version)
Chris@0 52 end
Chris@0 53
Chris@0 54 def template_path_for(version)
Chris@0 55 if ((version <=> [0,9,5]) > 0) || version.empty?
Chris@0 56 ver = "1.0"
Chris@0 57 else
Chris@0 58 ver = "0.9.5"
Chris@0 59 end
Chris@0 60 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
Chris@0 61 end
Chris@0 62 end
Chris@0 63
Chris@0 64 def info
Chris@0 65 cmd = "#{HG_BIN} -R #{target('')} root"
Chris@0 66 root_url = nil
Chris@0 67 shellout(cmd) do |io|
Chris@0 68 root_url = io.read
Chris@0 69 end
Chris@0 70 return nil if $? && $?.exitstatus != 0
Chris@0 71 info = Info.new({:root_url => root_url.chomp,
Chris@0 72 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
Chris@0 73 })
Chris@0 74 info
Chris@0 75 rescue CommandFailed
Chris@0 76 return nil
Chris@0 77 end
Chris@0 78
Chris@0 79 def entries(path=nil, identifier=nil)
Chris@0 80 path ||= ''
Chris@0 81 entries = Entries.new
Chris@0 82 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
Chris@0 83 cmd << " -r " + (identifier ? identifier.to_s : "tip")
Chris@0 84 cmd << " " + shell_quote("path:#{path}") unless path.empty?
Chris@0 85 shellout(cmd) do |io|
Chris@0 86 io.each_line do |line|
Chris@0 87 # HG uses antislashs as separator on Windows
Chris@0 88 line = line.gsub(/\\/, "/")
Chris@0 89 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
Chris@0 90 e ||= line
Chris@0 91 e = e.chomp.split(%r{[\/\\]})
Chris@0 92 entries << Entry.new({:name => e.first,
Chris@0 93 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
Chris@0 94 :kind => (e.size > 1 ? 'dir' : 'file'),
Chris@0 95 :lastrev => Revision.new
Chris@0 96 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
Chris@0 97 end
Chris@0 98 end
Chris@0 99 end
Chris@0 100 return nil if $? && $?.exitstatus != 0
Chris@0 101 entries.sort_by_name
Chris@0 102 end
Chris@0 103
Chris@0 104 # Fetch the revisions by using a template file that
Chris@0 105 # makes Mercurial produce a xml output.
Chris@0 106 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@0 107 revisions = Revisions.new
Chris@0 108 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
Chris@0 109 if identifier_from && identifier_to
Chris@0 110 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
Chris@0 111 elsif identifier_from
Chris@0 112 cmd << " -r #{identifier_from.to_i}:"
Chris@0 113 end
Chris@0 114 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
Chris@0 115 cmd << " #{path}" if path
Chris@0 116 shellout(cmd) do |io|
Chris@0 117 begin
Chris@0 118 # HG doesn't close the XML Document...
Chris@0 119 doc = REXML::Document.new(io.read << "</log>")
Chris@0 120 doc.elements.each("log/logentry") do |logentry|
Chris@0 121 paths = []
Chris@0 122 copies = logentry.get_elements('paths/path-copied')
Chris@0 123 logentry.elements.each("paths/path") do |path|
Chris@0 124 # Detect if the added file is a copy
Chris@0 125 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
Chris@0 126 from_path = c.attributes['copyfrom-path']
Chris@0 127 from_rev = logentry.attributes['revision']
Chris@0 128 end
Chris@0 129 paths << {:action => path.attributes['action'],
Chris@0 130 :path => "/#{path.text}",
Chris@0 131 :from_path => from_path ? "/#{from_path}" : nil,
Chris@0 132 :from_revision => from_rev ? from_rev : nil
Chris@0 133 }
Chris@0 134 end
Chris@0 135 paths.sort! { |x,y| x[:path] <=> y[:path] }
Chris@0 136
Chris@0 137 revisions << Revision.new({:identifier => logentry.attributes['revision'],
Chris@0 138 :scmid => logentry.attributes['node'],
Chris@0 139 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
Chris@0 140 :time => Time.parse(logentry.elements['date'].text).localtime,
Chris@0 141 :message => logentry.elements['msg'].text,
Chris@0 142 :paths => paths
Chris@0 143 })
Chris@0 144 end
Chris@0 145 rescue
Chris@0 146 logger.debug($!)
Chris@0 147 end
Chris@0 148 end
Chris@0 149 return nil if $? && $?.exitstatus != 0
Chris@0 150 revisions
Chris@0 151 end
Chris@0 152
Chris@0 153 def diff(path, identifier_from, identifier_to=nil)
Chris@0 154 path ||= ''
Chris@0 155 if identifier_to
Chris@0 156 identifier_to = identifier_to.to_i
Chris@0 157 else
Chris@0 158 identifier_to = identifier_from.to_i - 1
Chris@0 159 end
Chris@0 160 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
Chris@0 161 cmd << " -I #{target(path)}" unless path.empty?
Chris@0 162 diff = []
Chris@0 163 shellout(cmd) do |io|
Chris@0 164 io.each_line do |line|
Chris@0 165 diff << line
Chris@0 166 end
Chris@0 167 end
Chris@0 168 return nil if $? && $?.exitstatus != 0
Chris@0 169 diff
Chris@0 170 end
Chris@0 171
Chris@0 172 def cat(path, identifier=nil)
Chris@0 173 cmd = "#{HG_BIN} -R #{target('')} cat"
Chris@0 174 cmd << " -r " + (identifier ? identifier.to_s : "tip")
Chris@0 175 cmd << " #{target(path)}"
Chris@0 176 cat = nil
Chris@0 177 shellout(cmd) do |io|
Chris@0 178 io.binmode
Chris@0 179 cat = io.read
Chris@0 180 end
Chris@0 181 return nil if $? && $?.exitstatus != 0
Chris@0 182 cat
Chris@0 183 end
Chris@0 184
Chris@0 185 def annotate(path, identifier=nil)
Chris@0 186 path ||= ''
Chris@0 187 cmd = "#{HG_BIN} -R #{target('')}"
Chris@0 188 cmd << " annotate -n -u"
Chris@0 189 cmd << " -r " + (identifier ? identifier.to_s : "tip")
Chris@0 190 cmd << " -r #{identifier.to_i}" if identifier
Chris@0 191 cmd << " #{target(path)}"
Chris@0 192 blame = Annotate.new
Chris@0 193 shellout(cmd) do |io|
Chris@0 194 io.each_line do |line|
Chris@0 195 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
Chris@0 196 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
Chris@0 197 end
Chris@0 198 end
Chris@0 199 return nil if $? && $?.exitstatus != 0
Chris@0 200 blame
Chris@0 201 end
Chris@0 202 end
Chris@0 203 end
Chris@0 204 end
Chris@0 205 end