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