Chris@441: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@441: # Chris@441: # This program is free software; you can redistribute it and/or Chris@441: # modify it under the terms of the GNU General Public License Chris@441: # as published by the Free Software Foundation; either version 2 Chris@441: # of the License, or (at your option) any later version. Chris@441: # Chris@441: # This program is distributed in the hope that it will be useful, Chris@441: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@441: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@441: # GNU General Public License for more details. Chris@441: # Chris@441: # You should have received a copy of the GNU General Public License Chris@441: # along with this program; if not, write to the Free Software Chris@441: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@441: Chris@441: require 'redmine/scm/adapters/abstract_adapter' Chris@441: require 'uri' Chris@441: Chris@441: module Redmine Chris@441: module Scm Chris@441: module Adapters Chris@441: class SubversionAdapter < AbstractAdapter Chris@441: Chris@441: # SVN executable name Chris@441: SVN_BIN = Redmine::Configuration['scm_subversion_command'] || "svn" Chris@441: Chris@441: class << self Chris@441: def client_command Chris@441: @@bin ||= SVN_BIN Chris@441: end Chris@441: Chris@441: def sq_bin Chris@441: @@sq_bin ||= shell_quote(SVN_BIN) Chris@441: end Chris@441: Chris@441: def client_version Chris@441: @@client_version ||= (svn_binary_version || []) Chris@441: end Chris@441: Chris@441: def client_available Chris@441: # --xml options are introduced in 1.3. Chris@441: # http://subversion.apache.org/docs/release-notes/1.3.html Chris@441: client_version_above?([1, 3]) Chris@441: end Chris@441: Chris@441: def svn_binary_version Chris@441: scm_version = scm_version_from_command_line.dup Chris@441: if scm_version.respond_to?(:force_encoding) Chris@441: scm_version.force_encoding('ASCII-8BIT') Chris@441: end Chris@441: if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@441: m[2].scan(%r{\d+}).collect(&:to_i) Chris@441: end Chris@441: end Chris@441: Chris@441: def scm_version_from_command_line Chris@441: shellout("#{sq_bin} --version") { |io| io.read }.to_s Chris@441: end Chris@441: end Chris@441: Chris@441: # Get info about the svn repository Chris@441: def info Chris@441: cmd = "#{self.class.sq_bin} info --xml #{target}" Chris@441: cmd << credentials_string Chris@441: info = nil Chris@441: shellout(cmd) do |io| Chris@441: output = io.read Chris@441: if output.respond_to?(:force_encoding) Chris@441: output.force_encoding('UTF-8') Chris@441: end Chris@441: begin Chris@441: doc = ActiveSupport::XmlMini.parse(output) Chris@441: # root_url = doc.elements["info/entry/repository/root"].text Chris@441: info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'], Chris@441: :lastrev => Revision.new({ Chris@441: :identifier => doc['info']['entry']['commit']['revision'], Chris@441: :time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime, Chris@441: :author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "") Chris@441: }) Chris@441: }) Chris@441: rescue Chris@441: end Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: info Chris@441: rescue CommandFailed Chris@441: return nil Chris@441: end Chris@441: Chris@441: # Returns an Entries collection Chris@441: # or nil if the given path doesn't exist in the repository Chris@441: def entries(path=nil, identifier=nil, options={}) Chris@441: path ||= '' Chris@441: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@441: entries = Entries.new Chris@441: cmd = "#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}" Chris@441: cmd << credentials_string Chris@441: shellout(cmd) do |io| Chris@441: output = io.read Chris@441: if output.respond_to?(:force_encoding) Chris@441: output.force_encoding('UTF-8') Chris@441: end Chris@441: begin Chris@441: doc = ActiveSupport::XmlMini.parse(output) Chris@441: each_xml_element(doc['lists']['list'], 'entry') do |entry| Chris@441: commit = entry['commit'] Chris@441: commit_date = commit['date'] Chris@441: # Skip directory if there is no commit date (usually that Chris@441: # means that we don't have read access to it) Chris@441: next if entry['kind'] == 'dir' && commit_date.nil? Chris@441: name = entry['name']['__content__'] Chris@441: entries << Entry.new({:name => URI.unescape(name), Chris@441: :path => ((path.empty? ? "" : "#{path}/") + name), Chris@441: :kind => entry['kind'], Chris@441: :size => ((s = entry['size']) ? s['__content__'].to_i : nil), Chris@441: :lastrev => Revision.new({ Chris@441: :identifier => commit['revision'], Chris@441: :time => Time.parse(commit_date['__content__'].to_s).localtime, Chris@441: :author => ((a = commit['author']) ? a['__content__'] : nil) Chris@441: }) Chris@441: }) Chris@441: end Chris@441: rescue Exception => e Chris@441: logger.error("Error parsing svn output: #{e.message}") Chris@441: logger.error("Output was:\n #{output}") Chris@441: end Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug? Chris@441: entries.sort_by_name Chris@441: end Chris@441: Chris@441: def properties(path, identifier=nil) Chris@441: # proplist xml output supported in svn 1.5.0 and higher Chris@441: return nil unless self.class.client_version_above?([1, 5, 0]) Chris@441: Chris@441: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@441: cmd = "#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}" Chris@441: cmd << credentials_string Chris@441: properties = {} Chris@441: shellout(cmd) do |io| Chris@441: output = io.read Chris@441: if output.respond_to?(:force_encoding) Chris@441: output.force_encoding('UTF-8') Chris@441: end Chris@441: begin Chris@441: doc = ActiveSupport::XmlMini.parse(output) Chris@441: each_xml_element(doc['properties']['target'], 'property') do |property| Chris@441: properties[ property['name'] ] = property['__content__'].to_s Chris@441: end Chris@441: rescue Chris@441: end Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: properties Chris@441: end Chris@441: Chris@441: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@441: path ||= '' Chris@441: identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD" Chris@441: identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1 Chris@441: revisions = Revisions.new Chris@441: cmd = "#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}" Chris@441: cmd << credentials_string Chris@441: cmd << " --verbose " if options[:with_paths] Chris@441: cmd << " --limit #{options[:limit].to_i}" if options[:limit] Chris@441: cmd << ' ' + target(path) Chris@441: shellout(cmd) do |io| Chris@441: output = io.read Chris@441: if output.respond_to?(:force_encoding) Chris@441: output.force_encoding('UTF-8') Chris@441: end Chris@441: begin Chris@441: doc = ActiveSupport::XmlMini.parse(output) Chris@441: each_xml_element(doc['log'], 'logentry') do |logentry| Chris@441: paths = [] Chris@441: each_xml_element(logentry['paths'], 'path') do |path| Chris@441: paths << {:action => path['action'], Chris@441: :path => path['__content__'], Chris@441: :from_path => path['copyfrom-path'], Chris@441: :from_revision => path['copyfrom-rev'] Chris@441: } Chris@441: end if logentry['paths'] && logentry['paths']['path'] Chris@441: paths.sort! { |x,y| x[:path] <=> y[:path] } Chris@441: Chris@441: revisions << Revision.new({:identifier => logentry['revision'], Chris@441: :author => (logentry['author'] ? logentry['author']['__content__'] : ""), Chris@441: :time => Time.parse(logentry['date']['__content__'].to_s).localtime, Chris@441: :message => logentry['msg']['__content__'], Chris@441: :paths => paths Chris@441: }) Chris@441: end Chris@441: rescue Chris@441: end Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: revisions Chris@441: end Chris@441: Chris@441: def diff(path, identifier_from, identifier_to=nil, type="inline") Chris@441: path ||= '' Chris@441: identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : '' Chris@441: Chris@441: identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1) Chris@441: Chris@441: cmd = "#{self.class.sq_bin} diff -r " Chris@441: cmd << "#{identifier_to}:" Chris@441: cmd << "#{identifier_from}" Chris@441: cmd << " #{target(path)}@#{identifier_from}" Chris@441: cmd << credentials_string Chris@441: diff = [] Chris@441: shellout(cmd) do |io| Chris@441: io.each_line do |line| Chris@441: diff << line Chris@441: end Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: diff Chris@441: end Chris@441: Chris@441: def cat(path, identifier=nil) Chris@441: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@441: cmd = "#{self.class.sq_bin} cat #{target(path)}@#{identifier}" Chris@441: cmd << credentials_string Chris@441: cat = nil Chris@441: shellout(cmd) do |io| Chris@441: io.binmode Chris@441: cat = io.read Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: cat Chris@441: end Chris@441: Chris@441: def annotate(path, identifier=nil) Chris@441: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@441: cmd = "#{self.class.sq_bin} blame #{target(path)}@#{identifier}" Chris@441: cmd << credentials_string Chris@441: blame = Annotate.new Chris@441: shellout(cmd) do |io| Chris@441: io.each_line do |line| Chris@441: next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$} 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@441: end Chris@441: end Chris@441: return nil if $? && $?.exitstatus != 0 Chris@441: blame Chris@441: end Chris@441: Chris@441: private Chris@441: Chris@441: def credentials_string Chris@441: str = '' Chris@441: str << " --username #{shell_quote(@login)}" unless @login.blank? Chris@441: str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank? Chris@441: str << " --no-auth-cache --non-interactive" Chris@441: str Chris@441: end Chris@441: Chris@441: # Helper that iterates over the child elements of a xml node Chris@441: # MiniXml returns a hash when a single child is found Chris@441: # or an array of hashes for multiple children Chris@441: def each_xml_element(node, name) Chris@441: if node && node[name] Chris@441: if node[name].is_a?(Hash) Chris@441: yield node[name] Chris@441: else Chris@441: node[name].each do |element| Chris@441: yield element Chris@441: end Chris@441: end Chris@441: end Chris@441: end Chris@441: Chris@441: def target(path = '') Chris@441: base = path.match(/^\//) ? root_url : url Chris@441: uri = "#{base}/#{path}" Chris@441: uri = URI.escape(URI.escape(uri), '[]') Chris@441: shell_quote(uri.gsub(/[?<>\*]/, '')) Chris@441: end Chris@441: end Chris@441: end Chris@441: end Chris@441: end