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.

abstract_adapter.rb 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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 'cgi'
  19. require 'redmine/scm/adapters'
  20. module Redmine
  21. module Scm
  22. module Adapters
  23. # @private
  24. class AbstractAdapter
  25. include Redmine::Utils::Shell
  26. # raised if scm command exited with error, e.g. unknown revision.
  27. class ScmCommandAborted < ::Redmine::Scm::Adapters::CommandFailed; end
  28. class << self
  29. def client_command
  30. ""
  31. end
  32. def shell_quote(str)
  33. Redmine::Utils::Shell.shell_quote str
  34. end
  35. def shell_quote_command
  36. Redmine::Utils::Shell.shell_quote_command client_command
  37. end
  38. # Returns the version of the scm client
  39. # Eg: [1, 5, 0] or [] if unknown
  40. def client_version
  41. []
  42. end
  43. # Returns the version string of the scm client
  44. # Eg: '1.5.0' or 'Unknown version' if unknown
  45. def client_version_string
  46. v = client_version || 'Unknown version'
  47. v.is_a?(Array) ? v.join('.') : v.to_s
  48. end
  49. # Returns true if the current client version is above
  50. # or equals the given one
  51. # If option is :unknown is set to true, it will return
  52. # true if the client version is unknown
  53. def client_version_above?(v, options={})
  54. ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
  55. end
  56. def client_available
  57. true
  58. end
  59. end
  60. def initialize(url, root_url=nil, login=nil, password=nil,
  61. path_encoding=nil)
  62. @url = url
  63. @login = login if login && !login.empty?
  64. @password = (password || "") if @login
  65. @root_url = root_url.blank? ? retrieve_root_url : root_url
  66. end
  67. def adapter_name
  68. 'Abstract'
  69. end
  70. def supports_cat?
  71. true
  72. end
  73. def supports_annotate?
  74. respond_to?(:annotate)
  75. end
  76. def root_url
  77. @root_url
  78. end
  79. def url
  80. @url
  81. end
  82. def path_encoding
  83. nil
  84. end
  85. # get info about the svn repository
  86. def info
  87. return nil
  88. end
  89. # Returns the entry identified by path and revision identifier
  90. # or nil if entry doesn't exist in the repository
  91. def entry(path=nil, identifier=nil)
  92. parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
  93. search_path = parts[0..-2].join('/')
  94. search_name = parts[-1]
  95. if search_path.blank? && search_name.blank?
  96. # Root entry
  97. Entry.new(:path => '', :kind => 'dir')
  98. else
  99. # Search for the entry in the parent directory
  100. es = entries(search_path, identifier)
  101. es ? es.detect {|e| e.name == search_name} : nil
  102. end
  103. end
  104. # Returns an Entries collection
  105. # or nil if the given path doesn't exist in the repository
  106. def entries(path=nil, identifier=nil, options={})
  107. return nil
  108. end
  109. def branches
  110. return nil
  111. end
  112. def tags
  113. return nil
  114. end
  115. def default_branch
  116. return nil
  117. end
  118. def properties(path, identifier=nil)
  119. return nil
  120. end
  121. def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  122. return nil
  123. end
  124. def diff(path, identifier_from, identifier_to=nil)
  125. return nil
  126. end
  127. def cat(path, identifier=nil)
  128. return nil
  129. end
  130. def with_leading_slash(path)
  131. path ||= ''
  132. path.start_with?('/') ? path : "/#{path}"
  133. end
  134. def with_trailing_slash(path)
  135. path ||= ''
  136. path.end_with?('/') ? path : "#{path}/"
  137. end
  138. def with_trailling_slash(path)
  139. ActiveSupport::Deprecation.warn 'Redmine::Scm::Adapters::AbstractAdapter#with_trailling_slash is ' \
  140. 'deprecated and will be removed in Redmine 6.0. Please use #with_trailing_slash instead.'
  141. with_trailing_slash(path)
  142. end
  143. def without_leading_slash(path)
  144. path ||= ''
  145. path.gsub(%r{^/+}, '')
  146. end
  147. def without_trailing_slash(path)
  148. path ||= ''
  149. path.end_with?('/') ? path[0..-2] : path
  150. end
  151. def without_trailling_slash(path)
  152. ActiveSupport::Deprecation.warn 'Redmine::Scm::Adapters::AbstractAdapter#without_trailling_slash is ' \
  153. 'deprecated and will be removed in Redmine 6.0. Please use #without_trailing_slash instead.'
  154. without_trailing_slash(path)
  155. end
  156. def valid_name?(name)
  157. return true if name.nil?
  158. return true if name.is_a?(Integer) && name > 0
  159. return true if name.is_a?(String) && name =~ /\A[0-9]*\z/
  160. false
  161. end
  162. private
  163. def retrieve_root_url
  164. info = self.info
  165. info ? info.root_url : nil
  166. end
  167. def target(path, sq=true)
  168. path ||= ''
  169. base = /^\//.match?(path) ? root_url : url
  170. str = "#{base}/#{path}".gsub(/[?<>\*]/, '')
  171. if sq
  172. str = shell_quote(str)
  173. end
  174. str
  175. end
  176. def logger
  177. self.class.logger
  178. end
  179. def shellout(cmd, options = {}, &block)
  180. self.class.shellout(cmd, options, &block)
  181. end
  182. # Path to the file where scm stderr output is logged
  183. # Returns nil if the log file is not writable
  184. def self.stderr_log_file
  185. if @stderr_log_file.nil?
  186. writable = false
  187. path = Redmine::Configuration['scm_stderr_log_file'].presence
  188. path ||= Rails.root.join("log/#{Rails.env}.scm.stderr.log").to_s
  189. if File.exist?(path)
  190. if File.file?(path) && File.writable?(path)
  191. writable = true
  192. else
  193. logger.warn("SCM log file (#{path}) is not writable")
  194. end
  195. else
  196. begin
  197. File.open(path, "w") {}
  198. writable = true
  199. rescue => e
  200. logger.warn("SCM log file (#{path}) cannot be created: #{e.message}")
  201. end
  202. end
  203. @stderr_log_file = writable ? path : false
  204. end
  205. @stderr_log_file || nil
  206. end
  207. private_class_method :stderr_log_file
  208. # Singleton class method is public
  209. class << self
  210. def logger
  211. Rails.logger
  212. end
  213. def shellout(cmd, options = {}, &block)
  214. if logger && logger.debug?
  215. logger.debug "Shelling out: #{strip_credential(cmd)}"
  216. # Capture stderr in a log file
  217. if stderr_log_file
  218. cmd = "#{cmd} 2>>#{shell_quote(stderr_log_file)}"
  219. end
  220. end
  221. begin
  222. mode = "r+"
  223. IO.popen(cmd, mode) do |io|
  224. io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
  225. io.close_write unless options[:write_stdin]
  226. yield(io) if block
  227. end
  228. rescue => e
  229. msg = strip_credential(e.message)
  230. # The command failed, log it and re-raise
  231. logmsg = "SCM command failed, "
  232. logmsg += "make sure that your SCM command (e.g. svn) is "
  233. logmsg += "in PATH (#{ENV['PATH']})\n"
  234. logmsg += "You can configure your scm commands in config/configuration.yml.\n"
  235. logmsg += "#{strip_credential(cmd)}\n"
  236. logmsg += "with: #{msg}"
  237. logger.error(logmsg)
  238. raise CommandFailed.new(msg)
  239. end
  240. end
  241. end
  242. # Hides username/password in a given command
  243. def self.strip_credential(cmd)
  244. q = (Redmine::Platform.mswin? ? '"' : "'")
  245. cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
  246. end
  247. private_class_method :strip_credential
  248. def strip_credential(cmd)
  249. self.class.strip_credential(cmd)
  250. end
  251. def scm_iconv(to, from, str)
  252. return if str.nil?
  253. return str if to == from && str.encoding.to_s == from
  254. str = str.dup
  255. str.force_encoding(from)
  256. begin
  257. str.encode(to)
  258. rescue => err
  259. logger.error("failed to convert from #{from} to #{to}. #{err}")
  260. nil
  261. end
  262. end
  263. def parse_xml(xml)
  264. if RUBY_PLATFORM == 'java'
  265. xml = xml.sub(%r{<\?xml[^>]*\?>}, '')
  266. end
  267. ActiveSupport::XmlMini.parse(xml)
  268. end
  269. end
  270. class Entries < Array
  271. def sort_by_name
  272. dup.sort! do |x, y|
  273. if x.kind == y.kind
  274. x.name.to_s <=> y.name.to_s
  275. else
  276. x.kind <=> y.kind
  277. end
  278. end
  279. end
  280. def revisions
  281. revisions ||= Revisions.new(filter_map{|entry| entry.lastrev})
  282. end
  283. end
  284. class Info
  285. attr_accessor :root_url, :lastrev
  286. def initialize(attributes={})
  287. self.root_url = attributes[:root_url] if attributes[:root_url]
  288. self.lastrev = attributes[:lastrev]
  289. end
  290. end
  291. class Entry
  292. attr_accessor :name, :path, :kind, :size, :lastrev, :changeset
  293. def initialize(attributes={})
  294. self.name = attributes[:name] if attributes[:name]
  295. self.path = attributes[:path] if attributes[:path]
  296. self.kind = attributes[:kind] if attributes[:kind]
  297. self.size = attributes[:size].to_i if attributes[:size]
  298. self.lastrev = attributes[:lastrev]
  299. end
  300. def is_file?
  301. self.kind == 'file'
  302. end
  303. def is_dir?
  304. self.kind == 'dir'
  305. end
  306. def is_text?
  307. Redmine::MimeType.is_type?('text', name)
  308. end
  309. def author
  310. if changeset
  311. changeset.author.to_s
  312. elsif lastrev
  313. Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first)
  314. end
  315. end
  316. end
  317. class Revisions < Array
  318. def latest
  319. sort do |x, y|
  320. unless x.time.nil? or y.time.nil?
  321. x.time <=> y.time
  322. else
  323. 0
  324. end
  325. end.last
  326. end
  327. end
  328. class Revision
  329. attr_accessor :scmid, :name, :author, :time, :message,
  330. :paths, :revision, :branch, :identifier,
  331. :parents
  332. def initialize(attributes={})
  333. self.identifier = attributes[:identifier]
  334. self.scmid = attributes[:scmid]
  335. self.name = attributes[:name] || self.identifier
  336. self.author = attributes[:author]
  337. self.time = attributes[:time]
  338. self.message = attributes[:message] || ""
  339. self.paths = attributes[:paths]
  340. self.revision = attributes[:revision]
  341. self.branch = attributes[:branch]
  342. self.parents = attributes[:parents]
  343. end
  344. # Returns the readable identifier.
  345. def format_identifier
  346. self.identifier.to_s
  347. end
  348. def ==(other)
  349. if other.nil?
  350. false
  351. elsif scmid.present?
  352. scmid == other.scmid
  353. elsif identifier.present?
  354. identifier == other.identifier
  355. elsif revision.present?
  356. revision == other.revision
  357. end
  358. end
  359. end
  360. class Annotate
  361. attr_reader :lines, :revisions
  362. def initialize
  363. @lines = []
  364. @revisions = []
  365. end
  366. def add_line(line, revision)
  367. @lines << line
  368. @revisions << revision
  369. end
  370. def content
  371. content = lines.join("\n")
  372. end
  373. def empty?
  374. lines.empty?
  375. end
  376. end
  377. class Branch < String
  378. attr_accessor :revision, :scmid
  379. end
  380. module ScmData
  381. def self.binary?(data)
  382. unless data.empty?
  383. data.count("^ -~", "^\r\n").fdiv(data.size) > 0.3 || data.index("\x00")
  384. end
  385. end
  386. end
  387. end
  388. end
  389. end