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