annotate lib/redmine/scm/adapters/bazaar_adapter.rb @ 1173:5d3fb66f9ae4 bug_371

Close obsolete branch bug_371
author Chris Cannam
date Fri, 03 Feb 2012 15:02:13 +0000
parents cbce1fd3b1b7
children cbb26bc654de
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@245 34 @@sq_bin ||= shell_quote(BZR_BIN)
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@245 62 cmd = "#{self.class.sq_bin} revno #{target('')}"
Chris@0 63 info = nil
Chris@0 64 shellout(cmd) do |io|
Chris@0 65 if io.read =~ %r{^(\d+)\r?$}
Chris@0 66 info = Info.new({:root_url => url,
Chris@0 67 :lastrev => Revision.new({
Chris@0 68 :identifier => $1
Chris@0 69 })
Chris@0 70 })
Chris@0 71 end
Chris@0 72 end
Chris@0 73 return nil if $? && $?.exitstatus != 0
Chris@0 74 info
Chris@0 75 rescue CommandFailed
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@245 84 cmd = "#{self.class.sq_bin} ls -v --show-ids"
Chris@441 85 identifier = -1 unless identifier && identifier.to_i > 0
Chris@441 86 cmd << " -r#{identifier.to_i}"
Chris@0 87 cmd << " #{target(path)}"
Chris@0 88 shellout(cmd) 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@0 102 return nil if $? && $?.exitstatus != 0
Chris@0 103 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
Chris@0 104 entries.sort_by_name
Chris@0 105 end
Chris@245 106
Chris@0 107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@0 108 path ||= ''
Chris@119 109 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1'
Chris@119 110 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
Chris@0 111 revisions = Revisions.new
Chris@245 112 cmd = "#{self.class.sq_bin} log -v --show-ids -r#{identifier_to}..#{identifier_from} #{target(path)}"
Chris@0 113 shellout(cmd) do |io|
Chris@0 114 revision = nil
Chris@0 115 parsing = nil
Chris@0 116 io.each_line do |line|
Chris@0 117 if line =~ /^----/
Chris@0 118 revisions << revision if revision
Chris@0 119 revision = Revision.new(:paths => [], :message => '')
Chris@0 120 parsing = nil
Chris@0 121 else
Chris@0 122 next unless revision
Chris@0 123 if line =~ /^revno: (\d+)($|\s\[merge\]$)/
Chris@0 124 revision.identifier = $1.to_i
Chris@0 125 elsif line =~ /^committer: (.+)$/
Chris@0 126 revision.author = $1.strip
Chris@0 127 elsif line =~ /^revision-id:(.+)$/
Chris@0 128 revision.scmid = $1.strip
Chris@0 129 elsif line =~ /^timestamp: (.+)$/
Chris@0 130 revision.time = Time.parse($1).localtime
Chris@0 131 elsif line =~ /^ -----/
Chris@0 132 # partial revisions
Chris@0 133 parsing = nil unless parsing == 'message'
Chris@0 134 elsif line =~ /^(message|added|modified|removed|renamed):/
Chris@0 135 parsing = $1
Chris@0 136 elsif line =~ /^ (.*)$/
Chris@0 137 if parsing == 'message'
Chris@0 138 revision.message << "#{$1}\n"
Chris@0 139 else
Chris@0 140 if $1 =~ /^(.*)\s+(\S+)$/
Chris@0 141 path = $1.strip
Chris@0 142 revid = $2
Chris@0 143 case parsing
Chris@0 144 when 'added'
Chris@0 145 revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid}
Chris@0 146 when 'modified'
Chris@0 147 revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid}
Chris@0 148 when 'removed'
Chris@0 149 revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid}
Chris@0 150 when 'renamed'
Chris@0 151 new_path = path.split('=>').last
Chris@0 152 revision.paths << {:action => 'M', :path => "/#{new_path.strip}", :revision => revid} if new_path
Chris@0 153 end
Chris@0 154 end
Chris@0 155 end
Chris@0 156 else
Chris@0 157 parsing = nil
Chris@0 158 end
Chris@0 159 end
Chris@0 160 end
Chris@0 161 revisions << revision if revision
Chris@0 162 end
Chris@0 163 return nil if $? && $?.exitstatus != 0
Chris@0 164 revisions
Chris@0 165 end
Chris@245 166
Chris@0 167 def diff(path, identifier_from, identifier_to=nil)
Chris@0 168 path ||= ''
Chris@0 169 if identifier_to
Chris@441 170 identifier_to = identifier_to.to_i
Chris@0 171 else
Chris@0 172 identifier_to = identifier_from.to_i - 1
Chris@0 173 end
Chris@119 174 if identifier_from
Chris@119 175 identifier_from = identifier_from.to_i
Chris@119 176 end
Chris@245 177 cmd = "#{self.class.sq_bin} diff -r#{identifier_to}..#{identifier_from} #{target(path)}"
Chris@0 178 diff = []
Chris@0 179 shellout(cmd) do |io|
Chris@0 180 io.each_line do |line|
Chris@0 181 diff << line
Chris@0 182 end
Chris@0 183 end
Chris@0 184 #return nil if $? && $?.exitstatus != 0
Chris@0 185 diff
Chris@0 186 end
Chris@245 187
Chris@0 188 def cat(path, identifier=nil)
Chris@245 189 cmd = "#{self.class.sq_bin} cat"
Chris@0 190 cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0
Chris@0 191 cmd << " #{target(path)}"
Chris@0 192 cat = nil
Chris@0 193 shellout(cmd) do |io|
Chris@0 194 io.binmode
Chris@0 195 cat = io.read
Chris@0 196 end
Chris@0 197 return nil if $? && $?.exitstatus != 0
Chris@0 198 cat
Chris@0 199 end
Chris@245 200
Chris@0 201 def annotate(path, identifier=nil)
Chris@245 202 cmd = "#{self.class.sq_bin} annotate --all"
Chris@0 203 cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0
Chris@0 204 cmd << " #{target(path)}"
Chris@0 205 blame = Annotate.new
Chris@0 206 shellout(cmd) do |io|
Chris@0 207 author = nil
Chris@0 208 identifier = nil
Chris@0 209 io.each_line do |line|
Chris@0 210 next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$}
Chris@441 211 rev = $1
Chris@441 212 blame.add_line($3.rstrip,
Chris@441 213 Revision.new(
Chris@441 214 :identifier => rev,
Chris@441 215 :revision => rev,
Chris@441 216 :author => $2.strip
Chris@441 217 ))
Chris@0 218 end
Chris@0 219 end
Chris@0 220 return nil if $? && $?.exitstatus != 0
Chris@0 221 blame
Chris@0 222 end
Chris@0 223 end
Chris@0 224 end
Chris@0 225 end
Chris@0 226 end