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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 =
  62. Info.new(
  63. {
  64. :root_url => doc['info']['entry']['repository']['root']['__content__'],
  65. :lastrev =>
  66. Revision.new(
  67. {
  68. :identifier => doc['info']['entry']['commit']['revision'],
  69. :time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime,
  70. :author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "")
  71. }
  72. )
  73. }
  74. )
  75. rescue
  76. end
  77. end
  78. return nil if $? && $?.exitstatus != 0
  79. info
  80. rescue CommandFailed
  81. return nil
  82. end
  83. # Returns an Entries collection
  84. # or nil if the given path doesn't exist in the repository
  85. def entries(path=nil, identifier=nil, options={})
  86. path ||= ''
  87. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  88. entries = Entries.new
  89. cmd = +"#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}"
  90. cmd << credentials_string
  91. shellout(cmd) do |io|
  92. output = io.read.force_encoding('UTF-8')
  93. begin
  94. doc = parse_xml(output)
  95. each_xml_element(doc['lists']['list'], 'entry') do |entry|
  96. commit = entry['commit']
  97. commit_date = commit['date']
  98. # Skip directory if there is no commit date (usually that
  99. # means that we don't have read access to it)
  100. next if entry['kind'] == 'dir' && commit_date.nil?
  101. name = entry['name']['__content__']
  102. entries <<
  103. Entry.new(
  104. {
  105. :name => Addressable::URI.unescape(name),
  106. :path => ((path.empty? ? "" : "#{path}/") + name),
  107. :kind => entry['kind'],
  108. :size => ((s = entry['size']) ? s['__content__'].to_i : nil),
  109. :lastrev =>
  110. Revision.new(
  111. {
  112. :identifier => commit['revision'],
  113. :time => Time.parse(commit_date['__content__'].to_s).localtime,
  114. :author => ((a = commit['author']) ? a['__content__'] : nil)
  115. }
  116. )
  117. }
  118. )
  119. end
  120. rescue => e
  121. logger.error("Error parsing svn output: #{e.message}")
  122. logger.error("Output was:\n #{output}")
  123. end
  124. end
  125. return nil if $? && $?.exitstatus != 0
  126. logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
  127. entries.sort_by_name
  128. end
  129. def properties(path, identifier=nil)
  130. # proplist xml output supported in svn 1.5.0 and higher
  131. return nil unless self.class.client_version_above?([1, 5, 0])
  132. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  133. cmd = +"#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}"
  134. cmd << credentials_string
  135. properties = {}
  136. shellout(cmd) do |io|
  137. output = io.read.force_encoding('UTF-8')
  138. begin
  139. doc = parse_xml(output)
  140. each_xml_element(doc['properties']['target'], 'property') do |property|
  141. properties[property['name']] = property['__content__'].to_s
  142. end
  143. rescue
  144. end
  145. end
  146. return nil if $? && $?.exitstatus != 0
  147. properties
  148. end
  149. def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  150. path ||= ''
  151. identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
  152. identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1
  153. revisions = Revisions.new
  154. cmd = +"#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}"
  155. cmd << credentials_string
  156. cmd << " --verbose " if options[:with_paths]
  157. cmd << " --limit #{options[:limit].to_i}" if options[:limit]
  158. cmd << ' ' + target(path)
  159. shellout(cmd) do |io|
  160. output = io.read.force_encoding('UTF-8')
  161. begin
  162. doc = parse_xml(output)
  163. each_xml_element(doc['log'], 'logentry') do |logentry|
  164. paths = []
  165. if logentry['paths'] && logentry['paths']['path']
  166. each_xml_element(logentry['paths'], 'path') do |path|
  167. paths <<
  168. {
  169. :action => path['action'],
  170. :path => path['__content__'],
  171. :from_path => path['copyfrom-path'],
  172. :from_revision => path['copyfrom-rev']
  173. }
  174. end
  175. end
  176. paths.sort_by! {|e| e[:path]}
  177. revisions <<
  178. Revision.new(
  179. {
  180. :identifier => logentry['revision'],
  181. :author => (logentry['author'] ? logentry['author']['__content__'] : ""),
  182. :time => Time.parse(logentry['date']['__content__'].to_s).localtime,
  183. :message => logentry['msg']['__content__'],
  184. :paths => paths
  185. }
  186. )
  187. end
  188. rescue
  189. end
  190. end
  191. return nil if $? && $?.exitstatus != 0
  192. revisions
  193. end
  194. def diff(path, identifier_from, identifier_to=nil)
  195. path ||= ''
  196. identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
  197. identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
  198. cmd = +"#{self.class.sq_bin} diff -r "
  199. cmd << "#{identifier_to}:"
  200. cmd << "#{identifier_from}"
  201. cmd << " #{target(path)}@#{identifier_from}"
  202. cmd << credentials_string
  203. diff = []
  204. shellout(cmd) do |io|
  205. io.each_line do |line|
  206. diff << line
  207. end
  208. end
  209. return nil if $? && $?.exitstatus != 0
  210. diff
  211. end
  212. def cat(path, identifier=nil)
  213. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  214. cmd = +"#{self.class.sq_bin} cat #{target(path)}@#{identifier}"
  215. cmd << credentials_string
  216. cat = nil
  217. shellout(cmd) do |io|
  218. io.binmode
  219. cat = io.read
  220. end
  221. return nil if $? && $?.exitstatus != 0
  222. cat
  223. end
  224. def annotate(path, identifier=nil)
  225. identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
  226. cmd = +"#{self.class.sq_bin} blame #{target(path)}@#{identifier}"
  227. cmd << credentials_string
  228. blame = Annotate.new
  229. shellout(cmd) do |io|
  230. io.each_line do |line|
  231. next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
  232. rev = $1
  233. blame.add_line(
  234. $3.rstrip,
  235. Revision.new(
  236. :identifier => rev,
  237. :revision => rev,
  238. :author => $2.strip
  239. )
  240. )
  241. end
  242. end
  243. return nil if $? && $?.exitstatus != 0
  244. blame
  245. end
  246. private
  247. def credentials_string
  248. str = +''
  249. str << " --username #{shell_quote(@login)}" unless @login.blank?
  250. str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
  251. str << " --no-auth-cache --non-interactive"
  252. str
  253. end
  254. # Helper that iterates over the child elements of a xml node
  255. # MiniXml returns a hash when a single child is found
  256. # or an array of hashes for multiple children
  257. def each_xml_element(node, name)
  258. if node && node[name]
  259. if node[name].is_a?(Hash)
  260. yield node[name]
  261. else
  262. node[name].each do |element|
  263. yield element
  264. end
  265. end
  266. end
  267. end
  268. def target(path = '')
  269. base = /^\//.match?(path) ? root_url : url
  270. uri = "#{base}/#{path}"
  271. uri = Addressable::URI.encode(uri)
  272. shell_quote(uri.gsub(/[?<>\*]/, ''))
  273. end
  274. end
  275. end
  276. end
  277. end