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