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.

subversion_adapter.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 'uri'
  20. module Redmine
  21. module Scm
  22. module Adapters
  23. class SubversionAdapter < AbstractAdapter
  24. # SVN executable name
  25. SVN_BIN = Redmine::Configuration['scm_subversion_command'] || "svn"
  26. class << self
  27. def client_command
  28. @@bin ||= SVN_BIN
  29. end
  30. def sq_bin
  31. @@sq_bin ||= shell_quote_command
  32. end
  33. def client_version
  34. @@client_version ||= (svn_binary_version || [])
  35. end
  36. def client_available
  37. # --xml options are introduced in 1.3.
  38. # http://subversion.apache.org/docs/release-notes/1.3.html
  39. client_version_above?([1, 3])
  40. end
  41. def svn_binary_version
  42. scm_version = scm_version_from_command_line.b
  43. if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
  44. m[2].scan(%r{\d+}).collect(&:to_i)
  45. end
  46. end
  47. def scm_version_from_command_line
  48. shellout("#{sq_bin} --version") { |io| io.read }.to_s
  49. end
  50. end
  51. # Get info about the svn repository
  52. def info
  53. cmd = +"#{self.class.sq_bin} info --xml #{target}"
  54. cmd << credentials_string
  55. info = nil
  56. shellout(cmd) do |io|
  57. output = io.read.force_encoding('UTF-8')
  58. begin
  59. doc = parse_xml(output)
  60. # root_url = doc.elements["info/entry/repository/root"].text
  61. info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'],
  62. :lastrev => Revision.new({
  63. :identifier => doc['info']['entry']['commit']['revision'],
  64. :time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime,
  65. :author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "")
  66. })
  67. })
  68. rescue
  69. end
  70. end
  71. return nil if $? && $?.exitstatus != 0
  72. info
  73. rescue CommandFailed
  74. return nil
  75. end
  76. # Returns an Entries collection
  77. # or nil if the given path doesn't exist in the repository
  78. def entries(path=nil, identifier=nil, options={})
  79. path ||= ''
  80. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  81. entries = Entries.new
  82. cmd = +"#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}"
  83. cmd << credentials_string
  84. shellout(cmd) do |io|
  85. output = io.read.force_encoding('UTF-8')
  86. begin
  87. doc = parse_xml(output)
  88. each_xml_element(doc['lists']['list'], 'entry') do |entry|
  89. commit = entry['commit']
  90. commit_date = commit['date']
  91. # Skip directory if there is no commit date (usually that
  92. # means that we don't have read access to it)
  93. next if entry['kind'] == 'dir' && commit_date.nil?
  94. name = entry['name']['__content__']
  95. entries << Entry.new({:name => URI.unescape(name),
  96. :path => ((path.empty? ? "" : "#{path}/") + name),
  97. :kind => entry['kind'],
  98. :size => ((s = entry['size']) ? s['__content__'].to_i : nil),
  99. :lastrev => Revision.new({
  100. :identifier => commit['revision'],
  101. :time => Time.parse(commit_date['__content__'].to_s).localtime,
  102. :author => ((a = commit['author']) ? a['__content__'] : nil)
  103. })
  104. })
  105. end
  106. rescue => e
  107. logger.error("Error parsing svn output: #{e.message}")
  108. logger.error("Output was:\n #{output}")
  109. end
  110. end
  111. return nil if $? && $?.exitstatus != 0
  112. logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
  113. entries.sort_by_name
  114. end
  115. def properties(path, identifier=nil)
  116. # proplist xml output supported in svn 1.5.0 and higher
  117. return nil unless self.class.client_version_above?([1, 5, 0])
  118. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  119. cmd = +"#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}"
  120. cmd << credentials_string
  121. properties = {}
  122. shellout(cmd) do |io|
  123. output = io.read.force_encoding('UTF-8')
  124. begin
  125. doc = parse_xml(output)
  126. each_xml_element(doc['properties']['target'], 'property') do |property|
  127. properties[ property['name'] ] = property['__content__'].to_s
  128. end
  129. rescue
  130. end
  131. end
  132. return nil if $? && $?.exitstatus != 0
  133. properties
  134. end
  135. def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  136. path ||= ''
  137. identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
  138. identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1
  139. revisions = Revisions.new
  140. cmd = +"#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}"
  141. cmd << credentials_string
  142. cmd << " --verbose " if options[:with_paths]
  143. cmd << " --limit #{options[:limit].to_i}" if options[:limit]
  144. cmd << ' ' + target(path)
  145. shellout(cmd) do |io|
  146. output = io.read.force_encoding('UTF-8')
  147. begin
  148. doc = parse_xml(output)
  149. each_xml_element(doc['log'], 'logentry') do |logentry|
  150. paths = []
  151. each_xml_element(logentry['paths'], 'path') do |path|
  152. paths << {:action => path['action'],
  153. :path => path['__content__'],
  154. :from_path => path['copyfrom-path'],
  155. :from_revision => path['copyfrom-rev']
  156. }
  157. end if logentry['paths'] && logentry['paths']['path']
  158. paths.sort_by! {|e| e[:path]}
  159. revisions << Revision.new({:identifier => logentry['revision'],
  160. :author => (logentry['author'] ? logentry['author']['__content__'] : ""),
  161. :time => Time.parse(logentry['date']['__content__'].to_s).localtime,
  162. :message => logentry['msg']['__content__'],
  163. :paths => paths
  164. })
  165. end
  166. rescue
  167. end
  168. end
  169. return nil if $? && $?.exitstatus != 0
  170. revisions
  171. end
  172. def diff(path, identifier_from, identifier_to=nil)
  173. path ||= ''
  174. identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
  175. identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
  176. cmd = +"#{self.class.sq_bin} diff -r "
  177. cmd << "#{identifier_to}:"
  178. cmd << "#{identifier_from}"
  179. cmd << " #{target(path)}@#{identifier_from}"
  180. cmd << credentials_string
  181. diff = []
  182. shellout(cmd) do |io|
  183. io.each_line do |line|
  184. diff << line
  185. end
  186. end
  187. return nil if $? && $?.exitstatus != 0
  188. diff
  189. end
  190. def cat(path, identifier=nil)
  191. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  192. cmd = +"#{self.class.sq_bin} cat #{target(path)}@#{identifier}"
  193. cmd << credentials_string
  194. cat = nil
  195. shellout(cmd) do |io|
  196. io.binmode
  197. cat = io.read
  198. end
  199. return nil if $? && $?.exitstatus != 0
  200. cat
  201. end
  202. def annotate(path, identifier=nil)
  203. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  204. cmd = +"#{self.class.sq_bin} blame #{target(path)}@#{identifier}"
  205. cmd << credentials_string
  206. blame = Annotate.new
  207. shellout(cmd) do |io|
  208. io.each_line do |line|
  209. next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
  210. rev = $1
  211. blame.add_line($3.rstrip,
  212. Revision.new(
  213. :identifier => rev,
  214. :revision => rev,
  215. :author => $2.strip
  216. ))
  217. end
  218. end
  219. return nil if $? && $?.exitstatus != 0
  220. blame
  221. end
  222. private
  223. def credentials_string
  224. str = +''
  225. str << " --username #{shell_quote(@login)}" unless @login.blank?
  226. str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
  227. str << " --no-auth-cache --non-interactive"
  228. str
  229. end
  230. # Helper that iterates over the child elements of a xml node
  231. # MiniXml returns a hash when a single child is found
  232. # or an array of hashes for multiple children
  233. def each_xml_element(node, name)
  234. if node && node[name]
  235. if node[name].is_a?(Hash)
  236. yield node[name]
  237. else
  238. node[name].each do |element|
  239. yield element
  240. end
  241. end
  242. end
  243. end
  244. def target(path = '')
  245. base = /^\//.match?(path) ? root_url : url
  246. uri = "#{base}/#{path}"
  247. uri = URI.escape(URI.escape(uri), '[]')
  248. shell_quote(uri.gsub(/[?<>\*]/, ''))
  249. end
  250. end
  251. end
  252. end
  253. end