annotate .svn/pristine/f0/f036a37a46cad981a63df7b8fce61d2e4da796f3.svn-base @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 require 'redmine/scm/adapters/abstract_adapter'
Chris@909 19
Chris@909 20 module Redmine
Chris@909 21 module Scm
Chris@909 22 module Adapters
Chris@909 23 class BazaarAdapter < AbstractAdapter
Chris@909 24
Chris@909 25 # Bazaar executable name
Chris@909 26 BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr"
Chris@909 27
Chris@909 28 class << self
Chris@909 29 def client_command
Chris@909 30 @@bin ||= BZR_BIN
Chris@909 31 end
Chris@909 32
Chris@909 33 def sq_bin
Chris@909 34 @@sq_bin ||= shell_quote_command
Chris@909 35 end
Chris@909 36
Chris@909 37 def client_version
Chris@909 38 @@client_version ||= (scm_command_version || [])
Chris@909 39 end
Chris@909 40
Chris@909 41 def client_available
Chris@909 42 !client_version.empty?
Chris@909 43 end
Chris@909 44
Chris@909 45 def scm_command_version
Chris@909 46 scm_version = scm_version_from_command_line.dup
Chris@909 47 if scm_version.respond_to?(:force_encoding)
Chris@909 48 scm_version.force_encoding('ASCII-8BIT')
Chris@909 49 end
Chris@909 50 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
Chris@909 51 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@909 52 end
Chris@909 53 end
Chris@909 54
Chris@909 55 def scm_version_from_command_line
Chris@909 56 shellout("#{sq_bin} --version") { |io| io.read }.to_s
Chris@909 57 end
Chris@909 58 end
Chris@909 59
Chris@909 60 # Get info about the repository
Chris@909 61 def info
Chris@909 62 cmd_args = %w|revno|
Chris@909 63 cmd_args << bzr_target('')
Chris@909 64 info = nil
Chris@909 65 scm_cmd(*cmd_args) do |io|
Chris@909 66 if io.read =~ %r{^(\d+)\r?$}
Chris@909 67 info = Info.new({:root_url => url,
Chris@909 68 :lastrev => Revision.new({
Chris@909 69 :identifier => $1
Chris@909 70 })
Chris@909 71 })
Chris@909 72 end
Chris@909 73 end
Chris@909 74 info
Chris@909 75 rescue ScmCommandAborted
Chris@909 76 return nil
Chris@909 77 end
Chris@909 78
Chris@909 79 # Returns an Entries collection
Chris@909 80 # or nil if the given path doesn't exist in the repository
Chris@909 81 def entries(path=nil, identifier=nil, options={})
Chris@909 82 path ||= ''
Chris@909 83 entries = Entries.new
Chris@909 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@909 89 prefix = "#{url}/#{path}".gsub('\\', '/')
Chris@909 90 logger.debug "PREFIX: #{prefix}"
Chris@909 91 re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$}
Chris@909 92 io.each_line do |line|
Chris@909 93 next unless line =~ re
Chris@909 94 entries << Entry.new({:name => $3.strip,
Chris@909 95 :path => ((path.empty? ? "" : "#{path}/") + $3.strip),
Chris@909 96 :kind => ($4.blank? ? 'file' : 'dir'),
Chris@909 97 :size => nil,
Chris@909 98 :lastrev => Revision.new(:revision => $5.strip)
Chris@909 99 })
Chris@909 100 end
Chris@909 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@909 105 entries.sort_by_name
Chris@909 106 rescue ScmCommandAborted
Chris@909 107 return nil
Chris@909 108 end
Chris@909 109
Chris@909 110 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@909 111 path ||= ''
Chris@909 112 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1'
Chris@909 113 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
Chris@909 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@909 119 revision = nil
Chris@909 120 parsing = nil
Chris@909 121 io.each_line do |line|
Chris@909 122 if line =~ /^----/
Chris@909 123 revisions << revision if revision
Chris@909 124 revision = Revision.new(:paths => [], :message => '')
Chris@909 125 parsing = nil
Chris@909 126 else
Chris@909 127 next unless revision
Chris@909 128 if line =~ /^revno: (\d+)($|\s\[merge\]$)/
Chris@909 129 revision.identifier = $1.to_i
Chris@909 130 elsif line =~ /^committer: (.+)$/
Chris@909 131 revision.author = $1.strip
Chris@909 132 elsif line =~ /^revision-id:(.+)$/
Chris@909 133 revision.scmid = $1.strip
Chris@909 134 elsif line =~ /^timestamp: (.+)$/
Chris@909 135 revision.time = Time.parse($1).localtime
Chris@909 136 elsif line =~ /^ -----/
Chris@909 137 # partial revisions
Chris@909 138 parsing = nil unless parsing == 'message'
Chris@909 139 elsif line =~ /^(message|added|modified|removed|renamed):/
Chris@909 140 parsing = $1
Chris@909 141 elsif line =~ /^ (.*)$/
Chris@909 142 if parsing == 'message'
Chris@909 143 revision.message << "#{$1}\n"
Chris@909 144 else
Chris@909 145 if $1 =~ /^(.*)\s+(\S+)$/
Chris@909 146 path = $1.strip
Chris@909 147 revid = $2
Chris@909 148 case parsing
Chris@909 149 when 'added'
Chris@909 150 revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid}
Chris@909 151 when 'modified'
Chris@909 152 revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid}
Chris@909 153 when 'removed'
Chris@909 154 revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid}
Chris@909 155 when 'renamed'
Chris@909 156 new_path = path.split('=>').last
Chris@909 157 revision.paths << {:action => 'M', :path => "/#{new_path.strip}", :revision => revid} if new_path
Chris@909 158 end
Chris@909 159 end
Chris@909 160 end
Chris@909 161 else
Chris@909 162 parsing = nil
Chris@909 163 end
Chris@909 164 end
Chris@909 165 end
Chris@909 166 revisions << revision if revision
Chris@909 167 end
Chris@909 168 revisions
Chris@909 169 rescue ScmCommandAborted
Chris@909 170 return nil
Chris@909 171 end
Chris@909 172
Chris@909 173 def diff(path, identifier_from, identifier_to=nil)
Chris@909 174 path ||= ''
Chris@909 175 if identifier_to
Chris@909 176 identifier_to = identifier_to.to_i
Chris@909 177 else
Chris@909 178 identifier_to = identifier_from.to_i - 1
Chris@909 179 end
Chris@909 180 if identifier_from
Chris@909 181 identifier_from = identifier_from.to_i
Chris@909 182 end
Chris@909 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@909 188 io.each_line do |line|
Chris@909 189 diff << line
Chris@909 190 end
Chris@909 191 end
Chris@909 192 diff
Chris@909 193 end
Chris@909 194
Chris@909 195 def cat(path, identifier=nil)
Chris@909 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@909 201 io.binmode
Chris@909 202 cat = io.read
Chris@909 203 end
Chris@909 204 cat
Chris@909 205 rescue ScmCommandAborted
Chris@909 206 return nil
Chris@909 207 end
Chris@909 208
Chris@909 209 def annotate(path, identifier=nil)
Chris@909 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@909 216 identifier = nil
Chris@909 217 io.each_line do |line|
Chris@909 218 next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$}
Chris@909 219 rev = $1
Chris@909 220 blame.add_line($3.rstrip,
Chris@909 221 Revision.new(
Chris@909 222 :identifier => rev,
Chris@909 223 :revision => rev,
Chris@909 224 :author => $2.strip
Chris@909 225 ))
Chris@909 226 end
Chris@909 227 end
Chris@909 228 blame
Chris@909 229 rescue ScmCommandAborted
Chris@909 230 return nil
Chris@909 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@909 309 end
Chris@909 310 end
Chris@909 311 end
Chris@909 312 end