You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mercurial_adapter.rb 12KB

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