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.

bazaar_adapter.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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. module Redmine
  20. module Scm
  21. module Adapters
  22. class BazaarAdapter < AbstractAdapter
  23. # Bazaar executable name
  24. BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr"
  25. class << self
  26. def client_command
  27. @@bin ||= BZR_BIN
  28. end
  29. def sq_bin
  30. @@sq_bin ||= shell_quote_command
  31. end
  32. def client_version
  33. @@client_version ||= (scm_command_version || [])
  34. end
  35. def client_available
  36. !client_version.empty?
  37. end
  38. def scm_command_version
  39. scm_version = scm_version_from_command_line.b
  40. if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
  41. m[2].scan(%r{\d+}).collect(&:to_i)
  42. end
  43. end
  44. def scm_version_from_command_line
  45. shellout("#{sq_bin} --version") {|io| io.read}.to_s
  46. end
  47. end
  48. def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
  49. @url = url
  50. @root_url = url
  51. @path_encoding = 'UTF-8'
  52. # do not call *super* for non ASCII repository path
  53. end
  54. def bzr_path_encodig=(encoding)
  55. @path_encoding = encoding
  56. end
  57. # Get info about the repository
  58. def info
  59. cmd_args = %w|revno|
  60. cmd_args << bzr_target('')
  61. info = nil
  62. scm_cmd(*cmd_args) do |io|
  63. if io.read =~ %r{^(\d+)\r?$}
  64. info = Info.new(
  65. {
  66. :root_url => url,
  67. :lastrev =>
  68. Revision.new({:identifier => $1})
  69. }
  70. )
  71. end
  72. end
  73. info
  74. rescue ScmCommandAborted
  75. return nil
  76. end
  77. # Returns an Entries collection
  78. # or nil if the given path doesn't exist in the repository
  79. def entries(path=nil, identifier=nil, options={})
  80. path ||= ''
  81. entries = Entries.new
  82. identifier = -1 unless identifier && identifier.to_i > 0
  83. cmd_args = %w|ls -v --show-ids|
  84. cmd_args << "-r#{identifier.to_i}"
  85. cmd_args << bzr_target(path)
  86. scm_cmd(*cmd_args) do |io|
  87. prefix_utf8 = "#{url}/#{path}".tr('\\', '/')
  88. logger.debug "PREFIX: #{prefix_utf8}"
  89. prefix = scm_iconv(@path_encoding, 'UTF-8', prefix_utf8).b
  90. re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$}
  91. io.each_line do |line|
  92. next unless line =~ re
  93. name_locale, slash, revision = $3.strip, $4, $5.strip
  94. name = scm_iconv('UTF-8', @path_encoding, name_locale)
  95. entries << Entry.new(
  96. {
  97. :name => name,
  98. :path => ((path.empty? ? "" : "#{path}/") + name),
  99. :kind => (slash.blank? ? 'file' : 'dir'),
  100. :size => nil,
  101. :lastrev => Revision.new(:revision => revision)
  102. }
  103. )
  104. end
  105. end
  106. if logger && logger.debug?
  107. logger.debug("Found #{entries.size} entries in the repository for #{target(path)}")
  108. end
  109. entries.sort_by_name
  110. rescue ScmCommandAborted
  111. return nil
  112. end
  113. def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  114. path ||= ''
  115. identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1'
  116. identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
  117. revisions = Revisions.new
  118. cmd_args = %w|log -v --show-ids|
  119. cmd_args << "-r#{identifier_to}..#{identifier_from}"
  120. cmd_args << bzr_target(path)
  121. scm_cmd(*cmd_args) do |io|
  122. revision = nil
  123. parsing = nil
  124. io.each_line do |line|
  125. if /^----/.match?(line)
  126. revisions << revision if revision
  127. revision = Revision.new(:paths => [], :message => '')
  128. parsing = nil
  129. else
  130. next unless revision
  131. if line =~ /^revno: (\d+)($|\s\[merge\]$)/
  132. revision.identifier = $1.to_i
  133. elsif line =~ /^committer: (.+)$/
  134. revision.author = $1.strip
  135. elsif line =~ /^revision-id:(.+)$/
  136. revision.scmid = $1.strip
  137. elsif line =~ /^timestamp: (.+)$/
  138. revision.time = Time.parse($1).localtime
  139. elsif /^ -----/.match?(line)
  140. # partial revisions
  141. parsing = nil unless parsing == 'message'
  142. elsif line =~ /^(message|added|modified|removed|renamed):/
  143. parsing = $1
  144. elsif line =~ /^ (.*)$/
  145. if parsing == 'message'
  146. revision.message += "#{$1}\n"
  147. else
  148. if $1 =~ /^(.*)\s+(\S+)$/
  149. path_locale = $1.strip
  150. path = scm_iconv('UTF-8', @path_encoding, path_locale)
  151. revid = $2
  152. case parsing
  153. when 'added'
  154. revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid}
  155. when 'modified'
  156. revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid}
  157. when 'removed'
  158. revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid}
  159. when 'renamed'
  160. new_path = path.split('=>').last
  161. if new_path
  162. revision.paths << {:action => 'M', :path => "/#{new_path.strip}",
  163. :revision => revid}
  164. end
  165. end
  166. end
  167. end
  168. else
  169. parsing = nil
  170. end
  171. end
  172. end
  173. revisions << revision if revision
  174. end
  175. revisions
  176. rescue ScmCommandAborted
  177. return nil
  178. end
  179. def diff(path, identifier_from, identifier_to=nil)
  180. path ||= ''
  181. if identifier_to
  182. identifier_to = identifier_to.to_i
  183. else
  184. identifier_to = identifier_from.to_i - 1
  185. end
  186. if identifier_from
  187. identifier_from = identifier_from.to_i
  188. end
  189. diff = []
  190. cmd_args = %w|diff|
  191. cmd_args << "-r#{identifier_to}..#{identifier_from}"
  192. cmd_args << bzr_target(path)
  193. scm_cmd_no_raise(*cmd_args) do |io|
  194. io.each_line do |line|
  195. diff << line
  196. end
  197. end
  198. diff
  199. end
  200. def cat(path, identifier=nil)
  201. cat = nil
  202. cmd_args = %w|cat|
  203. cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0
  204. cmd_args << bzr_target(path)
  205. scm_cmd(*cmd_args) do |io|
  206. io.binmode
  207. cat = io.read
  208. end
  209. cat
  210. rescue ScmCommandAborted
  211. return nil
  212. end
  213. def annotate(path, identifier=nil)
  214. blame = Annotate.new
  215. cmd_args = %w|annotate -q --all|
  216. cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0
  217. cmd_args << bzr_target(path)
  218. scm_cmd(*cmd_args) do |io|
  219. author = nil
  220. identifier = nil
  221. io.each_line do |line|
  222. next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$}
  223. rev = $1
  224. blame.
  225. add_line(
  226. $3.rstrip,
  227. Revision.new(
  228. :identifier => rev,
  229. :revision => rev,
  230. :author => $2.strip
  231. )
  232. )
  233. end
  234. end
  235. blame
  236. rescue ScmCommandAborted
  237. return nil
  238. end
  239. def self.branch_conf_path(path)
  240. return if path.nil?
  241. m = path.match(%r{^(.*[/\\])\.bzr.*$})
  242. bcp = (m ? m[1] : path).gsub(%r{[\/\\]$}, "")
  243. File.join(bcp, ".bzr", "branch", "branch.conf")
  244. end
  245. def append_revisions_only
  246. return @aro unless @aro.nil?
  247. @aro = false
  248. bcp = self.class.branch_conf_path(url)
  249. if bcp && File.exist?(bcp)
  250. begin
  251. f = File.open(bcp, "r")
  252. cnt = 0
  253. f.each_line do |line|
  254. l = line.chomp.to_s
  255. if l =~ /^\s*append_revisions_only\s*=\s*(\w+)\s*$/
  256. str_aro = $1
  257. if str_aro.casecmp("TRUE") == 0
  258. @aro = true
  259. cnt += 1
  260. elsif str_aro.casecmp("FALSE") == 0
  261. @aro = false
  262. cnt += 1
  263. end
  264. if cnt > 1
  265. @aro = false
  266. break
  267. end
  268. end
  269. end
  270. ensure
  271. f.close
  272. end
  273. end
  274. @aro
  275. end
  276. def scm_cmd(*args, &block)
  277. full_args = []
  278. full_args += args
  279. full_args_locale = []
  280. full_args.map do |e|
  281. full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e)
  282. end
  283. ret = shellout(
  284. self.class.sq_bin + ' ' +
  285. full_args_locale.map {|e| shell_quote e.to_s}.join(' '),
  286. &block
  287. )
  288. if $? && $?.exitstatus != 0
  289. raise ScmCommandAborted, "bzr exited with non-zero status: #{$?.exitstatus}"
  290. end
  291. ret
  292. end
  293. private :scm_cmd
  294. def scm_cmd_no_raise(*args, &block)
  295. full_args = []
  296. full_args += args
  297. full_args_locale = []
  298. full_args.map do |e|
  299. full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e)
  300. end
  301. ret = shellout(
  302. self.class.sq_bin + ' ' +
  303. full_args_locale.map {|e| shell_quote e.to_s}.join(' '),
  304. &block
  305. )
  306. ret
  307. end
  308. private :scm_cmd_no_raise
  309. def bzr_target(path)
  310. target(path, false)
  311. end
  312. private :bzr_target
  313. end
  314. end
  315. end
  316. end