Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require 'redmine/scm/adapters/abstract_adapter' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@0: module Adapters Chris@0: class BazaarAdapter < AbstractAdapter Chris@0: Chris@0: # Bazaar executable name Chris@0: BZR_BIN = "bzr" Chris@0: Chris@0: # Get info about the repository Chris@0: def info Chris@0: cmd = "#{BZR_BIN} revno #{target('')}" Chris@0: info = nil Chris@0: shellout(cmd) do |io| Chris@0: if io.read =~ %r{^(\d+)\r?$} Chris@0: info = Info.new({:root_url => url, Chris@0: :lastrev => Revision.new({ Chris@0: :identifier => $1 Chris@0: }) Chris@0: }) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: info Chris@0: rescue CommandFailed Chris@0: return nil Chris@0: end Chris@0: Chris@0: # Returns an Entries collection Chris@0: # or nil if the given path doesn't exist in the repository Chris@0: def entries(path=nil, identifier=nil) Chris@0: path ||= '' Chris@0: entries = Entries.new Chris@0: cmd = "#{BZR_BIN} ls -v --show-ids" Chris@0: identifier = -1 unless identifier && identifier.to_i > 0 Chris@0: cmd << " -r#{identifier.to_i}" Chris@0: cmd << " #{target(path)}" Chris@0: shellout(cmd) do |io| Chris@0: prefix = "#{url}/#{path}".gsub('\\', '/') Chris@0: logger.debug "PREFIX: #{prefix}" Chris@0: re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$} Chris@0: io.each_line do |line| Chris@0: next unless line =~ re Chris@0: entries << Entry.new({:name => $3.strip, Chris@0: :path => ((path.empty? ? "" : "#{path}/") + $3.strip), Chris@0: :kind => ($4.blank? ? 'file' : 'dir'), Chris@0: :size => nil, Chris@0: :lastrev => Revision.new(:revision => $5.strip) Chris@0: }) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug? Chris@0: entries.sort_by_name Chris@0: end Chris@0: Chris@0: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@0: path ||= '' Chris@117: identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1' Chris@117: identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1 Chris@0: revisions = Revisions.new Chris@117: cmd = "#{BZR_BIN} log -v --show-ids -r#{identifier_to}..#{identifier_from} #{target(path)}" Chris@0: shellout(cmd) do |io| Chris@0: revision = nil Chris@0: parsing = nil Chris@0: io.each_line do |line| Chris@0: if line =~ /^----/ Chris@0: revisions << revision if revision Chris@0: revision = Revision.new(:paths => [], :message => '') Chris@0: parsing = nil Chris@0: else Chris@0: next unless revision Chris@0: Chris@0: if line =~ /^revno: (\d+)($|\s\[merge\]$)/ Chris@0: revision.identifier = $1.to_i Chris@0: elsif line =~ /^committer: (.+)$/ Chris@0: revision.author = $1.strip Chris@0: elsif line =~ /^revision-id:(.+)$/ Chris@0: revision.scmid = $1.strip Chris@0: elsif line =~ /^timestamp: (.+)$/ Chris@0: revision.time = Time.parse($1).localtime Chris@0: elsif line =~ /^ -----/ Chris@0: # partial revisions Chris@0: parsing = nil unless parsing == 'message' Chris@0: elsif line =~ /^(message|added|modified|removed|renamed):/ Chris@0: parsing = $1 Chris@0: elsif line =~ /^ (.*)$/ Chris@0: if parsing == 'message' Chris@0: revision.message << "#{$1}\n" Chris@0: else Chris@0: if $1 =~ /^(.*)\s+(\S+)$/ Chris@0: path = $1.strip Chris@0: revid = $2 Chris@0: case parsing Chris@0: when 'added' Chris@0: revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid} Chris@0: when 'modified' Chris@0: revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid} Chris@0: when 'removed' Chris@0: revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid} Chris@0: when 'renamed' Chris@0: new_path = path.split('=>').last Chris@0: revision.paths << {:action => 'M', :path => "/#{new_path.strip}", :revision => revid} if new_path Chris@0: end Chris@0: end Chris@0: end Chris@0: else Chris@0: parsing = nil Chris@0: end Chris@0: end Chris@0: end Chris@0: revisions << revision if revision Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: revisions Chris@0: end Chris@0: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path ||= '' Chris@0: if identifier_to Chris@0: identifier_to = identifier_to.to_i Chris@0: else Chris@0: identifier_to = identifier_from.to_i - 1 Chris@0: end Chris@117: if identifier_from Chris@117: identifier_from = identifier_from.to_i Chris@117: end Chris@0: cmd = "#{BZR_BIN} diff -r#{identifier_to}..#{identifier_from} #{target(path)}" Chris@0: diff = [] Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: #return nil if $? && $?.exitstatus != 0 Chris@0: diff Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: cmd = "#{BZR_BIN} cat" Chris@0: cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0 Chris@0: cmd << " #{target(path)}" Chris@0: cat = nil Chris@0: shellout(cmd) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: cat Chris@0: end Chris@0: Chris@0: def annotate(path, identifier=nil) Chris@0: cmd = "#{BZR_BIN} annotate --all" Chris@0: cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0 Chris@0: cmd << " #{target(path)}" Chris@0: blame = Annotate.new Chris@0: shellout(cmd) do |io| Chris@0: author = nil Chris@0: identifier = nil Chris@0: io.each_line do |line| Chris@0: next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$} Chris@0: blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip)) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: blame Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end