PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/redmine/scm/adapters/mercurial_adapter.rb

https://github.com/andreacampi/redmine
Ruby | 205 lines | 165 code | 15 blank | 25 comment | 31 complexity | c16d3e25388f47c99f1633eab2140ba7 MD5 | raw file
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'redmine/scm/adapters/abstract_adapter'
  18. module Redmine
  19. module Scm
  20. module Adapters
  21. class MercurialAdapter < AbstractAdapter
  22. # Mercurial executable name
  23. HG_BIN = "hg"
  24. TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
  25. TEMPLATE_NAME = "hg-template"
  26. TEMPLATE_EXTENSION = "tmpl"
  27. class << self
  28. def client_version
  29. @@client_version ||= (hgversion || [])
  30. end
  31. def hgversion
  32. # The hg version is expressed either as a
  33. # release number (eg 0.9.5 or 1.0) or as a revision
  34. # id composed of 12 hexa characters.
  35. theversion = hgversion_from_command_line
  36. if theversion.match(/^\d+(\.\d+)+/)
  37. theversion.split(".").collect(&:to_i)
  38. end
  39. end
  40. def hgversion_from_command_line
  41. %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
  42. end
  43. def template_path
  44. @@template_path ||= template_path_for(client_version)
  45. end
  46. def template_path_for(version)
  47. if ((version <=> [0,9,5]) > 0) || version.empty?
  48. ver = "1.0"
  49. else
  50. ver = "0.9.5"
  51. end
  52. "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
  53. end
  54. end
  55. def info
  56. cmd = "#{HG_BIN} -R #{target('')} root"
  57. root_url = nil
  58. shellout(cmd) do |io|
  59. root_url = io.read
  60. end
  61. return nil if $? && $?.exitstatus != 0
  62. info = Info.new({:root_url => root_url.chomp,
  63. :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
  64. })
  65. info
  66. rescue CommandFailed
  67. return nil
  68. end
  69. def entries(path=nil, identifier=nil)
  70. path ||= ''
  71. entries = Entries.new
  72. cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
  73. cmd << " -r " + (identifier ? identifier.to_s : "tip")
  74. cmd << " " + shell_quote("path:#{path}") unless path.empty?
  75. shellout(cmd) do |io|
  76. io.each_line do |line|
  77. # HG uses antislashs as separator on Windows
  78. line = line.gsub(/\\/, "/")
  79. if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
  80. e ||= line
  81. e = e.chomp.split(%r{[\/\\]})
  82. entries << Entry.new({:name => e.first,
  83. :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
  84. :kind => (e.size > 1 ? 'dir' : 'file'),
  85. :lastrev => Revision.new
  86. }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
  87. end
  88. end
  89. end
  90. return nil if $? && $?.exitstatus != 0
  91. entries.sort_by_name
  92. end
  93. # Fetch the revisions by using a template file that
  94. # makes Mercurial produce a xml output.
  95. def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  96. revisions = Revisions.new
  97. cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
  98. if identifier_from && identifier_to
  99. cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
  100. elsif identifier_from
  101. cmd << " -r #{identifier_from.to_i}:"
  102. end
  103. cmd << " --limit #{options[:limit].to_i}" if options[:limit]
  104. cmd << " #{path}" if path
  105. shellout(cmd) do |io|
  106. begin
  107. # HG doesn't close the XML Document...
  108. doc = REXML::Document.new(io.read << "</log>")
  109. doc.elements.each("log/logentry") do |logentry|
  110. paths = []
  111. copies = logentry.get_elements('paths/path-copied')
  112. logentry.elements.each("paths/path") do |path|
  113. # Detect if the added file is a copy
  114. if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
  115. from_path = c.attributes['copyfrom-path']
  116. from_rev = logentry.attributes['revision']
  117. end
  118. paths << {:action => path.attributes['action'],
  119. :path => "/#{path.text}",
  120. :from_path => from_path ? "/#{from_path}" : nil,
  121. :from_revision => from_rev ? from_rev : nil
  122. }
  123. end
  124. paths.sort! { |x,y| x[:path] <=> y[:path] }
  125. revisions << Revision.new({:identifier => logentry.attributes['revision'],
  126. :scmid => logentry.attributes['node'],
  127. :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
  128. :time => Time.parse(logentry.elements['date'].text).localtime,
  129. :message => logentry.elements['msg'].text,
  130. :paths => paths
  131. })
  132. end
  133. rescue
  134. logger.debug($!)
  135. end
  136. end
  137. return nil if $? && $?.exitstatus != 0
  138. revisions
  139. end
  140. def diff(path, identifier_from, identifier_to=nil)
  141. path ||= ''
  142. if identifier_to
  143. identifier_to = identifier_to.to_i
  144. else
  145. identifier_to = identifier_from.to_i - 1
  146. end
  147. cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
  148. cmd << " -I #{target(path)}" unless path.empty?
  149. diff = []
  150. shellout(cmd) do |io|
  151. io.each_line do |line|
  152. diff << line
  153. end
  154. end
  155. return nil if $? && $?.exitstatus != 0
  156. diff
  157. end
  158. def cat(path, identifier=nil)
  159. cmd = "#{HG_BIN} -R #{target('')} cat"
  160. cmd << " -r " + (identifier ? identifier.to_s : "tip")
  161. cmd << " #{target(path)}"
  162. cat = nil
  163. shellout(cmd) do |io|
  164. io.binmode
  165. cat = io.read
  166. end
  167. return nil if $? && $?.exitstatus != 0
  168. cat
  169. end
  170. def annotate(path, identifier=nil)
  171. path ||= ''
  172. cmd = "#{HG_BIN} -R #{target('')}"
  173. cmd << " annotate -n -u"
  174. cmd << " -r " + (identifier ? identifier.to_s : "tip")
  175. cmd << " -r #{identifier.to_i}" if identifier
  176. cmd << " #{target(path)}"
  177. blame = Annotate.new
  178. shellout(cmd) do |io|
  179. io.each_line do |line|
  180. next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
  181. blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
  182. end
  183. end
  184. return nil if $? && $?.exitstatus != 0
  185. blame
  186. end
  187. end
  188. end
  189. end
  190. end