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