Chris@441: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 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@441: # 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@441: # 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@245: module Adapters Chris@0: class BazaarAdapter < AbstractAdapter Chris@245: Chris@0: # Bazaar executable name Chris@210: BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr" Chris@245: Chris@245: class << self Chris@245: def client_command Chris@245: @@bin ||= BZR_BIN Chris@245: end Chris@245: Chris@245: def sq_bin Chris@909: @@sq_bin ||= shell_quote_command Chris@245: end Chris@245: Chris@245: def client_version Chris@245: @@client_version ||= (scm_command_version || []) Chris@245: end Chris@245: Chris@245: def client_available Chris@245: !client_version.empty? Chris@245: end Chris@245: Chris@245: def scm_command_version Chris@245: scm_version = scm_version_from_command_line.dup Chris@245: if scm_version.respond_to?(:force_encoding) Chris@245: scm_version.force_encoding('ASCII-8BIT') Chris@245: end Chris@245: if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@245: m[2].scan(%r{\d+}).collect(&:to_i) Chris@245: end Chris@245: end Chris@245: Chris@245: def scm_version_from_command_line Chris@245: shellout("#{sq_bin} --version") { |io| io.read }.to_s Chris@245: end Chris@245: end Chris@245: Chris@1115: def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) Chris@1115: @url = url Chris@1115: @root_url = url Chris@1115: @path_encoding = 'UTF-8' Chris@1115: # do not call *super* for non ASCII repository path Chris@1115: end Chris@1115: Chris@1115: def bzr_path_encodig=(encoding) Chris@1115: @path_encoding = encoding Chris@1115: end Chris@1115: Chris@0: # Get info about the repository Chris@0: def info Chris@909: cmd_args = %w|revno| Chris@909: cmd_args << bzr_target('') Chris@0: info = nil Chris@909: scm_cmd(*cmd_args) 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: info Chris@909: rescue ScmCommandAborted Chris@0: return nil Chris@0: end Chris@245: Chris@0: # Returns an Entries collection Chris@0: # or nil if the given path doesn't exist in the repository Chris@441: def entries(path=nil, identifier=nil, options={}) Chris@0: path ||= '' Chris@0: entries = Entries.new Chris@441: identifier = -1 unless identifier && identifier.to_i > 0 Chris@909: cmd_args = %w|ls -v --show-ids| Chris@909: cmd_args << "-r#{identifier.to_i}" Chris@909: cmd_args << bzr_target(path) Chris@909: scm_cmd(*cmd_args) do |io| Chris@1115: prefix_utf8 = "#{url}/#{path}".gsub('\\', '/') Chris@1115: logger.debug "PREFIX: #{prefix_utf8}" Chris@1115: prefix = scm_iconv(@path_encoding, 'UTF-8', prefix_utf8) Chris@1115: prefix.force_encoding('ASCII-8BIT') if prefix.respond_to?(:force_encoding) 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@1295: name_locale, slash, revision = $3.strip, $4, $5.strip Chris@1115: name = scm_iconv('UTF-8', @path_encoding, name_locale) Chris@1115: entries << Entry.new({:name => name, Chris@1115: :path => ((path.empty? ? "" : "#{path}/") + name), Chris@1295: :kind => (slash.blank? ? 'file' : 'dir'), Chris@0: :size => nil, Chris@1295: :lastrev => Revision.new(:revision => revision) Chris@0: }) Chris@0: end Chris@0: end Chris@909: if logger && logger.debug? Chris@909: logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") Chris@909: end Chris@0: entries.sort_by_name Chris@909: rescue ScmCommandAborted Chris@909: return nil Chris@0: end Chris@245: Chris@0: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@0: path ||= '' Chris@119: identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1' Chris@119: identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1 Chris@0: revisions = Revisions.new Chris@909: cmd_args = %w|log -v --show-ids| Chris@909: cmd_args << "-r#{identifier_to}..#{identifier_from}" Chris@909: cmd_args << bzr_target(path) Chris@909: scm_cmd(*cmd_args) do |io| Chris@0: revision = nil Chris@909: 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: 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@1115: path_locale = $1.strip Chris@1115: path = scm_iconv('UTF-8', @path_encoding, path_locale) 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@1115: if new_path Chris@1115: revision.paths << {:action => 'M', :path => "/#{new_path.strip}", Chris@1115: :revision => revid} Chris@1115: end 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: revisions Chris@909: rescue ScmCommandAborted Chris@909: return nil Chris@0: end Chris@245: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path ||= '' Chris@0: if identifier_to Chris@441: identifier_to = identifier_to.to_i Chris@0: else Chris@0: identifier_to = identifier_from.to_i - 1 Chris@0: end Chris@119: if identifier_from Chris@119: identifier_from = identifier_from.to_i Chris@119: end Chris@0: diff = [] Chris@909: cmd_args = %w|diff| Chris@909: cmd_args << "-r#{identifier_to}..#{identifier_from}" Chris@909: cmd_args << bzr_target(path) Chris@909: scm_cmd_no_raise(*cmd_args) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: diff Chris@0: end Chris@245: Chris@0: def cat(path, identifier=nil) Chris@0: cat = nil Chris@909: cmd_args = %w|cat| Chris@909: cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0 Chris@909: cmd_args << bzr_target(path) Chris@909: scm_cmd(*cmd_args) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: cat Chris@909: rescue ScmCommandAborted Chris@909: return nil Chris@0: end Chris@245: Chris@0: def annotate(path, identifier=nil) Chris@0: blame = Annotate.new Chris@909: cmd_args = %w|annotate -q --all| Chris@909: cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0 Chris@909: cmd_args << bzr_target(path) Chris@909: scm_cmd(*cmd_args) do |io| Chris@909: author = nil Chris@0: identifier = nil Chris@0: io.each_line do |line| Chris@0: next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$} Chris@441: rev = $1 Chris@441: blame.add_line($3.rstrip, Chris@441: Revision.new( Chris@441: :identifier => rev, Chris@441: :revision => rev, Chris@441: :author => $2.strip Chris@441: )) Chris@0: end Chris@0: end Chris@0: blame Chris@909: rescue ScmCommandAborted Chris@909: return nil Chris@0: end Chris@909: Chris@909: def self.branch_conf_path(path) Chris@909: bcp = nil Chris@909: m = path.match(%r{^(.*[/\\])\.bzr.*$}) Chris@909: if m Chris@909: bcp = m[1] Chris@909: else Chris@909: bcp = path Chris@909: end Chris@909: bcp.gsub!(%r{[\/\\]$}, "") Chris@909: if bcp Chris@909: bcp = File.join(bcp, ".bzr", "branch", "branch.conf") Chris@909: end Chris@909: bcp Chris@909: end Chris@909: Chris@909: def append_revisions_only Chris@909: return @aro if ! @aro.nil? Chris@909: @aro = false Chris@909: bcp = self.class.branch_conf_path(url) Chris@909: if bcp && File.exist?(bcp) Chris@909: begin Chris@909: f = File::open(bcp, "r") Chris@909: cnt = 0 Chris@909: f.each_line do |line| Chris@909: l = line.chomp.to_s Chris@909: if l =~ /^\s*append_revisions_only\s*=\s*(\w+)\s*$/ Chris@909: str_aro = $1 Chris@909: if str_aro.upcase == "TRUE" Chris@909: @aro = true Chris@909: cnt += 1 Chris@909: elsif str_aro.upcase == "FALSE" Chris@909: @aro = false Chris@909: cnt += 1 Chris@909: end Chris@909: if cnt > 1 Chris@909: @aro = false Chris@909: break Chris@909: end Chris@909: end Chris@909: end Chris@909: ensure Chris@909: f.close Chris@909: end Chris@909: end Chris@909: @aro Chris@909: end Chris@909: Chris@909: def scm_cmd(*args, &block) Chris@909: full_args = [] Chris@909: full_args += args Chris@1115: full_args_locale = [] Chris@1115: full_args.map do |e| Chris@1115: full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e) Chris@1115: end Chris@909: ret = shellout( Chris@1115: self.class.sq_bin + ' ' + Chris@1115: full_args_locale.map { |e| shell_quote e.to_s }.join(' '), Chris@909: &block Chris@909: ) Chris@909: if $? && $?.exitstatus != 0 Chris@909: raise ScmCommandAborted, "bzr exited with non-zero status: #{$?.exitstatus}" Chris@909: end Chris@909: ret Chris@909: end Chris@909: private :scm_cmd Chris@909: Chris@909: def scm_cmd_no_raise(*args, &block) Chris@909: full_args = [] Chris@909: full_args += args Chris@1115: full_args_locale = [] Chris@1115: full_args.map do |e| Chris@1115: full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e) Chris@1115: end Chris@909: ret = shellout( Chris@1115: self.class.sq_bin + ' ' + Chris@1115: full_args_locale.map { |e| shell_quote e.to_s }.join(' '), Chris@909: &block Chris@909: ) Chris@909: ret Chris@909: end Chris@909: private :scm_cmd_no_raise Chris@909: Chris@909: def bzr_target(path) Chris@909: target(path, false) Chris@909: end Chris@909: private :bzr_target Chris@0: end Chris@0: end Chris@0: end Chris@0: end