Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require 'redmine/scm/adapters/abstract_adapter' Chris@1296: Chris@1296: module Redmine Chris@1296: module Scm Chris@1296: module Adapters Chris@1296: class BazaarAdapter < AbstractAdapter Chris@1296: Chris@1296: # Bazaar executable name Chris@1296: BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr" Chris@1296: Chris@1296: class << self Chris@1296: def client_command Chris@1296: @@bin ||= BZR_BIN Chris@1296: end Chris@1296: Chris@1296: def sq_bin Chris@1296: @@sq_bin ||= shell_quote_command Chris@1296: end Chris@1296: Chris@1296: def client_version Chris@1296: @@client_version ||= (scm_command_version || []) Chris@1296: end Chris@1296: Chris@1296: def client_available Chris@1296: !client_version.empty? Chris@1296: end Chris@1296: Chris@1296: def scm_command_version Chris@1296: scm_version = scm_version_from_command_line.dup Chris@1296: if scm_version.respond_to?(:force_encoding) Chris@1296: scm_version.force_encoding('ASCII-8BIT') Chris@1296: end Chris@1296: if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@1296: m[2].scan(%r{\d+}).collect(&:to_i) Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def scm_version_from_command_line Chris@1296: shellout("#{sq_bin} --version") { |io| io.read }.to_s Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) Chris@1296: @url = url Chris@1296: @root_url = url Chris@1296: @path_encoding = 'UTF-8' Chris@1296: # do not call *super* for non ASCII repository path Chris@1296: end Chris@1296: Chris@1296: def bzr_path_encodig=(encoding) Chris@1296: @path_encoding = encoding Chris@1296: end Chris@1296: Chris@1296: # Get info about the repository Chris@1296: def info Chris@1296: cmd_args = %w|revno| Chris@1296: cmd_args << bzr_target('') Chris@1296: info = nil Chris@1296: scm_cmd(*cmd_args) do |io| Chris@1296: if io.read =~ %r{^(\d+)\r?$} Chris@1296: info = Info.new({:root_url => url, Chris@1296: :lastrev => Revision.new({ Chris@1296: :identifier => $1 Chris@1296: }) Chris@1296: }) Chris@1296: end Chris@1296: end Chris@1296: info Chris@1296: rescue ScmCommandAborted Chris@1296: return nil Chris@1296: end Chris@1296: Chris@1296: # Returns an Entries collection Chris@1296: # or nil if the given path doesn't exist in the repository Chris@1296: def entries(path=nil, identifier=nil, options={}) Chris@1296: path ||= '' Chris@1296: entries = Entries.new Chris@1296: identifier = -1 unless identifier && identifier.to_i > 0 Chris@1296: cmd_args = %w|ls -v --show-ids| Chris@1296: cmd_args << "-r#{identifier.to_i}" Chris@1296: cmd_args << bzr_target(path) Chris@1296: scm_cmd(*cmd_args) do |io| Chris@1296: prefix_utf8 = "#{url}/#{path}".gsub('\\', '/') Chris@1296: logger.debug "PREFIX: #{prefix_utf8}" Chris@1296: prefix = scm_iconv(@path_encoding, 'UTF-8', prefix_utf8) Chris@1296: prefix.force_encoding('ASCII-8BIT') if prefix.respond_to?(:force_encoding) Chris@1296: re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$} Chris@1296: io.each_line do |line| Chris@1296: next unless line =~ re Chris@1296: name_locale = $3.strip Chris@1296: name = scm_iconv('UTF-8', @path_encoding, name_locale) Chris@1296: entries << Entry.new({:name => name, Chris@1296: :path => ((path.empty? ? "" : "#{path}/") + name), Chris@1296: :kind => ($4.blank? ? 'file' : 'dir'), Chris@1296: :size => nil, Chris@1296: :lastrev => Revision.new(:revision => $5.strip) Chris@1296: }) Chris@1296: end Chris@1296: end Chris@1296: if logger && logger.debug? Chris@1296: logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") Chris@1296: end Chris@1296: entries.sort_by_name Chris@1296: rescue ScmCommandAborted Chris@1296: return nil Chris@1296: end Chris@1296: Chris@1296: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@1296: path ||= '' Chris@1296: identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1' Chris@1296: identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1 Chris@1296: revisions = Revisions.new Chris@1296: cmd_args = %w|log -v --show-ids| Chris@1296: cmd_args << "-r#{identifier_to}..#{identifier_from}" Chris@1296: cmd_args << bzr_target(path) Chris@1296: scm_cmd(*cmd_args) do |io| Chris@1296: revision = nil Chris@1296: parsing = nil Chris@1296: io.each_line do |line| Chris@1296: if line =~ /^----/ Chris@1296: revisions << revision if revision Chris@1296: revision = Revision.new(:paths => [], :message => '') Chris@1296: parsing = nil Chris@1296: else Chris@1296: next unless revision Chris@1296: if line =~ /^revno: (\d+)($|\s\[merge\]$)/ Chris@1296: revision.identifier = $1.to_i Chris@1296: elsif line =~ /^committer: (.+)$/ Chris@1296: revision.author = $1.strip Chris@1296: elsif line =~ /^revision-id:(.+)$/ Chris@1296: revision.scmid = $1.strip Chris@1296: elsif line =~ /^timestamp: (.+)$/ Chris@1296: revision.time = Time.parse($1).localtime Chris@1296: elsif line =~ /^ -----/ Chris@1296: # partial revisions Chris@1296: parsing = nil unless parsing == 'message' Chris@1296: elsif line =~ /^(message|added|modified|removed|renamed):/ Chris@1296: parsing = $1 Chris@1296: elsif line =~ /^ (.*)$/ Chris@1296: if parsing == 'message' Chris@1296: revision.message << "#{$1}\n" Chris@1296: else Chris@1296: if $1 =~ /^(.*)\s+(\S+)$/ Chris@1296: path_locale = $1.strip Chris@1296: path = scm_iconv('UTF-8', @path_encoding, path_locale) Chris@1296: revid = $2 Chris@1296: case parsing Chris@1296: when 'added' Chris@1296: revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid} Chris@1296: when 'modified' Chris@1296: revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid} Chris@1296: when 'removed' Chris@1296: revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid} Chris@1296: when 'renamed' Chris@1296: new_path = path.split('=>').last Chris@1296: if new_path Chris@1296: revision.paths << {:action => 'M', :path => "/#{new_path.strip}", Chris@1296: :revision => revid} Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: else Chris@1296: parsing = nil Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: revisions << revision if revision Chris@1296: end Chris@1296: revisions Chris@1296: rescue ScmCommandAborted Chris@1296: return nil Chris@1296: end Chris@1296: Chris@1296: def diff(path, identifier_from, identifier_to=nil) Chris@1296: path ||= '' Chris@1296: if identifier_to Chris@1296: identifier_to = identifier_to.to_i Chris@1296: else Chris@1296: identifier_to = identifier_from.to_i - 1 Chris@1296: end Chris@1296: if identifier_from Chris@1296: identifier_from = identifier_from.to_i Chris@1296: end Chris@1296: diff = [] Chris@1296: cmd_args = %w|diff| Chris@1296: cmd_args << "-r#{identifier_to}..#{identifier_from}" Chris@1296: cmd_args << bzr_target(path) Chris@1296: scm_cmd_no_raise(*cmd_args) do |io| Chris@1296: io.each_line do |line| Chris@1296: diff << line Chris@1296: end Chris@1296: end Chris@1296: diff Chris@1296: end Chris@1296: Chris@1296: def cat(path, identifier=nil) Chris@1296: cat = nil Chris@1296: cmd_args = %w|cat| Chris@1296: cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0 Chris@1296: cmd_args << bzr_target(path) Chris@1296: scm_cmd(*cmd_args) do |io| Chris@1296: io.binmode Chris@1296: cat = io.read Chris@1296: end Chris@1296: cat Chris@1296: rescue ScmCommandAborted Chris@1296: return nil Chris@1296: end Chris@1296: Chris@1296: def annotate(path, identifier=nil) Chris@1296: blame = Annotate.new Chris@1296: cmd_args = %w|annotate -q --all| Chris@1296: cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0 Chris@1296: cmd_args << bzr_target(path) Chris@1296: scm_cmd(*cmd_args) do |io| Chris@1296: author = nil Chris@1296: identifier = nil Chris@1296: io.each_line do |line| Chris@1296: next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$} Chris@1296: rev = $1 Chris@1296: blame.add_line($3.rstrip, Chris@1296: Revision.new( Chris@1296: :identifier => rev, Chris@1296: :revision => rev, Chris@1296: :author => $2.strip Chris@1296: )) Chris@1296: end Chris@1296: end Chris@1296: blame Chris@1296: rescue ScmCommandAborted Chris@1296: return nil Chris@1296: end Chris@1296: Chris@1296: def self.branch_conf_path(path) Chris@1296: bcp = nil Chris@1296: m = path.match(%r{^(.*[/\\])\.bzr.*$}) Chris@1296: if m Chris@1296: bcp = m[1] Chris@1296: else Chris@1296: bcp = path Chris@1296: end Chris@1296: bcp.gsub!(%r{[\/\\]$}, "") Chris@1296: if bcp Chris@1296: bcp = File.join(bcp, ".bzr", "branch", "branch.conf") Chris@1296: end Chris@1296: bcp Chris@1296: end Chris@1296: Chris@1296: def append_revisions_only Chris@1296: return @aro if ! @aro.nil? Chris@1296: @aro = false Chris@1296: bcp = self.class.branch_conf_path(url) Chris@1296: if bcp && File.exist?(bcp) Chris@1296: begin Chris@1296: f = File::open(bcp, "r") Chris@1296: cnt = 0 Chris@1296: f.each_line do |line| Chris@1296: l = line.chomp.to_s Chris@1296: if l =~ /^\s*append_revisions_only\s*=\s*(\w+)\s*$/ Chris@1296: str_aro = $1 Chris@1296: if str_aro.upcase == "TRUE" Chris@1296: @aro = true Chris@1296: cnt += 1 Chris@1296: elsif str_aro.upcase == "FALSE" Chris@1296: @aro = false Chris@1296: cnt += 1 Chris@1296: end Chris@1296: if cnt > 1 Chris@1296: @aro = false Chris@1296: break Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: ensure Chris@1296: f.close Chris@1296: end Chris@1296: end Chris@1296: @aro Chris@1296: end Chris@1296: Chris@1296: def scm_cmd(*args, &block) Chris@1296: full_args = [] Chris@1296: full_args += args Chris@1296: full_args_locale = [] Chris@1296: full_args.map do |e| Chris@1296: full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e) Chris@1296: end Chris@1296: ret = shellout( Chris@1296: self.class.sq_bin + ' ' + Chris@1296: full_args_locale.map { |e| shell_quote e.to_s }.join(' '), Chris@1296: &block Chris@1296: ) Chris@1296: if $? && $?.exitstatus != 0 Chris@1296: raise ScmCommandAborted, "bzr exited with non-zero status: #{$?.exitstatus}" Chris@1296: end Chris@1296: ret Chris@1296: end Chris@1296: private :scm_cmd Chris@1296: Chris@1296: def scm_cmd_no_raise(*args, &block) Chris@1296: full_args = [] Chris@1296: full_args += args Chris@1296: full_args_locale = [] Chris@1296: full_args.map do |e| Chris@1296: full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e) Chris@1296: end Chris@1296: ret = shellout( Chris@1296: self.class.sq_bin + ' ' + Chris@1296: full_args_locale.map { |e| shell_quote e.to_s }.join(' '), Chris@1296: &block Chris@1296: ) Chris@1296: ret Chris@1296: end Chris@1296: private :scm_cmd_no_raise Chris@1296: Chris@1296: def bzr_target(path) Chris@1296: target(path, false) Chris@1296: end Chris@1296: private :bzr_target Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end