Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2010 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@0: # 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@0: # 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: require 'uri' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@0: module Adapters Chris@0: class SubversionAdapter < AbstractAdapter Chris@0: Chris@0: # SVN executable name Chris@0: SVN_BIN = "svn" Chris@0: Chris@0: class << self Chris@0: def client_version Chris@0: @@client_version ||= (svn_binary_version || []) Chris@0: end Chris@0: Chris@0: def svn_binary_version Chris@0: cmd = "#{SVN_BIN} --version" Chris@0: version = nil Chris@0: shellout(cmd) do |io| Chris@0: # Read svn version in first returned line Chris@0: if m = io.gets.to_s.match(%r{((\d+\.)+\d+)}) Chris@0: version = m[0].scan(%r{\d+}).collect(&:to_i) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: version Chris@0: end Chris@0: end Chris@0: Chris@0: # Get info about the svn repository Chris@0: def info Chris@0: cmd = "#{SVN_BIN} info --xml #{target}" Chris@0: cmd << credentials_string Chris@0: info = nil Chris@0: shellout(cmd) do |io| Chris@0: output = io.read Chris@0: begin Chris@0: doc = ActiveSupport::XmlMini.parse(output) Chris@0: #root_url = doc.elements["info/entry/repository/root"].text Chris@0: info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'], Chris@0: :lastrev => Revision.new({ Chris@0: :identifier => doc['info']['entry']['commit']['revision'], Chris@0: :time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime, Chris@0: :author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "") Chris@0: }) Chris@0: }) Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: info Chris@0: rescue CommandFailed Chris@0: return nil Chris@0: end Chris@0: Chris@0: # Returns an Entries collection Chris@0: # or nil if the given path doesn't exist in the repository Chris@0: def entries(path=nil, identifier=nil) Chris@0: path ||= '' Chris@0: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@0: entries = Entries.new Chris@0: cmd = "#{SVN_BIN} list --xml #{target(path)}@#{identifier}" Chris@0: cmd << credentials_string Chris@0: shellout(cmd) do |io| Chris@0: output = io.read Chris@0: begin Chris@0: doc = ActiveSupport::XmlMini.parse(output) Chris@0: each_xml_element(doc['lists']['list'], 'entry') do |entry| Chris@0: commit = entry['commit'] Chris@0: commit_date = commit['date'] Chris@0: # Skip directory if there is no commit date (usually that Chris@0: # means that we don't have read access to it) Chris@0: next if entry['kind'] == 'dir' && commit_date.nil? Chris@0: name = entry['name']['__content__'] Chris@0: entries << Entry.new({:name => URI.unescape(name), Chris@0: :path => ((path.empty? ? "" : "#{path}/") + name), Chris@0: :kind => entry['kind'], Chris@0: :size => ((s = entry['size']) ? s['__content__'].to_i : nil), Chris@0: :lastrev => Revision.new({ Chris@0: :identifier => commit['revision'], Chris@0: :time => Time.parse(commit_date['__content__'].to_s).localtime, Chris@0: :author => ((a = commit['author']) ? a['__content__'] : nil) Chris@0: }) Chris@0: }) Chris@0: end Chris@0: rescue Exception => e Chris@0: logger.error("Error parsing svn output: #{e.message}") Chris@0: logger.error("Output was:\n #{output}") Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug? Chris@0: entries.sort_by_name Chris@0: end Chris@0: Chris@0: def properties(path, identifier=nil) Chris@0: # proplist xml output supported in svn 1.5.0 and higher Chris@0: return nil unless self.class.client_version_above?([1, 5, 0]) Chris@0: Chris@0: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@0: cmd = "#{SVN_BIN} proplist --verbose --xml #{target(path)}@#{identifier}" Chris@0: cmd << credentials_string Chris@0: properties = {} Chris@0: shellout(cmd) do |io| Chris@0: output = io.read Chris@0: begin Chris@0: doc = ActiveSupport::XmlMini.parse(output) Chris@0: each_xml_element(doc['properties']['target'], 'property') do |property| Chris@0: properties[ property['name'] ] = property['__content__'].to_s Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: properties Chris@0: end Chris@0: Chris@0: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@0: path ||= '' Chris@0: identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD" Chris@0: identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1 Chris@0: revisions = Revisions.new Chris@0: cmd = "#{SVN_BIN} log --xml -r #{identifier_from}:#{identifier_to}" Chris@0: cmd << credentials_string Chris@0: cmd << " --verbose " if options[:with_paths] Chris@0: cmd << " --limit #{options[:limit].to_i}" if options[:limit] Chris@0: cmd << ' ' + target(path) Chris@0: shellout(cmd) do |io| Chris@0: output = io.read Chris@0: begin Chris@0: doc = ActiveSupport::XmlMini.parse(output) Chris@0: each_xml_element(doc['log'], 'logentry') do |logentry| Chris@0: paths = [] Chris@0: each_xml_element(logentry['paths'], 'path') do |path| Chris@0: paths << {:action => path['action'], Chris@0: :path => path['__content__'], Chris@0: :from_path => path['copyfrom-path'], Chris@0: :from_revision => path['copyfrom-rev'] Chris@0: } Chris@0: end if logentry['paths'] && logentry['paths']['path'] Chris@0: paths.sort! { |x,y| x[:path] <=> y[:path] } Chris@0: Chris@0: revisions << Revision.new({:identifier => logentry['revision'], Chris@0: :author => (logentry['author'] ? logentry['author']['__content__'] : ""), Chris@0: :time => Time.parse(logentry['date']['__content__'].to_s).localtime, Chris@0: :message => logentry['msg']['__content__'], Chris@0: :paths => paths Chris@0: }) Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: revisions Chris@0: end Chris@0: Chris@0: def diff(path, identifier_from, identifier_to=nil, type="inline") Chris@0: path ||= '' Chris@0: identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : '' Chris@0: identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1) Chris@0: Chris@0: cmd = "#{SVN_BIN} diff -r " Chris@0: cmd << "#{identifier_to}:" Chris@0: cmd << "#{identifier_from}" Chris@0: cmd << " #{target(path)}@#{identifier_from}" Chris@0: cmd << credentials_string Chris@0: diff = [] Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: diff Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@0: cmd = "#{SVN_BIN} cat #{target(path)}@#{identifier}" Chris@0: cmd << credentials_string Chris@0: cat = nil Chris@0: shellout(cmd) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: cat Chris@0: end Chris@0: Chris@0: def annotate(path, identifier=nil) Chris@0: identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD" Chris@0: cmd = "#{SVN_BIN} blame #{target(path)}@#{identifier}" Chris@0: cmd << credentials_string Chris@0: blame = Annotate.new Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$} Chris@0: blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip)) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: blame Chris@0: end Chris@0: Chris@0: private Chris@0: Chris@0: def credentials_string Chris@0: str = '' Chris@0: str << " --username #{shell_quote(@login)}" unless @login.blank? Chris@0: str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank? Chris@0: str << " --no-auth-cache --non-interactive" Chris@0: str Chris@0: end Chris@0: Chris@0: # Helper that iterates over the child elements of a xml node Chris@0: # MiniXml returns a hash when a single child is found or an array of hashes for multiple children Chris@0: def each_xml_element(node, name) Chris@0: if node && node[name] Chris@0: if node[name].is_a?(Hash) Chris@0: yield node[name] Chris@0: else Chris@0: node[name].each do |element| Chris@0: yield element Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def target(path = '') Chris@0: base = path.match(/^\//) ? root_url : url Chris@0: uri = "#{base}/#{path}" Chris@0: uri = URI.escape(URI.escape(uri), '[]') Chris@0: shell_quote(uri.gsub(/[?<>\*]/, '')) Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end