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

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