annotate lib/redmine/scm/adapters/bazaar_adapter.rb @ 1169:492ff72268e3 bug_521

Close obsolete branch bug_521
author Chris Cannam
date Thu, 18 Oct 2012 10:42:48 +0100
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'redmine/scm/adapters/abstract_adapter'
Chris@0 19
Chris@0 20 module Redmine
Chris@0 21 module Scm
Chris@245 22 module Adapters
Chris@0 23 class BazaarAdapter < AbstractAdapter
Chris@245 24
Chris@0 25 # Bazaar executable name
Chris@210 26 BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr"
Chris@245 27
Chris@245 28 class << self
Chris@245 29 def client_command
Chris@245 30 @@bin ||= BZR_BIN
Chris@245 31 end
Chris@245 32
Chris@245 33 def sq_bin
Chris@909 34 @@sq_bin ||= shell_quote_command
Chris@245 35 end
Chris@245 36
Chris@245 37 def client_version
Chris@245 38 @@client_version ||= (scm_command_version || [])
Chris@245 39 end
Chris@245 40
Chris@245 41 def client_available
Chris@245 42 !client_version.empty?
Chris@245 43 end
Chris@245 44
Chris@245 45 def scm_command_version
Chris@245 46 scm_version = scm_version_from_command_line.dup
Chris@245 47 if scm_version.respond_to?(:force_encoding)
Chris@245 48 scm_version.force_encoding('ASCII-8BIT')
Chris@245 49 end
Chris@245 50 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
Chris@245 51 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@245 52 end
Chris@245 53 end
Chris@245 54
Chris@245 55 def scm_version_from_command_line
Chris@245 56 shellout("#{sq_bin} --version") { |io| io.read }.to_s
Chris@245 57 end
Chris@245 58 end
Chris@245 59
Chris@0 60 # Get info about the repository
Chris@0 61 def info
Chris@909 62 cmd_args = %w|revno|
Chris@909 63 cmd_args << bzr_target('')
Chris@0 64 info = nil
Chris@909 65 scm_cmd(*cmd_args) do |io|
Chris@0 66 if io.read =~ %r{^(\d+)\r?$}
Chris@0 67 info = Info.new({:root_url => url,
Chris@0 68 :lastrev => Revision.new({
Chris@0 69 :identifier => $1
Chris@0 70 })
Chris@0 71 })
Chris@0 72 end
Chris@0 73 end
Chris@0 74 info
Chris@909 75 rescue ScmCommandAborted
Chris@0 76 return nil
Chris@0 77 end
Chris@245 78
Chris@0 79 # Returns an Entries collection
Chris@0 80 # or nil if the given path doesn't exist in the repository
Chris@441 81 def entries(path=nil, identifier=nil, options={})
Chris@0 82 path ||= ''
Chris@0 83 entries = Entries.new
Chris@441 84 identifier = -1 unless identifier && identifier.to_i > 0
Chris@909 85 cmd_args = %w|ls -v --show-ids|
Chris@909 86 cmd_args << "-r#{identifier.to_i}"
Chris@909 87 cmd_args << bzr_target(path)
Chris@909 88 scm_cmd(*cmd_args) do |io|
Chris@0 89 prefix = "#{url}/#{path}".gsub('\\', '/')
Chris@0 90 logger.debug "PREFIX: #{prefix}"
Chris@0 91 re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$}
Chris@0 92 io.each_line do |line|
Chris@0 93 next unless line =~ re
Chris@0 94 entries << Entry.new({:name => $3.strip,
Chris@0 95 :path => ((path.empty? ? "" : "#{path}/") + $3.strip),
Chris@0 96 :kind => ($4.blank? ? 'file' : 'dir'),
Chris@0 97 :size => nil,
Chris@0 98 :lastrev => Revision.new(:revision => $5.strip)
Chris@0 99 })
Chris@0 100 end
Chris@0 101 end
Chris@909 102 if logger && logger.debug?
Chris@909 103 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}")
Chris@909 104 end
Chris@0 105 entries.sort_by_name
Chris@909 106 rescue ScmCommandAborted
Chris@909 107 return nil
Chris@0 108 end
Chris@245 109
Chris@0 110 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@0 111 path ||= ''
Chris@119 112 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1'
Chris@119 113 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
Chris@0 114 revisions = Revisions.new
Chris@909 115 cmd_args = %w|log -v --show-ids|
Chris@909 116 cmd_args << "-r#{identifier_to}..#{identifier_from}"
Chris@909 117 cmd_args << bzr_target(path)
Chris@909 118 scm_cmd(*cmd_args) do |io|
Chris@0 119 revision = nil
Chris@909 120 parsing = nil
Chris@0 121 io.each_line do |line|
Chris@0 122 if line =~ /^----/
Chris@0 123 revisions << revision if revision
Chris@0 124 revision = Revision.new(:paths => [], :message => '')
Chris@0 125 parsing = nil
Chris@0 126 else
Chris@0 127 next unless revision
Chris@0 128 if line =~ /^revno: (\d+)($|\s\[merge\]$)/
Chris@0 129 revision.identifier = $1.to_i
Chris@0 130 elsif line =~ /^committer: (.+)$/
Chris@0 131 revision.author = $1.strip
Chris@0 132 elsif line =~ /^revision-id:(.+)$/
Chris@0 133 revision.scmid = $1.strip
Chris@0 134 elsif line =~ /^timestamp: (.+)$/
Chris@0 135 revision.time = Time.parse($1).localtime
Chris@0 136 elsif line =~ /^ -----/
Chris@0 137 # partial revisions
Chris@0 138 parsing = nil unless parsing == 'message'
Chris@0 139 elsif line =~ /^(message|added|modified|removed|renamed):/
Chris@0 140 parsing = $1
Chris@0 141 elsif line =~ /^ (.*)$/
Chris@0 142 if parsing == 'message'
Chris@0 143 revision.message << "#{$1}\n"
Chris@0 144 else
Chris@0 145 if $1 =~ /^(.*)\s+(\S+)$/
Chris@0 146 path = $1.strip
Chris@0 147 revid = $2
Chris@0 148 case parsing
Chris@0 149 when 'added'
Chris@0 150 revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid}
Chris@0 151 when 'modified'
Chris@0 152 revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid}
Chris@0 153 when 'removed'
Chris@0 154 revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid}
Chris@0 155 when 'renamed'
Chris@0 156 new_path = path.split('=>').last
Chris@0 157 revision.paths << {:action => 'M', :path => "/#{new_path.strip}", :revision => revid} if new_path
Chris@0 158 end
Chris@0 159 end
Chris@0 160 end
Chris@0 161 else
Chris@0 162 parsing = nil
Chris@0 163 end
Chris@0 164 end
Chris@0 165 end
Chris@0 166 revisions << revision if revision
Chris@0 167 end
Chris@0 168 revisions
Chris@909 169 rescue ScmCommandAborted
Chris@909 170 return nil
Chris@0 171 end
Chris@245 172
Chris@0 173 def diff(path, identifier_from, identifier_to=nil)
Chris@0 174 path ||= ''
Chris@0 175 if identifier_to
Chris@441 176 identifier_to = identifier_to.to_i
Chris@0 177 else
Chris@0 178 identifier_to = identifier_from.to_i - 1
Chris@0 179 end
Chris@119 180 if identifier_from
Chris@119 181 identifier_from = identifier_from.to_i
Chris@119 182 end
Chris@0 183 diff = []
Chris@909 184 cmd_args = %w|diff|
Chris@909 185 cmd_args << "-r#{identifier_to}..#{identifier_from}"
Chris@909 186 cmd_args << bzr_target(path)
Chris@909 187 scm_cmd_no_raise(*cmd_args) do |io|
Chris@0 188 io.each_line do |line|
Chris@0 189 diff << line
Chris@0 190 end
Chris@0 191 end
Chris@0 192 diff
Chris@0 193 end
Chris@245 194
Chris@0 195 def cat(path, identifier=nil)
Chris@0 196 cat = nil
Chris@909 197 cmd_args = %w|cat|
Chris@909 198 cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0
Chris@909 199 cmd_args << bzr_target(path)
Chris@909 200 scm_cmd(*cmd_args) do |io|
Chris@0 201 io.binmode
Chris@0 202 cat = io.read
Chris@0 203 end
Chris@0 204 cat
Chris@909 205 rescue ScmCommandAborted
Chris@909 206 return nil
Chris@0 207 end
Chris@245 208
Chris@0 209 def annotate(path, identifier=nil)
Chris@0 210 blame = Annotate.new
Chris@909 211 cmd_args = %w|annotate -q --all|
Chris@909 212 cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0
Chris@909 213 cmd_args << bzr_target(path)
Chris@909 214 scm_cmd(*cmd_args) do |io|
Chris@909 215 author = nil
Chris@0 216 identifier = nil
Chris@0 217 io.each_line do |line|
Chris@0 218 next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$}
Chris@441 219 rev = $1
Chris@441 220 blame.add_line($3.rstrip,
Chris@441 221 Revision.new(
Chris@441 222 :identifier => rev,
Chris@441 223 :revision => rev,
Chris@441 224 :author => $2.strip
Chris@441 225 ))
Chris@0 226 end
Chris@0 227 end
Chris@0 228 blame
Chris@909 229 rescue ScmCommandAborted
Chris@909 230 return nil
Chris@0 231 end
Chris@909 232
Chris@909 233 def self.branch_conf_path(path)
Chris@909 234 bcp = nil
Chris@909 235 m = path.match(%r{^(.*[/\\])\.bzr.*$})
Chris@909 236 if m
Chris@909 237 bcp = m[1]
Chris@909 238 else
Chris@909 239 bcp = path
Chris@909 240 end
Chris@909 241 bcp.gsub!(%r{[\/\\]$}, "")
Chris@909 242 if bcp
Chris@909 243 bcp = File.join(bcp, ".bzr", "branch", "branch.conf")
Chris@909 244 end
Chris@909 245 bcp
Chris@909 246 end
Chris@909 247
Chris@909 248 def append_revisions_only
Chris@909 249 return @aro if ! @aro.nil?
Chris@909 250 @aro = false
Chris@909 251 bcp = self.class.branch_conf_path(url)
Chris@909 252 if bcp && File.exist?(bcp)
Chris@909 253 begin
Chris@909 254 f = File::open(bcp, "r")
Chris@909 255 cnt = 0
Chris@909 256 f.each_line do |line|
Chris@909 257 l = line.chomp.to_s
Chris@909 258 if l =~ /^\s*append_revisions_only\s*=\s*(\w+)\s*$/
Chris@909 259 str_aro = $1
Chris@909 260 if str_aro.upcase == "TRUE"
Chris@909 261 @aro = true
Chris@909 262 cnt += 1
Chris@909 263 elsif str_aro.upcase == "FALSE"
Chris@909 264 @aro = false
Chris@909 265 cnt += 1
Chris@909 266 end
Chris@909 267 if cnt > 1
Chris@909 268 @aro = false
Chris@909 269 break
Chris@909 270 end
Chris@909 271 end
Chris@909 272 end
Chris@909 273 ensure
Chris@909 274 f.close
Chris@909 275 end
Chris@909 276 end
Chris@909 277 @aro
Chris@909 278 end
Chris@909 279
Chris@909 280 def scm_cmd(*args, &block)
Chris@909 281 full_args = []
Chris@909 282 full_args += args
Chris@909 283 ret = shellout(
Chris@909 284 self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
Chris@909 285 &block
Chris@909 286 )
Chris@909 287 if $? && $?.exitstatus != 0
Chris@909 288 raise ScmCommandAborted, "bzr exited with non-zero status: #{$?.exitstatus}"
Chris@909 289 end
Chris@909 290 ret
Chris@909 291 end
Chris@909 292 private :scm_cmd
Chris@909 293
Chris@909 294 def scm_cmd_no_raise(*args, &block)
Chris@909 295 full_args = []
Chris@909 296 full_args += args
Chris@909 297 ret = shellout(
Chris@909 298 self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
Chris@909 299 &block
Chris@909 300 )
Chris@909 301 ret
Chris@909 302 end
Chris@909 303 private :scm_cmd_no_raise
Chris@909 304
Chris@909 305 def bzr_target(path)
Chris@909 306 target(path, false)
Chris@909 307 end
Chris@909 308 private :bzr_target
Chris@0 309 end
Chris@0 310 end
Chris@0 311 end
Chris@0 312 end