2011-05-06 17:48:36 +04:00
|
|
|
# Redmine - project management software
|
2012-05-05 16:56:53 +04:00
|
|
|
# Copyright (C) 2006-2012 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.
|
2011-05-06 17:48:36 +04:00
|
|
|
#
|
2007-06-13 00:12:05 +04:00
|
|
|
# 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.
|
2011-05-06 17:48:36 +04:00
|
|
|
#
|
2007-06-13 00:12:05 +04:00
|
|
|
# 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 'cgi'
|
|
|
|
|
|
|
|
module Redmine
|
|
|
|
module Scm
|
2011-02-18 03:57:43 +03:00
|
|
|
module Adapters
|
2007-06-13 00:12:05 +04:00
|
|
|
class CommandFailed < StandardError #:nodoc:
|
|
|
|
end
|
2011-02-18 03:57:43 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
class AbstractAdapter #:nodoc:
|
2011-05-25 20:14:15 +04:00
|
|
|
|
|
|
|
# raised if scm command exited with error, e.g. unknown revision.
|
|
|
|
class ScmCommandAborted < CommandFailed; end
|
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
class << self
|
2011-02-14 09:14:34 +03:00
|
|
|
def client_command
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2011-07-15 15:43:53 +04:00
|
|
|
def shell_quote_command
|
|
|
|
if Redmine::Platform.mswin? && RUBY_PLATFORM == 'java'
|
|
|
|
client_command
|
|
|
|
else
|
|
|
|
shell_quote(client_command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
# Returns the version of the scm client
|
2008-07-12 13:06:19 +04:00
|
|
|
# Eg: [1, 5, 0] or [] if unknown
|
2008-07-05 12:59:04 +04:00
|
|
|
def client_version
|
2008-07-12 13:06:19 +04:00
|
|
|
[]
|
2008-07-05 12:59:04 +04:00
|
|
|
end
|
2011-02-24 08:58:59 +03:00
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
# Returns the version string of the scm client
|
2008-07-12 13:06:19 +04:00
|
|
|
# Eg: '1.5.0' or 'Unknown version' if unknown
|
2008-07-05 12:59:04 +04:00
|
|
|
def client_version_string
|
2008-07-12 13:06:19 +04:00
|
|
|
v = client_version || 'Unknown version'
|
|
|
|
v.is_a?(Array) ? v.join('.') : v.to_s
|
|
|
|
end
|
2011-02-24 08:58:59 +03:00
|
|
|
|
2008-07-12 13:06:19 +04:00
|
|
|
# Returns true if the current client version is above
|
|
|
|
# or equals the given one
|
|
|
|
# If option is :unknown is set to true, it will return
|
|
|
|
# true if the client version is unknown
|
|
|
|
def client_version_above?(v, options={})
|
|
|
|
((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
|
2008-07-05 12:59:04 +04:00
|
|
|
end
|
2011-02-14 09:14:34 +03:00
|
|
|
|
|
|
|
def client_available
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def shell_quote(str)
|
|
|
|
if Redmine::Platform.mswin?
|
|
|
|
'"' + str.gsub(/"/, '\\"') + '"'
|
|
|
|
else
|
|
|
|
"'" + str.gsub(/'/, "'\"'\"'") + "'"
|
|
|
|
end
|
|
|
|
end
|
2008-07-05 12:59:04 +04:00
|
|
|
end
|
2011-02-14 09:14:34 +03:00
|
|
|
|
2011-02-24 08:58:59 +03:00
|
|
|
def initialize(url, root_url=nil, login=nil, password=nil,
|
|
|
|
path_encoding=nil)
|
2007-06-13 00:12:05 +04:00
|
|
|
@url = url
|
|
|
|
@login = login if login && !login.empty?
|
|
|
|
@password = (password || "") if @login
|
|
|
|
@root_url = root_url.blank? ? retrieve_root_url : root_url
|
|
|
|
end
|
2011-02-24 08:58:59 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def adapter_name
|
|
|
|
'Abstract'
|
|
|
|
end
|
2011-02-24 08:58:59 +03:00
|
|
|
|
2007-06-24 23:30:38 +04:00
|
|
|
def supports_cat?
|
|
|
|
true
|
|
|
|
end
|
2007-12-02 23:58:02 +03:00
|
|
|
|
|
|
|
def supports_annotate?
|
|
|
|
respond_to?('annotate')
|
|
|
|
end
|
2011-02-24 08:58:59 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def root_url
|
|
|
|
@root_url
|
|
|
|
end
|
2011-02-24 08:58:59 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def url
|
|
|
|
@url
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2011-05-21 06:16:51 +04:00
|
|
|
def path_encoding
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
# get info about the svn repository
|
|
|
|
def info
|
|
|
|
return nil
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
# Returns the entry identified by path and revision identifier
|
|
|
|
# or nil if entry doesn't exist in the repository
|
|
|
|
def entry(path=nil, identifier=nil)
|
2008-04-27 14:12:15 +04:00
|
|
|
parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
|
|
|
|
search_path = parts[0..-2].join('/')
|
|
|
|
search_name = parts[-1]
|
|
|
|
if search_path.blank? && search_name.blank?
|
|
|
|
# Root entry
|
|
|
|
Entry.new(:path => '', :kind => 'dir')
|
|
|
|
else
|
|
|
|
# Search for the entry in the parent directory
|
|
|
|
es = entries(search_path, identifier)
|
|
|
|
es ? es.detect {|e| e.name == search_name} : nil
|
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-05-06 17:48:36 +04: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
|
2011-05-04 18:11:25 +04:00
|
|
|
def entries(path=nil, identifier=nil, options={})
|
2007-06-13 00:12:05 +04:00
|
|
|
return nil
|
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
def branches
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2011-05-06 17:48:36 +04:00
|
|
|
def tags
|
2009-08-16 02:41:40 +04:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
|
|
|
return nil
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
def properties(path, identifier=nil)
|
|
|
|
return nil
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
|
|
|
return nil
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2008-06-08 20:28:42 +04:00
|
|
|
def diff(path, identifier_from, identifier_to=nil)
|
2007-06-13 00:12:05 +04:00
|
|
|
return nil
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def cat(path, identifier=nil)
|
|
|
|
return nil
|
|
|
|
end
|
2011-04-10 12:11:18 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def with_leading_slash(path)
|
|
|
|
path ||= ''
|
|
|
|
(path[0,1]!="/") ? "/#{path}" : path
|
|
|
|
end
|
2008-06-07 13:19:50 +04:00
|
|
|
|
|
|
|
def with_trailling_slash(path)
|
|
|
|
path ||= ''
|
|
|
|
(path[-1,1] == "/") ? path : "#{path}/"
|
|
|
|
end
|
2011-02-14 09:14:34 +03:00
|
|
|
|
2008-06-08 19:40:24 +04:00
|
|
|
def without_leading_slash(path)
|
|
|
|
path ||= ''
|
|
|
|
path.gsub(%r{^/+}, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def without_trailling_slash(path)
|
|
|
|
path ||= ''
|
|
|
|
(path[-1,1] == "/") ? path[0..-2] : path
|
|
|
|
end
|
2011-02-14 09:14:34 +03:00
|
|
|
|
2007-10-19 21:59:59 +04:00
|
|
|
def shell_quote(str)
|
2011-02-14 09:14:34 +03:00
|
|
|
self.class.shell_quote(str)
|
2007-10-19 21:59:59 +04:00
|
|
|
end
|
2008-06-07 13:19:50 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
private
|
|
|
|
def retrieve_root_url
|
|
|
|
info = self.info
|
|
|
|
info ? info.root_url : nil
|
|
|
|
end
|
2011-04-10 12:11:18 +04:00
|
|
|
|
2011-05-26 03:50:59 +04:00
|
|
|
def target(path, sq=true)
|
2008-03-13 02:00:11 +03:00
|
|
|
path ||= ''
|
|
|
|
base = path.match(/^\//) ? root_url : url
|
2011-05-26 03:50:59 +04:00
|
|
|
str = "#{base}/#{path}".gsub(/[?<>\*]/, '')
|
|
|
|
if sq
|
|
|
|
str = shell_quote(str)
|
|
|
|
end
|
|
|
|
str
|
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 logger
|
2008-07-05 12:59:04 +04:00
|
|
|
self.class.logger
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-02-14 09:14:34 +03:00
|
|
|
|
2012-03-28 19:40:37 +04:00
|
|
|
def shellout(cmd, options = {}, &block)
|
|
|
|
self.class.shellout(cmd, options, &block)
|
2008-07-05 12:59:04 +04:00
|
|
|
end
|
2011-02-18 03:57:43 +03:00
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
def self.logger
|
2011-06-23 03:42:37 +04:00
|
|
|
Rails.logger
|
2008-07-05 12:59:04 +04:00
|
|
|
end
|
2011-02-18 03:57:43 +03:00
|
|
|
|
2012-03-28 19:40:37 +04:00
|
|
|
def self.shellout(cmd, options = {}, &block)
|
2011-06-08 15:19:13 +04:00
|
|
|
if logger && logger.debug?
|
|
|
|
logger.debug "Shelling out: #{strip_credential(cmd)}"
|
|
|
|
end
|
2008-12-12 19:07:14 +03:00
|
|
|
if Rails.env == 'development'
|
|
|
|
# Capture stderr when running in dev environment
|
2011-12-08 23:22:00 +04:00
|
|
|
cmd = "#{cmd} 2>>#{shell_quote(Rails.root.join('log/scm.stderr.log').to_s)}"
|
2008-12-12 19:07:14 +03:00
|
|
|
end
|
2007-12-15 15:14:40 +03:00
|
|
|
begin
|
2012-03-28 14:54:38 +04:00
|
|
|
mode = "r+"
|
2011-02-24 07:03:16 +03:00
|
|
|
IO.popen(cmd, mode) do |io|
|
2012-03-28 14:54:38 +04:00
|
|
|
io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
|
2012-03-28 19:40:37 +04:00
|
|
|
io.close_write unless options[:write_stdin]
|
2007-12-15 15:14:40 +03:00
|
|
|
block.call(io) if block_given?
|
|
|
|
end
|
2011-07-11 01:47:30 +04:00
|
|
|
## If scm command does not exist,
|
|
|
|
## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException
|
|
|
|
## in production environment.
|
|
|
|
# rescue Errno::ENOENT => e
|
|
|
|
rescue Exception => e
|
2008-06-06 18:37:49 +04:00
|
|
|
msg = strip_credential(e.message)
|
2007-12-15 15:14:40 +03:00
|
|
|
# The command failed, log it and re-raise
|
2011-06-08 15:19:13 +04:00
|
|
|
logmsg = "SCM command failed, "
|
2011-06-08 15:19:57 +04:00
|
|
|
logmsg += "make sure that your SCM command (e.g. svn) is "
|
|
|
|
logmsg += "in PATH (#{ENV['PATH']})\n"
|
|
|
|
logmsg += "You can configure your scm commands in config/configuration.yml.\n"
|
2011-06-08 15:19:13 +04:00
|
|
|
logmsg += "#{strip_credential(cmd)}\n"
|
|
|
|
logmsg += "with: #{msg}"
|
|
|
|
logger.error(logmsg)
|
2008-06-06 18:37:49 +04:00
|
|
|
raise CommandFailed.new(msg)
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-02-18 03:57:43 +03:00
|
|
|
end
|
|
|
|
|
2008-06-06 18:37:49 +04:00
|
|
|
# Hides username/password in a given command
|
2008-08-25 16:27:15 +04:00
|
|
|
def self.strip_credential(cmd)
|
2008-08-25 15:01:37 +04:00
|
|
|
q = (Redmine::Platform.mswin? ? '"' : "'")
|
2008-06-06 18:37:49 +04:00
|
|
|
cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
|
|
|
|
end
|
2011-04-10 12:11:18 +04:00
|
|
|
|
2008-06-06 18:37:49 +04:00
|
|
|
def strip_credential(cmd)
|
2008-08-25 16:27:15 +04:00
|
|
|
self.class.strip_credential(cmd)
|
2008-06-06 18:37:49 +04:00
|
|
|
end
|
2011-02-21 15:10:16 +03:00
|
|
|
|
|
|
|
def scm_iconv(to, from, str)
|
|
|
|
return nil if str.nil?
|
|
|
|
return str if to == from
|
|
|
|
begin
|
|
|
|
Iconv.conv(to, from, str)
|
|
|
|
rescue Iconv::Failure => err
|
|
|
|
logger.error("failed to convert from #{from} to #{to}. #{err}")
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2012-04-26 21:57:49 +04:00
|
|
|
|
|
|
|
def parse_xml(xml)
|
|
|
|
if RUBY_PLATFORM == 'java'
|
|
|
|
xml = xml.sub(%r{<\?xml[^>]*\?>}, '')
|
|
|
|
end
|
|
|
|
ActiveSupport::XmlMini.parse(xml)
|
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-02-21 15:10:16 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
class Entries < Array
|
|
|
|
def sort_by_name
|
2012-06-10 21:01:33 +04:00
|
|
|
dup.sort! {|x,y|
|
2007-06-13 00:12:05 +04:00
|
|
|
if x.kind == y.kind
|
2009-10-24 15:08:09 +04:00
|
|
|
x.name.to_s <=> y.name.to_s
|
2007-06-13 00:12:05 +04:00
|
|
|
else
|
|
|
|
x.kind <=> y.kind
|
|
|
|
end
|
2011-02-18 03:57:43 +03:00
|
|
|
}
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def revisions
|
|
|
|
revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
|
|
|
|
end
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
class Info
|
|
|
|
attr_accessor :root_url, :lastrev
|
|
|
|
def initialize(attributes={})
|
|
|
|
self.root_url = attributes[:root_url] if attributes[:root_url]
|
|
|
|
self.lastrev = attributes[:lastrev]
|
|
|
|
end
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
class Entry
|
2012-06-10 22:32:09 +04:00
|
|
|
attr_accessor :name, :path, :kind, :size, :lastrev, :changeset
|
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def initialize(attributes={})
|
|
|
|
self.name = attributes[:name] if attributes[:name]
|
|
|
|
self.path = attributes[:path] if attributes[:path]
|
|
|
|
self.kind = attributes[:kind] if attributes[:kind]
|
|
|
|
self.size = attributes[:size].to_i if attributes[:size]
|
|
|
|
self.lastrev = attributes[:lastrev]
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def is_file?
|
|
|
|
'file' == self.kind
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def is_dir?
|
|
|
|
'dir' == self.kind
|
|
|
|
end
|
2011-05-04 17:16:10 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def is_text?
|
|
|
|
Redmine::MimeType.is_type?('text', name)
|
|
|
|
end
|
2012-06-10 23:19:22 +04:00
|
|
|
|
|
|
|
def author
|
|
|
|
if changeset
|
|
|
|
changeset.author.to_s
|
|
|
|
elsif lastrev
|
|
|
|
Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first)
|
|
|
|
end
|
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
class Revisions < Array
|
|
|
|
def latest
|
|
|
|
sort {|x,y|
|
|
|
|
unless x.time.nil? or y.time.nil?
|
|
|
|
x.time <=> y.time
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
}.last
|
2011-05-06 17:48:36 +04:00
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
class Revision
|
2011-04-11 14:24:44 +04:00
|
|
|
attr_accessor :scmid, :name, :author, :time, :message,
|
2011-10-28 08:55:54 +04:00
|
|
|
:paths, :revision, :branch, :identifier,
|
|
|
|
:parents
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def initialize(attributes={})
|
|
|
|
self.identifier = attributes[:identifier]
|
2011-04-11 14:25:38 +04:00
|
|
|
self.scmid = attributes[:scmid]
|
|
|
|
self.name = attributes[:name] || self.identifier
|
|
|
|
self.author = attributes[:author]
|
|
|
|
self.time = attributes[:time]
|
|
|
|
self.message = attributes[:message] || ""
|
|
|
|
self.paths = attributes[:paths]
|
|
|
|
self.revision = attributes[:revision]
|
|
|
|
self.branch = attributes[:branch]
|
2011-10-28 08:55:54 +04:00
|
|
|
self.parents = attributes[:parents]
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2011-01-02 12:45:05 +03:00
|
|
|
# Returns the readable identifier.
|
|
|
|
def format_identifier
|
2011-04-11 14:24:44 +04:00
|
|
|
self.identifier.to_s
|
2011-01-02 12:45:05 +03:00
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-02-18 03:57:43 +03:00
|
|
|
|
2007-12-02 23:58:02 +03:00
|
|
|
class Annotate
|
|
|
|
attr_reader :lines, :revisions
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-12-02 23:58:02 +03:00
|
|
|
def initialize
|
|
|
|
@lines = []
|
|
|
|
@revisions = []
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-12-02 23:58:02 +03:00
|
|
|
def add_line(line, revision)
|
|
|
|
@lines << line
|
|
|
|
@revisions << revision
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-12-02 23:58:02 +03:00
|
|
|
def content
|
|
|
|
content = lines.join("\n")
|
|
|
|
end
|
2011-05-06 17:48:36 +04:00
|
|
|
|
2007-12-02 23:58:02 +03:00
|
|
|
def empty?
|
|
|
|
lines.empty?
|
|
|
|
end
|
|
|
|
end
|
2011-10-28 09:17:13 +04:00
|
|
|
|
|
|
|
class Branch < String
|
|
|
|
attr_accessor :revision, :scmid
|
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|